tinympl  0.2
mini MPL library for C++11
map.hpp
1 // Copyright (C) 2013, Ennio Barbaro.
2 //
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://sbabbi.github.io/tinympl for documentation.
8 //
9 // You are welcome to contact the author at:
10 // enniobarbaro@gmail.com
11 //
12 
13 #ifndef TINYMPL_MAP_HPP
14 #define TINYMPL_MAP_HPP
15 
16 #include <tinympl/equal_to.hpp>
17 #include <tinympl/variadic/all_of.hpp>
18 #include <tinympl/variadic/is_unique.hpp>
19 #include <tinympl/variadic/find.hpp>
20 #include <tinympl/variadic/at.hpp>
21 #include <tinympl/variadic/left_fold.hpp>
22 #include <tinympl/variadic/remove_if.hpp>
23 #include <tinympl/unordered_equal.hpp>
24 #include <tinympl/sequence.hpp>
25 #include <type_traits>
26 #include <utility>
27 
28 namespace tinympl {
29 
33 template<class T> struct is_pair : std::false_type {};
34 
35 template<class FirstType,class SecondType>
36 struct is_pair<std::pair<FirstType,SecondType> > : std::true_type
37 {
38  typedef FirstType first_type;
39  typedef SecondType second_type;
40 };
41 
56 template<class ... Args> struct map
57 {
58  static_assert( variadic::all_of< is_pair, Args...>::type::value, "All the arguments of a map must be key/value pairs");
59  static_assert( variadic::is_unique<typename Args::first_type ...>::type::value,"Duplicate keys in the map");
60 
65  template<class T>
66  struct at
67  {
68  enum {index = variadic::find<T, typename Args::first_type ... >::type::value };
69 
70  static_assert(index < sizeof...(Args),"Key T not present in the map");
71 
72  typedef typename variadic::at<index,Args...>::type::second_type type;
73  };
74 
75  enum
76  {
77  size = sizeof ... (Args)
78  };
79 
80  enum
81  {
82  empty = (size == 0)
83  };
84 
89  template<class Key>
90  using count = std::integral_constant<
91  std::size_t,
92  (variadic::find<Key, typename Args::first_type ... >::type::value == size ? 0 : 1)>;
93 
98  template<class Key,class Value>
99  struct insert : std::conditional<
100  count<Key>::type::value == 0,
101  map<Args..., std::pair<Key,Value> >,
102  map<Args...> > {};
103 
108  template<class ... KeyValuePairs>
109  struct insert_many
110  {
111  static_assert( variadic::all_of< is_pair, KeyValuePairs...>::type::value, "All the arguments of insert_many must be key/value pairs");
112  template<class Map,class T> using insert_one_t = typename Map::template insert<typename T::first_type, typename T::second_type>;
113 
114  typedef typename variadic::left_fold<insert_one_t,map,KeyValuePairs...>::type type;
115  };
116 
121  template<class Key>
122  class erase
123  {
124  template<class T> using key_comparer = std::is_same<typename T::first_type,Key>;
125 
126  public:
127  typedef typename variadic::remove_if< key_comparer, tinympl::map, Args...>::type type;
128  };
129 };
130 
133 template<class ... As,class ... Bs>
134 struct equal_to< map<As...>, map<Bs...> > : unordered_equal< sequence<As...>, sequence<Bs...> > {};
135 
136 }
137 
138 #endif // TINYMPL_MAP_HPP
Determines whether every element in the sequence satisfies the given predicate.
Definition: all_of.hpp:31
Calls insert many times to insert many Key/Value pairs.
Definition: map.hpp:109
Extract the i-th element of a variadic template.
Definition: at.hpp:27
Determines whether it is possible to reorder the sequence A to match exactly the sequence B ...
Definition: unordered_equal.hpp:40
Return a new map constructed by the current map removing the Key key, if present, otherwise return th...
Definition: map.hpp:122
Remove the elements from the input sequence if they satisfy a given predicate.
Definition: remove_if.hpp:39
Collapses a sequence starting from left using a functor.
Definition: left_fold.hpp:28
Determines whether the types A and B are equal.
Definition: equal_to.hpp:27
Get the number of elements of a sequence.
Definition: size.hpp:27
Compute the index of the first element in the sequence which is equal to the given type T...
Definition: find.hpp:38
std::integral_constant< std::size_t,(variadic::find< Key, typename Args::first_type... >::type::value==size?0:1)> count
Count the number of elements in the map with a given key.
Definition: map.hpp:92
Returns a new map with the new Key/Value pair, or this map if the key is already present in the map...
Definition: map.hpp:99
Return the value element with the given key.
Definition: map.hpp:66
A compile time map from a type to another This class represents a compile time mapping between types...
Definition: map.hpp:56
Determines whether the input sequence contains only unique elements.
Definition: is_unique.hpp:35
Determine whether a type is an std::pair
Definition: map.hpp:33
Determines whether the map is empty.
Definition: map.hpp:82