You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/04/27 15:10:59 UTC

[GitHub] [arrow] edponce commented on a diff in pull request #13009: ARROW-602: [C++] Provide iterator access to primitive elements inside an Array

edponce commented on code in PR #13009:
URL: https://github.com/apache/arrow/pull/13009#discussion_r859921831


##########
cpp/src/arrow/stl_iterator.h:
##########
@@ -128,6 +131,215 @@ class ArrayIterator {
   int64_t index_;
 };
 
+template <typename ArrayType,
+          typename ValueAccessor = detail::DefaultValueAccessor<ArrayType>>
+class ChunkedArrayIterator {
+ public:
+  using value_type = arrow::util::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() : chunked_array_(NULLPTR), index_(0), current_chunk_index_(0) {}
+
+  explicit ChunkedArrayIterator(const ChunkedArray& chunked_array, int64_t index = 0)
+      : chunked_array_(&chunked_array), index_(index) {
+    InitializeComponents(*this);
+  }
+
+  // Value access
+  value_type operator*() const { return *iterators_list_[current_chunk_index_]; }
+
+  value_type operator[](difference_type n) const {
+    int64_t chunk_index = GetChunkIndex(index_ + n);
+    int64_t index_in_chunk =
+        chunk_index ? index_ + n - chunks_lengths_[chunk_index - 1] : index_ + n;
+    return iterators_list_[chunk_index]
+                          [index_in_chunk - iterators_list_[chunk_index].index()];

Review Comment:
   [`ChunkedArray`](https://github.com/apache/arrow/blob/master/cpp/src/arrow/chunked_array.h#L145) already has `GetScalar(index)` which is efficient for accessing sequential scalars at particular logical indices. I would expect this iterator to make use of it.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org