tinympl  0.2
mini MPL library for C++11
value_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_VALUE_MAP_HPP
14 #define TINYMPL_VALUE_MAP_HPP
15 
16 #include <tinympl/algorithm.hpp>
17 #include <tinympl/map.hpp>
18 
19 namespace tinympl {
20 
33 template<class KeyType,class ValueType,class ... Args>
34 struct value_map
35 {
39  template<class T> struct is_valid_key_type : std::false_type {};
40  template<KeyType k> struct is_valid_key_type<std::integral_constant<KeyType,k>> : std::true_type {};
41 
45  template<class T> struct is_valid_value_type : std::false_type {};
46  template<ValueType k> struct is_valid_value_type<std::integral_constant<ValueType,k>> : std::true_type {};
47 
48  static_assert( variadic::all_of< is_pair, Args...>::type::value, "All the arguments of a map must be key/value pairs");
49  static_assert( variadic::is_unique<typename Args::first_type ...>::type::value,"Duplicate keys in the map");
52 
56  template<KeyType k> using key = std::integral_constant<KeyType,k>;
57 
61  template<ValueType v> using value = std::integral_constant<ValueType,v>;
62 
66  template<KeyType k,ValueType v>
67  using pair = std::pair< key<k>,value<v> >;
68 
73  template<KeyType k>
74  struct at
75  {
76  enum {index = variadic::find<key<k>, typename Args::first_type ... >::type::value };
77 
78  static_assert(index < sizeof...(Args),"Key k not present in the map");
79 
80  typedef typename variadic::at<index,Args...>::type::second_type type;
81  enum {value = type::value};
82  };
83 
84  enum
85  {
86  size = sizeof ... (Args)
87  };
88 
89  enum
90  {
91  empty = (size == 0)
92  };
93 
98  template<KeyType k>
99  using count = std::integral_constant<
100  std::size_t,
101  (variadic::find<key<k>, typename Args::first_type ... >::type::value == size ? 0 : 1)>;
102 
107  template<KeyType k,ValueType v>
108  struct insert
109  {
110  typedef typename std::conditional<
112  value_map<KeyType,ValueType,Args..., pair<k,v> >,
113  value_map<KeyType,ValueType,Args...> >::type type;
114  };
115 
120  template<class ... KeyValuePairs>
121  struct insert_many
122  {
123  static_assert( variadic::all_of< is_pair, KeyValuePairs...>::type::value, "All the arguments of insert_many must be key/value pairs");
126 
127  template<class Map,class T> using insert_one_t = typename Map::template insert<T::first_type::value, T::second_type::value>;
128 
129  typedef typename variadic::left_fold<insert_one_t,value_map,KeyValuePairs...>::type type;
130  };
131 
136  template<KeyType k>
137  class erase
138  {
139  template<class T> struct key_comparer : std::is_same<typename T::first_type,key<k> > {};
140 
141  template<class ... Ts>
142  using rebind = value_map<KeyType,ValueType,Ts...>;
143 
144  public:
145  typedef typename variadic::remove_if< key_comparer, rebind, Args...>::type type;
146  };
147 };
148 
149 template<class KeyType,class ValueType,class ... As,class ... Bs>
150 struct equal_to< value_map<KeyType,ValueType,As...>, value_map<KeyType,ValueType,Bs...> > : unordered_equal< sequence<As...>, sequence<Bs...> > {};
151 
152 }
153 
154 #endif // TINYMPL_VALUE_MAP_HPP
std::integral_constant< std::size_t,(variadic::find< key< k >, typename Args::first_type... >::type::value==size?0:1)> count
Count the number of elements in the map with a given key.
Definition: value_map.hpp:101
Determines whether every element in the sequence satisfies the given predicate.
Definition: all_of.hpp:31
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
Remove the elements from the input sequence if they satisfy a given predicate.
Definition: remove_if.hpp:39
Calls insert many times to insert many Key/Value pairs.
Definition: value_map.hpp:121
Determines whether the map is empty.
Definition: value_map.hpp:91
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
A compile time map from a compile-time value to another This class represents a compile time mapping ...
Definition: value_map.hpp:34
Determines whether T is a valid value type, i.e. std::integral_constant
Definition: value_map.hpp:45
std::integral_constant< ValueType, v > value
Convenience using-declaration to quickly define values for this value_map
Definition: value_map.hpp:61
Return a new map constructed by the current map removing the k key, if present, otherwise return the ...
Definition: value_map.hpp:137
std::integral_constant< KeyType, k > key
Convenience using-declaration to quickly define keys for this value_map
Definition: value_map.hpp:56
Return the value element with the given key.
Definition: value_map.hpp:74
Returns a new map with the new Key/Value pair, or this map if the key is already present in the map...
Definition: value_map.hpp:108
Determines whether T is a valid key type, i.e. std::integral_constant ...
Definition: value_map.hpp:39
Determines whether the input sequence contains only unique elements.
Definition: is_unique.hpp:35
std::pair< key< k >, value< v > > pair
Convenience using-declaration to quickly define a key/value pair for this value_map ...
Definition: value_map.hpp:67