summaryrefslogtreecommitdiff
path: root/lib/collections.h
blob: 943b986a758ce5aaace32672dd0742b88ca7d9df (plain)
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
#pragma once

#include <algorithm>
#include <array>
#include <span>
#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, IterableCollection In>
[[nodiscard]] auto
materializeRange(In && in)
{
	return Rtn(in.begin(), in.end());
}

template<template<typename...> typename Rtn = std::vector, typename In>
[[nodiscard]] auto
materializeRange(const std::pair<In, In> & in)
{
	return 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>;