pitrou commented on PR #13009:
URL: https://github.com/apache/arrow/pull/13009#issuecomment-1118508742
Hmm, I think my `Iterate` suggestion was a bit too vague. To satisfy the
requirements for iteration and for call STL algorithms, we need "begin" and
"end" iterators. Basically, I would suggest something like:
```c++
/// 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 stl::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 stl::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
///
/// This makes it easy to use a chunked array in a for-range construct.
template <typename Type, typename ArrayType = typename
TypeTraits<Type>::ArrayType>
ChunkedArrayRange<ArrayType> Iterate(const ChunkedArray& chunked_array) {
return stl::ChunkedArrayRange<ArrayType>(&chunked_array);
}
```
This way you can write:
```c++
for (util::optional<int64_t> : Iterate<Int64Type>(chunked_array)) {
...
```
as well as:
```c++
// Find the first item that's non-null and at least 42
auto it = std::find_if(
Begin<Int64Type>(chunked_array), End<Int64Type>(chunked_array),
[util::optional<int64_t> item]() { return item.has_value() && *item >=
42});
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]