blob: 0acc5cfa28a1d82e77dc2e18c1fa98719661f17f [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"
22#include <functional>
23
24/*
25
26Types and operations provided by this header:
27
Marco Polettia19fd1e2015-03-07 18:50:37 +000028Vector<Ts...> : constructs a Vector with the specified elements.
Marco Poletti0c35fac2014-12-26 11:05:07 +010029None : a Vector element that should be ignored (all transformations should map it to itself).
30IsVector<V> : true if L is a Vector.
31IsInVector<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
66struct IsVector {
67 template <typename T>
Marco Polettia19fd1e2015-03-07 18:50:37 +000068 struct apply {
69 using type = Bool<false>;
70 };
Marco Poletti0c35fac2014-12-26 11:05:07 +010071
72 template <typename... Ts>
Marco Polettia19fd1e2015-03-07 18:50:37 +000073 struct apply<Vector<Ts...>> {
74 using type = Bool<true>;
75 };
Marco Poletti0c35fac2014-12-26 11:05:07 +010076};
77
78struct IsInVector {
79 template <typename T, typename V>
80 struct apply;
81
82 template <typename T, typename... Ts>
83 struct apply<T, Vector<Ts...>> {
Marco Polettia19fd1e2015-03-07 18:50:37 +000084 using type = Bool<StaticOr<std::is_same<T, Ts>::value...>::value>;
Marco Poletti0c35fac2014-12-26 11:05:07 +010085 };
86};
87
88struct IsEmptyVector {
89 template <typename V>
90 struct apply;
91
92 template <typename... Ts>
93 struct apply<Vector<Ts...>> {
Marco Polettia19fd1e2015-03-07 18:50:37 +000094 using type = Bool<StaticAnd<std::is_same<Ts, None>::value...>::value>;
Marco Poletti0c35fac2014-12-26 11:05:07 +010095 };
96};
97
98struct VectorSize {
99 template <typename V>
100 struct apply;
101
102 template <typename... Ts>
103 struct apply<Vector<Ts...>> {
Marco Polettia19fd1e2015-03-07 18:50:37 +0000104 using type = Int<StaticSum<!std::is_same<Ts, None>::value...>::value>;
Marco Poletti0c35fac2014-12-26 11:05:07 +0100105 };
106};
107
108struct VectorApparentSize {
109 template <typename V>
110 struct apply;
111
112 template <typename... Ts>
113 struct apply<Vector<Ts...>> {
Marco Polettia19fd1e2015-03-07 18:50:37 +0000114 using type = Int<sizeof...(Ts)>;
Marco Poletti0c35fac2014-12-26 11:05:07 +0100115 };
116};
117
Marco Polettif6335ae2015-01-01 15:13:04 +0100118struct PushFront {
119 template <typename V, typename T>
Marco Poletti0c35fac2014-12-26 11:05:07 +0100120 struct apply;
121
122 template <typename T, typename... Ts>
Marco Polettif6335ae2015-01-01 15:13:04 +0100123 struct apply<Vector<Ts...>, T> {
Marco Poletti0c35fac2014-12-26 11:05:07 +0100124 using type = Vector<T, Ts...>;
125 };
126};
127
Marco Polettid8943a82015-01-01 15:57:16 +0100128struct 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 Poletti0c35fac2014-12-26 11:05:07 +0100138struct 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
148struct 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 Poletti010daa62015-07-06 01:15:12 +0100162 using type = ConcatMultipleVectors(Vector<Ts..., Us...>, Vs...);
Marco Poletti0c35fac2014-12-26 11:05:07 +0100163 };
164};
165
Marco Poletti010daa62015-07-06 01:15:12 +0100166// CallWithVector(F, Vector<Args...>) is equivalent to Call(F, Args...)
167struct CallWithVector {
168 template <typename F, typename V>
Marco Poletti0c35fac2014-12-26 11:05:07 +0100169 struct apply;
170
Marco Poletti010daa62015-07-06 01:15:12 +0100171 template <typename F, typename... Args>
172 struct apply<F, Vector<Args...>> {
173 using type = F(Args...);
Marco Poletti0c35fac2014-12-26 11:05:07 +0100174 };
175};
176
Marco Poletti0c35fac2014-12-26 11:05:07 +0100177// TODO: This is slow when T is not in the vector, consider doing a IsInVector check first.
178struct RemoveFromVector {
179 template <typename T, typename V>
180 struct apply;
181
182 template <typename T, typename... Ts>
183 struct apply<T, Vector<Ts...>> {
Marco Poletti010daa62015-07-06 01:15:12 +0100184 using type = ConsVector(Id<If(IsSame(T, Ts), None, Ts)>
185 ...);
Marco Poletti0c35fac2014-12-26 11:05:07 +0100186 };
187};
188
189} // namespace meta
190} // namespace impl
191} // namespace fruit
192
193
194#endif // FRUIT_META_VECTOR_H