sparrow 0.9.0
Loading...
Searching...
No Matches
mutable_array_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
20
21namespace sparrow
22{
34 template <class D>
36 {
37 public:
38
41 using derived_type = D;
43
46
47 using bitmap_type = typename inner_types::bitmap_type;
48 using bitmap_reference = bitmap_type::reference;
49 using bitmap_const_reference = bitmap_type::const_reference;
50 using bitmap_iterator = bitmap_type::iterator;
51 using bitmap_range = std::ranges::subrange<bitmap_iterator>;
53
56
57 using inner_reference = typename inner_types::inner_reference;
59
62
63 using value_iterator = typename inner_types::value_iterator;
64
66
75
78
79 [[nodiscard]] reference operator[](size_type i);
80 using base_type::operator[];
81
82 [[nodiscard]] iterator begin();
83 [[nodiscard]] iterator end();
84
85 using base_type::begin;
86 using base_type::end;
87
88 template <typename T>
89 void resize(size_type new_size, const nullable<T>& value);
90
91 template <typename T>
93 template <typename T>
95 template <typename T>
96 iterator insert(const_iterator pos, std::initializer_list<nullable<T>> values);
97
107 template <typename InputIt>
108 requires std::input_iterator<InputIt>
110 iterator insert(const_iterator pos, InputIt first, InputIt last)
111 {
112 SPARROW_ASSERT_TRUE(pos >= this->cbegin())
113 SPARROW_ASSERT_TRUE(pos <= this->cend());
114 SPARROW_ASSERT_TRUE(first <= last);
115 const difference_type distance = std::distance(this->cbegin(), pos);
116 const auto validity_range = std::ranges::subrange(first, last)
117 | std::views::transform(
118 [](const auto& obj)
119 {
120 return obj.has_value();
121 }
122 );
123 auto& derived = this->derived_cast();
124 derived.insert_bitmap(
125 sparrow::next(this->bitmap_cbegin(), distance),
126 validity_range.begin(),
127 validity_range.end()
128 );
129
130 const auto value_range = std::ranges::subrange(first, last)
131 | std::views::transform(
132 [](const auto& obj)
133 {
134 return obj.get();
135 }
136 );
137 derived.insert_values(
138 sparrow::next(derived.value_cbegin(), distance),
139 value_range.begin(),
140 value_range.end()
141 );
142 const difference_type count = std::distance(first, last);
143 // The following must be done after modifying the bitmap and values
144 this->get_arrow_proxy().set_length(this->size() + static_cast<size_t>(count));
145
146 derived.update();
147 return sparrow::next(this->begin(), distance);
148 }
149
159 template <std::ranges::input_range R>
161 iterator insert(const_iterator pos, const R& range)
162 {
163 return insert(pos, std::ranges::begin(range), std::ranges::end(range));
164 }
165
168
169 template <typename T>
170 void push_back(const nullable<T>& value);
171 void pop_back();
172
174
175 protected:
176
178 mutable_array_base(const mutable_array_base&) noexcept = default;
180
182 mutable_array_base& operator=(mutable_array_base&&) noexcept = default;
183
184 [[nodiscard]] bitmap_reference has_value(size_type i);
185 using base_type::has_value;
186
187 [[nodiscard]] bitmap_iterator bitmap_begin();
189
190 friend class layout_iterator<iterator_types>;
191 };
192
193 /*************************************
194 * mutable_array_base implementation *
195 *************************************/
196
197 template <class D>
199 : array_crtp_base<D>(std::forward<arrow_proxy>(proxy))
200 {
201 }
202
206 template <class D>
208 {
209 auto& derived_cast = this->derived_cast();
210 return iterator(derived_cast.value_begin(), derived_cast.bitmap_begin());
211 }
212
217 template <class D>
219 {
220 auto& derived_cast = this->derived_cast();
221 return iterator(derived_cast.value_end(), derived_cast.bitmap_end());
222 }
223
229 template <class D>
231 {
232 SPARROW_ASSERT_TRUE(i < this->size());
233 auto& derived_cast = this->derived_cast();
234 return reference(inner_reference(derived_cast.value(i)), derived_cast.has_value(i));
235 }
236
237 template <class D>
243
244 template <class D>
246 {
247 return sparrow::next(this->derived_cast().get_bitmap().begin(), this->get_arrow_proxy().offset());
248 }
249
250 template <class D>
255
264 template <class D>
265 template <typename T>
267 {
268 auto& derived = this->derived_cast();
269 derived.resize_bitmap(new_length, value.has_value());
270 derived.resize_values(new_length, value.get());
271 this->get_arrow_proxy().set_length(new_length); // Must be done after resizing the bitmap and values
272 derived.update();
273 }
274
282 template <class D>
283 template <typename T>
285 {
286 return insert(pos, value, 1);
287 }
288
297 template <class D>
298 template <typename T>
300 -> iterator
301 {
302 SPARROW_ASSERT_TRUE(pos >= this->cbegin());
303 SPARROW_ASSERT_TRUE(pos <= this->cend());
304 const size_t distance = static_cast<size_t>(std::distance(this->cbegin(), pos));
305 auto& derived = this->derived_cast();
306 derived.insert_bitmap(sparrow::next(this->bitmap_cbegin(), distance), value.has_value(), count);
307 derived.insert_value(sparrow::next(derived.value_cbegin(), distance), value.get(), count);
308 this->get_arrow_proxy().set_length(this->size() + count); // Must be done after resizing the bitmap
309 // and values
310 derived.update();
311 return sparrow::next(this->begin(), distance);
312 }
313
321 template <class D>
322 template <typename T>
324 -> iterator
325 {
326 return insert(pos, values.begin(), values.end());
327 }
328
335 template <class D>
337 {
338 SPARROW_ASSERT_TRUE(this->cbegin() <= pos)
339 SPARROW_ASSERT_TRUE(pos < this->cend());
340 return erase(pos, pos + 1);
341 }
342
350 template <class D>
352 {
353 SPARROW_ASSERT_TRUE(first < last);
354 SPARROW_ASSERT_TRUE(this->cbegin() <= first)
355 SPARROW_ASSERT_TRUE(last <= this->cend());
356 const difference_type first_index = std::distance(this->cbegin(), first);
357 if (first == last)
358 {
359 return sparrow::next(begin(), first_index);
360 }
361 const auto count = static_cast<size_t>(std::distance(first, last));
362 auto& derived = this->derived_cast();
363 derived.erase_bitmap(sparrow::next(this->bitmap_cbegin(), first_index), count);
364 derived.erase_values(sparrow::next(derived.value_cbegin(), first_index), count);
365 this->get_arrow_proxy().set_length(this->size() - count); // Must be done after modifying the bitmap
366 // and values
367 derived.update();
368 return sparrow::next(begin(), first_index);
369 }
370
376 template <class D>
377 template <typename T>
379 {
380 insert(this->cend(), value);
381 }
382
386 template <class D>
388 {
389 erase(std::prev(this->cend()));
390 }
391
397 template <class D>
402}
const_value_range values() const
Returns the raw values of the array (i.e.
arrow_proxy & get_arrow_proxy()
nullable< inner_const_reference, bitmap_const_reference > const_reference
typename inner_types::iterator_tag iterator_tag
std::ranges::subrange< const_bitmap_iterator > const_bitmap_range
const_bitmap_iterator bitmap_cbegin() const
const_iterator begin() const
Returns a constant iterator to the first element of the array.
const_iterator end() const
Returns a constant iterator to the element following the last element of the array.
typename inner_types::inner_const_reference inner_const_reference
size_type size() const
Returns the number of elements in the array.
nullable< inner_value_type > value_type
const_iterator cbegin() const
Returns a constant iterator to the first element of the array.
std::ptrdiff_t difference_type
layout_iterator< iterator_types > const_iterator
const_iterator cend() const
Returns a constant iterator to the element following the last element of the array.
typename inner_types::inner_value_type inner_value_type
Proxy class over ArrowArray and ArrowSchema.
SPARROW_API void set_length(size_t length)
Set the length of the ArrowArray.
derived_type & derived_cast()
Definition crtp_base.hpp:39
Layout iterator class.
void pop_back()
Removes the last element of the array.
reference operator[](size_type i)
Returns a reference to the element at the specified position in the array.
iterator insert(const_iterator pos, InputIt first, InputIt last)
Inserts elements from range [first , last ) before pos in the array.
base_type::const_reference const_reference
typename base_type::inner_value_type inner_value_type
bitmap_type::const_reference bitmap_const_reference
mutable_array_base(const mutable_array_base &) noexcept=default
mutable_array_base(mutable_array_base &&) noexcept=default
iterator insert(const_iterator pos, const nullable< T > &value)
array_inner_types< derived_type > inner_types
void push_back(const nullable< T > &value)
Appends a copy of value to the end of the array.
typename inner_types::value_iterator value_iterator
typename base_type::value_type value_type
iterator insert(const_iterator pos, std::initializer_list< nullable< T > > values)
mutable_array_base< D > self_type
iterator insert(const_iterator pos, const nullable< T > &value, size_type count)
base_type::difference_type difference_type
layout_iterator< iterator_types > iterator
base_type::const_iterator const_iterator
typename base_type::inner_const_reference inner_const_reference
void resize(size_type new_size, const nullable< T > &value)
Resizes the array to contain new_length elements, does nothing if new_length == size().
typename inner_types::inner_reference inner_reference
iterator begin()
Returns an iterator to the first element of the array.
base_type::iterator_tag iterator_tag
std::ranges::subrange< bitmap_iterator > bitmap_range
bitmap_reference has_value(size_type i)
nullable< inner_reference, bitmap_reference > reference
bitmap_type::reference bitmap_reference
void zero_null_values(const inner_value_type &value=inner_value_type())
Sets all null values in the array to zero.
bitmap_type::iterator bitmap_iterator
base_type::const_bitmap_range const_bitmap_range
iterator erase(const_iterator pos)
Removes the element at pos from the array.
iterator end()
Returns a iterator to the element following the last element of the array.
typename inner_types::bitmap_type bitmap_type
mutable_array_base & operator=(const mutable_array_base &) noexcept=default
iterator insert(const_iterator pos, const R &range)
Inserts elements from range range before pos in the array.
The nullable class models a value or a reference that can be "null", or missing, like values traditio...
Definition nullable.hpp:281
constexpr bool has_value() const noexcept
Definition nullable.hpp:578
constexpr reference get() &noexcept
Definition nullable.hpp:622
Concept for iterator types.
#define SPARROW_ASSERT_TRUE(expr__)
constexpr bool is_type_instance_of_v
true if T is a concrete type template instanciation of U which is a type template.
Definition mp_utils.hpp:50
constexpr void zero_null_values(R &range, const T &default_value=T{})
Definition nullable.hpp:779
constexpr InputIt next(InputIt it, Distance n)
Definition iterator.hpp:503
Traits class that must be specialized by array classes inheriting from array_crtp_base.