sparrow ..
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
242 [[nodiscard]] constexpr reference operator[](size_type i);
243
249 [[nodiscard]] constexpr const_reference operator[](size_type i) const;
250
257 [[nodiscard]] constexpr reference at(size_type i);
258
265 [[nodiscard]] constexpr const_reference at(size_type i) const;
266
267 private:
268
278 [[nodiscard]] constexpr size_type offset(size_type index) const;
279
280 L* p_layout = nullptr;
281 size_type m_index = size_type(0);
282 };
283}
284
285namespace std
286{
287 template <typename Layout, template <typename> typename TQual, template <typename> typename UQual>
288 struct basic_common_reference<sparrow::fixed_width_binary_reference<Layout>, std::vector<sparrow::byte_t>, TQual, UQual>
289 {
290 using type = std::vector<sparrow::byte_t>;
291 };
292
293 template <typename Layout, template <typename> typename TQual, template <class> class UQual>
294 struct basic_common_reference<std::vector<sparrow::byte_t>, sparrow::fixed_width_binary_reference<Layout>, TQual, UQual>
295 {
296 using type = std::vector<sparrow::byte_t>;
297 };
298}
299
300namespace sparrow
301{
302 /***********************************************
303 * fixed_width_binary_reference implementation *
304 ***********************************************/
305
306 template <class L>
308 : p_layout(layout)
309 , m_index(index)
310 {
311 }
312
313 template <class L>
314 template <std::ranges::sized_range T>
317 {
318 SPARROW_ASSERT_TRUE(p_layout->m_element_size == std::ranges::size(rhs));
319 p_layout->assign(std::forward<T>(rhs), m_index);
320 p_layout->get_arrow_proxy().update_buffers();
321 return *this;
322 }
323
324 template <class L>
326 {
327 return p_layout->m_element_size;
328 }
329
330 template <class L>
332 {
333 return iterator(p_layout->data(offset(m_index)));
334 }
335
336 template <class L>
338 {
339 return iterator(p_layout->data(offset(m_index + 1)));
340 }
341
342 template <class L>
344 {
345 return cbegin();
346 }
347
348 template <class L>
350 {
351 return cend();
352 }
353
354 template <class L>
356 {
357 return const_iterator(p_layout->data(offset(m_index)));
358 }
359
360 template <class L>
362 {
363 return const_iterator(p_layout->data(offset(m_index + 1)));
364 }
365
366 template <class L>
367 template <std::ranges::input_range T>
369 constexpr bool fixed_width_binary_reference<L>::operator==(const T& rhs) const
370 {
371 return std::equal(cbegin(), cend(), std::cbegin(rhs), std::cend(rhs));
372 }
373
374 template <class L>
375 template <std::ranges::input_range T>
377 constexpr auto fixed_width_binary_reference<L>::operator<=>(const T& rhs) const
378 {
379 return lexicographical_compare_three_way(*this, rhs);
380 }
381
382 template <class L>
383 constexpr auto fixed_width_binary_reference<L>::offset(size_type index) const -> size_type
384 {
385 return p_layout->m_element_size * index;
386 }
387
388 template <class L>
390 {
391 return *(begin() + i);
392 }
393
394 template <class L>
395 [[nodiscard]] constexpr auto fixed_width_binary_reference<L>::operator[](size_type i) const
397 {
398 return *(cbegin() + i);
399 }
400
401 template <class L>
402 [[nodiscard]] constexpr auto fixed_width_binary_reference<L>::at(size_type i) -> reference
403 {
404 if (i >= size())
405 {
406 throw std::out_of_range("fixed_width_binary_reference::at() index out of range");
407 }
408 return operator[](i);
409 }
410
411 template <class L>
412 [[nodiscard]] constexpr auto fixed_width_binary_reference<L>::at(size_type i) const -> const_reference
413 {
414 if (i >= size())
415 {
416 throw std::out_of_range("fixed_width_binary_reference::at() index out of range");
417 }
418 return operator[](i);
419 }
420}
421
422#if defined(__cpp_lib_format)
423
424template <typename Layout>
425struct std::formatter<sparrow::fixed_width_binary_reference<Layout>>
426{
427 constexpr auto parse(std::format_parse_context& ctx)
428 {
429 return ctx.begin(); // Simple implementation
430 }
431
432 auto format(const sparrow::fixed_width_binary_reference<Layout>& ref, std::format_context& ctx) const
433 {
434 std::for_each(
435 ref.cbegin(),
436 sparrow::next(ref.cbegin(), ref.size() - 1),
437 [&ctx](const auto& value)
438 {
439 std::format_to(ctx.out(), "{}, ", value);
440 }
441 );
442
443 return std::format_to(ctx.out(), "{}>", *std::prev(ref.cend()));
444 }
445};
446
447namespace sparrow
448{
449 template <typename Layout>
450 std::ostream& operator<<(std::ostream& os, const fixed_width_binary_reference<Layout>& value)
451 {
452 os << std::format("{}", value);
453 return os;
454 }
455}
456
457#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 reference operator[](size_type i)
Byte-level access to the binary element (unchecked).
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 nullval_t &)