sparrow 2.3.0
C++20 idiomatic APIs for the Apache Arrow Columnar Format
Loading...
Searching...
No Matches
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
17#include <algorithm>
18#include <cstddef>
19#include <cstdint>
20#include <ranges>
21#include <utility>
22
31
32namespace sparrow
33{
48
70 template <class D>
72
107 template <class D>
108 class array_crtp_base : public crtp_base<D>
109 {
110 public:
111
113 using derived_type = D;
114
116
117 using size_type = std::size_t;
118 using difference_type = std::ptrdiff_t;
119
120 using bitmap_type = typename inner_types::bitmap_type;
121 using const_bitmap_type = typename inner_types::const_bitmap_type;
122 using bitmap_const_reference = bitmap_type::const_reference;
123 using bitmap_iterator = bitmap_type::iterator;
124 using const_bitmap_iterator = const_bitmap_type::const_iterator;
125 using const_bitmap_range = std::ranges::subrange<const_bitmap_iterator>;
126
127 using inner_value_type = typename inner_types::inner_value_type;
129
130 using inner_const_reference = typename inner_types::inner_const_reference;
132
133 using const_value_iterator = typename inner_types::const_value_iterator;
134 using const_value_range = std::ranges::subrange<const_value_iterator>;
135
136 using iterator_tag = typename inner_types::iterator_tag;
137
146
148 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
149
158 [[nodiscard]] constexpr std::optional<std::string_view> name() const;
159
168 [[nodiscard]] std::optional<key_value_view> metadata() const;
169
178 [[nodiscard]] constexpr bool empty() const;
179
188 [[nodiscard]] constexpr size_type size() const;
189
198 [[nodiscard]] constexpr size_type offset() const;
199
208 [[nodiscard]] constexpr std::int64_t null_count() const;
209
222 [[nodiscard]] constexpr const_reference at(size_type i) const;
223
236 [[nodiscard]] constexpr const_reference operator[](size_type i) const;
237
250 [[nodiscard]] constexpr const_reference front() const;
251
264 [[nodiscard]] constexpr const_reference back() const;
265
274 [[nodiscard]] constexpr const_iterator begin() const;
275
284 [[nodiscard]] constexpr const_iterator end() const;
285
294 [[nodiscard]] constexpr const_iterator cbegin() const;
295
304 [[nodiscard]] constexpr const_iterator cend() const;
305
314 [[nodiscard]] constexpr const_reverse_iterator rbegin() const;
315
324 [[nodiscard]] constexpr const_reverse_iterator rend() const;
325
334 [[nodiscard]] constexpr const_reverse_iterator crbegin() const;
335
344 [[nodiscard]] constexpr const_reverse_iterator crend() const;
345
355 [[nodiscard]] constexpr const_bitmap_range bitmap() const;
356
366 [[nodiscard]] constexpr const_value_range values() const;
367
387 [[nodiscard]] constexpr D slice(size_type start, size_type end) const;
388
409 [[nodiscard]] constexpr D slice_view(size_type start, size_type end) const;
410
411 protected:
412
423
424 constexpr array_crtp_base(const array_crtp_base&) = default;
425 constexpr array_crtp_base& operator=(const array_crtp_base&) = default;
426
427 constexpr array_crtp_base(array_crtp_base&&) noexcept = default;
428 constexpr array_crtp_base& operator=(array_crtp_base&&) noexcept = default;
429
437 [[nodiscard]] constexpr arrow_proxy& get_arrow_proxy() noexcept;
438
446 [[nodiscard]] constexpr const arrow_proxy& get_arrow_proxy() const noexcept;
447
461
470 constexpr const_bitmap_iterator bitmap_begin() const;
471
479 constexpr const_bitmap_iterator bitmap_end() const;
480
489 constexpr const_bitmap_iterator bitmap_cbegin() const;
490
499 constexpr const_bitmap_iterator bitmap_cend() const;
500
501 private:
502
503 arrow_proxy m_proxy;
504
505 // friend classes
506 friend class layout_iterator<iterator_types>;
507 friend class detail::array_access;
508#if defined(__cpp_lib_format)
509 friend struct std::formatter<D>;
510#endif
511 };
512
513 template <class D>
514 constexpr bool operator==(const array_crtp_base<D>& lhs, const array_crtp_base<D>& rhs);
515
516 /**********************************
517 * array_crtp_base implementation *
518 **********************************/
519
520 template <class D>
521 constexpr std::optional<std::string_view> array_crtp_base<D>::name() const
522 {
523 return get_arrow_proxy().name();
524 }
525
526 template <class D>
527 std::optional<key_value_view> array_crtp_base<D>::metadata() const
528 {
529 return get_arrow_proxy().metadata();
530 }
531
532 template <class D>
533 constexpr bool array_crtp_base<D>::empty() const
534 {
535 return size() == size_type(0);
536 }
537
538 template <class D>
539 constexpr auto array_crtp_base<D>::size() const -> size_type
540 {
541 return static_cast<size_type>(get_arrow_proxy().length());
542 }
543
544 template <class D>
545 constexpr auto array_crtp_base<D>::offset() const -> size_type
546 {
547 return static_cast<size_type>(get_arrow_proxy().offset());
548 }
549
550 template <class D>
551 constexpr std::int64_t array_crtp_base<D>::null_count() const
552 {
553 return get_arrow_proxy().null_count();
554 }
555
556 template <class D>
558 {
559 if (i >= size())
560 {
561 const std::string error_message = "Index " + std::to_string(i)
562 + " is greater or equal to size of array ("
563 + std::to_string(size()) + ")";
564 throw std::out_of_range(error_message);
565 }
566 return (*this)[i];
567 }
568
569 template <class D>
571 {
572 auto& derived_cast = this->derived_cast();
574 return const_reference(inner_const_reference(derived_cast.value(i)), derived_cast.has_value(i));
575 }
576
577 template <class D>
579 {
581 return (*this)[size_type(0)];
582 }
583
584 template <class D>
586 {
588 return (*this)[size() - 1];
589 }
590
591 template <class D>
593 {
594 return cbegin();
595 }
596
597 template <class D>
598 constexpr auto array_crtp_base<D>::end() const -> const_iterator
599 {
600 return cend();
601 }
602
603 template <class D>
605 {
606 return const_iterator(this->derived_cast().value_cbegin(), bitmap_begin());
607 }
608
609 template <class D>
610 constexpr auto array_crtp_base<D>::cend() const -> const_iterator
611 {
612 return const_iterator(this->derived_cast().value_cend(), bitmap_end());
613 }
614
615 template <class D>
617 {
618 return crbegin();
619 }
620
621 template <class D>
623 {
624 return crend();
625 }
626
627 template <class D>
629 {
631 }
632
633 template <class D>
635 {
637 }
638
639 template <class D>
641 {
643 }
644
645 template <class D>
647 {
648 return const_value_range(this->derived_cast().value_cbegin(), this->derived_cast().value_cend());
649 }
650
651 template <class D>
653 : m_proxy(std::move(proxy))
654 {
655 }
656
657 template <class D>
659 {
660 return m_proxy;
661 }
662
663 template <class D>
664 constexpr auto array_crtp_base<D>::get_arrow_proxy() const noexcept -> const arrow_proxy&
665 {
666 return m_proxy;
667 }
668
669 template <class D>
671 {
673 return *sparrow::next(bitmap_begin(), i);
674 }
675
676 template <class D>
678 {
679 return this->derived_cast().get_bitmap().cbegin();
680 }
681
682 template <class D>
684 {
685 return sparrow::next(bitmap_begin(), size());
686 }
687
688 template <class D>
690 {
691 return bitmap_begin();
692 }
693
694 template <class D>
696 {
697 return bitmap_end();
698 }
699
700 template <class D>
702 {
703 SPARROW_ASSERT_TRUE(start <= end);
704 return D{get_arrow_proxy().slice(start, end)};
705 }
706
707 template <class D>
709 {
710 SPARROW_ASSERT_TRUE(start <= end);
711 return D{get_arrow_proxy().slice_view(start, end)};
712 }
713
714 /*
715 * @brief Equality comparison operator for arrays.
716 *
717 * Compares two arrays element-wise, including both values and validity flags.
718 *
719 * @tparam D Array type
720 * @param lhs First array to compare
721 * @param rhs Second array to compare
722 * @return true if arrays are element-wise equal, false otherwise
723 *
724 * @post Returns true iff arrays have same size and all elements compare equal
725 * @post Comparison includes both values and validity states
726 */
727 template <class D>
728 constexpr bool operator==(const array_crtp_base<D>& lhs, const array_crtp_base<D>& rhs)
729 {
730 return std::ranges::equal(lhs, rhs);
731 }
732}
733
734#if defined(__cpp_lib_format)
735
736template <typename D>
737 requires std::derived_from<D, sparrow::array_crtp_base<D>>
738struct std::formatter<D>
739{
740 constexpr auto parse(std::format_parse_context& ctx)
741 {
742 return ctx.begin(); // Simple implementation
743 }
744
745 auto format(const D& ar, std::format_context& ctx) const
746 {
747 const auto& proxy = ar.get_arrow_proxy();
748 std::string type;
749 if (proxy.dictionary())
750 {
751 std::format_to(ctx.out(), "Dictionary<{}>", proxy.dictionary()->data_type());
752 }
753 else
754 {
755 std::format_to(ctx.out(), "{}", proxy.data_type());
756 }
757 std::format_to(ctx.out(), " [name={} | size={}] <", ar.name().value_or("nullptr"), proxy.length());
758
759 std::for_each(
760 ar.cbegin(),
761 std::prev(ar.cend()),
762 [&ctx](const auto& value)
763 {
764 std::format_to(ctx.out(), "{}, ", value);
765 }
766 );
767 return std::format_to(ctx.out(), "{}>", ar.back());
768 }
769};
770
771namespace sparrow
772{
773 template <typename D>
774 requires std::derived_from<D, array_crtp_base<D>>
775 std::ostream& operator<<(std::ostream& os, const D& value)
776 {
777 os << std::format("{}", value);
778 return os;
779 }
780}
781
782#endif
typename inner_types::const_bitmap_type const_bitmap_type
constexpr const_bitmap_iterator bitmap_cbegin() const
Gets const bitmap iterator to the beginning.
constexpr const_value_range values() const
Gets the raw values as a range.
array_crtp_base(arrow_proxy)
Protected constructor from Arrow proxy.
constexpr const_reverse_iterator rbegin() const
Gets reverse iterator to the beginning of reversed array.
constexpr D slice_view(size_type start, size_type end) const
Creates a sliced view of the array.
constexpr const_reference back() const
Gets reference to the last element.
typename inner_types::const_value_iterator const_value_iterator
bitmap_type::const_reference bitmap_const_reference
std::ranges::subrange< const_value_iterator > const_value_range
bitmap_type::iterator bitmap_iterator
constexpr const_reference front() const
Gets reference to the first element.
constexpr const_bitmap_iterator bitmap_end() const
Gets bitmap iterator to the end.
constexpr const_bitmap_iterator bitmap_cend() const
Gets const bitmap iterator to the end.
constexpr bool empty() const
Checks if the array is empty.
nullable< inner_const_reference, bitmap_const_reference > const_reference
constexpr const_iterator cbegin() const
Gets const iterator to the beginning of the array.
constexpr const_reference at(size_type i) const
Gets element at specified position with bounds checking.
typename inner_types::iterator_tag iterator_tag
constexpr bitmap_const_reference has_value(size_type i) const
Checks if element at index i has a valid value.
constexpr array_crtp_base(const array_crtp_base &)=default
constexpr size_type offset() const
Gets the starting offset within the buffers.
std::ranges::subrange< const_bitmap_iterator > const_bitmap_range
friend class detail::array_access
typename inner_types::bitmap_type bitmap_type
const_bitmap_type::const_iterator const_bitmap_iterator
constexpr const_reverse_iterator crbegin() const
Gets const reverse iterator to the beginning of reversed array.
typename inner_types::inner_const_reference inner_const_reference
constexpr const_reverse_iterator crend() const
Gets const reverse iterator to the end of reversed array.
nullable< inner_value_type > value_type
std::ptrdiff_t difference_type
constexpr const_reference operator[](size_type i) const
Gets element at specified position without bounds checking.
layout_iterator< iterator_types > const_iterator
constexpr arrow_proxy & get_arrow_proxy() noexcept
Gets mutable reference to the Arrow proxy.
array_crtp_base< D > self_type
std::reverse_iterator< const_iterator > const_reverse_iterator
constexpr const_iterator cend() const
Gets const iterator to the end of the array.
constexpr array_crtp_base & operator=(const array_crtp_base &)=default
constexpr const_bitmap_iterator bitmap_begin() const
Gets bitmap iterator to the beginning.
constexpr std::int64_t null_count() const
Gets the count of null elements in the array.
array_inner_types< derived_type > inner_types
constexpr const_reverse_iterator rend() const
Gets reverse iterator to the end of reversed array.
constexpr std::optional< std::string_view > name() const
Gets the optional name of the array.
constexpr const_iterator end() const
Gets iterator to the end of the array.
constexpr D slice(size_type start, size_type end) const
Creates a sliced copy of the array.
constexpr array_crtp_base(array_crtp_base &&) noexcept=default
constexpr const_iterator begin() const
Gets iterator to the beginning of the array.
constexpr const_bitmap_range bitmap() const
Gets the validity bitmap as a range.
std::optional< key_value_view > metadata() const
Gets the metadata associated with the array.
typename inner_types::inner_value_type inner_value_type
constexpr size_type size() const
Gets the number of elements in the array.
Base class for CRTP base classes.
Definition crtp_base.hpp:29
constexpr derived_type & derived_cast()
Definition crtp_base.hpp:39
A non-owning view to a dynamic size sequence of bits stored in external memory.
Layout iterator class.
constexpr bitmap_iterator bitmap_end()
base_type::const_reference const_reference
bitmap_type::const_reference bitmap_const_reference
base_type::const_iterator const_iterator
constexpr bitmap_reference has_value(size_type i)
base_type::const_bitmap_range const_bitmap_range
const_bitmap_type::const_iterator const_bitmap_iterator
constexpr iterator end()
Returns a iterator to the element following the last element of the array.
constexpr bitmap_iterator bitmap_begin()
Concept for iterator types.
#define SPARROW_ASSERT_TRUE(expr__)
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
constexpr InputIt next(InputIt it, Distance n)
Definition iterator.hpp:503
std::ostream & operator<<(std::ostream &os, const nullval_t &)
Extensions to the C++ standard library.
self_type::const_reference reference
self_type::const_bitmap_iterator bitmap_iterator
self_type::const_value_iterator value_iterator
Base class for array_inner_types specializations.
non_owning_dynamic_bitset< std::uint8_t > bitmap_type
dynamic_bitset_view< const std::uint8_t > const_bitmap_type
Traits class that must be specialized by array implementations.