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" |
| 22 | #include <functional> |
| 23 | |
| 24 | /* |
| 25 | |
| 26 | Types and operations provided by this header: |
| 27 | |
Marco Poletti | a19fd1e | 2015-03-07 18:50:37 +0000 | [diff] [blame] | 28 | Vector<Ts...> : constructs a Vector with the specified elements. |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 29 | None : a Vector element that should be ignored (all transformations should map it to itself). |
| 30 | IsVector<V> : true if L is a Vector. |
| 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 | |
| 66 | struct IsVector { |
| 67 | template <typename T> |
Marco Poletti | a19fd1e | 2015-03-07 18:50:37 +0000 | [diff] [blame] | 68 | struct apply { |
| 69 | using type = Bool<false>; |
| 70 | }; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 71 | |
| 72 | template <typename... Ts> |
Marco Poletti | a19fd1e | 2015-03-07 18:50:37 +0000 | [diff] [blame] | 73 | struct apply<Vector<Ts...>> { |
| 74 | using type = Bool<true>; |
| 75 | }; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | struct IsInVector { |
| 79 | template <typename T, typename V> |
| 80 | struct apply; |
| 81 | |
| 82 | template <typename T, typename... Ts> |
| 83 | struct apply<T, Vector<Ts...>> { |
Marco Poletti | a19fd1e | 2015-03-07 18:50:37 +0000 | [diff] [blame] | 84 | using type = Bool<StaticOr<std::is_same<T, Ts>::value...>::value>; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 85 | }; |
| 86 | }; |
| 87 | |
| 88 | struct IsEmptyVector { |
| 89 | template <typename V> |
| 90 | struct apply; |
| 91 | |
| 92 | template <typename... Ts> |
| 93 | struct apply<Vector<Ts...>> { |
Marco Poletti | a19fd1e | 2015-03-07 18:50:37 +0000 | [diff] [blame] | 94 | using type = Bool<StaticAnd<std::is_same<Ts, None>::value...>::value>; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 95 | }; |
| 96 | }; |
| 97 | |
| 98 | struct VectorSize { |
| 99 | template <typename V> |
| 100 | struct apply; |
| 101 | |
| 102 | template <typename... Ts> |
| 103 | struct apply<Vector<Ts...>> { |
Marco Poletti | a19fd1e | 2015-03-07 18:50:37 +0000 | [diff] [blame] | 104 | using type = Int<StaticSum<!std::is_same<Ts, None>::value...>::value>; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 105 | }; |
| 106 | }; |
| 107 | |
| 108 | struct VectorApparentSize { |
| 109 | template <typename V> |
| 110 | struct apply; |
| 111 | |
| 112 | template <typename... Ts> |
| 113 | struct apply<Vector<Ts...>> { |
Marco Poletti | a19fd1e | 2015-03-07 18:50:37 +0000 | [diff] [blame] | 114 | using type = Int<sizeof...(Ts)>; |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 115 | }; |
| 116 | }; |
| 117 | |
Marco Poletti | f6335ae | 2015-01-01 15:13:04 +0100 | [diff] [blame] | 118 | struct PushFront { |
| 119 | template <typename V, typename T> |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 120 | struct apply; |
| 121 | |
| 122 | template <typename T, typename... Ts> |
Marco Poletti | f6335ae | 2015-01-01 15:13:04 +0100 | [diff] [blame] | 123 | struct apply<Vector<Ts...>, T> { |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 124 | using type = Vector<T, Ts...>; |
| 125 | }; |
| 126 | }; |
| 127 | |
Marco Poletti | d8943a8 | 2015-01-01 15:57:16 +0100 | [diff] [blame] | 128 | struct PushBack { |
| 129 | template <typename V, typename T> |
| 130 | struct apply; |
| 131 | |
| 132 | template <typename T, typename... Ts> |
| 133 | struct apply<Vector<Ts...>, T> { |
| 134 | using type = Vector<Ts..., T>; |
| 135 | }; |
| 136 | }; |
| 137 | |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 138 | struct ConcatVectors { |
| 139 | template <typename V1, typename V2> |
| 140 | struct apply; |
| 141 | |
| 142 | template <typename... Ts, typename... Us> |
| 143 | struct apply<Vector<Ts...>, Vector<Us...>> { |
| 144 | using type = Vector<Ts..., Us...>; |
| 145 | }; |
| 146 | }; |
| 147 | |
| 148 | struct ConcatMultipleVectors { |
| 149 | // Empty vector. |
| 150 | template <typename... V> |
| 151 | struct apply { |
| 152 | using type = Vector<>; |
| 153 | }; |
| 154 | |
| 155 | template <typename V> |
| 156 | struct apply<V> { |
| 157 | using type = V; |
| 158 | }; |
| 159 | |
| 160 | template <typename... Ts, typename... Us, typename... Vs> |
| 161 | struct apply<Vector<Ts...>, Vector<Us...>, Vs...> { |
Marco Poletti | 010daa6 | 2015-07-06 01:15:12 +0100 | [diff] [blame^] | 162 | using type = ConcatMultipleVectors(Vector<Ts..., Us...>, Vs...); |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 163 | }; |
| 164 | }; |
| 165 | |
Marco Poletti | 010daa6 | 2015-07-06 01:15:12 +0100 | [diff] [blame^] | 166 | // CallWithVector(F, Vector<Args...>) is equivalent to Call(F, Args...) |
| 167 | struct CallWithVector { |
| 168 | template <typename F, typename V> |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 169 | struct apply; |
| 170 | |
Marco Poletti | 010daa6 | 2015-07-06 01:15:12 +0100 | [diff] [blame^] | 171 | template <typename F, typename... Args> |
| 172 | struct apply<F, Vector<Args...>> { |
| 173 | using type = F(Args...); |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 174 | }; |
| 175 | }; |
| 176 | |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 177 | // TODO: This is slow when T is not in the vector, consider doing a IsInVector check first. |
| 178 | struct RemoveFromVector { |
| 179 | template <typename T, typename V> |
| 180 | struct apply; |
| 181 | |
| 182 | template <typename T, typename... Ts> |
| 183 | struct apply<T, Vector<Ts...>> { |
Marco Poletti | 010daa6 | 2015-07-06 01:15:12 +0100 | [diff] [blame^] | 184 | using type = ConsVector(Id<If(IsSame(T, Ts), None, Ts)> |
| 185 | ...); |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 186 | }; |
| 187 | }; |
| 188 | |
| 189 | } // namespace meta |
| 190 | } // namespace impl |
| 191 | } // namespace fruit |
| 192 | |
| 193 | |
| 194 | #endif // FRUIT_META_VECTOR_H |