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