sparrow ..
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
26
27 // this iteratas over the **actual** values of the run encoded array
28 // Ie nullabes values, not values !!!
29 template <bool is_const>
31 run_encoded_array_iterator<is_const>,
32 array_traits::const_reference,
33 std::bidirectional_iterator_tag,
34 array_traits::const_reference>
35 {
36 private:
37
38 using array_ptr_type = std::conditional_t<is_const, const run_end_encoded_array*, run_end_encoded_array*>;
39
40 public:
41
43 run_encoded_array_iterator(array_ptr_type array_ptr, std::uint64_t index, std::uint64_t run_end_index);
44
45 private:
46
47 [[nodiscard]] bool equal(const run_encoded_array_iterator& rhs) const;
48 void increment();
49 void decrement();
50 [[nodiscard]] array_traits::const_reference dereference() const;
51
52 array_ptr_type p_array = nullptr;
53 array_wrapper* p_encoded_values_array = nullptr;
54 std::uint64_t m_index = 0; // the current index / the index the user sees
55 std::uint64_t m_run_end_index = 0; // the current index in the run ends array
56 std::uint64_t m_acc_length_up = 0; // the accumulated length at m_run_end_index
57 std::uint64_t m_acc_length_down = 0; // the accumulated length at m_run_end_index-1
58
59 friend class iterator_access;
60 };
61
62 template <bool is_const>
64 array_ptr_type array_ptr,
65 std::uint64_t index,
66 std::uint64_t run_end_index
67 )
68 : p_array(array_ptr)
69 , p_encoded_values_array(array_ptr->p_encoded_values_array.get())
70 , m_index(index)
71 , m_run_end_index(run_end_index)
72 , m_acc_length_up(
73 m_index < p_array->size() ? p_array->get_acc_length(m_run_end_index) : p_array->m_encoded_length
74 )
75 , m_acc_length_down(m_run_end_index == 0 ? 0 : p_array->get_acc_length(m_run_end_index - 1))
76 {
77 }
78
79 template <bool is_const>
80 bool run_encoded_array_iterator<is_const>::equal(const run_encoded_array_iterator& rhs) const
81 {
82 return m_index == rhs.m_index;
83 }
84
85 template <bool is_const>
86 void run_encoded_array_iterator<is_const>::increment()
87 {
88 ++m_index;
89 if (m_index == 0)
90 {
91 m_run_end_index = 0;
92 m_acc_length_up = p_array->get_acc_length(m_run_end_index);
93 m_acc_length_down = 0;
94 }
95 else if (m_index >= p_array->size())
96 {
97 m_run_end_index = p_array->m_encoded_length;
98 }
99 else if (m_index == m_acc_length_up)
100 {
101 ++m_run_end_index;
102 m_acc_length_up = p_array->get_acc_length(m_run_end_index);
103 m_acc_length_down = p_array->get_acc_length(m_run_end_index - 1);
104 }
105 }
106
107 template <bool is_const>
108 void run_encoded_array_iterator<is_const>::decrement()
109 {
110 if (m_index == 0)
111 {
112 m_run_end_index = p_array->m_encoded_length;
113 }
114 else if (m_index == p_array->size() || m_index == m_acc_length_down)
115 {
116 --m_run_end_index;
117 m_acc_length_up = p_array->get_acc_length(m_run_end_index);
118 m_acc_length_down = m_run_end_index == 0 ? 0 : p_array->get_acc_length(m_run_end_index - 1);
119 }
120 --m_index;
121 }
122
123 template <bool is_const>
124 typename array_traits::const_reference run_encoded_array_iterator<is_const>::dereference() const
125 {
126 return array_element(*p_encoded_values_array, static_cast<std::size_t>(m_run_end_index));
127 }
128
129} // namespace sparrow
Base class for array type erasure.
run_encoded_array_iterator(array_ptr_type array_ptr, std::uint64_t index, std::uint64_t run_end_index)
SPARROW_API array_traits::const_reference array_element(const array_wrapper &ar, std::size_t index)
mpl::rename< mpl::unique< mpl::transform< detail::array_const_reference_t, all_base_types_t > >, nullable_variant > const_reference