sparrow 0.3.0
Loading...
Searching...
No Matches
array_trivial_copyable.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
16#pragma once
17
25
26namespace sparrow
27{
28 template <trivial_copyable_type T>
30
31 template <trivial_copyable_type T>
50
51 template <trivial_copyable_type T>
52 class array_trivial_copyable : public mutable_array_bitmap_base<array_trivial_copyable<T>>,
53 public details::trivial_copyable_data_access<T, array_trivial_copyable<T>>
54 {
55 public:
56
60 using size_type = std::size_t;
61
63 using inner_value_type = typename inner_types::inner_value_type;
64 using inner_reference = typename inner_types::inner_reference;
65 using inner_const_reference = typename inner_types::inner_const_reference;
66
67 using pointer = typename inner_types::pointer;
68 using const_pointer = typename inner_types::const_pointer;
69
70 using value_iterator = typename base_type::value_iterator;
71 using const_value_iterator = typename base_type::const_value_iterator;
72
74
99 template <class... Args>
101 explicit array_trivial_copyable(Args&&... args)
102 : base_type(create_proxy(std::forward<Args>(args)...))
104 {
105 }
106
111 std::initializer_list<inner_value_type> init,
112 std::optional<std::string_view> name = std::nullopt,
113 std::optional<std::string_view> metadata = std::nullopt
114 )
115 : base_type(create_proxy(init, std::move(name), std::move(metadata)))
117 {
118 }
119
122
124 : base_type(std::move(rhs))
126 {
127 }
128
130 {
131 base_type::operator=(std::move(rhs));
132 return *this;
133 }
134
135 protected:
136
138 size_type n,
139 std::optional<std::string_view> name = std::nullopt,
140 std::optional<std::string_view> metadata = std::nullopt
141 );
142
143 template <validity_bitmap_input R = validity_bitmap>
144 static auto create_proxy(
145 u8_buffer<T>&& data_buffer,
146 R&& bitmaps = validity_bitmap{},
147 std::optional<std::string_view> name = std::nullopt,
148 std::optional<std::string_view> metadata = std::nullopt
149 ) -> arrow_proxy;
150
151 // range of values (no missing values)
152 template <std::ranges::input_range R>
153 requires std::convertible_to<std::ranges::range_value_t<R>, T>
154 static auto create_proxy(
155 R&& range,
156 std::optional<std::string_view> name = std::nullopt,
157 std::optional<std::string_view> metadata = std::nullopt
158 ) -> arrow_proxy;
159
160 template <class U>
161 requires std::convertible_to<U, T>
163 size_type n,
164 const U& value = U{},
165 std::optional<std::string_view> name = std::nullopt,
166 std::optional<std::string_view> metadata = std::nullopt
167 );
168
169 // range of values, validity_bitmap_input
170 template <std::ranges::input_range R, validity_bitmap_input R2>
171 requires(std::convertible_to<std::ranges::range_value_t<R>, T>)
173 R&&,
174 R2&&,
175 std::optional<std::string_view> name = std::nullopt,
176 std::optional<std::string_view> metadata = std::nullopt
177 );
178
179 // range of nullable values
180 template <std::ranges::input_range R>
181 requires std::is_same_v<std::ranges::range_value_t<R>, nullable<T>>
183 R&&,
184 std::optional<std::string_view> name = std::nullopt,
185 std::optional<std::string_view> metadata = std::nullopt
186 );
187
190
193
196
197 // Modifiers
198
203
204 static constexpr size_type DATA_BUFFER_INDEX = 1;
205
207 friend base_type;
210 };
211
212 /********************************************************
213 * array_trivial_copyable implementation *
214 ********************************************************/
215
216 template <trivial_copyable_type T>
222
223 template <trivial_copyable_type T>
229
230 template <trivial_copyable_type T>
236
237 template <trivial_copyable_type T>
242
243 template <trivial_copyable_type T>
245 {
246 return sparrow::next(value_begin(), this->size());
247 }
248
249 template <trivial_copyable_type T>
254
255 template <trivial_copyable_type T>
257 {
258 return sparrow::next(value_cbegin(), this->size());
259 }
260
261 template <trivial_copyable_type T>
262 template <validity_bitmap_input R>
264 u8_buffer<T>&& data_buffer,
265 R&& bitmap_input,
266 std::optional<std::string_view> name,
267 std::optional<std::string_view> metadata
268 ) -> arrow_proxy
269 {
270 const auto size = data_buffer.size();
271 validity_bitmap bitmap = ensure_validity_bitmap(size, std::forward<R>(bitmap_input));
272 const auto null_count = bitmap.null_count();
273
274 // create arrow schema and array
277 std::move(name), // name
278 std::move(metadata), // metadata
279 std::nullopt, // flags
280 0, // n_children
281 nullptr, // children
282 nullptr // dictionary
283 );
284
285 std::vector<buffer<uint8_t>> buffers(2);
286 buffers[0] = std::move(bitmap).extract_storage();
287 buffers[1] = std::move(data_buffer).extract_storage();
288
289 // create arrow array
291 static_cast<std::int64_t>(size), // length
292 static_cast<int64_t>(null_count),
293 0, // offset
294 std::move(buffers),
295 0, // n_children
296 nullptr, // children
297 nullptr // dictionary
298 );
299 return arrow_proxy(std::move(arr), std::move(schema));
300 }
301
302 template <trivial_copyable_type T>
303 template <std::ranges::input_range VALUE_RANGE, validity_bitmap_input R>
304 requires(std::convertible_to<std::ranges::range_value_t<VALUE_RANGE>, T>)
306 VALUE_RANGE&& values,
307 R&& validity_input,
308 std::optional<std::string_view> name,
309 std::optional<std::string_view> metadata
310 )
311 {
312 u8_buffer<T> data_buffer(std::forward<VALUE_RANGE>(values));
313 return create_proxy(
314 std::move(data_buffer),
315 std::forward<R>(validity_input),
316 std::move(name),
317 std::move(metadata)
318 );
319 }
320
321 template <trivial_copyable_type T>
322 template <class U>
323 requires std::convertible_to<U, T>
325 size_type n,
326 const U& value,
327 std::optional<std::string_view> name,
328 std::optional<std::string_view> metadata
329 )
330 {
331 // create data_buffer
332 u8_buffer<T> data_buffer(n, value);
333 return create_proxy(std::move(data_buffer), std::move(name), std::move(metadata));
334 }
335
336 template <trivial_copyable_type T>
337 template <std::ranges::input_range R>
338 requires std::convertible_to<std::ranges::range_value_t<R>, T>
340 R&& range,
341 std::optional<std::string_view> name,
342 std::optional<std::string_view> metadata
343 )
344 {
345 const std::size_t n = range_size(range);
346 const auto iota = std::ranges::iota_view{std::size_t(0), n};
347 std::ranges::transform_view iota_to_is_non_missing(
348 iota,
349 [](std::size_t)
350 {
351 return true;
352 }
353 );
355 std::forward<R>(range),
356 std::move(iota_to_is_non_missing),
357 std::move(name),
358 std::move(metadata)
359 );
360 }
361
362 // range of nullable values
363 template <trivial_copyable_type T>
364 template <std::ranges::input_range R>
365 requires std::is_same_v<std::ranges::range_value_t<R>, nullable<T>>
367 R&& range,
368 std::optional<std::string_view> name,
369 std::optional<std::string_view> metadata
370 )
371 {
372 // split into values and is_non_null ranges
373 auto values = range
374 | std::views::transform(
375 [](const auto& v)
376 {
377 return v.get();
378 }
379 );
380 auto is_non_null = range
381 | std::views::transform(
382 [](const auto& v)
383 {
384 return v.has_value();
385 }
386 );
387 return self_type::create_proxy(values, is_non_null, std::move(name), std::move(metadata));
388 }
389}
array_bitmap_base_impl & operator=(const array_bitmap_base_impl &)
std::conditional_t< is_mutable, mutable_array_base< D >, array_crtp_base< D > > base_type
typename base_type::value_iterator value_iterator
typename base_type::const_value_iterator const_value_iterator
array_trivial_copyable< T > self_type
typename inner_types::const_pointer const_pointer
typename inner_types::inner_reference inner_reference
static arrow_proxy create_proxy(R &&, std::optional< std::string_view > name=std::nullopt, std::optional< std::string_view > metadata=std::nullopt)
const_value_iterator value_cend() const
typename inner_types::inner_const_reference inner_const_reference
array_trivial_copyable(std::initializer_list< inner_value_type > init, std::optional< std::string_view > name=std::nullopt, std::optional< std::string_view > metadata=std::nullopt)
Constructs a primitive array from an initializer_list of raw values.
array_trivial_copyable(Args &&... args)
Constructs an array of trivial copyable type, with the passed range of values and an optional bitmap.
typename inner_types::inner_value_type inner_value_type
static arrow_proxy create_proxy(size_type n, std::optional< std::string_view > name=std::nullopt, std::optional< std::string_view > metadata=std::nullopt)
static auto create_proxy(R &&range, std::optional< std::string_view > name=std::nullopt, std::optional< std::string_view > metadata=std::nullopt) -> arrow_proxy
details::trivial_copyable_data_access< T, self_type > access_class_type
array_trivial_copyable(array_trivial_copyable &&rhs) noexcept
array_trivial_copyable & operator=(array_trivial_copyable &&rhs) noexcept
static auto create_proxy(u8_buffer< T > &&data_buffer, R &&bitmaps=validity_bitmap{}, std::optional< std::string_view > name=std::nullopt, std::optional< std::string_view > metadata=std::nullopt) -> arrow_proxy
const_value_iterator value_cbegin() const
static arrow_proxy create_proxy(R &&, R2 &&, std::optional< std::string_view > name=std::nullopt, std::optional< std::string_view > metadata=std::nullopt)
array_trivial_copyable(const array_trivial_copyable &)
array_inner_types< self_type > inner_types
array_trivial_copyable & operator=(const array_trivial_copyable &)
typename inner_types::pointer pointer
mutable_array_bitmap_base< array_trivial_copyable< T > > base_type
static arrow_proxy create_proxy(size_type n, const U &value=U{}, std::optional< std::string_view > name=std::nullopt, std::optional< std::string_view > metadata=std::nullopt)
Proxy class over ArrowArray and ArrowSchema.
Data access class for trivial copyable types.
constexpr value_iterator insert_values(const_value_iterator pos, InputIt first, InputIt last)
constexpr value_iterator erase_values(const_value_iterator pos, size_t count)
constexpr value_iterator insert_value(const_value_iterator pos, T value, size_t count)
constexpr size_type null_count() const noexcept
The nullable class models a value or a reference that can be "null", or missing, like values traditio...
Definition nullable.hpp:280
constexpr bool excludes_copy_and_move_ctor_v
Definition mp_utils.hpp:507
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 std::string_view data_type_format_of()
ArrowSchema make_arrow_schema(F format, N name, M metadata, std::optional< ArrowFlag > flags, int64_t n_children, ArrowSchema **children, ArrowSchema *dictionary)
Creates an ArrowSchema owned by a unique_ptr and holding the provided data.
constexpr InputIt next(InputIt it, Distance n)
Definition iterator.hpp:503
dynamic_bitset< std::uint8_t > validity_bitmap
validity_bitmap ensure_validity_bitmap(std::size_t size, R &&validity_input)
std::size_t range_size(R &&r)
Definition ranges.hpp:31
ArrowArray make_arrow_array(int64_t length, int64_t null_count, int64_t offset, B buffers, size_t n_children, ArrowArray **children, ArrowArray *dictionary)
Creates an ArrowArray.
nullable< inner_const_reference, bitmap_const_reference > const_reference
Base class for array_inner_types specialization.
Traits class that must be specialized by array classes inheriting from array_crtp_base.