sparrow 0.3.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{
37 template <class L>
39 {
40 public:
41
43 using value_type = typename L::inner_value_type;
44 using reference = typename L::inner_reference;
45 using const_reference = typename L::inner_const_reference;
46 using size_type = typename L::size_type;
47 using difference_type = std::ptrdiff_t;
48 using iterator = typename L::data_iterator;
49 using const_iterator = typename L::const_data_iterator;
50
54
55 template <std::ranges::sized_range T>
58
59 [[nodiscard]] size_type size() const;
60
61 [[nodiscard]] iterator begin();
62 [[nodiscard]] iterator end();
63
64 [[nodiscard]] const_iterator begin() const;
65 [[nodiscard]] const_iterator end() const;
66 [[nodiscard]] const_iterator cbegin() const;
67 [[nodiscard]] const_iterator cend() const;
68
69 template <std::ranges::input_range T>
71 bool operator==(const T& rhs) const;
72
73 template <std::ranges::input_range T>
75 auto operator<=>(const T& rhs) const;
76
77 private:
78
79 [[nodiscard]] size_type offset(size_type index) const;
80
81 L* p_layout = nullptr;
82 size_type m_index = size_type(0);
83 };
84}
85
86namespace std
87{
88 template <typename Layout, template <typename> typename TQual, template <typename> typename UQual>
89 struct basic_common_reference<sparrow::fixed_width_binary_reference<Layout>, std::vector<sparrow::byte_t>, TQual, UQual>
90 {
91 using type = std::vector<sparrow::byte_t>;
92 };
93
94 template <typename Layout, template <typename> typename TQual, template <class> class UQual>
95 struct basic_common_reference<std::vector<sparrow::byte_t>, sparrow::fixed_width_binary_reference<Layout>, TQual, UQual>
96 {
97 using type = std::vector<sparrow::byte_t>;
98 };
99}
100
101namespace sparrow
102{
103 /***********************************************
104 * fixed_width_binary_reference implementation *
105 ***********************************************/
106
107 template <class L>
109 : p_layout(layout)
110 , m_index(index)
111 {
112 }
113
114 template <class L>
115 template <std::ranges::sized_range T>
118 {
119 SPARROW_ASSERT_TRUE(p_layout->m_element_size == std::ranges::size(rhs));
120 p_layout->assign(std::forward<T>(rhs), m_index);
121 p_layout->get_arrow_proxy().update_buffers();
122 return *this;
123 }
124
125 template <class L>
127 {
128 return p_layout->m_element_size;
129 }
130
131 template <class L>
133 {
134 return iterator(p_layout->data(offset(m_index)));
135 }
136
137 template <class L>
139 {
140 return iterator(p_layout->data(offset(m_index + 1)));
141 }
142
143 template <class L>
145 {
146 return cbegin();
147 }
148
149 template <class L>
151 {
152 return cend();
153 }
154
155 template <class L>
157 {
158 return const_iterator(p_layout->data(offset(m_index)));
159 }
160
161 template <class L>
163 {
164 return const_iterator(p_layout->data(offset(m_index + 1)));
165 }
166
167 template <class L>
168 template <std::ranges::input_range T>
171 {
172 return std::equal(cbegin(), cend(), std::cbegin(rhs), std::cend(rhs));
173 }
174
175 template <class L>
176 template <std::ranges::input_range T>
179 {
180 return lexicographical_compare_three_way(*this, rhs);
181 }
182
183 template <class L>
184 auto fixed_width_binary_reference<L>::offset(size_type index) const -> size_type
185 {
186 return p_layout->m_element_size * index;
187 }
188}
189
190#if defined(__cpp_lib_format)
191
192template <typename Layout>
193struct std::formatter<sparrow::fixed_width_binary_reference<Layout>>
194{
195 constexpr auto parse(std::format_parse_context& ctx)
196 {
197 return ctx.begin(); // Simple implementation
198 }
199
200 auto format(const sparrow::fixed_width_binary_reference<Layout>& ref, std::format_context& ctx) const
201 {
202 std::for_each(
203 ref.cbegin(),
204 sparrow::next(ref.cbegin(), ref.size() - 1),
205 [&ctx](const auto& value)
206 {
207 std::format_to(ctx.out(), "{}, ", value);
208 }
209 );
210
211 return std::format_to(ctx.out(), "{}>", *std::prev(ref.cend()));
212 }
213};
214
215template <typename Layout>
216inline std::ostream& operator<<(std::ostream& os, const sparrow::fixed_width_binary_reference<Layout>& value)
217{
218 os << std::format("{}", value);
219 return os;
220}
221
222#endif
Implementation of reference to inner type used for layout L.
fixed_width_binary_reference(const fixed_width_binary_reference &)=default
fixed_width_binary_reference(fixed_width_binary_reference &&)=default
typename array_type::inner_const_reference const_reference
Matches range types From whose elements are convertible to elements of range type To.
Definition mp_utils.hpp:450
#define SPARROW_ASSERT_TRUE(expr__)
constexpr InputIt next(InputIt it, Distance n)
Definition iterator.hpp:503
std::ostream & operator<<(std::ostream &stream, T n)
Definition large_int.hpp:93