sparrow 1.3.0
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
30
31namespace sparrow
32{
46
68 template <class D>
70
105 template <class D>
106 class array_crtp_base : public crtp_base<D>
107 {
108 public:
109
111 using derived_type = D;
112
114
115 using size_type = std::size_t;
116 using difference_type = std::ptrdiff_t;
117
118 using bitmap_type = typename inner_types::bitmap_type;
119 using bitmap_const_reference = bitmap_type::const_reference;
120 using bitmap_iterator = bitmap_type::iterator;
121 using const_bitmap_iterator = bitmap_type::const_iterator;
122 using const_bitmap_range = std::ranges::subrange<const_bitmap_iterator>;
123
124 using inner_value_type = typename inner_types::inner_value_type;
126
127 using inner_const_reference = typename inner_types::inner_const_reference;
129
130 using const_value_iterator = typename inner_types::const_value_iterator;
131 using const_value_range = std::ranges::subrange<const_value_iterator>;
132
133 using iterator_tag = typename inner_types::iterator_tag;
134
143
145 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
146
155 [[nodiscard]] constexpr std::optional<std::string_view> name() const;
156
165 [[nodiscard]] std::optional<key_value_view> metadata() const;
166
175 [[nodiscard]] constexpr bool empty() const;
176
185 [[nodiscard]] constexpr size_type size() const;
186
195 [[nodiscard]] constexpr size_type offset() const;
196
205 [[nodiscard]] constexpr std::int64_t null_count() const;
206
219 [[nodiscard]] constexpr const_reference at(size_type i) const;
220
233 [[nodiscard]] constexpr const_reference operator[](size_type i) const;
234
247 [[nodiscard]] constexpr const_reference front() const;
248
261 [[nodiscard]] constexpr const_reference back() const;
262
271 [[nodiscard]] constexpr const_iterator begin() const;
272
281 [[nodiscard]] constexpr const_iterator end() const;
282
291 [[nodiscard]] constexpr const_iterator cbegin() const;
292
301 [[nodiscard]] constexpr const_iterator cend() const;
302
311 [[nodiscard]] constexpr const_reverse_iterator rbegin() const;
312
321 [[nodiscard]] constexpr const_reverse_iterator rend() const;
322
331 [[nodiscard]] constexpr const_reverse_iterator crbegin() const;
332
341 [[nodiscard]] constexpr const_reverse_iterator crend() const;
342
352 [[nodiscard]] constexpr const_bitmap_range bitmap() const;
353
363 [[nodiscard]] constexpr const_value_range values() const;
364
384 [[nodiscard]] constexpr D slice(size_type start, size_type end) const;
385
406 [[nodiscard]] constexpr D slice_view(size_type start, size_type end) const;
407
408 protected:
409
420
421 constexpr array_crtp_base(const array_crtp_base&) = default;
422 constexpr array_crtp_base& operator=(const array_crtp_base&) = default;
423
424 constexpr array_crtp_base(array_crtp_base&&) noexcept = default;
425 constexpr array_crtp_base& operator=(array_crtp_base&&) noexcept = default;
426
434 [[nodiscard]] constexpr arrow_proxy& get_arrow_proxy() noexcept;
435
443 [[nodiscard]] constexpr const arrow_proxy& get_arrow_proxy() const noexcept;
444
458
467 constexpr const_bitmap_iterator bitmap_begin() const;
468
476 constexpr const_bitmap_iterator bitmap_end() const;
477
486 constexpr const_bitmap_iterator bitmap_cbegin() const;
487
496 constexpr const_bitmap_iterator bitmap_cend() const;
497
498 private:
499
500 arrow_proxy m_proxy;
501
502 // friend classes
503 friend class layout_iterator<iterator_types>;
504 friend class detail::array_access;
505#if defined(__cpp_lib_format)
506 friend struct std::formatter<D>;
507#endif
508 };
509
510 template <class D>
511 constexpr bool operator==(const array_crtp_base<D>& lhs, const array_crtp_base<D>& rhs);
512
513 /**********************************
514 * array_crtp_base implementation *
515 **********************************/
516
517 template <class D>
518 constexpr std::optional<std::string_view> array_crtp_base<D>::name() const
519 {
520 return get_arrow_proxy().name();
521 }
522
523 template <class D>
524 std::optional<key_value_view> array_crtp_base<D>::metadata() const
525 {
526 return get_arrow_proxy().metadata();
527 }
528
529 template <class D>
530 constexpr bool array_crtp_base<D>::empty() const
531 {
532 return size() == size_type(0);
533 }
534
535 template <class D>
536 constexpr auto array_crtp_base<D>::size() const -> size_type
537 {
538 return static_cast<size_type>(get_arrow_proxy().length());
539 }
540
541 template <class D>
542 constexpr auto array_crtp_base<D>::offset() const -> size_type
543 {
544 return static_cast<size_type>(get_arrow_proxy().offset());
545 }
546
547 template <class D>
548 constexpr std::int64_t array_crtp_base<D>::null_count() const
549 {
550 return get_arrow_proxy().null_count();
551 }
552
553 template <class D>
555 {
556 if (i >= size())
557 {
558 const std::string error_message = "Index " + std::to_string(i)
559 + " is greater or equal to size of array ("
560 + std::to_string(size()) + ")";
561 throw std::out_of_range(error_message);
562 }
563 return (*this)[i];
564 }
565
566 template <class D>
568 {
569 auto& derived_cast = this->derived_cast();
571 return const_reference(inner_const_reference(derived_cast.value(i)), derived_cast.has_value(i));
572 }
573
574 template <class D>
576 {
578 return (*this)[size_type(0)];
579 }
580
581 template <class D>
583 {
585 return (*this)[size() - 1];
586 }
587
588 template <class D>
590 {
591 return cbegin();
592 }
593
594 template <class D>
595 constexpr auto array_crtp_base<D>::end() const -> const_iterator
596 {
597 return cend();
598 }
599
600 template <class D>
602 {
603 return const_iterator(this->derived_cast().value_cbegin(), bitmap_begin());
604 }
605
606 template <class D>
607 constexpr auto array_crtp_base<D>::cend() const -> const_iterator
608 {
609 return const_iterator(this->derived_cast().value_cend(), bitmap_end());
610 }
611
612 template <class D>
614 {
615 return crbegin();
616 }
617
618 template <class D>
620 {
621 return crend();
622 }
623
624 template <class D>
626 {
628 }
629
630 template <class D>
632 {
634 }
635
636 template <class D>
638 {
640 }
641
642 template <class D>
644 {
645 return const_value_range(this->derived_cast().value_cbegin(), this->derived_cast().value_cend());
646 }
647
648 template <class D>
650 : m_proxy(std::move(proxy))
651 {
652 }
653
654 template <class D>
656 {
657 return m_proxy;
658 }
659
660 template <class D>
661 constexpr auto array_crtp_base<D>::get_arrow_proxy() const noexcept -> const arrow_proxy&
662 {
663 return m_proxy;
664 }
665
666 template <class D>
668 {
670 return *sparrow::next(bitmap_begin(), i);
671 }
672
673 template <class D>
675 {
676 return sparrow::next(this->derived_cast().get_bitmap().cbegin(), get_arrow_proxy().offset());
677 }
678
679 template <class D>
681 {
682 return sparrow::next(bitmap_begin(), size());
683 }
684
685 template <class D>
687 {
688 return bitmap_begin();
689 }
690
691 template <class D>
693 {
694 return bitmap_end();
695 }
696
697 template <class D>
699 {
700 SPARROW_ASSERT_TRUE(start <= end);
701 return D{get_arrow_proxy().slice(start, end)};
702 }
703
704 template <class D>
706 {
707 SPARROW_ASSERT_TRUE(start <= end);
708 return D{get_arrow_proxy().slice_view(start, end)};
709 }
710
711 /*
712 * @brief Equality comparison operator for arrays.
713 *
714 * Compares two arrays element-wise, including both values and validity flags.
715 *
716 * @tparam D Array type
717 * @param lhs First array to compare
718 * @param rhs Second array to compare
719 * @return true if arrays are element-wise equal, false otherwise
720 *
721 * @post Returns true iff arrays have same size and all elements compare equal
722 * @post Comparison includes both values and validity states
723 */
724 template <class D>
725 constexpr bool operator==(const array_crtp_base<D>& lhs, const array_crtp_base<D>& rhs)
726 {
727 return std::ranges::equal(lhs, rhs);
728 }
729}
730
731#if defined(__cpp_lib_format)
732
733template <typename D>
734 requires std::derived_from<D, sparrow::array_crtp_base<D>>
735struct std::formatter<D>
736{
737 constexpr auto parse(std::format_parse_context& ctx)
738 {
739 return ctx.begin(); // Simple implementation
740 }
741
742 auto format(const D& ar, std::format_context& ctx) const
743 {
744 const auto& proxy = ar.get_arrow_proxy();
745 std::string type;
746 if (proxy.dictionary())
747 {
748 std::format_to(ctx.out(), "Dictionary<{}>", proxy.dictionary()->data_type());
749 }
750 else
751 {
752 std::format_to(ctx.out(), "{}", proxy.data_type());
753 }
754 std::format_to(ctx.out(), " [name={} | size={}] <", ar.name().value_or("nullptr"), proxy.length());
755
756 std::for_each(
757 ar.cbegin(),
758 std::prev(ar.cend()),
759 [&ctx](const auto& value)
760 {
761 std::format_to(ctx.out(), "{}, ", value);
762 }
763 );
764 return std::format_to(ctx.out(), "{}>", ar.back());
765 }
766};
767
768namespace sparrow
769{
770 template <typename D>
771 requires std::derived_from<D, array_crtp_base<D>>
772 std::ostream& operator<<(std::ostream& os, const D& value)
773 {
774 os << std::format("{}", value);
775 return os;
776 }
777}
778
779#endif
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
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
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 &)
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.
dynamic_bitset_view< std::uint8_t > bitmap_type
Traits class that must be specialized by array implementations.