Skip to content

Commit

Permalink
expose span at the buffer and array data level
Browse files Browse the repository at this point in the history
  • Loading branch information
jsjtxietian committed Nov 4, 2023
1 parent d4deb36 commit 4fcc934
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cpp/src/arrow/array/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ struct ARROW_EXPORT ArraySpan {
return GetValues<T>(i, this->offset);
}

// Access a buffer's data as a span
template <typename T>
util::span<const T> GetSpan(int i) const {
return util::span(buffers[i].data_as<T>(),
static_cast<size_t>(buffers[i].size) / sizeof(T))
.subspan(offset);
}

inline bool IsNull(int64_t i) const { return !IsValid(i); }

inline bool IsValid(int64_t i) const {
Expand Down
13 changes: 13 additions & 0 deletions cpp/src/arrow/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "arrow/status.h"
#include "arrow/type_fwd.h"
#include "arrow/util/macros.h"
#include "arrow/util/span.h"
#include "arrow/util/visibility.h"

namespace arrow {
Expand Down Expand Up @@ -233,6 +234,12 @@ class ARROW_EXPORT Buffer {
return reinterpret_cast<const T*>(data());
}

/// \brief Return the buffer's data as a span
template <typename T>
util::span<const T> span_as() const {
return util::span(data_as<T>(), static_cast<size_t>(size() / sizeof(T)));
}

/// \brief Return a writable pointer to the buffer's data
///
/// The buffer has to be a mutable CPU buffer (`is_cpu()` and `is_mutable()`
Expand Down Expand Up @@ -260,6 +267,12 @@ class ARROW_EXPORT Buffer {
return reinterpret_cast<T*>(mutable_data());
}

/// \brief Return the buffer's mutable data as a span
template <typename T>
util::span<T> mutable_span_as() const {
return util::span(mutable_data_as<T>(), static_cast<size_t>(size() / sizeof(T)));
}

/// \brief Return the device address of the buffer's data
uintptr_t address() const { return reinterpret_cast<uintptr_t>(data_); }

Expand Down

0 comments on commit 4fcc934

Please sign in to comment.