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 <map>
19 #include <unordered_map>
20 #include <utility>
21 #include <vector>
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, 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, std::move(value)));
109  }
110 
111  // Inserts a key, value pair into the map replacing an old value
112  // if the key is already present.
113  void put(const Key& key, const Value& value)
114  {
115  std::unordered_map<Key, Value, Hash, Equal>::erase(key);
116  std::unordered_map<Key, Value, Hash, Equal>::insert(
117  std::pair<Key, Value>(key, value));
118  }
119 
120  // Returns an Option for the binding to the key.
121  Option<Value> get(const Key& key) const
122  {
124  if (it == std::unordered_map<Key, Value, Hash, Equal>::end()) {
125  return None();
126  }
127  return it->second;
128  }
129 
130  // Returns the set of keys in this map.
131  // TODO(vinod/bmahler): Should return a list instead.
133  {
134  hashset<Key> result;
135  foreachkey (const Key& key, *this) {
136  result.insert(key);
137  }
138  return result;
139  }
140 
141  // Returns the list of values in this map.
142  std::vector<Value> values() const
143  {
144  std::vector<Value> result;
146 
147  foreachvalue (const Value& value, *this) {
148  result.push_back(value);
149  }
150 
151  return result;
152  }
153 };
154 
155 
156 template <typename K, typename V>
157 std::ostream& operator<<(std::ostream& stream, const hashmap<K, V>& map)
158 {
159  return stream << stringify(map);
160 }
161 
162 #endif // __STOUT_HASHMAP_HPP__
Definition: option.hpp:28
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:121
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
hashmap(std::initializer_list< std::pair< Key, Value >> list)
Definition: hashmap.hpp:74
void put(const Key &key, const Value &value)
Definition: hashmap.hpp:113
#define foreachvalue(VALUE, ELEMS)
Definition: foreach.hpp:77
Try< std::vector< Entry > > list(const std::string &hierarchy, const std::string &cgroup)
void put(const Key &key, Value &&value)
Definition: hashmap.hpp:104
Iterable< V > map(F &&f, const Iterable< U, Us... > &input)
Definition: lambda.hpp:46
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::string stringify(int flags)
hashset< Key > keys() const
Definition: hashmap.hpp:132
bool contains(const Key &key) const
Definition: hashmap.hpp:86
std::vector< Value > values() const
Definition: hashmap.hpp:142
Definition: hashset.hpp:31
Try< std::list< std::string > > find(const std::string &directory, const std::string &pattern)
Definition: find.hpp:37