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 | |
| 28 | Vector<Ts.. : constructs a Vector with the specified elements. |
| 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> |
| 50 | struct Vector {}; |
| 51 | |
| 52 | // None elements in a vector are just placeholders (put in place of real elements when removing an element) and should be ignored. |
| 53 | struct None {}; |
| 54 | |
| 55 | struct IsVector { |
| 56 | template <typename T> |
| 57 | struct apply : std::false_type {}; |
| 58 | |
| 59 | template <typename... Ts> |
| 60 | struct apply<Vector<Ts...>> : std::true_type {}; |
| 61 | }; |
| 62 | |
| 63 | struct IsInVector { |
| 64 | template <typename T, typename V> |
| 65 | struct apply; |
| 66 | |
| 67 | template <typename T, typename... Ts> |
| 68 | struct apply<T, Vector<Ts...>> { |
| 69 | static constexpr bool value = StaticOr<std::is_same<T, Ts>::value...>::value; |
| 70 | }; |
| 71 | }; |
| 72 | |
| 73 | struct IsEmptyVector { |
| 74 | template <typename V> |
| 75 | struct apply; |
| 76 | |
| 77 | template <typename... Ts> |
| 78 | struct apply<Vector<Ts...>> { |
| 79 | static constexpr bool value = StaticAnd<std::is_same<Ts, None>::value...>::value; |
| 80 | }; |
| 81 | }; |
| 82 | |
| 83 | struct VectorSize { |
| 84 | template <typename V> |
| 85 | struct apply; |
| 86 | |
| 87 | template <typename... Ts> |
| 88 | struct apply<Vector<Ts...>> { |
| 89 | static constexpr int value = StaticSum<!std::is_same<Ts, None>::value...>::value; |
| 90 | }; |
| 91 | }; |
| 92 | |
| 93 | struct VectorApparentSize { |
| 94 | template <typename V> |
| 95 | struct apply; |
| 96 | |
| 97 | template <typename... Ts> |
| 98 | struct apply<Vector<Ts...>> { |
| 99 | static constexpr int value = sizeof...(Ts); |
| 100 | }; |
| 101 | }; |
| 102 | |
Marco Poletti | f6335ae | 2015-01-01 15:13:04 +0100 | [diff] [blame^] | 103 | struct PushFront { |
| 104 | template <typename V, typename T> |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 105 | struct apply; |
| 106 | |
| 107 | template <typename T, typename... Ts> |
Marco Poletti | f6335ae | 2015-01-01 15:13:04 +0100 | [diff] [blame^] | 108 | struct apply<Vector<Ts...>, T> { |
Marco Poletti | 0c35fac | 2014-12-26 11:05:07 +0100 | [diff] [blame] | 109 | using type = Vector<T, Ts...>; |
| 110 | }; |
| 111 | }; |
| 112 | |
| 113 | struct ConcatVectors { |
| 114 | template <typename V1, typename V2> |
| 115 | struct apply; |
| 116 | |
| 117 | template <typename... Ts, typename... Us> |
| 118 | struct apply<Vector<Ts...>, Vector<Us...>> { |
| 119 | using type = Vector<Ts..., Us...>; |
| 120 | }; |
| 121 | }; |
| 122 | |
| 123 | struct ConcatMultipleVectors { |
| 124 | // Empty vector. |
| 125 | template <typename... V> |
| 126 | struct apply { |
| 127 | using type = Vector<>; |
| 128 | }; |
| 129 | |
| 130 | template <typename V> |
| 131 | struct apply<V> { |
| 132 | using type = V; |
| 133 | }; |
| 134 | |
| 135 | template <typename... Ts, typename... Us, typename... Vs> |
| 136 | struct apply<Vector<Ts...>, Vector<Us...>, Vs...> { |
| 137 | using type = Apply<ConcatMultipleVectors, Vector<Ts..., Us...>, Vs...>; |
| 138 | }; |
| 139 | }; |
| 140 | |
| 141 | struct ApplyWithVectorHelper { |
| 142 | template <typename F, typename V, typename... Args> |
| 143 | struct apply; |
| 144 | |
| 145 | template <typename F, typename... Elems, typename... Args> |
| 146 | struct apply<F, Vector<Elems...>, Args...> { |
| 147 | using type = Apply<F, Args..., Elems...>; |
| 148 | }; |
| 149 | }; |
| 150 | |
| 151 | template <typename F, typename V, typename... Args> |
| 152 | using ApplyWithVector = Apply<ApplyWithVectorHelper, F, V, Args...>; |
| 153 | |
| 154 | // TODO: This is slow when T is not in the vector, consider doing a IsInVector check first. |
| 155 | struct RemoveFromVector { |
| 156 | template <typename T, typename V> |
| 157 | struct apply; |
| 158 | |
| 159 | template <typename T, typename... Ts> |
| 160 | struct apply<T, Vector<Ts...>> { |
| 161 | using type = Vector< |
| 162 | Eval<std::conditional<std::is_same<T, Ts>::value, None, Ts>> |
| 163 | ...>; |
| 164 | }; |
| 165 | }; |
| 166 | |
| 167 | } // namespace meta |
| 168 | } // namespace impl |
| 169 | } // namespace fruit |
| 170 | |
| 171 | |
| 172 | #endif // FRUIT_META_VECTOR_H |