sparrow 0.9.0
Loading...
Searching...
No Matches
fixed_width_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 <ranges>
18#include <string>
19#include <vector>
20
24
25#if defined(__cpp_lib_format)
26# include <format>
27# include <ostream>
28#endif
29
30namespace sparrow
31{
80 template <class L>
82 {
83 public:
84
86 using value_type = typename L::inner_value_type;
87 using reference = typename L::inner_reference;
88 using const_reference = typename L::inner_const_reference;
89 using size_type = typename L::size_type;
90 using difference_type = std::ptrdiff_t;
91 using iterator = typename L::data_iterator;
92 using const_iterator = typename L::const_data_iterator;
93
106
107 constexpr fixed_width_binary_reference(const fixed_width_binary_reference&) noexcept = default;
109
129 template <std::ranges::sized_range T>
130 requires mpl::convertible_ranges<T, typename L::inner_value_type>
131 constexpr self_type& operator=(T&& rhs);
132
141 [[nodiscard]] constexpr size_type size() const;
142
151 [[nodiscard]] constexpr iterator begin();
152
161 [[nodiscard]] constexpr iterator end();
162
171 [[nodiscard]] constexpr const_iterator begin() const;
172
181 [[nodiscard]] constexpr const_iterator end() const;
182
191 [[nodiscard]] constexpr const_iterator cbegin() const;
192
201 [[nodiscard]] constexpr const_iterator cend() const;
202
216 template <std::ranges::input_range T>
217 requires mpl::convertible_ranges<T, typename L::inner_value_type>
218 constexpr bool operator==(const T& rhs) const;
219
233 template <std::ranges::input_range T>
234 requires mpl::convertible_ranges<T, typename L::inner_value_type>
235 constexpr auto operator<=>(const T& rhs) const;
236
237 private:
238
248 [[nodiscard]] constexpr size_type offset(size_type index) const;
249
250 L* p_layout = nullptr;
251 size_type m_index = size_type(0);
252 };
253}
254
255namespace std
256{
257 template <typename Layout, template <typename> typename TQual, template <typename> typename UQual>
258 struct basic_common_reference<sparrow::fixed_width_binary_reference<Layout>, std::vector<sparrow::byte_t>, TQual, UQual>
259 {
260 using type = std::vector<sparrow::byte_t>;
261 };
262
263 template <typename Layout, template <typename> typename TQual, template <class> class UQual>
264 struct basic_common_reference<std::vector<sparrow::byte_t>, sparrow::fixed_width_binary_reference<Layout>, TQual, UQual>
265 {
266 using type = std::vector<sparrow::byte_t>;
267 };
268}
269
270namespace sparrow
271{
272 /***********************************************
273 * fixed_width_binary_reference implementation *
274 ***********************************************/
275
276 template <class L>
278 : p_layout(layout)
279 , m_index(index)
280 {
281 }
282
283 template <class L>
284 template <std::ranges::sized_range T>
287 {
288 SPARROW_ASSERT_TRUE(p_layout->m_element_size == std::ranges::size(rhs));
289 p_layout->assign(std::forward<T>(rhs), m_index);
290 p_layout->get_arrow_proxy().update_buffers();
291 return *this;
292 }
293
294 template <class L>
296 {
297 return p_layout->m_element_size;
298 }
299
300 template <class L>
302 {
303 return iterator(p_layout->data(offset(m_index)));
304 }
305
306 template <class L>
308 {
309 return iterator(p_layout->data(offset(m_index + 1)));
310 }
311
312 template <class L>
314 {
315 return cbegin();
316 }
317
318 template <class L>
320 {
321 return cend();
322 }
323
324 template <class L>
326 {
327 return const_iterator(p_layout->data(offset(m_index)));
328 }
329
330 template <class L>
332 {
333 return const_iterator(p_layout->data(offset(m_index + 1)));
334 }
335
336 template <class L>
337 template <std::ranges::input_range T>
339 constexpr bool fixed_width_binary_reference<L>::operator==(const T& rhs) const
340 {
341 return std::equal(cbegin(), cend(), std::cbegin(rhs), std::cend(rhs));
342 }
343
344 template <class L>
345 template <std::ranges::input_range T>
347 constexpr auto fixed_width_binary_reference<L>::operator<=>(const T& rhs) const
348 {
349 return lexicographical_compare_three_way(*this, rhs);
350 }
351
352 template <class L>
353 constexpr auto fixed_width_binary_reference<L>::offset(size_type index) const -> size_type
354 {
355 return p_layout->m_element_size * index;
356 }
357}
358
359#if defined(__cpp_lib_format)
360
361template <typename Layout>
362struct std::formatter<sparrow::fixed_width_binary_reference<Layout>>
363{
364 constexpr auto parse(std::format_parse_context& ctx)
365 {
366 return ctx.begin(); // Simple implementation
367 }
368
369 auto format(const sparrow::fixed_width_binary_reference<Layout>& ref, std::format_context& ctx) const
370 {
371 std::for_each(
372 ref.cbegin(),
373 sparrow::next(ref.cbegin(), ref.size() - 1),
374 [&ctx](const auto& value)
375 {
376 std::format_to(ctx.out(), "{}, ", value);
377 }
378 );
379
380 return std::format_to(ctx.out(), "{}>", *std::prev(ref.cend()));
381 }
382};
383
384template <typename Layout>
385inline std::ostream& operator<<(std::ostream& os, const sparrow::fixed_width_binary_reference<Layout>& value)
386{
387 os << std::format("{}", value);
388 return os;
389}
390
391#endif
constexpr bool operator==(const T &rhs) const
Equality comparison with another range of binary data.
constexpr auto operator<=>(const T &rhs) const
Three-way comparison with another range of binary data.
constexpr self_type & operator=(T &&rhs)
Assignment from a sized range of binary data.
typename array_type::inner_const_reference const_reference
constexpr fixed_width_binary_reference(const fixed_width_binary_reference &) noexcept=default
constexpr fixed_width_binary_reference(fixed_width_binary_reference &&) noexcept=default
constexpr fixed_width_binary_reference(L *layout, size_type index)
Constructs a binary reference for the given layout and index.
Concept for layouts.
Concept for convertible range types.
Definition mp_utils.hpp:931
#define SPARROW_ASSERT_TRUE(expr__)
constexpr InputIt next(InputIt it, Distance n)
Definition iterator.hpp:503
std::ostream & operator<<(std::ostream &os, const sparrow::nullval_t &)