Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef FRUIT_META_VECTOR_H |
| 18 | #define FRUIT_META_VECTOR_H |
| 19 | |
Marco Poletti | f289510 | 2016-01-30 13:38:37 +0000 | [diff] [blame^] | 20 | #include <fruit/impl/meta/basics.h> |
| 21 | #include <fruit/impl/meta/logical_operations.h> |
| 22 | #include <fruit/impl/meta/numeric_operations.h> |
| 23 | #include <fruit/impl/meta/eval.h> |
| 24 | #include <fruit/impl/meta/fold.h> |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 25 | #include <functional> |
| 26 | |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 27 | namespace fruit { |
| 28 | namespace impl { |
| 29 | namespace meta { |
| 30 | |
| 31 | // Used to pass around a Vector<Types...>, no meaning per se. |
| 32 | template <typename... Types> |
Marco Poletti | 010daa6 | 2015-07-06 01:15:12 +0100 | [diff] [blame] | 33 | struct Vector {}; |
| 34 | |
| 35 | // Using ConsVector(MetaExpr...) instead of Vector<MetaExpr...> in a meta-expression allows the |
| 36 | // types to be evaluated. Avoid using Vector<...> directly in a meta-expression, unless you're sure |
| 37 | // that the arguments have already been evaluated (e.g. if Args... are arguments of a metafunction, |
| 38 | // Vector<Args...> is ok but Vector<MyFunction(Args)...> is wrong. |
| 39 | struct ConsVector { |
| 40 | template <typename... Types> |
| 41 | struct apply { |
| 42 | using type = Vector<Types...>; |
| 43 | }; |
| 44 | }; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 45 | |
Marco Poletti | 36253c9 | 2015-07-19 17:12:47 +0100 | [diff] [blame] | 46 | struct GenerateIntSequenceEvenHelper { |
| 47 | template <typename Half> |
| 48 | struct apply; |
| 49 | |
| 50 | template <int... ns> |
| 51 | struct apply<Vector<Int<ns>...>> { |
| 52 | using type = Vector<Int<ns>..., Int<sizeof...(ns) + ns>...>; |
| 53 | }; |
| 54 | }; |
| 55 | |
| 56 | struct GenerateIntSequenceOddHelper { |
| 57 | template <typename Half> |
| 58 | struct apply; |
| 59 | |
| 60 | template <int... ns> |
| 61 | struct apply<Vector<Int<ns>...>> { |
| 62 | using type = Vector<Int<ns>..., Int<sizeof...(ns)>, Int<sizeof...(ns) + 1 + ns>...>; |
| 63 | }; |
| 64 | }; |
| 65 | |
| 66 | struct GenerateIntSequence { |
| 67 | template <typename N> |
| 68 | struct apply { |
| 69 | using type = If(Bool<(N::value % 2) == 0>, |
| 70 | GenerateIntSequenceEvenHelper(GenerateIntSequence(Int<N::value/2>)), |
| 71 | GenerateIntSequenceOddHelper(GenerateIntSequence(Int<N::value/2>))); |
| 72 | }; |
| 73 | }; |
| 74 | |
| 75 | template <> |
| 76 | struct GenerateIntSequence::apply<Int<0>> { |
| 77 | using type = Vector<>; |
| 78 | }; |
| 79 | |
| 80 | template <> |
| 81 | struct GenerateIntSequence::apply<Int<1>> { |
| 82 | using type = Vector<Int<0>>; |
| 83 | }; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 84 | |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 85 | struct IsInVector { |
Marco Poletti | 0954754 | 2015-07-25 12:13:24 +0100 | [diff] [blame] | 86 | template <typename T> |
| 87 | struct AlwaysFalseBool { |
| 88 | constexpr static bool value = false; |
| 89 | }; |
| 90 | |
| 91 | template <bool... bs> |
| 92 | struct BoolVector; |
| 93 | |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 94 | template <typename T, typename V> |
| 95 | struct apply; |
| 96 | |
| 97 | template <typename T, typename... Ts> |
| 98 | struct apply<T, Vector<Ts...>> { |
Marco Poletti | 0954754 | 2015-07-25 12:13:24 +0100 | [diff] [blame] | 99 | using type = Bool<!std::is_same<BoolVector<AlwaysFalseBool<Ts>::value...>, |
| 100 | BoolVector<std::is_same<T, Ts>::value...> |
| 101 | >::value>; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 102 | }; |
| 103 | }; |
| 104 | |
Marco Poletti | ec5c520 | 2015-07-26 11:18:44 +0100 | [diff] [blame] | 105 | struct IsVectorContained { |
| 106 | template <typename V1, typename V2> |
| 107 | struct apply; |
| 108 | |
| 109 | template <typename T> |
| 110 | struct AlwaysTrueBool { |
| 111 | constexpr static bool value = true; |
| 112 | }; |
| 113 | |
| 114 | template <bool... bs> |
| 115 | struct BoolVector; |
| 116 | |
| 117 | template <typename... Ts, typename V2> |
| 118 | struct apply<Vector<Ts...>, V2> { |
| 119 | using type = Bool<std::is_same<BoolVector<AlwaysTrueBool<Ts>::value...>, |
| 120 | BoolVector<IsInVector::template apply<Ts, V2>::type::value...> |
| 121 | >::value>; |
| 122 | }; |
| 123 | }; |
| 124 | |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 125 | struct VectorSize { |
| 126 | template <typename V> |
| 127 | struct apply; |
| 128 | |
| 129 | template <typename... Ts> |
| 130 | struct apply<Vector<Ts...>> { |
Marco Poletti | a19fd1e | 2015-03-07 18:50:37 +0000 | [diff] [blame] | 131 | using type = Int<sizeof...(Ts)>; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 132 | }; |
| 133 | }; |
| 134 | |
Marco Poletti | f6335ae | 2015-01-01 15:13:04 +0100 | [diff] [blame] | 135 | struct PushFront { |
| 136 | template <typename V, typename T> |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 137 | struct apply; |
| 138 | |
| 139 | template <typename T, typename... Ts> |
Marco Poletti | f6335ae | 2015-01-01 15:13:04 +0100 | [diff] [blame] | 140 | struct apply<Vector<Ts...>, T> { |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 141 | using type = Vector<T, Ts...>; |
| 142 | }; |
| 143 | }; |
| 144 | |
Marco Poletti | d8943a8 | 2015-01-01 15:57:16 +0100 | [diff] [blame] | 145 | struct PushBack { |
| 146 | template <typename V, typename T> |
| 147 | struct apply; |
| 148 | |
| 149 | template <typename T, typename... Ts> |
| 150 | struct apply<Vector<Ts...>, T> { |
| 151 | using type = Vector<Ts..., T>; |
| 152 | }; |
| 153 | }; |
| 154 | |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 155 | struct ConcatVectors { |
| 156 | template <typename V1, typename V2> |
| 157 | struct apply; |
| 158 | |
| 159 | template <typename... Ts, typename... Us> |
| 160 | struct apply<Vector<Ts...>, Vector<Us...>> { |
| 161 | using type = Vector<Ts..., Us...>; |
| 162 | }; |
| 163 | }; |
| 164 | |
Marco Poletti | ebc3312 | 2015-07-11 14:41:40 +0100 | [diff] [blame] | 165 | struct TransformVector { |
| 166 | template <typename V, typename F> |
| 167 | struct apply; |
| 168 | |
| 169 | template <typename... Ts, typename F> |
| 170 | struct apply<Vector<Ts...>, F> { |
Marco Poletti | 36253c9 | 2015-07-19 17:12:47 +0100 | [diff] [blame] | 171 | using type = Vector<Eval<typename F::template apply<Ts>::type>...>; |
Marco Poletti | ebc3312 | 2015-07-11 14:41:40 +0100 | [diff] [blame] | 172 | }; |
| 173 | }; |
| 174 | |
Marco Poletti | 36253c9 | 2015-07-19 17:12:47 +0100 | [diff] [blame] | 175 | struct ReplaceInVectorHelper { |
| 176 | template <typename ToReplace, typename NewElem, typename T> |
Marco Poletti | ebc3312 | 2015-07-11 14:41:40 +0100 | [diff] [blame] | 177 | struct apply { |
Marco Poletti | 36253c9 | 2015-07-19 17:12:47 +0100 | [diff] [blame] | 178 | using type = T; |
Marco Poletti | daf6222 | 2015-07-11 14:57:41 +0100 | [diff] [blame] | 179 | }; |
| 180 | |
Marco Poletti | 36253c9 | 2015-07-19 17:12:47 +0100 | [diff] [blame] | 181 | template <typename ToReplace, typename NewElem> |
| 182 | struct apply<ToReplace, NewElem, ToReplace> { |
| 183 | using type = NewElem; |
Marco Poletti | ebc3312 | 2015-07-11 14:41:40 +0100 | [diff] [blame] | 184 | }; |
| 185 | }; |
| 186 | |
Marco Poletti | 36253c9 | 2015-07-19 17:12:47 +0100 | [diff] [blame] | 187 | struct ReplaceInVector { |
| 188 | template <typename V, typename ToReplace, typename NewElem> |
Marco Poletti | ebc3312 | 2015-07-11 14:41:40 +0100 | [diff] [blame] | 189 | struct apply { |
Marco Poletti | 36253c9 | 2015-07-19 17:12:47 +0100 | [diff] [blame] | 190 | using type = TransformVector(V, PartialCall(ReplaceInVectorHelper, ToReplace, NewElem)); |
| 191 | }; |
| 192 | }; |
| 193 | |
| 194 | // If V is Vector<T1, ..., Tn> this calculates F(InitialValue, F(T1, F(..., F(Tn) ...))). |
| 195 | // If V is Vector<> this returns InitialValue. |
| 196 | struct FoldVector { |
| 197 | template <typename V, typename F, typename InitialValue> |
| 198 | struct apply; |
| 199 | |
| 200 | template <typename... Ts, typename F, typename InitialValue> |
| 201 | struct apply<Vector<Ts...>, F, InitialValue> { |
| 202 | using type = Fold(F, InitialValue, Ts...); |
| 203 | }; |
| 204 | }; |
| 205 | |
| 206 | template <typename Unused> |
| 207 | using AlwaysVoidPtr = void*; |
| 208 | |
| 209 | // Returns a copy of V but without the first N elements. |
| 210 | // N must be at least 0 and at most VectorSize(V). |
| 211 | struct VectorRemoveFirstN { |
| 212 | template <typename V, typename N, typename Indexes = Eval<GenerateIntSequence(N)>> |
| 213 | struct apply; |
| 214 | |
| 215 | template <typename... Types, typename N, int... indexes> |
| 216 | struct apply<Vector<Types...>, N, Vector<Int<indexes>...>> { |
| 217 | template <typename... RemainingTypes> |
| 218 | static Vector<RemainingTypes...> f(AlwaysVoidPtr<Int<indexes>>..., RemainingTypes*...); |
| 219 | |
| 220 | using type = decltype(f((Types*)nullptr...)); |
| 221 | }; |
| 222 | }; |
| 223 | |
| 224 | struct VectorEndsWith { |
| 225 | template <typename V, typename T> |
| 226 | struct apply { |
| 227 | using type = IsSame(VectorRemoveFirstN(V, Int<Eval<VectorSize(V)>::value - 1>), |
| 228 | Vector<T>); |
| 229 | }; |
| 230 | |
| 231 | template <typename T> |
| 232 | struct apply<Vector<>, T> { |
| 233 | using type = Bool<false>; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 234 | }; |
| 235 | }; |
| 236 | |
Marco Poletti | 7e8b15a | 2015-12-07 22:54:38 +0000 | [diff] [blame] | 237 | struct ConstructErrorWithArgVector { |
| 238 | template <typename ErrorTag, typename ArgsVector, typename... OtherArgs> |
| 239 | struct apply; |
| 240 | |
| 241 | template <typename ErrorTag, typename... Args, typename... OtherArgs> |
| 242 | struct apply<ErrorTag, Vector<Args...>, OtherArgs...> { |
| 243 | using type = ConstructError(ErrorTag, OtherArgs..., Args...); |
| 244 | }; |
| 245 | }; |
| 246 | |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 247 | } // namespace meta |
| 248 | } // namespace impl |
| 249 | } // namespace fruit |
| 250 | |
| 251 | |
| 252 | #endif // FRUIT_META_VECTOR_H |