blob: c6f3b43e24a3b126b221d260ef469a67ba15514e [file] [log] [blame]
Marco Poletti36253c92015-07-19 17:12:47 +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_MAP_H
18#define FRUIT_META_MAP_H
19
Marco Polettif2895102016-01-30 13:38:37 +000020#include <fruit/impl/meta/set.h>
Marco Poletti36253c92015-07-19 17:12:47 +010021
22namespace fruit {
23namespace impl {
24namespace meta {
25
26// A Map is a Set whose elements have the form Pair<Key, Value>
27
28
29struct GetMapKeys {
30 template <typename M>
31 struct apply;
32
33 template <typename... Pairs>
34 struct apply<Vector<Pairs...>> {
35 using type = Vector<typename Pairs::First...>;
36 };
37};
38
39// TODO: Consider implementing this by finding the position.
40struct MapContainsKey {
41 template <typename TToFind>
42 struct Helper {
43 template <typename CurrentResult, typename T>
44 struct apply {
45 using type = CurrentResult;
46 };
47 template <typename CurrentResult, typename Value>
48 struct apply<CurrentResult, Pair<TToFind, Value>> {
49 using type = Bool<true>;
50 };
51 };
52
53 template <typename M, typename TToFind>
54 struct apply {
55 using type = FoldVector(M, Helper<TToFind>, Bool<false>);
56 };
57};
58
59// TODO: Consider implementing this by finding the position first, then calling VectorRemoveFirstN
60// and getting the first element.
61struct FindInMap {
62 template <typename TToFind>
63 struct Helper {
64 template <typename CurrentResult, typename T>
65 struct apply {
66 using type = CurrentResult;
67 };
68 template <typename CurrentResult, typename Value>
69 struct apply<CurrentResult, Pair<TToFind, Value>> {
70 using type = Value;
71 };
72 };
73
74 template <typename M, typename TToFind>
75 struct apply {
76 using type = FoldVector(M, Helper<TToFind>, None);
77 };
78};
79
Marco Poletti1e44d552017-05-20 14:10:38 +010080// TODO: Consider implementing this by finding the position first, then calling VectorRemoveFirstN
81// and getting the first element.
82struct FindValueInMap {
83 template <typename TToFind>
84 struct Helper {
85 template <typename CurrentResult, typename T>
86 struct apply {
87 using type = CurrentResult;
88 };
89 template <typename CurrentResult, typename Value>
90 struct apply<CurrentResult, Pair<Value, TToFind>> {
91 using type = Value;
92 };
93 };
94
95 template <typename M, typename TToFind>
96 struct apply {
97 using type = FoldVector(M, Helper<TToFind>, None);
98 };
99};
100
Marco Poletti36253c92015-07-19 17:12:47 +0100101} // namespace meta
102} // namespace impl
103} // namespace fruit
104
105
106#endif // FRUIT_META_MAP_H