sparrow 0.3.0
Loading...
Searching...
No Matches
vector_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 <vector>
20
21#if defined(__cpp_lib_format)
22# include <format>
23#endif
24
25namespace sparrow
26{
33 template <class T>
34 class vector_view : public std::span<T>
35 {
36 public:
37
38 using base_type = std::span<T>;
39 using value_type = typename base_type::value_type;
40
41 using base_type::base_type;
42
43 explicit operator std::vector<value_type>() const noexcept
44 {
45 return std::vector<value_type>(this->begin(), this->end());
46 }
47 };
48
49 template <class T>
50 constexpr bool operator==(const vector_view<T>& lhs, const vector_view<T>& rhs)
51 {
52 return std::ranges::equal(lhs, rhs);
53 }
54
55 template <class T>
56 constexpr bool operator==(const vector_view<T>& lhs, const std::vector<std::decay_t<T>>& rhs)
57 {
58 return std::ranges::equal(lhs, rhs);
59 }
60
61 template <class T>
62 constexpr std::compare_three_way_result<T> operator<=>(const vector_view<T>& lhs, const vector_view<T>& rhs)
63 {
64 return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
65 }
66
67 template <class T>
68 constexpr std::compare_three_way_result<T>
69 operator<=>(const vector_view<T>& lhs, const std::vector<std::decay_t<T>>& rhs)
70 {
71 return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
72 }
73}
74
75#if defined(__cpp_lib_format)
76template <class T>
77struct std::formatter<sparrow::vector_view<T>>
78{
79 constexpr auto parse(std::format_parse_context& ctx)
80 {
81 return ctx.begin(); // Simple implementation
82 }
83
84 auto format(const sparrow::vector_view<T>& vec, std::format_context& ctx) const
85 {
86 std::format_to(ctx.out(), "<");
87 if (!vec.empty())
88 {
89 for (std::size_t i = 0; i < vec.size() - 1; ++i)
90 {
91 std::format_to(ctx.out(), "{}, ", vec[i]);
92 }
93 }
94 return std::format_to(ctx.out(), "{}>", vec.back());
95 }
96};
97
98template <typename T>
99std::ostream& operator<<(std::ostream& os, const sparrow::vector_view<T>& value)
100{
101 os << std::format("{}", value);
102 return os;
103}
104
105#endif
The class vector_view describes an object that can refer to a constant contiguous sequence of T with ...
typename base_type::value_type value_type
std::span< const byte_t > 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:475
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
std::ostream & operator<<(std::ostream &stream, T n)
Definition large_int.hpp:93