-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTVec.hpp
62 lines (62 loc) · 2.44 KB
/
CTVec.hpp
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
#pragma once
#include "CTCBase.hpp"
#include <tuple>
namespace CTC {
namespace details {
template <auto I>
constexpr auto setter(auto& data, const auto&... args) {
data[I] = std::get<I>(std::forward_as_tuple(args...));
if constexpr (I + 1 < sizeof...(args)) {
return setter<I + 1>(data, args...);
}
}
} // namespace details
template <typename T, auto L>
struct CTVec : basic_CTC_container {
using size_type = ::std::remove_cvref_t<decltype(L)>;
using value_type = T;
constexpr static auto length = L;
T data[L];
constexpr auto& operator[](size_type i) { return data[i]; }
constexpr const auto& operator[](size_type i) const { return data[i]; }
constexpr auto begin() { return &data[0]; }
constexpr const auto cbegin() const { return &data[0]; }
constexpr const auto begin() const { return &data[0]; }
constexpr auto end() { return &data[L]; }
constexpr const auto cend() const { return &data[L]; }
constexpr const auto end() const { return &data[L]; }
constexpr operator auto() { return data; }
constexpr operator const auto() const { return data; }
constexpr CTVec() = default;
constexpr CTVec(const CTVec&) = default;
constexpr CTVec(CTVec&&) = default;
constexpr CTVec& operator=(const CTVec&) = default;
constexpr CTVec& operator=(CTVec&&) = default;
template <typename... Ts>
constexpr CTVec(Ts... args) {
details::setter<0>(data, args...);
}
template <size_type NewStringLength>
requires(NewStringLength <= length)
constexpr CTVec<value_type, NewStringLength> subvec(size_type s) {
CTVec<value_type, NewStringLength> res = {};
for (size_type i = 0; i < NewStringLength; i++) {
res[i] = data[s + i];
}
return res;
}
};
template <typename T, typename... Ts>
CTVec(T first, Ts... rest) -> CTVec<std::remove_cvref_t<T>, sizeof...(Ts) + 1>;
template <typename T, auto L1, auto L2>
constexpr auto operator+(const CTVec<T, L1>& a, const CTVec<T, L2>& b) {
CTVec<T, L1 + L2> res;
for (std::remove_cvref_t<decltype(L1)> i = 0; i < L1; i++) {
res[i] = a[i];
}
for (std::remove_cvref_t<decltype(L2)> i = 0; i < L2; i++) {
res[L1 + i] = b[i];
}
return res;
}
} // namespace CTC