Sequoia
Loading...
Searching...
No Matches
Sequences.hpp
Go to the documentation of this file.
1
2// Copyright Oliver J. Rosten 2024. //
3// Distributed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. //
4// (See accompanying file LICENSE.md or copy at //
5// https://www.gnu.org/licenses/gpl-3.0.en.html) //
7
8#pragma once
9
12#include <utility>
13
14namespace sequoia
15{
16 template<class T>
17 struct is_index_sequence : std::false_type {};
18
19 template<std::size_t... Is>
20 struct is_index_sequence<std::index_sequence<Is...>> : std::true_type {};
21
22 template<class T>
23 using is_index_sequence_t = typename is_index_sequence<T>::type;
24
25 template<class T>
26 inline constexpr bool is_index_sequence_v{is_index_sequence<T>::value};
27
28 template<class...>
30
31 template<class... Ts>
32 using concat_sequences_t = typename concat_sequences<Ts...>::type;
33
34 template<std::size_t... Is>
35 struct concat_sequences<std::index_sequence<Is...>>
36 {
37 using type = std::index_sequence<Is...>;
38 };
39
40 template<std::size_t... Is, std::size_t... Js, class... Sequences>
41 requires (is_index_sequence_v<Sequences> && ...)
42 struct concat_sequences<std::index_sequence<Is...>, std::index_sequence<Js...>, Sequences...>
43 {
44 using type = concat_sequences_t<std::index_sequence<Is..., Js...>, Sequences...>;
45 };
46}
47
48namespace sequoia::impl
49{
50 template<class Sequence, std::size_t Total, class Excluded, template<class> class TypeToType, class... Ts>
51 struct aggregate;
52
53 template<std::size_t... M, std::size_t Total, class Excluded, template<class> class TypeToType>
54 struct aggregate<std::index_sequence<M...>, Total, Excluded, TypeToType>
55 {
56 using type = std::index_sequence<M...>;
57 };
58
59 template<std::size_t... M, std::size_t Total, class Excluded, template<class> class TypeToType, class T, class... Ts>
60 struct aggregate<std::index_sequence<M...>, Total, Excluded, TypeToType, T, Ts...>
61 {
62 private:
63 constexpr static bool cond{std::is_same_v<Excluded, typename TypeToType<T>::type>};
64 using add = std::conditional_t<cond, std::index_sequence<>, std::index_sequence<Total - sizeof...(Ts) - 1>>;
65 using appended = concat_sequences_t<std::index_sequence<M...>, add>;
66
67 public:
68 using type = typename aggregate<appended, Total, Excluded, TypeToType, Ts...>::type;
69 };
70}
71
72namespace sequoia
73{
74 //==================================================== filtered_sequence ===================================================//
75
76 template<class Excluded, template<class> class TypeToType, class... Ts>
78 {
79 using type = typename impl::aggregate<std::index_sequence<>, sizeof...(Ts), Excluded, TypeToType, Ts...>::type;
80 };
81
82 template<class Excluded, template<class> class TypeToType, class... Ts>
83 using make_filtered_sequence = typename filtered_sequence<Excluded, TypeToType, Ts...>::type;
84
85 //==================================================== rotate_sequence ===================================================//
86
87 template<class> struct rotate_sequence;
88
89 template<>
90 struct rotate_sequence<std::index_sequence<>>
91 {
92 using type = std::index_sequence<>;
93 };
94
95 template<std::size_t I, std::size_t... Is>
96 struct rotate_sequence<std::index_sequence<I, Is...>>
97 {
98 using type = std::index_sequence<Is..., I>;
99 };
100
101 template<class T>
102 using rotate_sequence_t = typename rotate_sequence<T>::type;
103
104
105 //==================================================== reverse_sequence ===================================================//
106
107 template<class> struct reverse_sequence;
108
109 template<class T>
110 using reverse_sequence_t = typename reverse_sequence<T>::type;
111
112 template<>
113 struct reverse_sequence<std::index_sequence<>>
114 {
115 using type = std::index_sequence<>;
116 };
117
118 template<std::size_t I>
119 struct reverse_sequence<std::index_sequence<I>>
120 {
121 using type = std::index_sequence<I>;
122 };
123
124 template<std::size_t I, std::size_t... Is>
125 struct reverse_sequence<std::index_sequence<I, Is...>>
126 {
127 using type = concat_sequences_t<reverse_sequence_t<std::index_sequence<Is...>>, std::index_sequence<I>>;
128 };
129
130 //==================================================== shift_sequence ===================================================//
131
132 template<class T, std::size_t N>
134
135 template<class T, std::size_t N>
136 using shift_sequence_t = shift_sequence<T, N>::type;
137
138 template<std::size_t... Is, std::size_t N>
139 struct shift_sequence<std::index_sequence<Is...>, N>
140 {
141 using type = std::index_sequence<N+Is...>;
142 };
143
144 //==================================================== sequence_partial_sum ===================================================//
145
146 namespace impl
147 {
148 template<class, std::size_t...>
150
151 template<class T, std::size_t... Is>
152 using sequence_partial_sum_t = typename sequence_partial_sum<T, Is...>::type;
153
154 template<std::size_t I, std::size_t... Is>
155 struct sequence_partial_sum<std::index_sequence<I, Is...>>
156 {
157 using type = std::index_sequence<I, Is...>;
158 };
159
160 template<std::size_t I, std::size_t... Is, std::size_t Next, std::size_t... Remaining>
161 struct sequence_partial_sum<std::index_sequence<I, Is...>, Next, Remaining...>
162 {
163 using type = sequence_partial_sum_t<std::index_sequence<I + Next, I, Is...>, Remaining...>;
164 };
165 }
166
167 template<class>
169
170 template<>
171 struct sequence_partial_sum<std::index_sequence<>>
172 {
173 using type = std::index_sequence<>;
174 };
175
176 template<std::size_t I, std::size_t... Is>
177 struct sequence_partial_sum<std::index_sequence<I, Is...>>
178 {
179 using type = reverse_sequence_t<impl::sequence_partial_sum_t<std::index_sequence<I>, Is...>>;
180 };
181
182 template<class T>
183 using sequence_partial_sum_t = typename sequence_partial_sum<T>::type;
184}
Definition: Sequences.hpp:29
Definition: Sequences.hpp:78
Definition: Sequences.hpp:51
Definition: Sequences.hpp:149
Definition: Sequences.hpp:17
Definition: Sequences.hpp:107
Definition: Sequences.hpp:87
Definition: Sequences.hpp:168
Definition: Sequences.hpp:133