Apache Mesos
multihashmap.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_MULTIHASHMAP_HPP__
14 #define __STOUT_MULTIHASHMAP_HPP__
15 
16 #include <algorithm> // For find.
17 #include <list>
18 #include <map>
19 #include <set>
20 #include <unordered_map>
21 #include <utility>
22 
23 #include <stout/foreach.hpp>
24 
25 // Implementation of a hash multimap via 'std::unordered_multimap'
26 // but with a better interface. The rationale for creating this is
27 // that the std::unordered_multimap interface is painful to use
28 // (requires lots of iterator garbage, as well as the use of
29 // 'equal_range' which makes for cluttered code).
30 template <typename Key,
31  typename Value,
32  typename Hash = std::hash<Key>,
33  typename Equal = std::equal_to<Key>>
34 class multihashmap : public std::unordered_multimap<Key, Value, Hash, Equal>
35 {
36 public:
38  multihashmap(const std::multimap<Key, Value>& multimap);
39  multihashmap(std::multimap<Key, Value>&& multimap);
40  multihashmap(std::initializer_list<std::pair<const Key, Value>> list);
41 
42  void put(const Key& key, const Value& value);
43  std::list<Value> get(const Key& key) const;
44  std::set<Key> keys() const;
45  bool remove(const Key& key);
46  bool remove(const Key& key, const Value& value);
47  bool contains(const Key& key) const;
48  bool contains(const Key& key, const Value& value) const;
49 };
50 
51 
52 template <typename Key, typename Value, typename Hash, typename Equal>
54  const std::multimap<Key, Value>& multimap)
55 {
56  std::unordered_multimap<Key, Value, Hash, Equal>::reserve(multimap.size());
57 
58  foreachpair (const Key& key, const Value& value, multimap) {
59  std::unordered_multimap<Key, Value, Hash, Equal>::emplace(key, value);
60  }
61 }
62 
63 
64 template <typename Key, typename Value, typename Hash, typename Equal>
66  std::multimap<Key, Value>&& multimap)
67 {
68  std::unordered_multimap<Key, Value, Hash, Equal>::insert(
69  std::make_move_iterator(multimap.begin()),
70  std::make_move_iterator(multimap.end()));
71 }
72 
73 
74 template <typename Key, typename Value, typename Hash, typename Equal>
76  std::initializer_list<std::pair<const Key, Value>> list)
77  : std::unordered_multimap<Key, Value, Hash, Equal>(list) {}
78 
79 
80 template <typename Key, typename Value, typename Hash, typename Equal>
82  const Key& key,
83  const Value& value)
84 {
85  std::unordered_multimap<Key, Value, Hash, Equal>::insert({key, value});
86 }
87 
88 
89 template <typename Key, typename Value, typename Hash, typename Equal>
91  const Key& key) const
92 {
93  std::list<Value> values; // Values to return.
94 
95  auto range =
96  std::unordered_multimap<Key, Value, Hash, Equal>::equal_range(key);
97 
98  for (auto i = range.first; i != range.second; ++i) {
99  values.push_back(i->second);
100  }
101 
102  return values;
103 }
104 
105 
106 template <typename Key, typename Value, typename Hash, typename Equal>
108 {
109  std::set<Key> keys;
110  foreachkey (const Key& key, *this) {
111  keys.insert(key);
112  }
113  return keys;
114 }
115 
116 
117 template <typename Key, typename Value, typename Hash, typename Equal>
119 {
120  return std::unordered_multimap<Key, Value, Hash, Equal>::erase(key) > 0;
121 }
122 
123 
124 template <typename Key, typename Value, typename Hash, typename Equal>
126  const Key& key,
127  const Value& value)
128 {
129  auto range =
130  std::unordered_multimap<Key, Value, Hash, Equal>::equal_range(key);
131 
132  for (auto i = range.first; i != range.second; ++i) {
133  if (i->second == value) {
134  std::unordered_multimap<Key, Value, Hash, Equal>::erase(i);
135  return true;
136  }
137  }
138 
139  return false;
140 }
141 
142 
143 template <typename Key, typename Value, typename Hash, typename Equal>
145 {
147 }
148 
149 
150 template <typename Key, typename Value, typename Hash, typename Equal>
152  const Key& key,
153  const Value& value) const
154 {
155  const std::list<Value> values = get(key);
156  return std::find(values.begin(), values.end(), value) != values.end();
157 }
158 
159 #endif // __STOUT_MULTIHASHMAP_HPP__
bool contains(const Key &key) const
Definition: multihashmap.hpp:144
Definition: type_utils.hpp:619
multihashmap()
Definition: multihashmap.hpp:37
void put(const Key &key, const Value &value)
Definition: multihashmap.hpp:81
Definition: multihashmap.hpp:34
#define foreachpair(KEY, VALUE, ELEMS)
Definition: foreach.hpp:51
Try< std::vector< Entry > > list(const std::string &hierarchy, const std::string &cgroup)
std::list< Value > get(const Key &key) const
Definition: multihashmap.hpp:90
std::set< Key > keys() const
Definition: multihashmap.hpp:107
#define foreachkey(KEY, ELEMS)
Definition: foreach.hpp:74
bool remove(const Key &key)
Definition: multihashmap.hpp:118
Try< std::list< std::string > > find(const std::string &directory, const std::string &pattern)
Definition: find.hpp:37