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