sparrow
2.4.0
C++20 idiomatic APIs for the Apache Arrow Columnar Format
Toggle main menu visibility
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
17
#include "
sparrow/layout/array_helper.hpp
"
18
#include "
sparrow/layout/array_wrapper.hpp
"
19
#include "
sparrow/layout/nested_value_types.hpp
"
20
#include "
sparrow/utils/iterator.hpp
"
21
22
namespace
sparrow
23
{
24
25
class
run_end_encoded_array
;
26
class
run_end_encoded_reference
;
27
28
// this iteratas over the **actual** values of the run encoded array
29
// Ie nullabes values, not values !!!
30
template
<
bool
is_const>
31
class
run_encoded_array_iterator
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
45
run_encoded_array_iterator
() =
default
;
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>
66
run_encoded_array_iterator<is_const>::run_encoded_array_iterator
(
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
array_helper.hpp
array_wrapper.hpp
sparrow::array
Dynamically typed array encapsulating an Arrow layout.
Definition
array_api.hpp:50
sparrow::iterator_base
Definition
iterator.hpp:381
sparrow::run_encoded_array_iterator
Definition
run_end_encoded_iterator.hpp:37
sparrow::run_encoded_array_iterator::run_encoded_array_iterator
run_encoded_array_iterator(array_ptr_type array_ptr, std::uint64_t index, std::uint64_t run_end_index)
Definition
run_end_encoded_iterator.hpp:66
sparrow::run_encoded_array_iterator::run_encoded_array_iterator
run_encoded_array_iterator()=default
sparrow::run_encoded_array_iterator< false >::iterator_access
friend class iterator_access
Definition
run_end_encoded_iterator.hpp:62
sparrow::run_end_encoded_array
A run-end encoded array.
Definition
run_end_encoded_array.hpp:101
sparrow::run_end_encoded_reference
Definition
run_end_encoded_array.hpp:28
iterator.hpp
sparrow
Definition
array.hpp:21
nested_value_types.hpp
sparrow
layout
run_end_encoded_iterator.hpp
Generated by
1.17.0