sparrow 0.3.0
Loading...
Searching...
No Matches
array_bitmap_base.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 mplied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
19
20namespace sparrow
21{
31 template <class D, bool is_mutable>
33 : public std::conditional_t<is_mutable, mutable_array_base<D>, array_crtp_base<D>>
34 {
35 public:
36
37 using base_type = std::conditional_t<is_mutable, mutable_array_base<D>, array_crtp_base<D>>;
38
39 using size_type = std::size_t; // typename base_type::size_type;
40
41 using bitmap_type = typename base_type::bitmap_type;
42 using bitmap_iterator = typename base_type::bitmap_iterator;
43 using const_bitmap_iterator = typename base_type::const_bitmap_iterator;
44
45 using bitmap_const_reference = typename base_type::bitmap_const_reference;
46 using difference_type = typename base_type::difference_type;
47
48 using const_bitmap_range = typename base_type::const_bitmap_range;
49
50 using iterator_tag = typename base_type::iterator_tag;
51
52 protected:
53
55
58
60 array_bitmap_base_impl& operator=(array_bitmap_base_impl&&) noexcept = default;
61
62 [[nodiscard]] bitmap_type& get_bitmap()
63 requires is_mutable;
64 [[nodiscard]] const bitmap_type& get_bitmap() const;
65
66 void resize_bitmap(size_type new_length, bool value)
67 requires is_mutable;
68
70 requires is_mutable;
71
72 template <std::input_iterator InputIt>
73 requires std::same_as<typename std::iterator_traits<InputIt>::value_type, bool>
74 bitmap_iterator insert_bitmap(const_bitmap_iterator pos, InputIt first, InputIt last)
75 requires is_mutable;
76
78 requires is_mutable;
79
80 void update()
81 requires is_mutable;
82
84
85 [[nodiscard]] bitmap_type make_bitmap();
86
87 private:
88
89 bitmap_type m_bitmap;
90
91 friend array_crtp_base<D>;
92 friend mutable_array_base<D>;
93 };
94
101 template <class D>
103
110 template <class D>
112
113 /************************************
114 * array_bitmap_base implementation *
115 ************************************/
116
117 template <class D, bool is_mutable>
119 : base_type(std::move(proxy_param))
120 , m_bitmap(make_bitmap())
121 {
122 }
123
124 template <class D, bool is_mutable>
130
131 template <class D, bool is_mutable>
134 {
135 base_type::operator=(rhs);
136 m_bitmap = make_bitmap();
137 return *this;
138 }
139
140 template <class D, bool is_mutable>
142 requires is_mutable
143 {
144 return m_bitmap;
145 }
146
147 template <class D, bool is_mutable>
149 {
150 return m_bitmap;
151 }
152
153 template <class D, bool is_mutable>
155 {
156 static constexpr size_t bitmap_buffer_index = 0;
157 arrow_proxy& arrow_proxy = this->get_arrow_proxy();
158 SPARROW_ASSERT_TRUE(arrow_proxy.buffers().size() > bitmap_buffer_index);
159 const auto bitmap_size = arrow_proxy.length() + arrow_proxy.offset();
160 return bitmap_type(arrow_proxy.buffers()[bitmap_buffer_index].data(), bitmap_size);
161 }
162
163 template <class D, bool is_mutable>
165 requires is_mutable
166 {
167 arrow_proxy& arrow_proxy = this->get_arrow_proxy();
168 const size_t new_size = new_length + arrow_proxy.offset();
169 arrow_proxy.resize_bitmap(new_size, value);
170 }
171
172 template <class D, bool is_mutable>
173 auto
176 requires is_mutable
177 {
178 SPARROW_ASSERT_TRUE(this->bitmap_cbegin() <= pos)
179 SPARROW_ASSERT_TRUE(pos <= this->bitmap_cend())
180 arrow_proxy& arrow_proxy = this->get_arrow_proxy();
181 const auto pos_index = static_cast<size_t>(std::distance(this->bitmap_cbegin(), pos))
183 const auto idx = arrow_proxy.insert_bitmap(pos_index, value, count);
184 return sparrow::next(this->bitmap_begin(), idx);
185 }
186
187 template <class D, bool is_mutable>
188 template <std::input_iterator InputIt>
189 requires std::same_as<typename std::iterator_traits<InputIt>::value_type, bool>
190 auto
193 requires is_mutable
194 {
195 SPARROW_ASSERT_TRUE(this->bitmap_cbegin() <= pos)
196 SPARROW_ASSERT_TRUE(pos <= this->bitmap_cend());
197 SPARROW_ASSERT_TRUE(first <= last);
198 arrow_proxy& arrow_proxy = this->get_arrow_proxy();
199 const auto pos_index = static_cast<size_t>(std::distance(this->bitmap_cbegin(), pos))
201 const auto idx = arrow_proxy.insert_bitmap(pos_index, std::ranges::subrange(first, last));
202 return sparrow::next(this->bitmap_begin(), idx);
203 }
204
205 template <class D, bool is_mutable>
208 requires is_mutable
209 {
210 SPARROW_ASSERT_TRUE(this->bitmap_cbegin() <= pos)
211 SPARROW_ASSERT_TRUE(pos < this->bitmap_cend())
212 arrow_proxy& arrow_proxy = this->get_arrow_proxy();
213 const auto pos_idx = static_cast<size_t>(std::distance(this->bitmap_cbegin(), pos));
214 const auto idx = arrow_proxy.erase_bitmap(pos_idx, count);
215 return sparrow::next(this->bitmap_begin(), idx);
216 }
217
218 template <class D, bool is_mutable>
220 requires is_mutable
221 {
222 m_bitmap = make_bitmap();
223 }
224}
Base class for arrays using a validity buffer for defining their bitmap.
array_bitmap_base_impl(const array_bitmap_base_impl &)
typename base_type::const_bitmap_range const_bitmap_range
typename base_type::bitmap_iterator bitmap_iterator
array_bitmap_base_impl(array_bitmap_base_impl &&) noexcept=default
bitmap_iterator erase_bitmap(const_bitmap_iterator pos, size_type count)
array_bitmap_base_impl & operator=(const array_bitmap_base_impl &)
typename base_type::iterator_tag iterator_tag
typename base_type::const_bitmap_iterator const_bitmap_iterator
void resize_bitmap(size_type new_length, bool value)
non_owning_dynamic_bitset< uint8_t > get_non_owning_dynamic_bitset()
std::conditional_t< is_mutable, mutable_array_base< D >, array_crtp_base< D > > base_type
typename base_type::bitmap_const_reference bitmap_const_reference
typename base_type::bitmap_type bitmap_type
bitmap_iterator insert_bitmap(const_bitmap_iterator pos, bool value, size_type count)
typename base_type::difference_type difference_type
Base class defining common immutable interface for arrays with a bitmap.
Proxy class over ArrowArray and ArrowSchema.
SPARROW_API size_t offset() const
SPARROW_API size_t length() const
SPARROW_API void resize_bitmap(size_t new_size, bool value=true)
Resize the bitmap buffer of the ArrowArray.
SPARROW_API size_t erase_bitmap(size_t index, size_t count=1)
Erase several elements in the bitmap buffer at the given index.
SPARROW_API const std::vector< sparrow::buffer_view< uint8_t > > & buffers() const
SPARROW_API size_t insert_bitmap(size_t index, bool value, size_t count=1)
Insert elements of the same value in the bitmap buffer at the given index.
Base class definining common interface for arrays with a bitmap.
#define SPARROW_ASSERT_TRUE(expr__)
array_bitmap_base_impl< D, true > mutable_array_bitmap_base
Convenient typedef to be used as a crtp base class for arrays using a mutable validity buffer.
constexpr InputIt next(InputIt it, Distance n)
Definition iterator.hpp:503
array_bitmap_base_impl< D, false > array_bitmap_base
Convenient typedef to be used as a crtp base class for arrays using an immutable validity buffer.