sparrow 0.3.0
Loading...
Searching...
No Matches
variable_size_binary_reference.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 <algorithm>
18#include <concepts>
19#include <ranges>
20#include <string>
21#include <type_traits>
22#include <vector>
23
24#if defined(__cpp_lib_format)
25# include <format>
26# include <ostream>
27#endif
28
31
32namespace sparrow
33{
39 template <class L>
41 {
42 public:
43
45 using value_type = typename L::inner_value_type;
46 using reference = typename L::inner_reference;
47 using const_reference = typename L::inner_const_reference;
48 using size_type = typename L::size_type;
49 using difference_type = std::ptrdiff_t;
50 using iterator = typename L::data_iterator;
51 using const_iterator = typename L::const_data_iterator;
52 using offset_type = typename L::offset_type;
53
57
58 template <std::ranges::sized_range T>
61
62 // This is to avoid const char* from begin caught by the previous
63 // operator= overload. It would convert const char* to const char[N],
64 // including the null-terminating char.
65 template <class U = typename L::inner_value_type>
66 requires std::assignable_from<U&, const char*>
67 self_type& operator=(const char* rhs);
68
69 [[nodiscard]] size_type size() const;
70
71 [[nodiscard]] [[nodiscard]] iterator begin();
73
74 [[nodiscard]] const_iterator begin() const;
75 [[nodiscard]] const_iterator end() const;
76 [[nodiscard]] const_iterator cbegin() const;
77 [[nodiscard]] const_iterator cend() const;
78
79 template <std::ranges::input_range T>
81 bool operator==(const T& rhs) const;
82
83 template <class U = typename L::inner_value_type>
84 requires std::assignable_from<U&, const char*>
85 bool operator==(const char* rhs) const;
86
87 template <std::ranges::input_range T>
89 auto operator<=>(const T& rhs) const;
90
91 template <class U = typename L::inner_value_type>
92 requires std::assignable_from<U&, const char*>
93 auto operator<=>(const char* rhs) const;
94
95 private:
96
97 [[nodiscard]] offset_type offset(size_type index) const;
98 [[nodiscard]] size_type uoffset(size_type index) const;
99
100 L* p_layout = nullptr;
101 size_type m_index = size_type(0);
102 };
103}
104
105namespace std
106{
107 template <typename Layout, template <typename> typename TQual, template <typename> typename UQual>
108 struct basic_common_reference<sparrow::variable_size_binary_reference<Layout>, std::string, TQual, UQual>
109 {
110 using type = std::string;
111 };
112
113 template <typename Layout, template <typename> typename TQual, template <class> class UQual>
114 struct basic_common_reference<std::string, sparrow::variable_size_binary_reference<Layout>, TQual, UQual>
115 {
116 using type = std::string;
117 };
118
119 template <typename Layout, template <typename> typename TQual, template <typename> typename UQual>
120 struct basic_common_reference<sparrow::variable_size_binary_reference<Layout>, std::vector<std::byte>, TQual, UQual>
121 {
122 using type = std::vector<std::byte>;
123 };
124
125 template <typename Layout, template <typename> typename TQual, template <class> class UQual>
126 struct basic_common_reference<std::vector<std::byte>, sparrow::variable_size_binary_reference<Layout>, TQual, UQual>
127 {
128 using type = std::vector<std::byte>;
129 };
130}
131
132namespace sparrow
133{
134 /*************************************************
135 * variable_size_binary_reference implementation *
136 *************************************************/
137
138 template <class L>
140 : p_layout(layout)
141 , m_index(index)
142 {
143 }
144
145 template <class L>
146 template <std::ranges::sized_range T>
149 {
150 p_layout->assign(std::forward<T>(rhs), m_index);
151 p_layout->get_arrow_proxy().update_buffers();
152 return *this;
153 }
154
155 template <class L>
156 template <class U>
157 requires std::assignable_from<U&, const char*>
159 {
160 return *this = std::string_view(rhs);
161 }
162
163 template <class L>
165 {
166 return static_cast<size_type>(offset(m_index + 1) - offset(m_index));
167 }
168
169 template <class L>
171 {
172 return iterator(p_layout->data(uoffset(m_index)));
173 }
174
175 template <class L>
177 {
178 return iterator(p_layout->data(uoffset(m_index + 1)));
179 }
180
181 template <class L>
183 {
184 return cbegin();
185 }
186
187 template <class L>
189 {
190 return cend();
191 }
192
193 template <class L>
195 {
196 return const_iterator(p_layout->data(uoffset(m_index)));
197 }
198
199 template <class L>
201 {
202 return const_iterator(p_layout->data(uoffset(m_index + 1)));
203 }
204
205 template <class L>
206 template <std::ranges::input_range T>
209 {
210 return std::equal(cbegin(), cend(), std::cbegin(rhs), std::cend(rhs));
211 }
212
213 template <class L>
214 template <class U>
215 requires std::assignable_from<U&, const char*>
217 {
218 return operator==(std::string_view(rhs));
219 }
220
221 template <class L>
222 template <std::ranges::input_range T>
225 {
226 return lexicographical_compare_three_way(*this, rhs);
227 }
228
229 template <class L>
230 template <class U>
231 requires std::assignable_from<U&, const char*>
233 {
234 return operator<=>(std::string_view(rhs));
235 }
236
237 template <class L>
238 auto variable_size_binary_reference<L>::offset(size_type index) const -> offset_type
239 {
240 return *(p_layout->offset(index));
241 }
242
243 template <class L>
244 auto variable_size_binary_reference<L>::uoffset(size_type index) const -> size_type
245 {
246 return static_cast<size_type>(offset(index));
247 }
248}
249
250#if defined(__cpp_lib_format)
251template <typename Layout>
252struct std::formatter<sparrow::variable_size_binary_reference<Layout>>
253{
254 constexpr auto parse(std::format_parse_context& ctx)
255 {
256 return ctx.begin(); // Simple implementation
257 }
258
259 auto format(const sparrow::variable_size_binary_reference<Layout>& ref, std::format_context& ctx) const
260 {
261 std::for_each(
262 ref.cbegin(),
263 sparrow::next(ref.cbegin(), ref.size() - 1),
264 [&ctx](const auto& value)
265 {
266 std::format_to(ctx.out(), "{}, ", value);
267 }
268 );
269
270 return std::format_to(ctx.out(), "{}>", *std::prev(ref.cend()));
271 }
272};
273
274template <typename Layout>
275inline std::ostream& operator<<(std::ostream& os, const sparrow::variable_size_binary_reference<Layout>& value)
276{
277 os << std::format("{}", value);
278 return os;
279}
280
281#endif
Implementation of reference to inner type used for layout L.
variable_size_binary_reference(variable_size_binary_reference &&)=default
variable_size_binary_reference(const variable_size_binary_reference &)=default
self_type & operator=(const char *rhs)
Matches range types From whose elements are convertible to elements of range type To.
Definition mp_utils.hpp:450
constexpr std::compare_three_way_result_t< typename cloning_ptr< T1 >::pointer, typename cloning_ptr< T2 >::pointer > operator<=>(const cloning_ptr< T1 > &lhs, const cloning_ptr< T2 > &rhs) noexcept
Definition memory.hpp:475
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
constexpr InputIt next(InputIt it, Distance n)
Definition iterator.hpp:503
std::ostream & operator<<(std::ostream &stream, T n)
Definition large_int.hpp:93