sparrow 0.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 <ranges>
19
27
28namespace sparrow
29{
30 /*
31 * @class empty_iterator
32 *
33 * @brief Iterator used by the null_layout class.
34 *
35 * @tparam T the value_type of the iterator
36 */
37 template <class T>
38 class empty_iterator : public iterator_base<empty_iterator<T>, T, std::contiguous_iterator_tag, T>
39 {
40 public:
41
44 using reference = typename base_type::reference;
45 using difference_type = typename base_type::difference_type;
46
47 explicit empty_iterator(difference_type index = difference_type()) noexcept;
48
49 private:
50
51 [[nodiscard]] reference dereference() const;
52 void increment();
53 void decrement();
54 void advance(difference_type n);
55 [[nodiscard]] difference_type distance_to(const self_type& rhs) const;
56 [[nodiscard]] bool equal(const self_type& rhs) const;
57 [[nodiscard]] bool less_than(const self_type& rhs) const;
58
59 difference_type m_index;
60
61 friend class iterator_access;
62 };
63
64 class null_array;
65
69 template <class T>
70 constexpr bool is_null_array_v = std::same_as<T, null_array>;
71
73 {
74 public:
75
82 using size_type = std::size_t;
84 using iterator_tag = std::random_access_iterator_tag;
85
88
89 using const_value_range = std::ranges::subrange<const_value_iterator>;
90 using const_bitmap_range = std::ranges::subrange<const_bitmap_iterator>;
91
93 size_t length,
94 std::optional<std::string_view> name = std::nullopt,
95 std::optional<std::string_view> metadata = std::nullopt
96 );
97
99
100 [[nodiscard]] SPARROW_API std::optional<std::string_view> name() const;
101 [[nodiscard]] SPARROW_API std::optional<std::string_view> metadata() const;
102
103 [[nodiscard]] SPARROW_API size_type size() const;
104
107
108 [[nodiscard]] SPARROW_API iterator begin();
109 [[nodiscard]] SPARROW_API iterator end();
110
111 [[nodiscard]] SPARROW_API const_iterator begin() const;
112 [[nodiscard]] SPARROW_API const_iterator end() const;
113
114 [[nodiscard]] SPARROW_API const_iterator cbegin() const;
115 [[nodiscard]] SPARROW_API const_iterator cend() const;
116
117 [[nodiscard]] SPARROW_API reference front();
118 [[nodiscard]] SPARROW_API const_reference front() const;
119
120 [[nodiscard]] SPARROW_API reference back();
121 [[nodiscard]] SPARROW_API const_reference back() const;
122
123 [[nodiscard]] SPARROW_API const_value_range values() const;
124 [[nodiscard]] SPARROW_API const_bitmap_range bitmap() const;
125
126 private:
127
128 [[nodiscard]] SPARROW_API static arrow_proxy
129 create_proxy(size_t length, std::optional<std::string_view> name, std::optional<std::string_view> metadata);
130
131 [[nodiscard]] SPARROW_API difference_type ssize() const;
132
133 [[nodiscard]] SPARROW_API arrow_proxy& get_arrow_proxy();
134 [[nodiscard]] SPARROW_API const arrow_proxy& get_arrow_proxy() const;
135
136 arrow_proxy m_proxy;
137
139 };
140
142 bool operator==(const null_array& lhs, const null_array& rhs);
143
144 /*********************************
145 * empty_iterator implementation *
146 *********************************/
147
148 template <class T>
150 : m_index(index)
151 {
152 }
153
154 template <class T>
155 auto empty_iterator<T>::dereference() const -> reference
156 {
157 return T();
158 }
159
160 template <class T>
161 void empty_iterator<T>::increment()
162 {
163 ++m_index;
164 }
165
166 template <class T>
168 {
169 --m_index;
170 }
171
172 template <class T>
174 {
175 m_index += n;
176 }
177
178 template <class T>
180 {
181 return rhs.m_index - m_index;
182 }
183
184 template <class T>
185 bool empty_iterator<T>::equal(const self_type& rhs) const
186 {
187 return m_index == rhs.m_index;
188 }
189
190 template <class T>
191 bool empty_iterator<T>::less_than(const self_type& rhs) const
192 {
193 return m_index < rhs.m_index;
194 }
195}
196
197#if defined(__cpp_lib_format)
198
199template <>
200struct std::formatter<sparrow::null_array>
201{
202 constexpr auto parse(std::format_parse_context& ctx)
203 {
204 return ctx.begin(); // Simple implementation
205 }
206
207 auto format(const sparrow::null_array& ar, std::format_context& ctx) const
208 {
209 return std::format_to(ctx.out(), "Null array [{}]", ar.size());
210 }
211};
212
213inline std::ostream& operator<<(std::ostream& os, const sparrow::null_array& value)
214{
215 os << std::format("{}", value);
216 return os;
217}
218
219#endif
Proxy class over ArrowArray and ArrowSchema.
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
SPARROW_API const_reference back() const
SPARROW_API iterator begin()
SPARROW_API iterator end()
SPARROW_API std::optional< std::string_view > name() const
iterator::reference reference
null_type inner_value_type
empty_iterator< value_type > const_iterator
SPARROW_API std::optional< std::string_view > metadata() const
empty_iterator< int > const_value_iterator
SPARROW_API reference back()
SPARROW_API size_type size() const
SPARROW_API const_bitmap_range bitmap() const
SPARROW_API const_iterator cend() const
SPARROW_API const_reference front() const
SPARROW_API const_iterator end() const
std::ranges::subrange< const_bitmap_iterator > const_bitmap_range
empty_iterator< bool > const_bitmap_iterator
SPARROW_API const_reference operator[](size_type i) const
empty_iterator< value_type > iterator
nullable< inner_value_type > value_type
SPARROW_API null_array(arrow_proxy)
std::random_access_iterator_tag iterator_tag
SPARROW_API null_array(size_t length, std::optional< std::string_view > name=std::nullopt, std::optional< std::string_view > metadata=std::nullopt)
std::ranges::subrange< const_value_iterator > const_value_range
SPARROW_API const_iterator begin() const
iterator::difference_type difference_type
std::size_t size_type
const_iterator::reference const_reference
SPARROW_API const_value_range values() const
SPARROW_API reference front()
SPARROW_API reference operator[](size_type i)
SPARROW_API const_iterator cbegin() const
The nullable class models a value or a reference that can be "null", or missing, like values traditio...
Definition nullable.hpp:280
#define SPARROW_API
Definition config.hpp:38
constexpr int64_t ssize(const T &value)
Get the size of a range, a tuple or an optional.
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
constexpr bool is_null_array_v
Checks whether T is a null_array type.
std::ostream & operator<<(std::ostream &stream, T n)
Definition large_int.hpp:93