1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
#pragma clang system_header
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#pragma once
#include <cassert>
#include <cstddef>
#include <iterator>
#include <optional>
#include <utility>
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/chunked_array.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/type.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/type_fwd.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/type_traits.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/macros.h"
namespace arrow20 {
namespace stl {
namespace detail {
template <typename ArrayType>
struct DefaultValueAccessor {
using ValueType = decltype(std::declval<ArrayType>().GetView(0));
ValueType operator()(const ArrayType& array, int64_t index) {
return array.GetView(index);
}
};
} // namespace detail
template <typename ArrayType,
typename ValueAccessor = detail::DefaultValueAccessor<ArrayType>>
class ArrayIterator {
public:
using value_type = std::optional<typename ValueAccessor::ValueType>;
using difference_type = int64_t;
using pointer = value_type*;
using reference = value_type&;
using iterator_category = std::random_access_iterator_tag;
// Some algorithms need to default-construct an iterator
ArrayIterator() : array_(NULLPTR), index_(0) {}
explicit ArrayIterator(const ArrayType& array, int64_t index = 0)
: array_(&array), index_(index) {}
// Value access
value_type operator*() const {
assert(array_);
return array_->IsNull(index_) ? value_type{} : array_->GetView(index_);
}
value_type operator[](difference_type n) const {
assert(array_);
return array_->IsNull(index_ + n) ? value_type{} : array_->GetView(index_ + n);
}
int64_t index() const { return index_; }
// Forward / backward
ArrayIterator& operator++() {
++index_;
return *this;
}
ArrayIterator& operator--() {
--index_;
return *this;
}
ArrayIterator operator++(int) {
ArrayIterator tmp(*this);
++index_;
return tmp;
}
ArrayIterator operator--(int) {
ArrayIterator tmp(*this);
--index_;
return tmp;
}
// Arithmetic
difference_type operator-(const ArrayIterator& other) const {
return index_ - other.index_;
}
ArrayIterator operator+(difference_type n) const {
return ArrayIterator(*array_, index_ + n);
}
ArrayIterator operator-(difference_type n) const {
return ArrayIterator(*array_, index_ - n);
}
friend inline ArrayIterator operator+(difference_type diff,
const ArrayIterator& other) {
return ArrayIterator(*other.array_, diff + other.index_);
}
friend inline ArrayIterator operator-(difference_type diff,
const ArrayIterator& other) {
return ArrayIterator(*other.array_, diff - other.index_);
}
ArrayIterator& operator+=(difference_type n) {
index_ += n;
return *this;
}
ArrayIterator& operator-=(difference_type n) {
index_ -= n;
return *this;
}
// Comparisons
bool operator==(const ArrayIterator& other) const { return index_ == other.index_; }
bool operator!=(const ArrayIterator& other) const { return index_ != other.index_; }
bool operator<(const ArrayIterator& other) const { return index_ < other.index_; }
bool operator>(const ArrayIterator& other) const { return index_ > other.index_; }
bool operator<=(const ArrayIterator& other) const { return index_ <= other.index_; }
bool operator>=(const ArrayIterator& other) const { return index_ >= other.index_; }
private:
const ArrayType* array_;
int64_t index_;
};
template <typename ArrayType,
typename ValueAccessor = detail::DefaultValueAccessor<ArrayType>>
class ChunkedArrayIterator {
public:
using value_type = std::optional<typename ValueAccessor::ValueType>;
using difference_type = int64_t;
using pointer = value_type*;
using reference = value_type&;
using iterator_category = std::random_access_iterator_tag;
// Some algorithms need to default-construct an iterator
ChunkedArrayIterator() noexcept : chunked_array_(NULLPTR), index_(0) {}
explicit ChunkedArrayIterator(const ChunkedArray& chunked_array,
int64_t index = 0) noexcept
: chunked_array_(&chunked_array), index_(index) {}
// Value access
value_type operator*() const {
auto chunk_location = GetChunkLocation(index_);
ArrayIterator<ArrayType> target_iterator{
arrow20::internal::checked_cast<const ArrayType&>(
*chunked_array_->chunk(static_cast<int>(chunk_location.chunk_index)))};
return target_iterator[chunk_location.index_in_chunk];
}
value_type operator[](difference_type n) const { return *(*this + n); }
int64_t index() const { return index_; }
// Forward / backward
ChunkedArrayIterator& operator++() {
(*this) += 1;
return *this;
}
ChunkedArrayIterator& operator--() {
(*this) -= 1;
return *this;
}
ChunkedArrayIterator operator++(int) {
ChunkedArrayIterator tmp(*this);
++*this;
return tmp;
}
ChunkedArrayIterator operator--(int) {
ChunkedArrayIterator tmp(*this);
--*this;
return tmp;
}
// Arithmetic
difference_type operator-(const ChunkedArrayIterator& other) const {
return index_ - other.index_;
}
ChunkedArrayIterator operator+(difference_type n) const {
assert(chunked_array_);
return ChunkedArrayIterator(*chunked_array_, index_ + n);
}
ChunkedArrayIterator operator-(difference_type n) const {
assert(chunked_array_);
return ChunkedArrayIterator(*chunked_array_, index_ - n);
}
friend inline ChunkedArrayIterator operator+(difference_type diff,
const ChunkedArrayIterator& other) {
assert(other.chunked_array_);
return ChunkedArrayIterator(*other.chunked_array_, diff + other.index_);
}
friend inline ChunkedArrayIterator operator-(difference_type diff,
const ChunkedArrayIterator& other) {
assert(other.chunked_array_);
return ChunkedArrayIterator(*other.chunked_array_, diff - other.index_);
}
ChunkedArrayIterator& operator+=(difference_type n) {
index_ += n;
return *this;
}
ChunkedArrayIterator& operator-=(difference_type n) {
(*this) += -n;
return *this;
}
// Comparisons
bool operator==(const ChunkedArrayIterator& other) const {
return index_ == other.index_;
}
bool operator!=(const ChunkedArrayIterator& other) const {
return index_ != other.index_;
}
bool operator<(const ChunkedArrayIterator& other) const {
return index_ < other.index_;
}
bool operator>(const ChunkedArrayIterator& other) const {
return index_ > other.index_;
}
bool operator<=(const ChunkedArrayIterator& other) const {
return index_ <= other.index_;
}
bool operator>=(const ChunkedArrayIterator& other) const {
return index_ >= other.index_;
}
private:
arrow20::ChunkLocation GetChunkLocation(int64_t index) const {
assert(chunked_array_);
return chunked_array_->chunk_resolver_.Resolve(index);
}
const ChunkedArray* chunked_array_;
int64_t index_;
};
/// Return an iterator to the beginning of the chunked array
template <typename Type, typename ArrayType = typename TypeTraits<Type>::ArrayType>
ChunkedArrayIterator<ArrayType> Begin(const ChunkedArray& chunked_array) {
return ChunkedArrayIterator<ArrayType>(chunked_array);
}
/// Return an iterator to the end of the chunked array
template <typename Type, typename ArrayType = typename TypeTraits<Type>::ArrayType>
ChunkedArrayIterator<ArrayType> End(const ChunkedArray& chunked_array) {
return ChunkedArrayIterator<ArrayType>(chunked_array, chunked_array.length());
}
template <typename ArrayType>
struct ChunkedArrayRange {
const ChunkedArray* chunked_array;
ChunkedArrayIterator<ArrayType> begin() {
return stl::ChunkedArrayIterator<ArrayType>(*chunked_array);
}
ChunkedArrayIterator<ArrayType> end() {
return stl::ChunkedArrayIterator<ArrayType>(*chunked_array, chunked_array->length());
}
};
/// Return an iterable range over the chunked array
template <typename Type, typename ArrayType = typename TypeTraits<Type>::ArrayType>
ChunkedArrayRange<ArrayType> Iterate(const ChunkedArray& chunked_array) {
return stl::ChunkedArrayRange<ArrayType>{&chunked_array};
}
} // namespace stl
} // namespace arrow20
namespace std {
template <typename ArrayType>
struct iterator_traits<::arrow20::stl::ArrayIterator<ArrayType>> {
using IteratorType = ::arrow20::stl::ArrayIterator<ArrayType>;
using difference_type = typename IteratorType::difference_type;
using value_type = typename IteratorType::value_type;
using pointer = typename IteratorType::pointer;
using reference = typename IteratorType::reference;
using iterator_category = typename IteratorType::iterator_category;
};
template <typename ArrayType>
struct iterator_traits<::arrow20::stl::ChunkedArrayIterator<ArrayType>> {
using IteratorType = ::arrow20::stl::ChunkedArrayIterator<ArrayType>;
using difference_type = typename IteratorType::difference_type;
using value_type = typename IteratorType::value_type;
using pointer = typename IteratorType::pointer;
using reference = typename IteratorType::reference;
using iterator_category = typename IteratorType::iterator_category;
};
} // namespace std
|