summaryrefslogtreecommitdiff
path: root/lib/collections.hpp
blob: 16870be1cf29efab674b59da4d9c65d69a191f59 (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
#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>
[[nodiscard]] constexpr auto
operator*(const C<T...> & in, auto && f)
{
	C<decltype(f(in[0]))> out;

	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>
[[nodiscard]] static auto
vectorOfN(std::integral auto N, unsigned int start = {}, unsigned int step = 1)
{
	std::vector<unsigned int> 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);
}