summaryrefslogtreecommitdiff
path: root/lib/collections.hpp
blob: 5d39e79036d218b6fae872abc404b6ed63a12797 (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
#pragma once

#include <array>
#include <span>

template<typename T, typename E>
concept SequentialCollection = requires(T c) {
								   {
									   c.size()
									   } -> std::integral;
								   {
									   c.data()
									   } -> std::same_as<const E *>;
							   };

template<typename T, std::size_t first, std::size_t second>
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>
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>
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;
}

template<typename T>
constexpr auto &
operator*=(std::span<T> & in, auto && f)
{
	for (auto & v : in) {
		f(v);
	}
	return in;
}