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