blob: 5b350a07e345db7d3d8229f5b0b284c167276d5d [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
28Vector<Ts.. : constructs a Vector with the specified elements.
29None : 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>
50struct 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.
53struct None {};
54
55struct 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
63struct 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
73struct 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
83struct 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
93struct 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 Polettif6335ae2015-01-01 15:13:04 +0100103struct PushFront {
104 template <typename V, typename T>
Marco Poletti0c35fac2014-12-26 11:05:07 +0100105 struct apply;
106
107 template <typename T, typename... Ts>
Marco Polettif6335ae2015-01-01 15:13:04 +0100108 struct apply<Vector<Ts...>, T> {
Marco Poletti0c35fac2014-12-26 11:05:07 +0100109 using type = Vector<T, Ts...>;
110 };
111};
112
113struct 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
123struct 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
141struct 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
151template <typename F, typename V, typename... Args>
152using 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.
155struct 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