sparrow 0.9.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
512 private:
513
528 template <input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
529 [[nodiscard]] static arrow_proxy
530 create_proxy(size_t length, std::optional<std::string_view> name, std::optional<METADATA_RANGE> metadata);
531
539 [[nodiscard]] SPARROW_API difference_type ssize() const;
540
548 [[nodiscard]] SPARROW_API arrow_proxy& get_arrow_proxy();
549
557 [[nodiscard]] SPARROW_API const arrow_proxy& get_arrow_proxy() const;
558
559 arrow_proxy m_proxy;
560
562 };
563
578 bool operator==(const null_array& lhs, const null_array& rhs);
579
580 /*********************************
581 * empty_iterator implementation *
582 *********************************/
583
584 template <class T>
586 : m_index(index)
587 {
588 }
589
590 template <class T>
591 auto empty_iterator<T>::dereference() const -> reference
592 {
593 return T();
594 }
595
596 template <class T>
597 void empty_iterator<T>::increment()
598 {
599 ++m_index;
600 }
601
602 template <class T>
604 {
605 --m_index;
606 }
607
608 template <class T>
610 {
611 m_index += n;
612 }
613
614 template <class T>
616 {
617 return rhs.m_index - m_index;
618 }
619
620 template <class T>
621 bool empty_iterator<T>::equal(const self_type& rhs) const
622 {
623 return m_index == rhs.m_index;
624 }
625
626 template <class T>
627 bool empty_iterator<T>::less_than(const self_type& rhs) const
628 {
629 return m_index < rhs.m_index;
630 }
631
632 template <input_metadata_container METADATA_RANGE>
633 arrow_proxy
634 null_array::create_proxy(size_t length, std::optional<std::string_view> name, std::optional<METADATA_RANGE> metadata)
635 {
636 using namespace std::literals;
637 static const std::optional<std::unordered_set<sparrow::ArrowFlag>> flags{{ArrowFlag::NULLABLE}};
638 ArrowSchema schema = make_arrow_schema(
639 "n"sv,
640 std::move(name),
641 std::move(metadata),
642 flags,
643 0,
644 repeat_view<bool>(false, 0),
645 nullptr,
646 false
647 );
648
649 using buffer_type = sparrow::buffer<std::uint8_t>;
650 std::vector<buffer_type> arr_buffs = {};
651
652 ArrowArray arr = make_arrow_array(
653 static_cast<int64_t>(length),
654 static_cast<int64_t>(length),
655 0,
656 std::move(arr_buffs),
657 nullptr,
658 repeat_view<bool>(false, 0),
659 nullptr,
660 false
661 );
662 return arrow_proxy{std::move(arr), std::move(schema)};
663 }
664}
665
666#if defined(__cpp_lib_format)
667
668
669template <>
670struct std::formatter<sparrow::null_array>
671{
672 constexpr auto parse(std::format_parse_context& ctx)
673 {
674 return ctx.begin(); // Simple implementation
675 }
676
677 auto format(const sparrow::null_array& ar, std::format_context& ctx) const
678 {
679 return std::format_to(ctx.out(), "Null array [{}]", ar.size());
680 }
681};
682
683inline std::ostream& operator<<(std::ostream& os, const sparrow::null_array& value)
684{
685 os << std::format("{}", value);
686 return os;
687}
688#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.
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.
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.
std::ostream & operator<<(std::ostream &os, const sparrow::nullval_t &)
static constexpr sparrow::data_type get()
Metafunction for retrieving the data_type of a typed array.