Apache Mesos
hashmap.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_HASHMAP_HPP__
14 #define __STOUT_HASHMAP_HPP__
15 
16 #include <functional>
17 #include <iosfwd>
18 #include <list>
19 #include <map>
20 #include <unordered_map>
21 #include <utility>
22 
23 #include "foreach.hpp"
24 #include "hashset.hpp"
25 #include "none.hpp"
26 #include "option.hpp"
27 
28 // Provides a hash map via 'std::unordered_map'. We inherit from it to add
29 // new functions as well as to provide better names for some of the
30 // existing functions.
31 template <typename Key,
32  typename Value,
33  typename Hash = typename std::conditional<
34  std::is_enum<Key>::value,
36  std::hash<Key>>::type,
37  typename Equal = std::equal_to<Key>>
38 class hashmap : public std::unordered_map<Key, Value, Hash, Equal>
39 {
40 public:
41  // An explicit default constructor is needed so
42  // 'const hashmap<T> map;' is not an error.
43  hashmap() {}
44 
45  // An implicit constructor for converting from a std::map.
46  //
47  // TODO(benh): Allow any arbitrary type that supports 'begin()' and
48  // 'end()' passed into the specified 'emplace'?
49  hashmap(const std::map<Key, Value>& map)
50  {
51  std::unordered_map<Key, Value, Hash, Equal>::reserve(map.size());
52 
53  for (auto iterator = map.begin(); iterator != map.end(); ++iterator) {
54  std::unordered_map<Key, Value, Hash, Equal>::emplace(
55  iterator->first,
56  iterator->second);
57  }
58  }
59 
60  // An implicit constructor for converting from an r-value std::map.
61  //
62  // TODO(benh): Allow any arbitrary type that supports 'begin()' and
63  // 'end()' passed into the specified 'insert'?
64  hashmap(std::map<Key, Value>&& map)
65  {
66  // NOTE: We're using 'insert' here with a move iterator in order
67  // to avoid copies because we know we have an r-value paramater.
68  std::unordered_map<Key, Value, Hash, Equal>::insert(
69  std::make_move_iterator(map.begin()),
70  std::make_move_iterator(map.end()));
71  }
72 
73  // Allow simple construction via initializer list.
74  hashmap(std::initializer_list<std::pair<Key, Value>> list)
75  {
76  std::unordered_map<Key, Value, Hash, Equal>::reserve(list.size());
77 
78  for (auto iterator = list.begin(); iterator != list.end(); ++iterator) {
79  std::unordered_map<Key, Value, Hash, Equal>::emplace(
80  iterator->first,
81  iterator->second);
82  }
83  }
84 
85  // Checks whether this map contains a binding for a key.
86  bool contains(const Key& key) const
87  {
88  return std::unordered_map<Key, Value, Hash, Equal>::count(key) > 0;
89  }
90 
91  // Checks whether there exists a bound value in this map.
92  bool containsValue(const Value& v) const
93  {
94  foreachvalue (const Value& value, *this) {
95  if (value == v) {
96  return true;
97  }
98  }
99  return false;
100  }
101 
102  // Inserts a key, value pair into the map replacing an old value
103  // if the key is already present.
104  void put(const Key& key, const Value& value)
105  {
106  std::unordered_map<Key, Value, Hash, Equal>::erase(key);
107  std::unordered_map<Key, Value, Hash, Equal>::insert(
108  std::pair<Key, Value>(key, value));
109  }
110 
111  // Returns an Option for the binding to the key.
112  Option<Value> get(const Key& key) const
113  {
115  if (it == std::unordered_map<Key, Value, Hash, Equal>::end()) {
116  return None();
117  }
118  return it->second;
119  }
120 
121  // Returns the set of keys in this map.
122  // TODO(vinod/bmahler): Should return a list instead.
124  {
125  hashset<Key> result;
126  foreachkey (const Key& key, *this) {
127  result.insert(key);
128  }
129  return result;
130  }
131 
132  // Returns the list of values in this map.
133  std::list<Value> values() const
134  {
135  std::list<Value> result;
136  foreachvalue (const Value& value, *this) {
137  result.push_back(value);
138  }
139  return result;
140  }
141 };
142 
143 
144 template <typename K, typename V>
145 std::ostream& operator<<(std::ostream& stream, const hashmap<K, V>& map)
146 {
147  return stream << stringify(map);
148 }
149 
150 #endif // __STOUT_HASHMAP_HPP__
Definition: option.hpp:28
hashmap(std::map< Key, Value > &&map)
Definition: hashmap.hpp:64
hashmap(const std::map< Key, Value > &map)
Definition: hashmap.hpp:49
Definition: hashset.hpp:53
hashmap()
Definition: hashmap.hpp:43
Definition: hashmap.hpp:38
Iterable< V > map(F &&f, const Iterable< U, Us...> &input)
Definition: lambda.hpp:45
hashmap(std::initializer_list< std::pair< Key, Value >> list)
Definition: hashmap.hpp:74
void put(const Key &key, const Value &value)
Definition: hashmap.hpp:104
#define foreachvalue(VALUE, ELEMS)
Definition: foreach.hpp:77
Try< std::vector< Entry > > list(const std::string &hierarchy, const std::string &cgroup)
Definition: none.hpp:27
#define foreachkey(KEY, ELEMS)
Definition: foreach.hpp:74
Try< uint32_t > type(const std::string &path)
bool containsValue(const Value &v) const
Definition: hashmap.hpp:92
std::list< Value > values() const
Definition: hashmap.hpp:133
std::string stringify(int flags)
hashset< Key > keys() const
Definition: hashmap.hpp:123
bool contains(const Key &key) const
Definition: hashmap.hpp:86
Definition: hashset.hpp:31
Try< std::list< std::string > > find(const std::string &directory, const std::string &pattern)
Definition: find.hpp:37