sparrow 0.9.0
Loading...
Searching...
No Matches
sequence_view.hpp
Go to the documentation of this file.
1// 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 implied.
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 <ranges>
19#include <span>
20#include <vector>
21
22#if defined(__cpp_lib_format)
23# include <format>
24#endif
25
27
28namespace sparrow
29{
36 template <class T, std::size_t Extent = std::dynamic_extent>
37 class sequence_view : public std::span<T, Extent>
38 {
39 public:
40
41 using base_type = std::span<T, Extent>;
42 using value_type = typename base_type::value_type;
43
44 using base_type::base_type;
45
46 explicit operator std::vector<value_type>() const noexcept
47 {
48 return std::vector<value_type>(this->begin(), this->end());
49 }
50 };
51
52 template <class T, std::size_t E>
53 constexpr bool operator==(const sequence_view<T, E>& lhs, const sequence_view<T, E>& rhs);
54
55 template <class T, std::size_t E, std::ranges::input_range R>
56 requires mpl::weakly_equality_comparable_with<T, std::ranges::range_value_t<R>>
57 constexpr bool operator==(const sequence_view<T, E>& lhs, const R& rhs);
58
59 template <class T, std::size_t E>
60 constexpr std::compare_three_way_result<T>
61 operator<=>(const sequence_view<T, E>& lhs, const sequence_view<T, E>& rhs);
62
63 template <class T, std::size_t E, std::ranges::input_range R>
64 constexpr std::compare_three_way_result<T, std::ranges::range_value_t<R>>
65 operator<=>(const sequence_view<T, E>& lhs, const R& rhs);
66}
67
68namespace std
69{
70 template <class T, std::size_t E>
71 struct tuple_size<sparrow::sequence_view<T, E>> : tuple_size<std::span<T, E>>
72 {
73 };
74
75 namespace ranges
76 {
77 template <class T, std::size_t E>
78 constexpr bool enable_borrowed_range<sparrow::sequence_view<T, E>> = true;
79
80 template <class T, std::size_t E>
81 constexpr bool enable_view<sparrow::sequence_view<T, E>> = true;
82 }
83}
84
85namespace sparrow
86{
87 // Concept for fixed-size std::sequence_view
88 template <typename T>
89 concept fixed_size_sequence_view = requires {
90 typename std::remove_cvref_t<T>::element_type;
91 requires std::tuple_size_v<T> != std::dynamic_extent;
92 requires std::same_as<
94 std::remove_cvref_t<T>>;
95 };
96
97 /********************************
98 * sequence_view implementation *
99 ********************************/
100
101 template <class T, std::size_t E>
102 constexpr bool operator==(const sequence_view<T, E>& lhs, const sequence_view<T, E>& rhs)
103 {
104 return std::ranges::equal(lhs, rhs);
105 }
106
107 template <class T, std::size_t E, std::ranges::input_range R>
108 requires mpl::weakly_equality_comparable_with<T, std::ranges::range_value_t<R>>
109 constexpr bool operator==(const sequence_view<T, E>& lhs, const R& rhs)
110 {
111 return std::ranges::equal(lhs, rhs);
112 }
113
114 template <class T, std::size_t E>
115 constexpr std::compare_three_way_result<T>
117 {
118 return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
119 }
120
121 template <class T, std::size_t E, std::ranges::input_range R>
122 constexpr std::compare_three_way_result<T, std::ranges::range_value_t<R>>
123 operator<=>(const sequence_view<T, E>& lhs, const R& rhs)
124 {
125 return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
126 }
127}
128
129#if defined(__cpp_lib_format)
130template <class T, std::size_t E>
131struct std::formatter<sparrow::sequence_view<T, E>>
132{
133 constexpr auto parse(std::format_parse_context& ctx)
134 {
135 return ctx.begin(); // Simple implementation
136 }
137
138 auto format(const sparrow::sequence_view<T, E>& vec, std::format_context& ctx) const
139 {
140 std::format_to(ctx.out(), "<");
141 if (!vec.empty())
142 {
143 for (std::size_t i = 0; i < vec.size() - 1; ++i)
144 {
145 std::format_to(ctx.out(), "{}, ", vec[i]);
146 }
147 }
148 return std::format_to(ctx.out(), "{}>", vec.back());
149 }
150};
151
152template <typename T, std::size_t E>
153std::ostream& operator<<(std::ostream& os, const sparrow::sequence_view<T, E>& value)
154{
155 os << std::format("{}", value);
156 return os;
157}
158
159#endif
The class sequence_view describes an object that can refer to a constant contiguous sequence of T wit...
typename base_type::value_type value_type
std::span< const byte_t, std::dynamic_extent > base_type
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.
std::ostream & operator<<(std::ostream &os, const sparrow::nullval_t &)
Definition nullable.hpp:929