Skip to content

Commit df9dbbc

Browse files
GH-48588 [C++] Migrate to stdlib span (#49492)
### Rationale for this change C++ 20 already includes a span implementation so there is no reason to maintain a custom one in utils as described in #48588 ### What changes are included in this PR? arrow::utils::span definition and tests are removed and usages are replaced with std::span. Some span comparisons in tests are replaced from ASSERT_EQ to ASSERT_TRUE(std::ranges::equal(span1, span2)) because ASSERT_EQ inernally relies on == operator which is not defined for some types that we used to compare. ### Are these changes tested? Yes ### Are there any user-facing changes? No * GitHub Issue: #48588 Lead-authored-by: Paweł Biegun <biegunpawel900@gmail.com> Co-authored-by: Will Ayd <william.ayd@icloud.com> Signed-off-by: Antoine Pitrou <antoine@python.org>
1 parent af184f5 commit df9dbbc

54 files changed

Lines changed: 246 additions & 576 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cpp/src/arrow/array/concatenate.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <cstdint>
2323
#include <limits>
2424
#include <memory>
25+
#include <span>
2526
#include <utility>
2627
#include <vector>
2728

@@ -424,7 +425,7 @@ class ConcatenateImpl {
424425
out_->buffers.resize(2);
425426

426427
for (const auto& in_data : in_) {
427-
for (const auto& buf : util::span(in_data->buffers).subspan(2)) {
428+
for (const auto& buf : std::span(in_data->buffers).subspan(2)) {
428429
out_->buffers.push_back(buf);
429430
}
430431
}

cpp/src/arrow/array/data.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <cstddef>
2222
#include <cstdint>
2323
#include <memory>
24+
#include <span>
2425
#include <string>
2526
#include <utility>
2627
#include <vector>
@@ -102,7 +103,7 @@ bool DictionaryMayHaveLogicalNulls(const ArrayData& data) {
102103

103104
namespace {
104105

105-
BufferSpan PackVariadicBuffers(util::span<const std::shared_ptr<Buffer>> buffers) {
106+
BufferSpan PackVariadicBuffers(std::span<const std::shared_ptr<Buffer>> buffers) {
106107
return {const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffers.data())),
107108
static_cast<int64_t>(buffers.size() * sizeof(std::shared_ptr<Buffer>))};
108109
}
@@ -322,7 +323,7 @@ void ArraySpan::SetMembers(const ArrayData& data) {
322323

323324
if (type_id == Type::STRING_VIEW || type_id == Type::BINARY_VIEW) {
324325
// store the span of data buffers in the third buffer
325-
this->buffers[2] = internal::PackVariadicBuffers(util::span(data.buffers).subspan(2));
326+
this->buffers[2] = internal::PackVariadicBuffers(std::span(data.buffers).subspan(2));
326327
}
327328

328329
if (type_id == Type::DICTIONARY) {
@@ -679,7 +680,7 @@ std::shared_ptr<ArrayData> ArraySpan::ToArrayData() const {
679680
return result;
680681
}
681682

682-
util::span<const std::shared_ptr<Buffer>> ArraySpan::GetVariadicBuffers() const {
683+
std::span<const std::shared_ptr<Buffer>> ArraySpan::GetVariadicBuffers() const {
683684
DCHECK(HasVariadicBuffers());
684685
return {buffers[2].data_as<std::shared_ptr<Buffer>>(),
685686
static_cast<size_t>(buffers[2].size) / sizeof(std::shared_ptr<Buffer>)};

cpp/src/arrow/array/data.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <cassert>
2222
#include <cstdint>
2323
#include <memory>
24+
#include <span>
2425
#include <utility>
2526
#include <vector>
2627

@@ -31,7 +32,6 @@
3132
#include "arrow/type_fwd.h"
3233
#include "arrow/util/bit_util.h"
3334
#include "arrow/util/macros.h"
34-
#include "arrow/util/span.h"
3535
#include "arrow/util/visibility.h"
3636

3737
namespace arrow {
@@ -577,11 +577,11 @@ struct ARROW_EXPORT ArraySpan {
577577
/// this array type
578578
/// \return A span<const T> of the requested length
579579
template <typename T>
580-
util::span<const T> GetSpan(int i, int64_t length) const {
580+
std::span<const T> GetSpan(int i, int64_t length) const {
581581
const int64_t buffer_length = buffers[i].size / static_cast<int64_t>(sizeof(T));
582582
assert(i > 0 && length + offset <= buffer_length);
583583
ARROW_UNUSED(buffer_length);
584-
return util::span<const T>(buffers[i].data_as<T>() + this->offset, length);
584+
return std::span<const T>(buffers[i].data_as<T>() + this->offset, length);
585585
}
586586

587587
/// \brief Access a buffer's data as a span
@@ -593,11 +593,11 @@ struct ARROW_EXPORT ArraySpan {
593593
/// this array type
594594
/// \return A span<T> of the requested length
595595
template <typename T>
596-
util::span<T> GetSpan(int i, int64_t length) {
596+
std::span<T> GetSpan(int i, int64_t length) {
597597
const int64_t buffer_length = buffers[i].size / static_cast<int64_t>(sizeof(T));
598598
assert(i > 0 && length + offset <= buffer_length);
599599
ARROW_UNUSED(buffer_length);
600-
return util::span<T>(buffers[i].mutable_data_as<T>() + this->offset, length);
600+
return std::span<T>(buffers[i].mutable_data_as<T>() + this->offset, length);
601601
}
602602

603603
inline bool IsNull(int64_t i) const { return !IsValid(i); }
@@ -709,7 +709,7 @@ struct ARROW_EXPORT ArraySpan {
709709
/// sizeof(shared_ptr<Buffer>).
710710
///
711711
/// \see HasVariadicBuffers
712-
util::span<const std::shared_ptr<Buffer>> GetVariadicBuffers() const;
712+
std::span<const std::shared_ptr<Buffer>> GetVariadicBuffers() const;
713713
bool HasVariadicBuffers() const;
714714

715715
private:

cpp/src/arrow/array/util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <cstring>
2424
#include <limits>
2525
#include <memory>
26+
#include <span>
2627
#include <type_traits>
2728
#include <utility>
2829
#include <vector>
@@ -44,7 +45,6 @@
4445
#include "arrow/util/endian.h"
4546
#include "arrow/util/logging_internal.h"
4647
#include "arrow/util/sort_internal.h"
47-
#include "arrow/util/span.h"
4848
#include "arrow/visit_data_inline.h"
4949
#include "arrow/visit_type_inline.h"
5050

cpp/src/arrow/array/validate.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "arrow/array/validate.h"
1919

20+
#include <span>
2021
#include <vector>
2122

2223
#include "arrow/array.h" // IWYU pragma: keep
@@ -650,9 +651,9 @@ struct ValidateArrayImpl {
650651
HexEncode(data, BinaryViewType::kPrefixSize));
651652
};
652653

653-
util::span views(data.GetValues<BinaryViewType::c_type>(1),
654-
static_cast<size_t>(data.length));
655-
util::span data_buffers(data.buffers.data() + 2, data.buffers.size() - 2);
654+
std::span views(data.GetValues<BinaryViewType::c_type>(1),
655+
static_cast<size_t>(data.length));
656+
std::span data_buffers(data.buffers.data() + 2, data.buffers.size() - 2);
656657

657658
for (size_t i = 0; i < static_cast<size_t>(data.length); ++i) {
658659
if (data.IsNull(i)) continue;
@@ -663,7 +664,7 @@ struct ValidateArrayImpl {
663664
}
664665

665666
if (views[i].is_inline()) {
666-
auto padding_bytes = util::span(views[i].inlined.data).subspan(views[i].size());
667+
auto padding_bytes = std::span(views[i].inlined.data).subspan(views[i].size());
667668
for (auto padding_byte : padding_bytes) {
668669
if (padding_byte != 0) {
669670
return Status::Invalid("View at slot ", i, " was inline with size ",

cpp/src/arrow/buffer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <cstring>
2222
#include <memory>
2323
#include <optional>
24+
#include <span>
2425
#include <string>
2526
#include <string_view>
2627
#include <utility>
@@ -30,7 +31,6 @@
3031
#include "arrow/status.h"
3132
#include "arrow/type_fwd.h"
3233
#include "arrow/util/macros.h"
33-
#include "arrow/util/span.h"
3434
#include "arrow/util/visibility.h"
3535

3636
namespace arrow {
@@ -236,8 +236,8 @@ class ARROW_EXPORT Buffer {
236236

237237
/// \brief Return the buffer's data as a span
238238
template <typename T>
239-
util::span<const T> span_as() const {
240-
return util::span(data_as<T>(), static_cast<size_t>(size() / sizeof(T)));
239+
std::span<const T> span_as() const {
240+
return std::span(data_as<T>(), static_cast<size_t>(size() / sizeof(T)));
241241
}
242242

243243
/// \brief Return a writable pointer to the buffer's data
@@ -269,8 +269,8 @@ class ARROW_EXPORT Buffer {
269269

270270
/// \brief Return the buffer's mutable data as a span
271271
template <typename T>
272-
util::span<T> mutable_span_as() {
273-
return util::span(mutable_data_as<T>(), static_cast<size_t>(size() / sizeof(T)));
272+
std::span<T> mutable_span_as() {
273+
return std::span(mutable_data_as<T>(), static_cast<size_t>(size() / sizeof(T)));
274274
}
275275

276276
/// \brief Return the device address of the buffer's data

cpp/src/arrow/c/bridge.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <memory>
2525
#include <mutex>
2626
#include <queue>
27+
#include <span>
2728
#include <string>
2829
#include <string_view>
2930
#include <utility>
@@ -603,7 +604,7 @@ struct ArrayExporter {
603604
});
604605

605606
if (need_variadic_buffer_sizes) {
606-
auto variadic_buffers = util::span(data->buffers).subspan(2);
607+
auto variadic_buffers = std::span(data->buffers).subspan(2);
607608
export_.variadic_buffer_sizes_.resize(variadic_buffers.size());
608609
size_t i = 0;
609610
for (const auto& buf : variadic_buffers) {

cpp/src/arrow/c/bridge_test.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cerrno>
1919
#include <deque>
2020
#include <functional>
21+
#include <span>
2122
#include <string>
2223
#include <string_view>
2324
#include <type_traits>
@@ -592,8 +593,8 @@ struct ArrayExportChecker {
592593
ASSERT_EQ(c_export->buffers[i], expected_ptr);
593594
}
594595
if (has_variadic_buffer_sizes) {
595-
auto variadic_buffers = util::span(expected_data.buffers).subspan(2);
596-
auto variadic_buffer_sizes = util::span(
596+
auto variadic_buffers = std::span(expected_data.buffers).subspan(2);
597+
auto variadic_buffer_sizes = std::span(
597598
static_cast<const int64_t*>(c_export->buffers[c_export->n_buffers - 1]),
598599
variadic_buffers.size());
599600
for (auto [buf, size] : Zip(variadic_buffers, variadic_buffer_sizes)) {

cpp/src/arrow/chunk_resolver.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@
2121
#include <cstdint>
2222
#include <limits>
2323
#include <memory>
24+
#include <span>
2425
#include <vector>
2526

2627
#include "arrow/array.h"
2728
#include "arrow/record_batch.h"
2829

2930
namespace arrow {
3031

31-
using util::span;
32-
3332
namespace {
3433
template <typename T>
3534
int64_t GetLength(const T& array) {
@@ -44,7 +43,7 @@ int64_t GetLength<std::shared_ptr<RecordBatch>>(
4443
}
4544

4645
template <typename T>
47-
inline std::vector<int64_t> MakeChunksOffsets(span<T> chunks) {
46+
inline std::vector<int64_t> MakeChunksOffsets(std::span<T> chunks) {
4847
std::vector<int64_t> offsets(chunks.size() + 1);
4948
int64_t offset = 0;
5049
std::transform(chunks.begin(), chunks.end(), offsets.begin(),
@@ -114,13 +113,13 @@ void ResolveManyInline(uint32_t num_offsets, const int64_t* signed_offsets,
114113
} // namespace
115114

116115
ChunkResolver::ChunkResolver(const ArrayVector& chunks) noexcept
117-
: offsets_(MakeChunksOffsets(span(chunks))), cached_chunk_(0) {}
116+
: offsets_(MakeChunksOffsets(std::span(chunks))), cached_chunk_(0) {}
118117

119-
ChunkResolver::ChunkResolver(span<const Array* const> chunks) noexcept
118+
ChunkResolver::ChunkResolver(std::span<const Array* const> chunks) noexcept
120119
: offsets_(MakeChunksOffsets(chunks)), cached_chunk_(0) {}
121120

122121
ChunkResolver::ChunkResolver(const RecordBatchVector& batches) noexcept
123-
: offsets_(MakeChunksOffsets(span(batches))), cached_chunk_(0) {}
122+
: offsets_(MakeChunksOffsets(std::span(batches))), cached_chunk_(0) {}
124123

125124
ChunkResolver::ChunkResolver(ChunkResolver&& other) noexcept
126125
: offsets_(std::move(other.offsets_)),

cpp/src/arrow/chunk_resolver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
#include <cassert>
2222
#include <cstdint>
2323
#include <limits>
24+
#include <span>
2425
#include <type_traits>
2526
#include <vector>
2627

2728
#include "arrow/type_fwd.h"
2829
#include "arrow/util/macros.h"
29-
#include "arrow/util/span.h"
3030

3131
namespace arrow {
3232

@@ -77,7 +77,7 @@ class ARROW_EXPORT ChunkResolver {
7777

7878
public:
7979
explicit ChunkResolver(const ArrayVector& chunks) noexcept;
80-
explicit ChunkResolver(util::span<const Array* const> chunks) noexcept;
80+
explicit ChunkResolver(std::span<const Array* const> chunks) noexcept;
8181
explicit ChunkResolver(const RecordBatchVector& batches) noexcept;
8282

8383
/// \brief Construct a ChunkResolver from a vector of chunks.size() + 1 offsets.

0 commit comments

Comments
 (0)