sparrow 2.0.0
C++20 idiomatic APIs for the Apache Arrow Columnar Format
Loading...
Searching...
No Matches
ranges.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 implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14#pragma once
15
16#include <algorithm>
17#include <bitset>
18#include <numeric>
19#include <ranges>
20#include <vector>
21
22#if defined(__cpp_lib_format)
23# include <format>
24
26#endif
27
30
31namespace sparrow
32{
33 template <std::ranges::input_range R>
34 requires(std::ranges::sized_range<R>)
35 [[nodiscard]] constexpr std::size_t range_size(R&& r)
36 {
37 return static_cast<std::size_t>(std::ranges::size(r));
38 }
39
40 template <std::ranges::input_range R>
41 requires(!std::ranges::sized_range<R>)
42 [[nodiscard]] constexpr std::size_t range_size(R&& r)
43 {
44 return static_cast<std::size_t>(std::ranges::distance(r));
45 }
46
47 template <std::ranges::range Range>
48 requires std::ranges::sized_range<std::ranges::range_value_t<Range>>
49 [[nodiscard]] constexpr bool all_same_size(const Range& range)
50 {
51 // Optimization for std::array and fixed-size sequence_view
54 {
55 return true;
56 }
57 else
58 {
59 if (std::ranges::empty(range))
60 {
61 return true;
62 }
63
64 const std::size_t first_size = range.front().size();
65 return std::ranges::all_of(
66 range,
67 [first_size](const auto& element)
68 {
69 return element.size() == first_size;
70 }
71 );
72 }
73 }
74
75 namespace ranges
76 {
77 template <typename InputRange, typename OutputIterator>
78 concept has_ranges_copy = requires(InputRange input, OutputIterator output) {
79 {
80 std::ranges::copy(input, output)
81 } -> std::same_as<std::ranges::copy_result<std::ranges::iterator_t<InputRange>, OutputIterator>>;
82 };
83
89 struct copy_fn
90 {
91 template <std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O>
92 requires std::indirectly_copyable<I, O>
93 constexpr std::ranges::copy_result<I, O> operator()(I first, S last, O result) const
94 {
95 for (; first != last; ++first, (void) ++result)
96 {
97#if defined(__GNUC__) && __GNUC__ < 12
98# pragma GCC diagnostic push
99# pragma GCC diagnostic ignored "-Wnull-dereference"
100#endif
101 *result = *first;
102#if defined(__GNUC__) && __GNUC__ < 12
103# pragma GCC diagnostic pop
104#endif
105 }
106 return {std::move(first), std::move(result)};
107 }
108
109 template <std::ranges::input_range R, std::weakly_incrementable O>
110 requires std::indirectly_copyable<std::ranges::iterator_t<R>, O>
111 constexpr std::ranges::copy_result<std::ranges::borrowed_iterator_t<R>, O>
112 operator()(R&& r, O result) const
113 {
114 return (*this)(std::ranges::begin(r), std::ranges::end(r), std::move(result));
115 }
116 };
117
118 template <std::ranges::input_range R, std::weakly_incrementable O>
119 requires std::indirectly_copyable<std::ranges::iterator_t<R>, O>
120 constexpr std::ranges::copy_result<std::ranges::borrowed_iterator_t<R>, O> copy(R&& r, O result)
121 {
122 if constexpr (has_ranges_copy<R, O>)
123 {
124 return std::ranges::copy(std::forward<R>(r), std::move(result));
125 }
126 else
127 {
128 return copy_fn{}(std::forward<R>(r), std::move(result));
129 }
130 }
131
132 template <std::ranges::input_range R, class T>
133 constexpr T accumulate(R&& r, T init)
134 {
135 return std::accumulate(r.begin(), r.end(), init);
136 }
137
138 template <std::ranges::input_range R, class T, std::copy_constructible Op>
139 constexpr T accumulate(R&& r, T init, Op op)
140 {
141 return std::accumulate(r.begin(), r.end(), init, op);
142 }
143 }
144};
145
146#if defined(__cpp_lib_format)
147
148template <typename T, std::size_t N>
149struct std::formatter<std::array<T, N>>
150{
151 constexpr auto parse(std::format_parse_context& ctx)
152 {
153 return m_spec.parse(ctx.begin(), ctx.end());
154 }
155
156 auto format(const std::array<T, N>& array, std::format_context& ctx) const
157 {
158 std::string core = m_spec.build_core(array);
159 std::string out_str = m_spec.apply_alignment(std::move(core));
160 return std::ranges::copy(out_str, ctx.out()).out;
161 }
162
163private:
164
165 sparrow::detail::sequence_format_spec m_spec;
166};
167
168template <typename T>
169struct std::formatter<std::vector<T>>
170{
171 constexpr auto parse(std::format_parse_context& ctx)
172 {
173 return m_spec.parse(ctx.begin(), ctx.end());
174 }
175
176 auto format(const std::vector<T>& vector, std::format_context& ctx) const
177 {
178 std::string core = m_spec.build_core(vector);
179 std::string out_str = m_spec.apply_alignment(std::move(core));
180 return std::ranges::copy(out_str, ctx.out()).out;
181 }
182
183private:
184
185 sparrow::detail::sequence_format_spec m_spec;
186};
187
188template <std::size_t T>
189struct std::formatter<std::bitset<T>>
190{
191 constexpr auto parse(std::format_parse_context& ctx)
192 {
193 return m_spec.parse(ctx.begin(), ctx.end());
194 }
195
196 auto format(const std::bitset<T>& vector, std::format_context& ctx) const
197 {
198 std::string core = m_spec.build_core(vector);
199 std::string out_str = m_spec.apply_alignment(std::move(core));
200 return std::ranges::copy(out_str, ctx.out()).out;
201 }
202
203private:
204
205 sparrow::detail::sequence_format_spec m_spec;
206};
207
208#endif
Concept for std::array types.
constexpr T accumulate(R &&r, T init)
Definition ranges.hpp:133
constexpr std::ranges::copy_result< std::ranges::borrowed_iterator_t< R >, O > copy(R &&r, O result)
Definition ranges.hpp:120
constexpr bool all_same_size(const Range &range)
Definition ranges.hpp:49
constexpr std::size_t range_size(R &&r)
Definition ranges.hpp:35
Extensions to the C++ standard library.
Copies the elements from the input range to the output iterator.
Definition ranges.hpp:90
constexpr std::ranges::copy_result< I, O > operator()(I first, S last, O result) const
Definition ranges.hpp:93
constexpr std::ranges::copy_result< std::ranges::borrowed_iterator_t< R >, O > operator()(R &&r, O result) const
Definition ranges.hpp:112