sparrow 1.3.0
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
282 [[nodiscard]] SPARROW_API std::optional<std::string_view> name() const;
283
292 [[nodiscard]] SPARROW_API std::optional<key_value_view> metadata() const;
293
302 [[nodiscard]] SPARROW_API size_type size() const;
303
315
327
336 [[nodiscard]] SPARROW_API iterator begin();
337
346 [[nodiscard]] SPARROW_API iterator end();
347
356 [[nodiscard]] SPARROW_API const_iterator begin() const;
357
366 [[nodiscard]] SPARROW_API const_iterator end() const;
367
377
387
397
407
416 [[nodiscard]] SPARROW_API const_iterator cbegin() const;
417
426 [[nodiscard]] SPARROW_API const_iterator cend() const;
427
437
447
457 [[nodiscard]] SPARROW_API reference front();
458
468 [[nodiscard]] SPARROW_API const_reference front() const;
469
479 [[nodiscard]] SPARROW_API reference back();
480
490 [[nodiscard]] SPARROW_API const_reference back() const;
491
500 [[nodiscard]] SPARROW_API const_value_range values() const;
501
510 [[nodiscard]] SPARROW_API const_bitmap_range bitmap() const;
511
534
535 private:
536
551 template <input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
552 [[nodiscard]] static arrow_proxy
553 create_proxy(size_t length, std::optional<std::string_view> name, std::optional<METADATA_RANGE> metadata);
554
562 [[nodiscard]] SPARROW_API difference_type ssize() const;
563
571 [[nodiscard]] SPARROW_API arrow_proxy& get_arrow_proxy();
572
580 [[nodiscard]] SPARROW_API const arrow_proxy& get_arrow_proxy() const;
581
582 arrow_proxy m_proxy;
583
585 };
586
601 bool operator==(const null_array& lhs, const null_array& rhs);
602
603 /*********************************
604 * empty_iterator implementation *
605 *********************************/
606
607 template <class T>
609 : m_index(index)
610 {
611 }
612
613 template <class T>
614 auto empty_iterator<T>::dereference() const -> reference
615 {
616 return T();
617 }
618
619 template <class T>
620 void empty_iterator<T>::increment()
621 {
622 ++m_index;
623 }
624
625 template <class T>
627 {
628 --m_index;
629 }
630
631 template <class T>
633 {
634 m_index += n;
635 }
636
637 template <class T>
639 {
640 return rhs.m_index - m_index;
641 }
642
643 template <class T>
644 bool empty_iterator<T>::equal(const self_type& rhs) const
645 {
646 return m_index == rhs.m_index;
647 }
648
649 template <class T>
650 bool empty_iterator<T>::less_than(const self_type& rhs) const
651 {
652 return m_index < rhs.m_index;
653 }
654
655 template <input_metadata_container METADATA_RANGE>
656 arrow_proxy
657 null_array::create_proxy(size_t length, std::optional<std::string_view> name, std::optional<METADATA_RANGE> metadata)
658 {
659 using namespace std::literals;
660 static const std::optional<std::unordered_set<sparrow::ArrowFlag>> flags{{ArrowFlag::NULLABLE}};
661 ArrowSchema schema = make_arrow_schema(
662 "n"sv,
663 std::move(name),
664 std::move(metadata),
665 flags,
666 0,
667 repeat_view<bool>(false, 0),
668 nullptr,
669 false
670 );
671
672 using buffer_type = sparrow::buffer<std::uint8_t>;
673 std::vector<buffer_type> arr_buffs = {};
674
675 ArrowArray arr = make_arrow_array(
676 static_cast<int64_t>(length),
677 static_cast<int64_t>(length),
678 0,
679 std::move(arr_buffs),
680 nullptr,
681 repeat_view<bool>(false, 0),
682 nullptr,
683 false
684 );
685 return arrow_proxy{std::move(arr), std::move(schema)};
686 }
687}
688
689#if defined(__cpp_lib_format)
690
691
692template <>
693struct std::formatter<sparrow::null_array>
694{
695 constexpr auto parse(std::format_parse_context& ctx)
696 {
697 return ctx.begin(); // Simple implementation
698 }
699
700 auto format(const sparrow::null_array& ar, std::format_context& ctx) const
701 {
702 return std::format_to(ctx.out(), "Null array [{}]", ar.size());
703 }
704};
705
706namespace sparrow
707{
708 inline std::ostream& operator<<(std::ostream& os, const null_array& value)
709 {
710 os << std::format("{}", value);
711 return os;
712 }
713}
714
715#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 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 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
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.
data_type
Runtime identifier of arrow data types, usually associated with raw bytes with the associated value.
static constexpr sparrow::data_type get()
Metafunction for retrieving the data_type of a typed array.