sparrow ..
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
199 [[nodiscard]] constexpr const_reference at(size_type i) const;
200
213 [[nodiscard]] constexpr const_reference operator[](size_type i) const;
214
227 [[nodiscard]] constexpr const_reference front() const;
228
241 [[nodiscard]] constexpr const_reference back() const;
242
251 [[nodiscard]] constexpr const_iterator begin() const;
252
261 [[nodiscard]] constexpr const_iterator end() const;
262
271 [[nodiscard]] constexpr const_iterator cbegin() const;
272
281 [[nodiscard]] constexpr const_iterator cend() const;
282
291 [[nodiscard]] constexpr const_reverse_iterator rbegin() const;
292
301 [[nodiscard]] constexpr const_reverse_iterator rend() const;
302
311 [[nodiscard]] constexpr const_reverse_iterator crbegin() const;
312
321 [[nodiscard]] constexpr const_reverse_iterator crend() const;
322
332 [[nodiscard]] constexpr const_bitmap_range bitmap() const;
333
343 [[nodiscard]] constexpr const_value_range values() const;
344
364 [[nodiscard]] constexpr D slice(size_type start, size_type end) const;
365
386 [[nodiscard]] constexpr D slice_view(size_type start, size_type end) const;
387
388 protected:
389
400
401 constexpr array_crtp_base(const array_crtp_base&) = default;
402 constexpr array_crtp_base& operator=(const array_crtp_base&) = default;
403
404 constexpr array_crtp_base(array_crtp_base&&) noexcept = default;
405 constexpr array_crtp_base& operator=(array_crtp_base&&) noexcept = default;
406
414 [[nodiscard]] constexpr arrow_proxy& get_arrow_proxy() noexcept;
415
423 [[nodiscard]] constexpr const arrow_proxy& get_arrow_proxy() const noexcept;
424
438
447 constexpr const_bitmap_iterator bitmap_begin() const;
448
456 constexpr const_bitmap_iterator bitmap_end() const;
457
466 constexpr const_bitmap_iterator bitmap_cbegin() const;
467
476 constexpr const_bitmap_iterator bitmap_cend() const;
477
478 private:
479
480 arrow_proxy m_proxy;
481
482 // friend classes
483 friend class layout_iterator<iterator_types>;
484 friend class detail::array_access;
485#if defined(__cpp_lib_format)
486 friend struct std::formatter<D>;
487#endif
488 };
489
490 template <class D>
491 constexpr bool operator==(const array_crtp_base<D>& lhs, const array_crtp_base<D>& rhs);
492
493 /**********************************
494 * array_crtp_base implementation *
495 **********************************/
496
497 template <class D>
498 constexpr std::optional<std::string_view> array_crtp_base<D>::name() const
499 {
500 return get_arrow_proxy().name();
501 }
502
503 template <class D>
504 std::optional<key_value_view> array_crtp_base<D>::metadata() const
505 {
506 return get_arrow_proxy().metadata();
507 }
508
509 template <class D>
510 constexpr bool array_crtp_base<D>::empty() const
511 {
512 return size() == size_type(0);
513 }
514
515 template <class D>
516 constexpr auto array_crtp_base<D>::size() const -> size_type
517 {
518 return static_cast<size_type>(get_arrow_proxy().length());
519 }
520
521 template <class D>
523 {
524 if (i >= size())
525 {
526 const std::string error_message = "Index " + std::to_string(i)
527 + " is greater or equal to size of array ("
528 + std::to_string(size()) + ")";
529 throw std::out_of_range(error_message);
530 }
531 return (*this)[i];
532 }
533
534 template <class D>
536 {
537 auto& derived_cast = this->derived_cast();
539 return const_reference(inner_const_reference(derived_cast.value(i)), derived_cast.has_value(i));
540 }
541
542 template <class D>
544 {
546 return (*this)[size_type(0)];
547 }
548
549 template <class D>
551 {
553 return (*this)[size() - 1];
554 }
555
556 template <class D>
558 {
559 return cbegin();
560 }
561
562 template <class D>
563 constexpr auto array_crtp_base<D>::end() const -> const_iterator
564 {
565 return cend();
566 }
567
568 template <class D>
570 {
571 return const_iterator(this->derived_cast().value_cbegin(), bitmap_begin());
572 }
573
574 template <class D>
575 constexpr auto array_crtp_base<D>::cend() const -> const_iterator
576 {
577 return const_iterator(this->derived_cast().value_cend(), bitmap_end());
578 }
579
580 template <class D>
582 {
583 return crbegin();
584 }
585
586 template <class D>
588 {
589 return crend();
590 }
591
592 template <class D>
594 {
596 }
597
598 template <class D>
600 {
602 }
603
604 template <class D>
606 {
608 }
609
610 template <class D>
612 {
613 return const_value_range(this->derived_cast().value_cbegin(), this->derived_cast().value_cend());
614 }
615
616 template <class D>
618 : m_proxy(std::move(proxy))
619 {
620 }
621
622 template <class D>
624 {
625 return m_proxy;
626 }
627
628 template <class D>
629 constexpr auto array_crtp_base<D>::get_arrow_proxy() const noexcept -> const arrow_proxy&
630 {
631 return m_proxy;
632 }
633
634 template <class D>
636 {
638 return *sparrow::next(bitmap_begin(), i);
639 }
640
641 template <class D>
643 {
644 return sparrow::next(this->derived_cast().get_bitmap().cbegin(), get_arrow_proxy().offset());
645 }
646
647 template <class D>
649 {
650 return sparrow::next(bitmap_begin(), size());
651 }
652
653 template <class D>
655 {
656 return bitmap_begin();
657 }
658
659 template <class D>
661 {
662 return bitmap_end();
663 }
664
665 template <class D>
667 {
668 SPARROW_ASSERT_TRUE(start <= end);
669 return D{get_arrow_proxy().slice(start, end)};
670 }
671
672 template <class D>
674 {
675 SPARROW_ASSERT_TRUE(start <= end);
676 return D{get_arrow_proxy().slice_view(start, end)};
677 }
678
679 /*
680 * @brief Equality comparison operator for arrays.
681 *
682 * Compares two arrays element-wise, including both values and validity flags.
683 *
684 * @tparam D Array type
685 * @param lhs First array to compare
686 * @param rhs Second array to compare
687 * @return true if arrays are element-wise equal, false otherwise
688 *
689 * @post Returns true iff arrays have same size and all elements compare equal
690 * @post Comparison includes both values and validity states
691 */
692 template <class D>
693 constexpr bool operator==(const array_crtp_base<D>& lhs, const array_crtp_base<D>& rhs)
694 {
695 return std::ranges::equal(lhs, rhs);
696 }
697}
698
699#if defined(__cpp_lib_format)
700
701template <typename D>
702 requires std::derived_from<D, sparrow::array_crtp_base<D>>
703struct std::formatter<D>
704{
705 constexpr auto parse(std::format_parse_context& ctx)
706 {
707 return ctx.begin(); // Simple implementation
708 }
709
710 auto format(const D& ar, std::format_context& ctx) const
711 {
712 const auto& proxy = ar.get_arrow_proxy();
713 std::string type;
714 if (proxy.dictionary())
715 {
716 std::format_to(ctx.out(), "Dictionary<{}>", proxy.dictionary()->data_type());
717 }
718 else
719 {
720 std::format_to(ctx.out(), "{}", proxy.data_type());
721 }
722 std::format_to(ctx.out(), " [name={} | size={}] <", ar.name().value_or("nullptr"), proxy.length());
723
724 std::for_each(
725 ar.cbegin(),
726 std::prev(ar.cend()),
727 [&ctx](const auto& value)
728 {
729 std::format_to(ctx.out(), "{}, ", value);
730 }
731 );
732 return std::format_to(ctx.out(), "{}>", ar.back());
733 }
734};
735
736namespace sparrow
737{
738 template <typename D>
739 requires std::derived_from<D, array_crtp_base<D>>
740 std::ostream& operator<<(std::ostream& os, const D& value)
741 {
742 os << std::format("{}", value);
743 return os;
744 }
745}
746
747#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
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.
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.