sparrow 0.3.0
Loading...
Searching...
No Matches
fixed_width_binary_array.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
17#include <cstdint>
18#include <iterator>
19#include <ranges>
20#include <string>
21#include <type_traits>
22#include <vector>
23
35
36namespace sparrow
37{
38 template <std::ranges::sized_range T, class CR>
40
42
44 fixed_width_binary_traits::value_type,
45 fixed_width_binary_traits::const_reference>;
46
47 template <std::ranges::sized_range T, class CR>
74
75 namespace detail
76 {
77 template <class T>
78 struct get_data_type_from_array;
79
80 template <>
82 {
83 [[nodiscard]] static constexpr sparrow::data_type get()
84 {
86 }
87 };
88 }
89
90 template <std::ranges::sized_range T, class CR>
92 : public mutable_array_bitmap_base<fixed_width_binary_array_impl<T, CR>>
93 {
94 private:
95
96 static_assert(
97 sizeof(std::ranges::range_value_t<T>) == sizeof(byte_t),
98 "Only sequences of types with the same size as byte_t are supported"
99 );
100
101 public:
102
105
107 using inner_value_type = typename inner_types::inner_value_type;
108 using inner_reference = typename inner_types::inner_reference;
109 using inner_const_reference = typename inner_types::inner_const_reference;
110
112 using bitmap_reference = typename base_type::bitmap_reference;
115
119
123 using data_iterator = typename inner_types::data_iterator;
124
125 using const_data_iterator = typename inner_types::const_data_iterator;
126 using data_value_type = typename inner_types::data_value_type;
127
128 using value_iterator = typename inner_types::value_iterator;
129 using const_value_iterator = typename inner_types::const_value_iterator;
130
131 using functor_type = typename inner_types::functor_type;
132 using const_functor_type = typename inner_types::const_functor_type;
133
135
140 template <class... ARGS>
143 : base_type(create_proxy(std::forward<ARGS>(args)...))
144 , m_element_size(num_bytes_for_fixed_sized_binary(this->get_arrow_proxy().format()))
145 {
146 }
147
148 using base_type::get_arrow_proxy;
149 using base_type::size;
150
152 [[nodiscard]] inner_const_reference value(size_type i) const;
153
154 private:
155
166 template <mpl::char_like C, validity_bitmap_input VB = validity_bitmap>
167 [[nodiscard]] static arrow_proxy create_proxy(
168 u8_buffer<C>&& data_buffer,
169 size_t element_size,
170 VB&& validity_input = validity_bitmap{},
171 std::optional<std::string_view> name = std::nullopt,
172 std::optional<std::string_view> metadata = std::nullopt
173 );
174
184 template <std::ranges::input_range R, validity_bitmap_input VB = validity_bitmap>
185 requires(
186 std::ranges::input_range<std::ranges::range_value_t<R>> && // a range of ranges
188 // range of
189 // char-like
190 )
191 [[nodiscard]] static arrow_proxy create_proxy(
192 R&& values,
193 VB&& validity_input = validity_bitmap{},
194 std::optional<std::string_view> name = std::nullopt,
195 std::optional<std::string_view> metadata = std::nullopt
196 );
197
207 template <std::ranges::input_range R>
209 && std::ranges::input_range<typename std::ranges::range_value_t<R>::value_type>
210 && std::is_same_v<std::ranges::range_value_t<typename std::ranges::range_value_t<R>::value_type>, byte_t>
211 [[nodiscard]] static arrow_proxy create_proxy(
212 R&&,
213 std::optional<std::string_view> name = std::nullopt,
214 std::optional<std::string_view> metadata = std::nullopt
215 );
216
217 static constexpr size_t DATA_BUFFER_INDEX = 1;
218
219 [[nodiscard]] data_iterator data(size_type i);
220
221 [[nodiscard]] value_iterator value_begin();
222 [[nodiscard]] value_iterator value_end();
223
224 [[nodiscard]] const_value_iterator value_cbegin() const;
225 [[nodiscard]] const_value_iterator value_cend() const;
226
227 [[nodiscard]] const_data_iterator data(size_type i) const;
228
229 // Modifiers
230
231 template <std::ranges::sized_range U>
232 requires mpl::convertible_ranges<U, T>
233 void resize_values(size_type new_length, U value);
234
235 template <std::ranges::sized_range U>
236 requires mpl::convertible_ranges<U, T>
237 value_iterator insert_value(const_value_iterator pos, U value, size_type count);
238
239 template <typename InputIt>
240 requires std::input_iterator<InputIt>
241 && mpl::convertible_ranges<typename std::iterator_traits<InputIt>::value_type, T>
242 value_iterator insert_values(const_value_iterator pos, InputIt first, InputIt last);
243
244 value_iterator erase_values(const_value_iterator pos, size_type count);
245
246 template <std::ranges::sized_range U>
247 requires mpl::convertible_ranges<U, T>
248 void assign(U&& rhs, size_type index);
249
250 size_t m_element_size = 0;
251
254 friend base_type;
257 };
258
259 /************************************************
260 * fixed_width_binary_array_impl implementation *
261 ************************************************/
262
263 template <std::ranges::sized_range T, class CR>
265 : base_type(std::move(proxy))
266 , m_element_size(num_bytes_for_fixed_sized_binary(this->get_arrow_proxy().format()))
267 {
268 SPARROW_ASSERT_TRUE(this->get_arrow_proxy().data_type() == data_type::FIXED_WIDTH_BINARY);
269 }
270
271 template <std::ranges::sized_range T, class CR>
272 template <mpl::char_like C, validity_bitmap_input VB>
273 arrow_proxy fixed_width_binary_array_impl<T, CR>::create_proxy(
274 u8_buffer<C>&& data_buffer,
275 size_t element_size,
276 VB&& validity_input,
277 std::optional<std::string_view> name,
278 std::optional<std::string_view> metadata
279 )
280 {
281 SPARROW_ASSERT_TRUE((data_buffer.size() % element_size) == 0);
282 const size_t element_count = data_buffer.size() / element_size;
283 validity_bitmap vbitmap = ensure_validity_bitmap(element_count, std::forward<VB>(validity_input));
284 const auto null_count = vbitmap.null_count();
285
286 std::string format_str = "w:" + std::to_string(element_size);
287
289 std::move(format_str),
290 std::move(name), // name
291 std::move(metadata), // metadata
292 std::nullopt, // flags,
293 0, // n_children
294 nullptr, // children
295 nullptr // dictionary
296
297 );
298 std::vector<buffer<std::uint8_t>> arr_buffs = {
299 std::move(vbitmap).extract_storage(),
300 std::move(data_buffer).extract_storage()
301 };
302
304 static_cast<std::int64_t>(element_count), // length
305 static_cast<int64_t>(null_count),
306 0, // offset
307 std::move(arr_buffs),
308 0, // n_children
309 nullptr, // children
310 nullptr // dictionary
311 );
312 return arrow_proxy{std::move(arr), std::move(schema)};
313 }
314
315 template <std::ranges::sized_range T, class CR>
316 template <std::ranges::input_range R, validity_bitmap_input VB>
317 requires(
318 std::ranges::input_range<std::ranges::range_value_t<R>> && // a range of ranges
320 // range of char-like
321 )
322 arrow_proxy fixed_width_binary_array_impl<T, CR>::create_proxy(
323 R&& values,
324 VB&& validity_input,
325 std::optional<std::string_view> name,
326 std::optional<std::string_view> metadata
327 )
328 {
329 using values_type = std::ranges::range_value_t<R>;
330 using values_inner_value_type = std::ranges::range_value_t<values_type>;
331
332 SPARROW_ASSERT_TRUE(!std::ranges::empty(values));
334 const size_t element_size = std::ranges::size(*values.begin());
335
336 auto data_buffer = u8_buffer<values_inner_value_type>(std::ranges::views::join(values));
337 return create_proxy(
338 std::move(data_buffer),
339 element_size,
340 std::forward<VB>(validity_input),
341 std::forward<std::optional<std::string_view>>(name),
342 std::forward<std::optional<std::string_view>>(metadata)
343 );
344 }
345
346 template <std::ranges::sized_range T, class CR>
347 template <std::ranges::input_range R>
349 && std::ranges::input_range<typename std::ranges::range_value_t<R>::value_type>
350 && std::is_same_v<std::ranges::range_value_t<typename std::ranges::range_value_t<R>::value_type>, byte_t>
351 arrow_proxy fixed_width_binary_array_impl<T, CR>::create_proxy(
352 R&& range,
353 std::optional<std::string_view> name,
354 std::optional<std::string_view> metadata
355 )
356 {
357 // split into values and is_non_null ranges
358 const auto values = range
359 | std::views::transform(
360 [](const auto& v)
361 {
362 return v.get();
363 }
364 );
365 const auto is_non_null = range
366 | std::views::transform(
367 [](const auto& v)
368 {
369 return v.has_value();
370 }
371 );
372 return self_type::create_proxy(values, is_non_null, std::move(name), std::move(metadata));
373 }
374
375 template <std::ranges::sized_range T, class CR>
376 auto fixed_width_binary_array_impl<T, CR>::data(size_type i) -> data_iterator
377 {
378 const arrow_proxy& proxy = this->get_arrow_proxy();
379 auto data_buffer = proxy.buffers()[DATA_BUFFER_INDEX];
380 const size_t data_buffer_size = data_buffer.size();
381 const size_type index_offset = (static_cast<size_type>(proxy.offset()) * m_element_size) + i;
382 SPARROW_ASSERT_TRUE(data_buffer_size >= index_offset);
383 return data_buffer.template data<data_value_type>() + index_offset;
384 }
385
386 template <std::ranges::sized_range T, class CR>
387 auto fixed_width_binary_array_impl<T, CR>::data(size_type i) const -> const_data_iterator
388 {
389 const arrow_proxy& proxy = this->get_arrow_proxy();
390 const auto data_buffer = proxy.buffers()[DATA_BUFFER_INDEX];
391 const size_t data_buffer_size = data_buffer.size();
392 const size_type index_offset = (static_cast<size_type>(proxy.offset()) * m_element_size) + i;
393 SPARROW_ASSERT_TRUE(data_buffer_size >= index_offset);
394 return data_buffer.template data<const data_value_type>() + index_offset;
395 }
396
397 template <std::ranges::sized_range T, class CR>
398 template <std::ranges::sized_range U>
400 void fixed_width_binary_array_impl<T, CR>::assign(U&& rhs, size_type index)
401 {
402 SPARROW_ASSERT_TRUE(std::ranges::size(rhs) == m_element_size);
403 SPARROW_ASSERT_TRUE(index < size());
404 std::copy(std::ranges::begin(rhs), std::ranges::end(rhs), data(index * m_element_size));
405 }
406
407 template <std::ranges::sized_range T, class CR>
413
414 template <std::ranges::sized_range T, class CR>
416 {
417 SPARROW_ASSERT_TRUE(i < this->size());
418 const auto offset_begin = i * m_element_size;
419 const auto offset_end = offset_begin + m_element_size;
420 const const_data_iterator pointer_begin = data(static_cast<size_type>(offset_begin));
421 const const_data_iterator pointer_end = data(static_cast<size_type>(offset_end));
422 return inner_const_reference(pointer_begin, pointer_end);
423 }
424
425 template <std::ranges::sized_range T, class CR>
426 auto fixed_width_binary_array_impl<T, CR>::value_begin() -> value_iterator
427 {
428 return value_iterator{functor_type{&(this->derived_cast())}, 0};
429 }
430
431 template <std::ranges::sized_range T, class CR>
432 auto fixed_width_binary_array_impl<T, CR>::value_end() -> value_iterator
433 {
434 return sparrow::next(value_begin(), size());
435 }
436
437 template <std::ranges::sized_range T, class CR>
438 auto fixed_width_binary_array_impl<T, CR>::value_cbegin() const -> const_value_iterator
439 {
440 return const_value_iterator{const_functor_type{&(this->derived_cast())}, 0};
441 }
442
443 template <std::ranges::sized_range T, class CR>
444 auto fixed_width_binary_array_impl<T, CR>::value_cend() const -> const_value_iterator
445 {
446 return sparrow::next(value_cbegin(), this->size());
447 }
448
449 template <std::ranges::sized_range T, class CR>
450 template <std::ranges::sized_range U>
452 void fixed_width_binary_array_impl<T, CR>::resize_values(size_type new_length, U value)
453 {
454 SPARROW_ASSERT_TRUE(m_element_size == value.size());
455 if (new_length < size())
456 {
457 arrow_proxy& proxy = this->get_arrow_proxy();
458 const size_t new_size = new_length + static_cast<size_t>(proxy.offset());
459 const auto offset = new_size * m_element_size;
460 auto& data_buffer = proxy.get_array_private_data()->buffers()[DATA_BUFFER_INDEX];
461 data_buffer.resize(offset);
462 }
463 else if (new_length > size())
464 {
465 insert_value(value_cend(), value, new_length - size());
466 }
467 }
468
469 template <std::ranges::sized_range T, class CR>
470 template <std::ranges::sized_range U>
472 auto fixed_width_binary_array_impl<T, CR>::insert_value(const_value_iterator pos, U value, size_type count)
474 {
475 SPARROW_ASSERT_TRUE(m_element_size == value.size());
476 const auto idx = static_cast<size_t>(std::distance(value_cbegin(), pos));
477
478 const uint8_t* uint8_ptr = reinterpret_cast<const uint8_t*>(value.data());
479 const std::vector<uint8_t> casted_value(uint8_ptr, uint8_ptr + value.size());
480 const repeat_view<std::vector<uint8_t>> my_repeat_view{casted_value, count};
481 const auto joined_repeated_value_range = std::ranges::views::join(my_repeat_view);
482 arrow_proxy& proxy = this->get_arrow_proxy();
483 auto& data_buffer = proxy.get_array_private_data()->buffers()[DATA_BUFFER_INDEX];
484 const auto offset_begin = (idx + proxy.offset()) * m_element_size;
485 const auto pos_to_insert = sparrow::next(data_buffer.cbegin(), offset_begin);
486 data_buffer.insert(pos_to_insert, joined_repeated_value_range.begin(), joined_repeated_value_range.end());
487 return sparrow::next(value_begin(), idx);
488 }
489
490 template <std::ranges::sized_range T, class CR>
491 template <typename InputIt>
492 requires std::input_iterator<InputIt>
494 auto
495 fixed_width_binary_array_impl<T, CR>::insert_values(const_value_iterator pos, InputIt first, InputIt last)
497 {
498 SPARROW_ASSERT_TRUE(value_cbegin() <= pos)
499 SPARROW_ASSERT_TRUE(pos <= value_cend());
500 SPARROW_ASSERT_TRUE(first <= last);
501 SPARROW_ASSERT_TRUE(all_same_size(std::ranges::subrange(first, last)));
502 SPARROW_ASSERT_TRUE(m_element_size == std::ranges::size(*first));
503
504 auto values = std::ranges::subrange(first, last);
505 const size_t cumulative_sizes = values.size() * m_element_size;
506 auto& data_buffer = get_arrow_proxy().get_array_private_data()->buffers()[DATA_BUFFER_INDEX];
507 data_buffer.resize(data_buffer.size() + cumulative_sizes);
508 const auto idx = static_cast<size_t>(std::distance(value_cbegin(), pos));
509 std::span<byte_t> casted_values{reinterpret_cast<byte_t*>(data_buffer.data()), data_buffer.size()};
510 const auto offset_begin = m_element_size * (idx + get_arrow_proxy().offset());
511 auto insert_pos = sparrow::next(casted_values.begin(), offset_begin);
512
513 // Move elements to make space for the new value
514 std::move_backward(
515 insert_pos,
516 sparrow::next(casted_values.end(), -static_cast<difference_type>(cumulative_sizes)),
517 casted_values.end()
518 );
519
520 for (const auto& val : values)
521 {
522 std::copy(val.begin(), val.end(), insert_pos);
523 std::advance(insert_pos, m_element_size);
524 }
525 return sparrow::next(value_begin(), idx);
526 }
527
528 template <std::ranges::sized_range T, class CR>
529 auto fixed_width_binary_array_impl<T, CR>::erase_values(const_value_iterator pos, size_type count)
530 -> value_iterator
531 {
532 SPARROW_ASSERT_TRUE(pos >= value_cbegin());
533 SPARROW_ASSERT_TRUE(pos <= value_cend());
534 const size_t index = static_cast<size_t>(std::distance(value_cbegin(), pos));
535 if (count == 0)
536 {
537 return sparrow::next(value_begin(), index);
538 }
539 auto& data_buffer = get_arrow_proxy().get_array_private_data()->buffers()[DATA_BUFFER_INDEX];
540 const size_type byte_count = m_element_size * count;
541 const auto offset_begin = m_element_size * (index + static_cast<size_type>(get_arrow_proxy().offset()));
542 const auto offset_end = offset_begin + byte_count;
543 // move the values after the erased ones
544 std::move(
545 data_buffer.begin() + static_cast<difference_type>(offset_end),
546 data_buffer.end(),
547 data_buffer.begin() + static_cast<difference_type>(offset_begin)
548 );
549 data_buffer.resize(data_buffer.size() - byte_count);
550 return sparrow::next(value_begin(), index);
551 }
552}
typename base_type::const_bitmap_range const_bitmap_range
typename base_type::iterator_tag iterator_tag
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
typename base_type::difference_type difference_type
constexpr BufferType & buffers() noexcept
Proxy class over ArrowArray and ArrowSchema.
SPARROW_API size_t offset() const
SPARROW_API arrow_array_private_data * get_array_private_data()
constexpr size_type null_count() const noexcept
bitset_iterator< self_type, true > const_iterator
fixed_width_binary_array_impl(ARGS &&... args)
Constructs a fixed-width binary array.
inner_const_reference value(size_type i) const
Implementation of reference to inner type used for layout L.
The nullable class models a value or a reference that can be "null", or missing, like values traditio...
Definition nullable.hpp:280
A view that repeats a value a given number of times.
Matches range types From whose elements are convertible to elements of range type To.
Definition mp_utils.hpp:450
#define SPARROW_ASSERT_TRUE(expr__)
constexpr std::size_t size(typelist< T... >={})
Definition mp_utils.hpp:107
constexpr bool excludes_copy_and_move_ctor_v
Definition mp_utils.hpp:507
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
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.
std::byte byte_t
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 bool all_same_size(const Range &range)
Definition ranges.hpp:45
arrow_traits< std::vector< byte_t > > fixed_width_binary_traits
constexpr InputIt next(InputIt it, Distance n)
Definition iterator.hpp:503
fixed_width_binary_array_impl< fixed_width_binary_traits::value_type, fixed_width_binary_traits::const_reference > fixed_width_binary_array
dynamic_bitset< std::uint8_t > validity_bitmap
SPARROW_API std::size_t num_bytes_for_fixed_sized_binary(std::string_view format)
Get the number of bytes for a fixed width binary layout from the ArrowArray format string.
validity_bitmap ensure_validity_bitmap(std::size_t size, R &&validity_input)
data_type
Runtime identifier of arrow data types, usually associated with raw bytes with the associated value.
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.
detail::layout_value_functor< array_type, inner_reference > functor_type
detail::layout_value_functor< const array_type, inner_const_reference > const_functor_type
Base class for array_inner_types specialization.
Traits class that must be specialized by array classes inheriting from array_crtp_base.
Provides compile-time information about Arrow data types.