diff --git a/cpp/src/arrow/array/data.h b/cpp/src/arrow/array/data.h index 40a77640cd1e5..0ef5231b841f5 100644 --- a/cpp/src/arrow/array/data.h +++ b/cpp/src/arrow/array/data.h @@ -434,6 +434,14 @@ struct ARROW_EXPORT ArraySpan { return GetValues(i, this->offset); } + // Access a buffer's data as a span + template + util::span GetSpan(int i) const { + return util::span(buffers[i].data_as(), + static_cast(buffers[i].size) / sizeof(T)) + .subspan(offset); + } + inline bool IsNull(int64_t i) const { return !IsValid(i); } inline bool IsValid(int64_t i) const { diff --git a/cpp/src/arrow/buffer.h b/cpp/src/arrow/buffer.h index 7cc2d2c9cc8c4..0fb33a22fcb8a 100644 --- a/cpp/src/arrow/buffer.h +++ b/cpp/src/arrow/buffer.h @@ -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 { @@ -233,6 +234,12 @@ class ARROW_EXPORT Buffer { return reinterpret_cast(data()); } + /// \brief Return the buffer's data as a span + template + util::span span_as() const { + return util::span(data_as(), static_cast(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()` @@ -260,6 +267,12 @@ class ARROW_EXPORT Buffer { return reinterpret_cast(mutable_data()); } + /// \brief Return the buffer's mutable data as a span + template + util::span mutable_span_as() const { + return util::span(mutable_data_as(), static_cast(size() / sizeof(T))); + } + /// \brief Return the device address of the buffer's data uintptr_t address() const { return reinterpret_cast(data_); }