blob: 3690a4792a3b287ab38038d25b090a26619d6b6e [file] [log] [blame]
Marco Poletti0c35fac2014-12-26 11:05:07 +01001/*
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 Poletti3b95a512015-07-11 13:43:39 +010022#include "numeric_operations.h"
Marco Poletti0c35fac2014-12-26 11:05:07 +010023#include <functional>
24
25/*
26
27Types and operations provided by this header:
28
Marco Polettia19fd1e2015-03-07 18:50:37 +000029Vector<Ts...> : constructs a Vector with the specified elements.
Marco Poletti0c35fac2014-12-26 11:05:07 +010030None : a Vector element that should be ignored (all transformations should map it to itself).
Marco Poletti0c35fac2014-12-26 11:05:07 +010031IsInVector<V, T> : true if T appears at least once in V.
32IsEmptyVector<V> : true if L is empty (i.e. if all the elements are None)
33VectorSize<V> : the number of (non-None) elements of the vector (as an int).
34VectorApparentSize<V> : the number of elements of the vector *including* any None elements.
Marco Polettif6335ae2015-01-01 15:13:04 +010035PushFront<V, T> : adds T in front of V.
Marco Poletti0c35fac2014-12-26 11:05:07 +010036ConcatVectors<V1, V2> : returns the concatenation of the given vectors.
37ConcatMultipleVectors<Vs...> : extension of ConcatVectors that works with an arbitrary number of vectors. Use sparingly, it's
38 quite slow.
39TransformVector<F, V> : returns the list obtained by applying F to each element of V. F<None>::type must be None.
40RemoveFromVector<V, T> : returns a vector equivalent to V but with all the occurrences of T replaced with None.
41
42*/
43
44namespace fruit {
45namespace impl {
46namespace meta {
47
48// Used to pass around a Vector<Types...>, no meaning per se.
49template <typename... Types>
Marco Poletti010daa62015-07-06 01:15:12 +010050struct 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.
56struct ConsVector {
57 template <typename... Types>
58 struct apply {
59 using type = Vector<Types...>;
60 };
61};
Marco Poletti0c35fac2014-12-26 11:05:07 +010062
63// None elements in a vector are just placeholders (put in place of real elements when removing an element) and should be ignored.
Marco Poletti010daa62015-07-06 01:15:12 +010064struct None {};
Marco Poletti0c35fac2014-12-26 11:05:07 +010065
Marco Poletti0c35fac2014-12-26 11:05:07 +010066struct IsInVector {
67 template <typename T, typename V>
68 struct apply;
69
70 template <typename T, typename... Ts>
71 struct apply<T, Vector<Ts...>> {
Marco Poletti3b95a512015-07-11 13:43:39 +010072 using type = Bool<staticOr(std::is_same<T, Ts>::value...)>;
Marco Poletti0c35fac2014-12-26 11:05:07 +010073 };
74};
75
76struct IsEmptyVector {
77 template <typename V>
78 struct apply;
79
80 template <typename... Ts>
81 struct apply<Vector<Ts...>> {
Marco Poletti3b95a512015-07-11 13:43:39 +010082 using type = Bool<staticAnd(std::is_same<Ts, None>::value...)>;
Marco Poletti0c35fac2014-12-26 11:05:07 +010083 };
84};
85
86struct VectorSize {
87 template <typename V>
88 struct apply;
89
90 template <typename... Ts>
91 struct apply<Vector<Ts...>> {
Marco Poletti3b95a512015-07-11 13:43:39 +010092 using type = Int<staticSum(!std::is_same<Ts, None>::value...)>;
Marco Poletti0c35fac2014-12-26 11:05:07 +010093 };
94};
95
96struct VectorApparentSize {
97 template <typename V>
98 struct apply;
99
100 template <typename... Ts>
101 struct apply<Vector<Ts...>> {
Marco Polettia19fd1e2015-03-07 18:50:37 +0000102 using type = Int<sizeof...(Ts)>;
Marco Poletti0c35fac2014-12-26 11:05:07 +0100103 };
104};
105
Marco Polettif6335ae2015-01-01 15:13:04 +0100106struct PushFront {
107 template <typename V, typename T>
Marco Poletti0c35fac2014-12-26 11:05:07 +0100108 struct apply;
109
110 template <typename T, typename... Ts>
Marco Polettif6335ae2015-01-01 15:13:04 +0100111 struct apply<Vector<Ts...>, T> {
Marco Poletti0c35fac2014-12-26 11:05:07 +0100112 using type = Vector<T, Ts...>;
113 };
114};
115
Marco Polettid8943a82015-01-01 15:57:16 +0100116struct 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 Poletti0c35fac2014-12-26 11:05:07 +0100126struct 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
136struct 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 Poletti010daa62015-07-06 01:15:12 +0100150 using type = ConcatMultipleVectors(Vector<Ts..., Us...>, Vs...);
Marco Poletti0c35fac2014-12-26 11:05:07 +0100151 };
152};
153
Marco Poletti010daa62015-07-06 01:15:12 +0100154// CallWithVector(F, Vector<Args...>) is equivalent to Call(F, Args...)
155struct CallWithVector {
156 template <typename F, typename V>
Marco Poletti0c35fac2014-12-26 11:05:07 +0100157 struct apply;
158
Marco Poletti010daa62015-07-06 01:15:12 +0100159 template <typename F, typename... Args>
160 struct apply<F, Vector<Args...>> {
161 using type = F(Args...);
Marco Poletti0c35fac2014-12-26 11:05:07 +0100162 };
163};
164
Marco Polettiebc33122015-07-11 14:41:40 +0100165struct 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 Polettiebc33122015-07-11 14:41:40 +0100175struct RemoveFromVectorHelper {
Marco Polettidaf62222015-07-11 14:57:41 +0100176 template <typename ElemToRemove, typename Elem>
Marco Polettiebc33122015-07-11 14:41:40 +0100177 struct apply {
Marco Polettidaf62222015-07-11 14:57:41 +0100178 using type = Elem;
179 };
180
181 template <typename Elem>
182 struct apply<Elem, Elem> {
183 using type = None;
Marco Polettiebc33122015-07-11 14:41:40 +0100184 };
185};
186
Marco Poletti0c35fac2014-12-26 11:05:07 +0100187// TODO: This is slow when T is not in the vector, consider doing a IsInVector check first.
188struct RemoveFromVector {
189 template <typename T, typename V>
Marco Polettiebc33122015-07-11 14:41:40 +0100190 struct apply {
Marco Polettidaf62222015-07-11 14:57:41 +0100191 using type = TransformVector(V, PartialCall(RemoveFromVectorHelper, T));
Marco Poletti0c35fac2014-12-26 11:05:07 +0100192 };
193};
194
195} // namespace meta
196} // namespace impl
197} // namespace fruit
198
199
200#endif // FRUIT_META_VECTOR_H