sparrow 0.6.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
28
29namespace sparrow
30{
31 /*
32 * @class empty_iterator
33 *
34 * @brief Iterator used by the null_layout class.
35 *
36 * @tparam T the value_type of the iterator
37 */
38 template <class T>
39 class empty_iterator : public iterator_base<empty_iterator<T>, T, std::contiguous_iterator_tag, T>
40 {
41 public:
42
45 using reference = typename base_type::reference;
46 using difference_type = typename base_type::difference_type;
47
48 explicit empty_iterator(difference_type index = difference_type()) noexcept;
49
50 private:
51
52 [[nodiscard]] reference dereference() const;
53 void increment();
54 void decrement();
55 void advance(difference_type n);
56 [[nodiscard]] difference_type distance_to(const self_type& rhs) const;
57 [[nodiscard]] bool equal(const self_type& rhs) const;
58 [[nodiscard]] bool less_than(const self_type& rhs) const;
59
60 difference_type m_index;
61
62 friend class iterator_access;
63 };
64
65 class null_array;
66
70 template <class T>
71 constexpr bool is_null_array_v = std::same_as<T, null_array>;
72
74 {
75 public:
76
83 using size_type = std::size_t;
85 using iterator_tag = std::random_access_iterator_tag;
86
89
90 using const_value_range = std::ranges::subrange<const_value_iterator>;
91 using const_bitmap_range = std::ranges::subrange<const_bitmap_iterator>;
92
93 template <input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
95 size_t length,
96 std::optional<std::string_view> name = std::nullopt,
97 std::optional<METADATA_RANGE> metadata = std::nullopt
98 )
99 : m_proxy(create_proxy(length, std::move(name), std::move(metadata)))
100 {
101 }
102
104
105 [[nodiscard]] SPARROW_API std::optional<std::string_view> name() const;
106 [[nodiscard]] SPARROW_API std::optional<key_value_view> metadata() const;
107
108 [[nodiscard]] SPARROW_API size_type size() const;
109
112
113 [[nodiscard]] SPARROW_API iterator begin();
114 [[nodiscard]] SPARROW_API iterator end();
115
116 [[nodiscard]] SPARROW_API const_iterator begin() const;
117 [[nodiscard]] SPARROW_API const_iterator end() const;
118
119 [[nodiscard]] SPARROW_API const_iterator cbegin() const;
120 [[nodiscard]] SPARROW_API const_iterator cend() const;
121
122 [[nodiscard]] SPARROW_API reference front();
123 [[nodiscard]] SPARROW_API const_reference front() const;
124
125 [[nodiscard]] SPARROW_API reference back();
126 [[nodiscard]] SPARROW_API const_reference back() const;
127
128 [[nodiscard]] SPARROW_API const_value_range values() const;
129 [[nodiscard]] SPARROW_API const_bitmap_range bitmap() const;
130
131 private:
132
133 template <input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
134 [[nodiscard]] static arrow_proxy
135 create_proxy(size_t length, std::optional<std::string_view> name, std::optional<METADATA_RANGE> metadata);
136
137 [[nodiscard]] SPARROW_API difference_type ssize() const;
138
139 [[nodiscard]] SPARROW_API arrow_proxy& get_arrow_proxy();
140 [[nodiscard]] SPARROW_API const arrow_proxy& get_arrow_proxy() const;
141
142 arrow_proxy m_proxy;
143
145 };
146
148 bool operator==(const null_array& lhs, const null_array& rhs);
149
150 /*********************************
151 * empty_iterator implementation *
152 *********************************/
153
154 template <class T>
156 : m_index(index)
157 {
158 }
159
160 template <class T>
161 auto empty_iterator<T>::dereference() const -> reference
162 {
163 return T();
164 }
165
166 template <class T>
167 void empty_iterator<T>::increment()
168 {
169 ++m_index;
170 }
171
172 template <class T>
174 {
175 --m_index;
176 }
177
178 template <class T>
180 {
181 m_index += n;
182 }
183
184 template <class T>
186 {
187 return rhs.m_index - m_index;
188 }
189
190 template <class T>
191 bool empty_iterator<T>::equal(const self_type& rhs) const
192 {
193 return m_index == rhs.m_index;
194 }
195
196 template <class T>
197 bool empty_iterator<T>::less_than(const self_type& rhs) const
198 {
199 return m_index < rhs.m_index;
200 }
201
202 template <input_metadata_container METADATA_RANGE>
203 arrow_proxy
204 null_array::create_proxy(size_t length, std::optional<std::string_view> name, std::optional<METADATA_RANGE> metadata)
205 {
206 using namespace std::literals;
207 ArrowSchema schema = make_arrow_schema(
208 "n"sv,
209 std::move(name),
210 std::move(metadata),
211 std::nullopt,
212 0,
213 repeat_view<bool>(false, 0),
214 nullptr,
215 false
216 );
217
218 using buffer_type = sparrow::buffer<std::uint8_t>;
219 std::vector<buffer_type> arr_buffs = {};
220
221 ArrowArray arr = make_arrow_array(
222 static_cast<int64_t>(length),
223 static_cast<int64_t>(length),
224 0,
225 std::move(arr_buffs),
226 nullptr,
227 repeat_view<bool>(false, 0),
228 nullptr,
229 false
230 );
231 return arrow_proxy{std::move(arr), std::move(schema)};
232 }
233}
234
235#if defined(__cpp_lib_format)
236
237template <>
238struct std::formatter<sparrow::null_array>
239{
240 constexpr auto parse(std::format_parse_context& ctx)
241 {
242 return ctx.begin(); // Simple implementation
243 }
244
245 auto format(const sparrow::null_array& ar, std::format_context& ctx) const
246 {
247 return std::format_to(ctx.out(), "Null array [{}]", ar.size());
248 }
249};
250
251inline std::ostream& operator<<(std::ostream& os, const sparrow::null_array& value)
252{
253 os << std::format("{}", value);
254 return os;
255}
256#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
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 std::optional< key_value_view > metadata() const
SPARROW_API null_array(arrow_proxy)
std::random_access_iterator_tag iterator_tag
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
null_array(size_t length, std::optional< std::string_view > name=std::nullopt, std::optional< METADATA_RANGE > metadata=std::nullopt)
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
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.
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.
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
Checks whether T is a null_array type.
std::ostream & operator<<(std::ostream &stream, T n)
Definition large_int.hpp:93