Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-37199: [C++] Expose a span converter for Buffer and ArraySpan #38027

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cpp/src/arrow/array/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <atomic> // IWYU pragma: export
#include <cassert>
#include <cstdint>
#include <memory>
#include <utility>
Expand Down Expand Up @@ -434,6 +435,28 @@ struct ARROW_EXPORT ArraySpan {
return GetValues<T>(i, this->offset);
}

/// \brief Access a buffer's data as a span
///
/// \param i The buffer index
/// \param length The required length (in number of typed values) of the requested span
/// \pre i > 0
/// \pre length <= the length of the buffer (in number of values) that's expected for
/// this array type \return A span<const T> of the requested length
jsjtxietian marked this conversation as resolved.
Show resolved Hide resolved
template <typename T>
util::span<const T> GetSpan(int i, int64_t length) const {
jsjtxietian marked this conversation as resolved.
Show resolved Hide resolved
const int64_t buffer_length = buffers[i].size / static_cast<int64_t>(sizeof(T));
assert(i > 0 && length + offset <= buffer_length);
return util::span<const T>(buffers[i].data_as<T>() + this->offset, length);
}

jsjtxietian marked this conversation as resolved.
Show resolved Hide resolved
template <typename T>
util::span<T> GetSpan(int i, int64_t length) {
const int64_t buffer_length = buffers[i].size / static_cast<int64_t>(sizeof(T));
;
jsjtxietian marked this conversation as resolved.
Show resolved Hide resolved
assert(i > 0 && length + offset <= buffer_length);
return util::span<T>(buffers[i].mutable_data_as<T>() + this->offset, length);
}

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
Loading