sparrow 0.6.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
19
20namespace sparrow
21{
33 template <class D>
35 {
36 public:
37
40 using derived_type = D;
42
45
46 using bitmap_type = typename inner_types::bitmap_type;
47 using bitmap_reference = bitmap_type::reference;
48 using bitmap_const_reference = bitmap_type::const_reference;
49 using bitmap_iterator = bitmap_type::iterator;
50 using bitmap_range = std::ranges::subrange<bitmap_iterator>;
52
55
56 using inner_reference = typename inner_types::inner_reference;
58
61
62 using value_iterator = typename inner_types::value_iterator;
63
65
74
77
78 [[nodiscard]] reference operator[](size_type i);
79 using base_type::operator[];
80
81 [[nodiscard]] iterator begin();
82 [[nodiscard]] iterator end();
83
84 using base_type::begin;
85 using base_type::end;
86
87 template <typename T>
88 void resize(size_type new_size, const nullable<T>& value);
89
90 template <typename T>
92 template <typename T>
94 template <typename T>
95 iterator insert(const_iterator pos, std::initializer_list<nullable<T>> values);
96
106 template <typename InputIt>
107 requires std::input_iterator<InputIt>
109 iterator insert(const_iterator pos, InputIt first, InputIt last)
110 {
111 SPARROW_ASSERT_TRUE(pos >= this->cbegin())
112 SPARROW_ASSERT_TRUE(pos <= this->cend());
113 SPARROW_ASSERT_TRUE(first <= last);
114 const difference_type distance = std::distance(this->cbegin(), pos);
115 const auto validity_range = std::ranges::subrange(first, last)
116 | std::views::transform(
117 [](const auto& obj)
118 {
119 return obj.has_value();
120 }
121 );
122 auto& derived = this->derived_cast();
123 derived.insert_bitmap(
124 sparrow::next(this->bitmap_cbegin(), distance),
125 validity_range.begin(),
126 validity_range.end()
127 );
128
129 const auto value_range = std::ranges::subrange(first, last)
130 | std::views::transform(
131 [](const auto& obj)
132 {
133 return obj.get();
134 }
135 );
136 derived.insert_values(
137 sparrow::next(derived.value_cbegin(), distance),
138 value_range.begin(),
139 value_range.end()
140 );
141 const difference_type count = std::distance(first, last);
142 // The following must be done after modifying the bitmap and values
143 this->get_arrow_proxy().set_length(this->size() + static_cast<size_t>(count));
144
145 derived.update();
146 return sparrow::next(this->begin(), distance);
147 }
148
158 template <std::ranges::input_range R>
160 iterator insert(const_iterator pos, const R& range)
161 {
162 return insert(pos, std::ranges::begin(range), std::ranges::end(range));
163 }
164
167
168 template <typename T>
169 void push_back(const nullable<T>& value);
170 void pop_back();
171
172 protected:
173
177
180
181 [[nodiscard]] bitmap_reference has_value(size_type i);
183
184 [[nodiscard]] bitmap_iterator bitmap_begin();
186
187 friend class layout_iterator<iterator_types>;
188 };
189
190 /*************************************
191 * mutable_array_base implementation *
192 *************************************/
193
194 template <class D>
199
203 template <class D>
205 {
206 return iterator(this->derived_cast().value_begin(), this->derived_cast().bitmap_begin());
207 }
208
213 template <class D>
215 {
216 return iterator(this->derived_cast().value_end(), this->derived_cast().bitmap_end());
217 }
218
224 template <class D>
226 {
227 SPARROW_ASSERT_TRUE(i < this->size());
228 return reference(inner_reference(this->derived_cast().value(i)), this->derived_cast().has_value(i));
229 }
230
231 template <class D>
237
238 template <class D>
240 {
241 return sparrow::next(this->derived_cast().get_bitmap().begin(), this->get_arrow_proxy().offset());
242 }
243
244 template <class D>
249
258 template <class D>
259 template <typename T>
261 {
262 auto& derived = this->derived_cast();
263 derived.resize_bitmap(new_length, value.has_value());
264 derived.resize_values(new_length, value.get());
265 this->get_arrow_proxy().set_length(new_length); // Must be done after resizing the bitmap and values
266 derived.update();
267 }
268
276 template <class D>
277 template <typename T>
279 {
280 return insert(pos, value, 1);
281 }
282
291 template <class D>
292 template <typename T>
294 -> iterator
295 {
296 SPARROW_ASSERT_TRUE(pos >= this->cbegin());
297 SPARROW_ASSERT_TRUE(pos <= this->cend());
298 const size_t distance = static_cast<size_t>(std::distance(this->cbegin(), pos));
299 auto& derived = this->derived_cast();
300 derived.insert_bitmap(sparrow::next(this->bitmap_cbegin(), distance), value.has_value(), count);
301 derived.insert_value(sparrow::next(derived.value_cbegin(), distance), value.get(), count);
302 this->get_arrow_proxy().set_length(this->size() + count); // Must be done after resizing the bitmap
303 // and values
304 derived.update();
305 return sparrow::next(this->begin(), distance);
306 }
307
315 template <class D>
316 template <typename T>
318 -> iterator
319 {
320 return insert(pos, values.begin(), values.end());
321 }
322
329 template <class D>
331 {
332 SPARROW_ASSERT_TRUE(this->cbegin() <= pos)
333 SPARROW_ASSERT_TRUE(pos < this->cend());
334 return erase(pos, pos + 1);
335 }
336
344 template <class D>
346 {
347 SPARROW_ASSERT_TRUE(first < last);
348 SPARROW_ASSERT_TRUE(this->cbegin() <= first)
349 SPARROW_ASSERT_TRUE(last <= this->cend());
350 const difference_type first_index = std::distance(this->cbegin(), first);
351 if (first == last)
352 {
353 return sparrow::next(begin(), first_index);
354 }
355 const auto count = static_cast<size_t>(std::distance(first, last));
356 auto& derived = this->derived_cast();
357 derived.erase_bitmap(sparrow::next(this->bitmap_cbegin(), first_index), count);
358 derived.erase_values(sparrow::next(derived.value_cbegin(), first_index), count);
359 this->get_arrow_proxy().set_length(this->size() - count); // Must be done after modifying the bitmap
360 // and values
361 derived.update();
362 return sparrow::next(begin(), first_index);
363 }
364
370 template <class D>
371 template <typename T>
373 {
374 insert(this->cend(), value);
375 }
376
380 template <class D>
382 {
383 erase(std::prev(this->cend()));
384 }
385}
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
bitmap_const_reference has_value(size_type i) const
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
mutable_array_base(mutable_array_base &&)=default
bitmap_type::const_reference bitmap_const_reference
mutable_array_base & operator=(const mutable_array_base &)=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
mutable_array_base & operator=(mutable_array_base &&)=default
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
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.
mutable_array_base(const mutable_array_base &)=default
iterator end()
Returns a iterator to the element following the last element of the array.
typename inner_types::bitmap_type bitmap_type
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:280
constexpr bool has_value() const noexcept
Definition nullable.hpp:565
constexpr reference get() &noexcept
Definition nullable.hpp:609
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 InputIt next(InputIt it, Distance n)
Definition iterator.hpp:503
Traits class that must be specialized by array classes inheriting from array_crtp_base.