1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#pragma once
#include <algorithm>
#include <array>
#include <cstdint>
#include <span>
#include <tuple>
#include <utility>
#include <vector>
template<typename T, typename E>
concept SequentialCollection = requires(T c) {
{
c.size()
} -> std::integral;
{
c.data()
} -> std::same_as<const E *>;
};
template<typename T>
concept IterableCollection = std::is_same_v<decltype(std::declval<T>().begin()), decltype(std::declval<T>().end())>;
template<typename T, std::size_t first, std::size_t second>
[[nodiscard]] constexpr std::array<T, first + second>
operator+(const std::array<T, first> & a, const std::array<T, second> & b)
{
std::array<T, first + second> r;
auto out = r.begin();
out = std::copy(a.begin(), a.end(), out);
std::copy(b.begin(), b.end(), out);
return r;
}
template<typename T, typename V, std::size_t first, std::size_t second>
[[nodiscard]] constexpr std::array<std::pair<T, V>, first * second>
operator*(const std::array<T, first> & a, const std::array<V, second> & b)
{
std::array<std::pair<T, V>, first * second> r;
auto out = r.begin();
for (const auto & ae : a) {
for (const auto & be : b) {
*out++ = {ae, be};
}
}
return r;
}
template<typename T, std::size_t N>
[[nodiscard]] constexpr auto
operator*(const std::array<T, N> & in, auto && f)
{
std::array<decltype(f(in[0])), N> out;
for (auto outitr = out.begin(); const auto & v : in) {
*outitr++ = f(v);
}
return out;
}
constexpr auto &
operator*=(IterableCollection auto & in, auto && f)
{
for (auto & v : in) {
f(v);
}
return in;
}
template<template<typename...> typename C, typename... T>
requires IterableCollection<C<T...>>
[[nodiscard]] constexpr auto
operator*(const C<T...> & in, auto && f)
{
C<decltype(f(*in.begin()))> out;
if constexpr (requires { out.reserve(in.size()); }) {
out.reserve(in.size());
}
std::transform(in.begin(), in.end(), std::inserter(out, out.end()), f);
return out;
}
template<typename T, std::size_t N>
[[nodiscard]] constexpr auto
operator*(const std::span<T, N> in, auto && f)
{
std::array<decltype(f(std::declval<T>())), N> out;
std::transform(in.begin(), in.end(), out.begin(), f);
return out;
}
template<typename T>
[[nodiscard]] constexpr auto
operator*(const std::span<T, std::dynamic_extent> in, auto && f)
{
std::vector<decltype(f(std::declval<T>()))> out;
out.reserve(in.size());
std::transform(in.begin(), in.end(), std::inserter(out, out.end()), f);
return out;
}
template<typename... T>
constexpr auto &
operator+=(std::vector<T...> & in, std::vector<T...> && src)
{
in.reserve(in.size() + src.size());
std::move(src.begin(), src.end(), std::back_inserter(in));
return in;
}
template<typename... T, typename Vn>
[[nodiscard]] constexpr auto
operator+(const std::vector<T...> & in, Vn && vn)
{
auto out(in);
out.emplace_back(std::forward<Vn>(vn));
return out;
}
template<template<typename> typename Direction = std::plus, typename T = unsigned int>
[[nodiscard]] static auto
vectorOfN(std::integral auto N, T start = {}, T step = 1)
{
std::vector<T> v;
v.resize(N);
std::generate_n(v.begin(), N, [&start, step, adj = Direction {}]() {
return std::exchange(start, adj(start, step));
});
return v;
}
template<template<typename...> typename Rtn = std::vector, typename In>
[[nodiscard]] auto
materializeRange(const In begin, const In end)
{
return Rtn(begin, end);
}
template<template<typename...> typename Rtn = std::vector, IterableCollection In>
[[nodiscard]] auto
materializeRange(const In & in)
{
return materializeRange<Rtn>(in.begin(), in.end());
}
template<template<typename...> typename Rtn = std::vector, typename In>
[[nodiscard]] auto
materializeRange(const std::pair<In, In> & in)
{
return materializeRange<Rtn>(in.first, in.second);
}
template<typename T> struct pair_range {
constexpr auto &
begin() const noexcept
{
return pair.first;
}
constexpr auto &
end() const noexcept
{
return pair.second;
}
const std::pair<T, T> & pair;
};
template<typename T> pair_range(std::pair<T, T>) -> pair_range<T>;
template<typename iter> struct stripiter {
[[nodiscard]] constexpr bool
operator!=(const stripiter & other) const
{
return current != other.current;
}
[[nodiscard]] constexpr bool
operator==(const stripiter & other) const
{
return current == other.current;
}
constexpr stripiter &
operator++()
{
++current;
off = 1 - off;
return *this;
}
constexpr stripiter &
operator--()
{
--current;
off = 1 - off;
return *this;
}
constexpr auto
operator-(const stripiter & other) const
{
return current - other.current;
}
constexpr auto
operator*() const
{
return std::tie(*(current - (2 - off)), *(current - off - 1), *current);
}
iter current;
uint8_t off {};
};
template<typename T> struct std::iterator_traits<stripiter<T>> : std::iterator_traits<T> { };
constexpr auto
strip_begin(IterableCollection auto & cont)
{
return stripiter {cont.begin() + 2};
}
constexpr auto
strip_end(IterableCollection auto & cont)
{
return stripiter {cont.end()};
}
|