blob: a8ecbe751f7d20df8d9dc830e9805dd55d222cdc [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 Polettia19fd1e2015-03-07 18:50:37 +000050struct Vector;
Marco Poletti0c35fac2014-12-26 11:05:07 +010051
52// None elements in a vector are just placeholders (put in place of real elements when removing an element) and should be ignored.
Marco Polettia19fd1e2015-03-07 18:50:37 +000053struct None;
Marco Poletti0c35fac2014-12-26 11:05:07 +010054
55struct IsVector {
56 template <typename T>
Marco Polettia19fd1e2015-03-07 18:50:37 +000057 struct apply {
58 using type = Bool<false>;
59 };
Marco Poletti0c35fac2014-12-26 11:05:07 +010060
61 template <typename... Ts>
Marco Polettia19fd1e2015-03-07 18:50:37 +000062 struct apply<Vector<Ts...>> {
63 using type = Bool<true>;
64 };
Marco Poletti0c35fac2014-12-26 11:05:07 +010065};
66
67struct IsInVector {
68 template <typename T, typename V>
69 struct apply;
70
71 template <typename T, typename... Ts>
72 struct apply<T, Vector<Ts...>> {
Marco Polettia19fd1e2015-03-07 18:50:37 +000073 using type = Bool<StaticOr<std::is_same<T, Ts>::value...>::value>;
Marco Poletti0c35fac2014-12-26 11:05:07 +010074 };
75};
76
77struct IsEmptyVector {
78 template <typename V>
79 struct apply;
80
81 template <typename... Ts>
82 struct apply<Vector<Ts...>> {
Marco Polettia19fd1e2015-03-07 18:50:37 +000083 using type = Bool<StaticAnd<std::is_same<Ts, None>::value...>::value>;
Marco Poletti0c35fac2014-12-26 11:05:07 +010084 };
85};
86
87struct VectorSize {
88 template <typename V>
89 struct apply;
90
91 template <typename... Ts>
92 struct apply<Vector<Ts...>> {
Marco Polettia19fd1e2015-03-07 18:50:37 +000093 using type = Int<StaticSum<!std::is_same<Ts, None>::value...>::value>;
Marco Poletti0c35fac2014-12-26 11:05:07 +010094 };
95};
96
97struct VectorApparentSize {
98 template <typename V>
99 struct apply;
100
101 template <typename... Ts>
102 struct apply<Vector<Ts...>> {
Marco Polettia19fd1e2015-03-07 18:50:37 +0000103 using type = Int<sizeof...(Ts)>;
Marco Poletti0c35fac2014-12-26 11:05:07 +0100104 };
105};
106
Marco Polettif6335ae2015-01-01 15:13:04 +0100107struct PushFront {
108 template <typename V, typename T>
Marco Poletti0c35fac2014-12-26 11:05:07 +0100109 struct apply;
110
111 template <typename T, typename... Ts>
Marco Polettif6335ae2015-01-01 15:13:04 +0100112 struct apply<Vector<Ts...>, T> {
Marco Poletti0c35fac2014-12-26 11:05:07 +0100113 using type = Vector<T, Ts...>;
114 };
115};
116
Marco Polettid8943a82015-01-01 15:57:16 +0100117struct PushBack {
118 template <typename V, typename T>
119 struct apply;
120
121 template <typename T, typename... Ts>
122 struct apply<Vector<Ts...>, T> {
123 using type = Vector<Ts..., T>;
124 };
125};
126
Marco Poletti0c35fac2014-12-26 11:05:07 +0100127struct ConcatVectors {
128 template <typename V1, typename V2>
129 struct apply;
130
131 template <typename... Ts, typename... Us>
132 struct apply<Vector<Ts...>, Vector<Us...>> {
133 using type = Vector<Ts..., Us...>;
134 };
135};
136
137struct ConcatMultipleVectors {
138 // Empty vector.
139 template <typename... V>
140 struct apply {
141 using type = Vector<>;
142 };
143
144 template <typename V>
145 struct apply<V> {
146 using type = V;
147 };
148
149 template <typename... Ts, typename... Us, typename... Vs>
150 struct apply<Vector<Ts...>, Vector<Us...>, Vs...> {
151 using type = Apply<ConcatMultipleVectors, Vector<Ts..., Us...>, Vs...>;
152 };
153};
154
155struct ApplyWithVectorHelper {
156 template <typename F, typename V, typename... Args>
157 struct apply;
158
159 template <typename F, typename... Elems, typename... Args>
160 struct apply<F, Vector<Elems...>, Args...> {
161 using type = Apply<F, Args..., Elems...>;
162 };
163};
164
165template <typename F, typename V, typename... Args>
166using ApplyWithVector = Apply<ApplyWithVectorHelper, F, V, Args...>;
167
168// TODO: This is slow when T is not in the vector, consider doing a IsInVector check first.
169struct RemoveFromVector {
170 template <typename T, typename V>
171 struct apply;
172
173 template <typename T, typename... Ts>
174 struct apply<T, Vector<Ts...>> {
175 using type = Vector<
176 Eval<std::conditional<std::is_same<T, Ts>::value, None, Ts>>
177 ...>;
178 };
179};
180
181} // namespace meta
182} // namespace impl
183} // namespace fruit
184
185
186#endif // FRUIT_META_VECTOR_H