sparrow 2.3.1
C++20 idiomatic APIs for the Apache Arrow Columnar Format
Loading...
Searching...
No Matches
null_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 <cstddef>
18#include <optional>
19#include <ranges>
20
30
31namespace sparrow
32{
57 template <class T>
58 class empty_iterator : public iterator_base<empty_iterator<T>, T, std::contiguous_iterator_tag, T>
59 {
60 public:
61
64 using reference = typename base_type::reference;
65 using difference_type = typename base_type::difference_type;
66
75 explicit empty_iterator(difference_type index = difference_type()) noexcept;
76
77 private:
78
87 [[nodiscard]] reference dereference() const;
88
95 void increment();
96
103 void decrement();
104
113 void advance(difference_type n);
114
124 [[nodiscard]] difference_type distance_to(const self_type& rhs) const;
125
134 [[nodiscard]] bool equal(const self_type& rhs) const;
135
144 [[nodiscard]] bool less_than(const self_type& rhs) const;
145
146 difference_type m_index;
147
148 friend class iterator_access;
149 };
150
151 class null_array;
152
158 template <class T>
159 constexpr bool is_null_array_v = std::same_as<T, null_array>;
160
161 namespace detail
162 {
163 template <>
165 {
166 [[nodiscard]] static constexpr sparrow::data_type get()
167 {
169 }
170 };
171 }
172
217 {
218 public:
219
223 using reverse_iterator = std::reverse_iterator<iterator>;
225 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
228 using size_type = std::size_t;
230 using iterator_tag = std::random_access_iterator_tag;
231
234
235 using const_value_range = std::ranges::subrange<const_value_iterator>;
236 using const_bitmap_range = std::ranges::subrange<const_bitmap_iterator>;
237
252 template <input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
254 size_t length,
255 std::optional<std::string_view> name = std::nullopt,
256 std::optional<METADATA_RANGE> metadata = std::nullopt
257 )
258 : m_proxy(create_proxy(length, std::move(name), std::move(metadata)))
259 {
260 }
261
273
276
279
288 [[nodiscard]] SPARROW_API std::optional<std::string_view> name() const;
289
298 [[nodiscard]] SPARROW_API std::optional<key_value_view> metadata() const;
299
308 [[nodiscard]] SPARROW_API size_type size() const;
309
321
333
342 [[nodiscard]] SPARROW_API iterator begin();
343
352 [[nodiscard]] SPARROW_API iterator end();
353
362 [[nodiscard]] SPARROW_API const_iterator begin() const;
363
372 [[nodiscard]] SPARROW_API const_iterator end() const;
373
383
393
403
413
422 [[nodiscard]] SPARROW_API const_iterator cbegin() const;
423
432 [[nodiscard]] SPARROW_API const_iterator cend() const;
433
443
453
463 [[nodiscard]] SPARROW_API reference front();
464
474 [[nodiscard]] SPARROW_API const_reference front() const;
475
485 [[nodiscard]] SPARROW_API reference back();
486
496 [[nodiscard]] SPARROW_API const_reference back() const;
497
506 [[nodiscard]] SPARROW_API const_value_range values() const;
507
516 [[nodiscard]] SPARROW_API const_bitmap_range bitmap() const;
517
540
550
551 private:
552
567 template <input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
568 [[nodiscard]] static arrow_proxy
569 create_proxy(size_t length, std::optional<std::string_view> name, std::optional<METADATA_RANGE> metadata);
570
578 [[nodiscard]] SPARROW_API difference_type ssize() const;
579
587 [[nodiscard]] SPARROW_API arrow_proxy& get_arrow_proxy();
588
596 [[nodiscard]] SPARROW_API const arrow_proxy& get_arrow_proxy() const;
597
598 arrow_proxy m_proxy;
599
601 };
602
617 bool operator==(const null_array& lhs, const null_array& rhs);
618
619 /*********************************
620 * empty_iterator implementation *
621 *********************************/
622
623 template <class T>
625 : m_index(index)
626 {
627 }
628
629 template <class T>
630 auto empty_iterator<T>::dereference() const -> reference
631 {
632 return T();
633 }
634
635 template <class T>
636 void empty_iterator<T>::increment()
637 {
638 ++m_index;
639 }
640
641 template <class T>
643 {
644 --m_index;
645 }
646
647 template <class T>
649 {
650 m_index += n;
651 }
652
653 template <class T>
655 {
656 return rhs.m_index - m_index;
657 }
658
659 template <class T>
660 bool empty_iterator<T>::equal(const self_type& rhs) const
661 {
662 return m_index == rhs.m_index;
663 }
664
665 template <class T>
666 bool empty_iterator<T>::less_than(const self_type& rhs) const
667 {
668 return m_index < rhs.m_index;
669 }
670
671 template <input_metadata_container METADATA_RANGE>
672 arrow_proxy
673 null_array::create_proxy(size_t length, std::optional<std::string_view> name, std::optional<METADATA_RANGE> metadata)
674 {
675 using namespace std::literals;
676 static const std::optional<std::unordered_set<sparrow::ArrowFlag>> flags{{ArrowFlag::NULLABLE}};
677 ArrowSchema schema = make_arrow_schema(
678 "n"sv,
679 std::move(name),
680 std::move(metadata),
681 flags,
682 0,
683 repeat_view<bool>(false, 0),
684 nullptr,
685 false
686 );
687
688 using buffer_type = sparrow::buffer<std::uint8_t>;
689 std::vector<buffer_type> arr_buffs = {};
690
691 ArrowArray arr = make_arrow_array(
692 static_cast<int64_t>(length),
693 static_cast<int64_t>(length),
694 0,
695 std::move(arr_buffs),
696 nullptr,
697 repeat_view<bool>(false, 0),
698 nullptr,
699 false
700 );
701 return arrow_proxy{std::move(arr), std::move(schema)};
702 }
703}
704
705#if defined(__cpp_lib_format)
706
707
708template <>
709struct std::formatter<sparrow::null_array>
710{
711 constexpr auto parse(std::format_parse_context& ctx)
712 {
713 return ctx.begin(); // Simple implementation
714 }
715
716 auto format(const sparrow::null_array& ar, std::format_context& ctx) const
717 {
718 return std::format_to(ctx.out(), "Null array [{}]", ar.size());
719 }
720};
721
722namespace sparrow
723{
724 inline std::ostream& operator<<(std::ostream& os, const null_array& value)
725 {
726 os << std::format("{}", value);
727 return os;
728 }
729}
730
731#endif
Iterator for null arrays where all elements are null.
empty_iterator< T > self_type
typename base_type::reference reference
iterator_base< self_type, T, std::contiguous_iterator_tag, T > base_type
typename base_type::difference_type difference_type
empty_iterator(difference_type index=difference_type()) noexcept
Constructs an empty iterator at the specified position.
Memory-efficient array implementation for null data types.
SPARROW_API const_reference back() const
Gets const reference to the last element.
SPARROW_API const_reverse_iterator crend() const
Gets const reverse iterator to the end of reversed array.
SPARROW_API null_array & operator=(const null_array &)
Copy assignment operator.
SPARROW_API iterator begin()
Gets iterator to the beginning of the array.
SPARROW_API reverse_iterator rbegin()
Gets reverse iterator to the beginning of reversed array.
SPARROW_API null_array(const null_array &)
Copy constructor.
SPARROW_API iterator end()
Gets iterator to the end of the array.
SPARROW_API std::optional< std::string_view > name() const
Gets the optional name of the array.
iterator::reference reference
SPARROW_API const_reverse_iterator rend() const
Gets const reverse iterator to the end of reversed array.
SPARROW_API void resize(size_type new_size)
Resizes the null array to the specified size.
null_type inner_value_type
empty_iterator< value_type > const_iterator
empty_iterator< int > const_value_iterator
SPARROW_API reference back()
Gets reference to the last element.
SPARROW_API size_type size() const
Gets the number of elements in the array.
SPARROW_API const_bitmap_range bitmap() const
Gets the validity bitmap as a range (all false for null arrays).
SPARROW_API const_iterator cend() const
Gets const iterator to the end of the array.
SPARROW_API const_reference front() const
Gets const reference to the first element.
SPARROW_API const_iterator end() const
Gets const iterator to the end of the array.
SPARROW_API const_reverse_iterator crbegin() const
Gets const reverse iterator to the beginning of reversed array.
std::ranges::subrange< const_bitmap_iterator > const_bitmap_range
SPARROW_API void slice_inplace(size_type start, size_type end)
Slices the array in place, updating offset and length.
empty_iterator< bool > const_bitmap_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
SPARROW_API const_reference operator[](size_type i) const
Gets const reference to element at specified position.
empty_iterator< value_type > iterator
nullable< inner_value_type > value_type
SPARROW_API std::optional< key_value_view > metadata() const
Gets the metadata associated with the array.
SPARROW_API null_array(arrow_proxy)
Constructs null array from Arrow proxy.
std::random_access_iterator_tag iterator_tag
std::ranges::subrange< const_value_iterator > const_value_range
SPARROW_API const_reverse_iterator rbegin() const
Gets const reverse iterator to the beginning of reversed array.
SPARROW_API const_iterator begin() const
Gets const iterator to the beginning of the array.
iterator::difference_type difference_type
std::size_t size_type
null_array(size_t length, std::optional< std::string_view > name=std::nullopt, std::optional< METADATA_RANGE > metadata=std::nullopt)
Constructs a null array with specified length and metadata.
const_iterator::reference const_reference
SPARROW_API const_value_range values() const
Gets the values as a range (conceptually empty for null arrays).
SPARROW_API reference front()
Gets reference to the first element.
SPARROW_API reference operator[](size_type i)
Gets mutable reference to element at specified position.
SPARROW_API const_iterator cbegin() const
Gets const iterator to the beginning of the array.
SPARROW_API reverse_iterator rend()
Gets reverse iterator to the end of reversed array.
std::reverse_iterator< iterator > reverse_iterator
#define SPARROW_API
Definition config.hpp:38
ArrowSchema make_arrow_schema(F format, N name, std::optional< M > metadata, std::optional< std::unordered_set< ArrowFlag > > flags, ArrowSchema **children, const CHILDREN_OWNERSHIP &children_ownership, ArrowSchema *dictionary, bool dictionary_ownership)
Creates an ArrowSchema owned by a unique_ptr and holding the provided data.
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
ArrowArray make_arrow_array(int64_t length, int64_t null_count, int64_t offset, B buffers, ArrowArray **children, const CHILDREN_OWNERSHIP &children_ownership, ArrowArray *dictionary, bool dictionary_ownership)
Creates an ArrowArray.
std::ostream & operator<<(std::ostream &os, const nullval_t &)
constexpr bool is_null_array_v
Type trait to check if a type is a null_array.
repeat_view(T &, size_t) -> repeat_view< T & >
data_type
Runtime identifier of arrow data types, usually associated with raw bytes with the associated value.
Extensions to the C++ standard library.
static constexpr sparrow::data_type get()
Metafunction for retrieving the data_type of a typed array.