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 | |
| 20 | #include "basics.h" |
| 21 | #include "logical_operations.h" |
Marco Poletti | 3b95a51 | 2015-07-11 13:43:39 +0100 | [diff] [blame] | 22 | #include "numeric_operations.h" |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 23 | #include <functional> |
| 24 | |
| 25 | /* |
| 26 | |
| 27 | Types and operations provided by this header: |
| 28 | |
Marco Poletti | a19fd1e | 2015-03-07 18:50:37 +0000 | [diff] [blame] | 29 | Vector<Ts...> : constructs a Vector with the specified elements. |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 30 | None : a Vector element that should be ignored (all transformations should map it to itself). |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 31 | IsInVector<V, T> : true if T appears at least once in V. |
| 32 | IsEmptyVector<V> : true if L is empty (i.e. if all the elements are None) |
| 33 | VectorSize<V> : the number of (non-None) elements of the vector (as an int). |
| 34 | VectorApparentSize<V> : the number of elements of the vector *including* any None elements. |
Marco Poletti | f6335ae | 2015-01-01 15:13:04 +0100 | [diff] [blame] | 35 | PushFront<V, T> : adds T in front of V. |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 36 | ConcatVectors<V1, V2> : returns the concatenation of the given vectors. |
| 37 | ConcatMultipleVectors<Vs...> : extension of ConcatVectors that works with an arbitrary number of vectors. Use sparingly, it's |
| 38 | quite slow. |
| 39 | TransformVector<F, V> : returns the list obtained by applying F to each element of V. F<None>::type must be None. |
| 40 | RemoveFromVector<V, T> : returns a vector equivalent to V but with all the occurrences of T replaced with None. |
| 41 | |
| 42 | */ |
| 43 | |
| 44 | namespace fruit { |
| 45 | namespace impl { |
| 46 | namespace meta { |
| 47 | |
| 48 | // Used to pass around a Vector<Types...>, no meaning per se. |
| 49 | template <typename... Types> |
Marco Poletti | 010daa6 | 2015-07-06 01:15:12 +0100 | [diff] [blame] | 50 | struct Vector {}; |
| 51 | |
| 52 | // Using ConsVector(MetaExpr...) instead of Vector<MetaExpr...> in a meta-expression allows the |
| 53 | // types to be evaluated. Avoid using Vector<...> directly in a meta-expression, unless you're sure |
| 54 | // that the arguments have already been evaluated (e.g. if Args... are arguments of a metafunction, |
| 55 | // Vector<Args...> is ok but Vector<MyFunction(Args)...> is wrong. |
| 56 | struct ConsVector { |
| 57 | template <typename... Types> |
| 58 | struct apply { |
| 59 | using type = Vector<Types...>; |
| 60 | }; |
| 61 | }; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 62 | |
| 63 | // None elements in a vector are just placeholders (put in place of real elements when removing an element) and should be ignored. |
Marco Poletti | 010daa6 | 2015-07-06 01:15:12 +0100 | [diff] [blame] | 64 | struct None {}; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 65 | |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 66 | struct IsInVector { |
| 67 | template <typename T, typename V> |
| 68 | struct apply; |
| 69 | |
| 70 | template <typename T, typename... Ts> |
| 71 | struct apply<T, Vector<Ts...>> { |
Marco Poletti | 3b95a51 | 2015-07-11 13:43:39 +0100 | [diff] [blame] | 72 | using type = Bool<staticOr(std::is_same<T, Ts>::value...)>; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 73 | }; |
| 74 | }; |
| 75 | |
| 76 | struct IsEmptyVector { |
| 77 | template <typename V> |
| 78 | struct apply; |
| 79 | |
| 80 | template <typename... Ts> |
| 81 | struct apply<Vector<Ts...>> { |
Marco Poletti | 3b95a51 | 2015-07-11 13:43:39 +0100 | [diff] [blame] | 82 | using type = Bool<staticAnd(std::is_same<Ts, None>::value...)>; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 83 | }; |
| 84 | }; |
| 85 | |
| 86 | struct VectorSize { |
| 87 | template <typename V> |
| 88 | struct apply; |
| 89 | |
| 90 | template <typename... Ts> |
| 91 | struct apply<Vector<Ts...>> { |
Marco Poletti | 3b95a51 | 2015-07-11 13:43:39 +0100 | [diff] [blame] | 92 | using type = Int<staticSum(!std::is_same<Ts, None>::value...)>; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 93 | }; |
| 94 | }; |
| 95 | |
| 96 | struct VectorApparentSize { |
| 97 | template <typename V> |
| 98 | struct apply; |
| 99 | |
| 100 | template <typename... Ts> |
| 101 | struct apply<Vector<Ts...>> { |
Marco Poletti | a19fd1e | 2015-03-07 18:50:37 +0000 | [diff] [blame] | 102 | using type = Int<sizeof...(Ts)>; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 103 | }; |
| 104 | }; |
| 105 | |
Marco Poletti | f6335ae | 2015-01-01 15:13:04 +0100 | [diff] [blame] | 106 | struct PushFront { |
| 107 | template <typename V, typename T> |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 108 | struct apply; |
| 109 | |
| 110 | template <typename T, typename... Ts> |
Marco Poletti | f6335ae | 2015-01-01 15:13:04 +0100 | [diff] [blame] | 111 | struct apply<Vector<Ts...>, T> { |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 112 | using type = Vector<T, Ts...>; |
| 113 | }; |
| 114 | }; |
| 115 | |
Marco Poletti | d8943a8 | 2015-01-01 15:57:16 +0100 | [diff] [blame] | 116 | struct PushBack { |
| 117 | template <typename V, typename T> |
| 118 | struct apply; |
| 119 | |
| 120 | template <typename T, typename... Ts> |
| 121 | struct apply<Vector<Ts...>, T> { |
| 122 | using type = Vector<Ts..., T>; |
| 123 | }; |
| 124 | }; |
| 125 | |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 126 | struct ConcatVectors { |
| 127 | template <typename V1, typename V2> |
| 128 | struct apply; |
| 129 | |
| 130 | template <typename... Ts, typename... Us> |
| 131 | struct apply<Vector<Ts...>, Vector<Us...>> { |
| 132 | using type = Vector<Ts..., Us...>; |
| 133 | }; |
| 134 | }; |
| 135 | |
| 136 | struct ConcatMultipleVectors { |
| 137 | // Empty vector. |
| 138 | template <typename... V> |
| 139 | struct apply { |
| 140 | using type = Vector<>; |
| 141 | }; |
| 142 | |
| 143 | template <typename V> |
| 144 | struct apply<V> { |
| 145 | using type = V; |
| 146 | }; |
| 147 | |
| 148 | template <typename... Ts, typename... Us, typename... Vs> |
| 149 | struct apply<Vector<Ts...>, Vector<Us...>, Vs...> { |
Marco Poletti | 010daa6 | 2015-07-06 01:15:12 +0100 | [diff] [blame] | 150 | using type = ConcatMultipleVectors(Vector<Ts..., Us...>, Vs...); |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 151 | }; |
| 152 | }; |
| 153 | |
Marco Poletti | 010daa6 | 2015-07-06 01:15:12 +0100 | [diff] [blame] | 154 | // CallWithVector(F, Vector<Args...>) is equivalent to Call(F, Args...) |
| 155 | struct CallWithVector { |
| 156 | template <typename F, typename V> |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 157 | struct apply; |
| 158 | |
Marco Poletti | 010daa6 | 2015-07-06 01:15:12 +0100 | [diff] [blame] | 159 | template <typename F, typename... Args> |
| 160 | struct apply<F, Vector<Args...>> { |
| 161 | using type = F(Args...); |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 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> { |
| 171 | using type = Vector<Eval<F(Ts)>...>; |
| 172 | }; |
| 173 | }; |
| 174 | |
Marco Poletti | ebc3312 | 2015-07-11 14:41:40 +0100 | [diff] [blame] | 175 | struct RemoveFromVectorHelper { |
Marco Poletti | daf6222 | 2015-07-11 14:57:41 +0100 | [diff] [blame^] | 176 | template <typename ElemToRemove, typename Elem> |
Marco Poletti | ebc3312 | 2015-07-11 14:41:40 +0100 | [diff] [blame] | 177 | struct apply { |
Marco Poletti | daf6222 | 2015-07-11 14:57:41 +0100 | [diff] [blame^] | 178 | using type = Elem; |
| 179 | }; |
| 180 | |
| 181 | template <typename Elem> |
| 182 | struct apply<Elem, Elem> { |
| 183 | using type = None; |
Marco Poletti | ebc3312 | 2015-07-11 14:41:40 +0100 | [diff] [blame] | 184 | }; |
| 185 | }; |
| 186 | |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 187 | // TODO: This is slow when T is not in the vector, consider doing a IsInVector check first. |
| 188 | struct RemoveFromVector { |
| 189 | template <typename T, typename V> |
Marco Poletti | ebc3312 | 2015-07-11 14:41:40 +0100 | [diff] [blame] | 190 | struct apply { |
Marco Poletti | daf6222 | 2015-07-11 14:57:41 +0100 | [diff] [blame^] | 191 | using type = TransformVector(V, PartialCall(RemoveFromVectorHelper, T)); |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 192 | }; |
| 193 | }; |
| 194 | |
| 195 | } // namespace meta |
| 196 | } // namespace impl |
| 197 | } // namespace fruit |
| 198 | |
| 199 | |
| 200 | #endif // FRUIT_META_VECTOR_H |