sparrow ..
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{
88 template <class L>
90 {
91 public:
92
94 using value_type = typename L::inner_value_type;
95 using reference = typename L::inner_reference;
96 using const_reference = typename L::inner_const_reference;
97 using size_type = typename L::size_type;
98 using difference_type = std::ptrdiff_t;
99 using iterator = typename L::data_iterator;
100 using const_iterator = typename L::const_data_iterator;
101 using offset_type = typename L::offset_type;
102
115
118
137 template <std::ranges::sized_range T>
139 constexpr self_type& operator=(T&& rhs);
140
161 template <class U = typename L::inner_value_type>
162 requires std::assignable_from<U&, const char*>
163 constexpr self_type& operator=(const char* rhs);
164
174 [[nodiscard]] constexpr size_type size() const;
175
183 [[nodiscard]] constexpr bool empty() const;
184
193 [[nodiscard]] constexpr iterator begin();
194
203 [[nodiscard]] constexpr iterator end();
204
213 [[nodiscard]] constexpr const_iterator begin() const;
214
223 [[nodiscard]] constexpr const_iterator end() const;
224
233 [[nodiscard]] constexpr const_iterator cbegin() const;
234
243 [[nodiscard]] constexpr const_iterator cend() const;
244
258 template <std::ranges::input_range T>
260 constexpr bool operator==(const T& rhs) const;
261
277 template <class U = typename L::inner_value_type>
278 requires std::assignable_from<U&, const char*>
279 constexpr bool operator==(const char* rhs) const;
280
294 template <std::ranges::input_range T>
296 constexpr auto operator<=>(const T& rhs) const;
297
313 template <class U = typename L::inner_value_type>
314 requires std::assignable_from<U&, const char*>
315 constexpr auto operator<=>(const char* rhs) const;
316
317 private:
318
328 [[nodiscard]] constexpr offset_type offset(size_type index) const;
329
339 [[nodiscard]] constexpr size_type uoffset(size_type index) const;
340
341 L* p_layout = nullptr;
342 size_type m_index = size_type(0);
343 };
344}
345
346namespace std
347{
348 template <typename Layout, template <typename> typename TQual, template <typename> typename UQual>
349 struct basic_common_reference<sparrow::variable_size_binary_reference<Layout>, std::string, TQual, UQual>
350 {
351 using type = std::string;
352 };
353
354 template <typename Layout, template <typename> typename TQual, template <class> class UQual>
355 struct basic_common_reference<std::string, sparrow::variable_size_binary_reference<Layout>, TQual, UQual>
356 {
357 using type = std::string;
358 };
359
360 template <typename Layout, template <typename> typename TQual, template <typename> typename UQual>
361 struct basic_common_reference<sparrow::variable_size_binary_reference<Layout>, std::vector<std::byte>, TQual, UQual>
362 {
363 using type = std::vector<std::byte>;
364 };
365
366 template <typename Layout, template <typename> typename TQual, template <class> class UQual>
367 struct basic_common_reference<std::vector<std::byte>, sparrow::variable_size_binary_reference<Layout>, TQual, UQual>
368 {
369 using type = std::vector<std::byte>;
370 };
371}
372
373namespace sparrow
374{
375 /*************************************************
376 * variable_size_binary_reference implementation *
377 *************************************************/
378
379 template <class L>
381 : p_layout(layout)
382 , m_index(index)
383 {
384 }
385
386 template <class L>
387 template <std::ranges::sized_range T>
390 {
391 p_layout->assign(std::forward<T>(rhs), m_index);
392 p_layout->get_arrow_proxy().update_buffers();
393 return *this;
394 }
395
396 template <class L>
397 template <class U>
398 requires std::assignable_from<U&, const char*>
399 constexpr auto variable_size_binary_reference<L>::operator=(const char* rhs) -> self_type&
400 {
401 return *this = std::string_view(rhs);
402 }
403
404 template <class L>
406 {
407 return static_cast<size_type>(offset(m_index + 1) - offset(m_index));
408 }
409
410 template <class L>
411 constexpr auto variable_size_binary_reference<L>::empty() const -> bool
412 {
413 return size() == 0;
414 }
415
416 template <class L>
418 {
419 return iterator(p_layout->data(uoffset(m_index)));
420 }
421
422 template <class L>
424 {
425 return iterator(p_layout->data(uoffset(m_index + 1)));
426 }
427
428 template <class L>
430 {
431 return cbegin();
432 }
433
434 template <class L>
436 {
437 return cend();
438 }
439
440 template <class L>
442 {
443 return const_iterator(p_layout->data(uoffset(m_index)));
444 }
445
446 template <class L>
448 {
449 return const_iterator(p_layout->data(uoffset(m_index + 1)));
450 }
451
452 template <class L>
453 template <std::ranges::input_range T>
455 constexpr bool variable_size_binary_reference<L>::operator==(const T& rhs) const
456 {
457 return std::equal(cbegin(), cend(), std::cbegin(rhs), std::cend(rhs));
458 }
459
460 template <class L>
461 template <class U>
462 requires std::assignable_from<U&, const char*>
463 constexpr bool variable_size_binary_reference<L>::operator==(const char* rhs) const
464 {
465 return operator==(std::string_view(rhs));
466 }
467
468 template <class L>
469 template <std::ranges::input_range T>
471 constexpr auto variable_size_binary_reference<L>::operator<=>(const T& rhs) const
472 {
473 return lexicographical_compare_three_way(*this, rhs);
474 }
475
476 template <class L>
477 template <class U>
478 requires std::assignable_from<U&, const char*>
479 constexpr auto variable_size_binary_reference<L>::operator<=>(const char* rhs) const
480 {
481 return operator<=>(std::string_view(rhs));
482 }
483
484 template <class L>
485 constexpr auto variable_size_binary_reference<L>::offset(size_type index) const -> offset_type
486 {
487 return *(p_layout->offset(index));
488 }
489
490 template <class L>
491 constexpr auto variable_size_binary_reference<L>::uoffset(size_type index) const -> size_type
492 {
493 return static_cast<size_type>(offset(index));
494 }
495}
496
497#if defined(__cpp_lib_format)
498
499template <typename Layout>
500struct std::formatter<sparrow::variable_size_binary_reference<Layout>>
501{
502 constexpr auto parse(std::format_parse_context& ctx)
503 {
504 return ctx.begin(); // Simple implementation
505 }
506
507 auto format(const sparrow::variable_size_binary_reference<Layout>& ref, std::format_context& ctx) const
508 {
509 std::for_each(
510 ref.cbegin(),
511 sparrow::next(ref.cbegin(), ref.size() - 1),
512 [&ctx](const auto& value)
513 {
514 std::format_to(ctx.out(), "{}, ", value);
515 }
516 );
517
518 return std::format_to(ctx.out(), "{}>", *std::prev(ref.cend()));
519 }
520};
521
522namespace sparrow
523{
524 template <typename Layout>
525 std::ostream& operator<<(std::ostream& os, const variable_size_binary_reference<Layout>& value)
526 {
527 os << std::format("{}", value);
528 return os;
529 }
530}
531
532#endif
constexpr iterator end()
Gets mutable iterator to the end of binary data.
constexpr const_iterator cbegin() const
Gets const iterator to the beginning of binary data.
constexpr bool operator==(const T &rhs) const
Equality comparison with another range of binary data.
constexpr variable_size_binary_reference(L *layout, size_type index)
Constructs a variable-size binary reference for the given layout and index.
constexpr self_type & operator=(T &&rhs)
Assignment from a sized range of binary data.
constexpr const_iterator cend() const
Gets const iterator to the end of binary data.
constexpr bool empty() const
Checks if the binary element is empty.
constexpr const_iterator begin() const
Gets const iterator to the beginning of binary data.
constexpr const_iterator end() const
Gets const iterator to the end of binary data.
constexpr self_type & operator=(const char *rhs)
Assignment from a C-string.
constexpr iterator begin()
Gets mutable iterator to the beginning of binary data.
constexpr variable_size_binary_reference(const variable_size_binary_reference &)=default
constexpr variable_size_binary_reference(variable_size_binary_reference &&)=default
constexpr auto operator<=>(const char *rhs) const
Three-way comparison with a C-string.
constexpr auto operator<=>(const T &rhs) const
Three-way comparison with another range of binary data.
constexpr bool operator==(const char *rhs) const
Equality comparison with a C-string.
constexpr size_type size() const
Gets the size of the binary element in bytes/characters.
Concept for layouts.
Concept for convertible range types.
Definition mp_utils.hpp:931
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:474
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 &os, const nullval_t &)