sparrow 2.4.0
C++20 idiomatic APIs for the Apache Arrow Columnar Format
Loading...
Searching...
No Matches
run_end_encoded_iterator.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
21
22namespace sparrow
23{
24
27
28 // this iteratas over the **actual** values of the run encoded array
29 // Ie nullabes values, not values !!!
30 template <bool is_const>
32 : public iterator_base<
33 run_encoded_array_iterator<is_const>,
34 array_traits::value_type,
35 std::bidirectional_iterator_tag,
36 std::conditional_t<is_const, array_traits::const_reference, run_end_encoded_reference>>
37 {
38 private:
39
40 using array_ptr_type = std::conditional_t<is_const, const run_end_encoded_array*, run_end_encoded_array*>;
41 using reference_type = std::conditional_t<is_const, array_traits::const_reference, run_end_encoded_reference>;
42
43 public:
44
46 run_encoded_array_iterator(array_ptr_type array_ptr, std::uint64_t index, std::uint64_t run_end_index);
47
48 private:
49
50 [[nodiscard]] bool equal(const run_encoded_array_iterator& rhs) const;
51 void increment();
52 void decrement();
53 [[nodiscard]] reference_type dereference() const;
54
55 array_ptr_type p_array = nullptr;
56 const array* p_encoded_values_array = nullptr;
57 std::uint64_t m_index = 0; // the current index / the index the user sees
58 std::uint64_t m_run_end_index = 0; // the current index in the run ends array
59 std::uint64_t m_acc_length_up = 0; // the accumulated length at m_run_end_index
60 std::uint64_t m_acc_length_down = 0; // the accumulated length at m_run_end_index-1
61
62 friend class iterator_access;
63 };
64
65 template <bool is_const>
67 array_ptr_type array_ptr,
68 std::uint64_t index,
69 std::uint64_t run_end_index
70 )
71 : p_array(array_ptr)
72 , p_encoded_values_array(&array_ptr->p_encoded_values_array)
73 , m_index(index)
74 , m_run_end_index(run_end_index)
75 , m_acc_length_up(
76 m_index < p_array->size() ? p_array->get_acc_length(m_run_end_index) : p_array->m_encoded_length
77 )
78 , m_acc_length_down(m_run_end_index == 0 ? 0 : p_array->get_acc_length(m_run_end_index - 1))
79 {
80 }
81
82 template <bool is_const>
83 bool run_encoded_array_iterator<is_const>::equal(const run_encoded_array_iterator& rhs) const
84 {
85 return m_index == rhs.m_index;
86 }
87
88 template <bool is_const>
89 void run_encoded_array_iterator<is_const>::increment()
90 {
91 ++m_index;
92 if (m_index == 0)
93 {
94 m_run_end_index = 0;
95 m_acc_length_up = p_array->get_acc_length(m_run_end_index);
96 m_acc_length_down = 0;
97 }
98 else if (m_index >= p_array->size())
99 {
100 m_run_end_index = p_array->m_encoded_length;
101 }
102 else if (m_index == m_acc_length_up)
103 {
104 ++m_run_end_index;
105 m_acc_length_up = p_array->get_acc_length(m_run_end_index);
106 m_acc_length_down = p_array->get_acc_length(m_run_end_index - 1);
107 }
108 }
109
110 template <bool is_const>
111 void run_encoded_array_iterator<is_const>::decrement()
112 {
113 if (m_index == 0)
114 {
115 m_run_end_index = p_array->m_encoded_length;
116 }
117 else if (m_index == p_array->size() || m_index == m_acc_length_down)
118 {
119 --m_run_end_index;
120 m_acc_length_up = p_array->get_acc_length(m_run_end_index);
121 m_acc_length_down = m_run_end_index == 0 ? 0 : p_array->get_acc_length(m_run_end_index - 1);
122 }
123 --m_index;
124 }
125
126 template <bool is_const>
127 auto run_encoded_array_iterator<is_const>::dereference() const -> reference_type
128 {
129 if constexpr (is_const)
130 {
131 return (*p_encoded_values_array)[static_cast<std::size_t>(m_run_end_index)];
132 }
133 else
134 {
135 return reference_type(
136 *p_array,
137 static_cast<std::size_t>(m_index),
138 static_cast<std::size_t>(m_run_end_index)
139 );
140 }
141 }
142
143} // namespace sparrow
Dynamically typed array encapsulating an Arrow layout.
Definition array_api.hpp:50
run_encoded_array_iterator(array_ptr_type array_ptr, std::uint64_t index, std::uint64_t run_end_index)