ScummVM API documentation
stablemap.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_STABLEMAP_H
23 #define COMMON_STABLEMAP_H
24 
25 #include "common/rb_tree.h"
26 
27 namespace Common {
28 
42 template<class Key, class Val, class CompFunc = Common::Less<Key> >
43 class StableMap {
44  using TreeT = RBTree<Pair<Key, Val>, Key, PairFirst<Key, Val>, CompFunc>;
45 
46 public:
48  using iterator = typename TreeT::BasicIterator;
52  void clear() {
53  _items.clear();
54  }
55 
58  return _items.begin();
59  }
60 
63  return _items.end();
64  }
65 
68  return _items.begin();
69  }
70 
72  const_iterator end() const {
73  return _items.end();
74  }
75 
80  const_iterator lower_bound(const Key &key) const {
81  return _items.lowerBound(key);
82  }
83 
88  iterator lower_bound(const Key &key) {
89  return _items.lowerBound(key);
90  }
91 
96  iterator upper_bound(const Key &key) {
97  return _items.upperBound(key);
98  }
99 
104  iterator find(const Key &theKey) {
105  iterator it = _items.lowerBound(theKey);
106  if (it != this->end() && compareEqual(it->first, theKey))
107  return it;
108  return this->end();
109  }
110 
115  const_iterator find(const Key &theKey) const {
116  const_iterator it = _items.lowerBound(theKey);
117  if (it != this->end() && compareEqual(it->first, theKey))
118  return it;
119  return this->end();
120  }
121 
125  Val &operator[](const Key &theKey) {
126  iterator it = _items.lowerBound(theKey);
127  if (it == this->end() || !compareEqual(it->first, theKey)) {
128  iterator i = _items.insert(value_type(theKey, Val()));
129  return i->second;
130  }
131  return it->second;
132  }
133 
136  return _items.erase(it);
137  }
138 
145  return _items.erase(first, last);
146  }
147 
152  size_t erase(const Key &theKey) {
153  iterator it = find(theKey);
154  if (it != this->end()) {
155  erase(it);
156  return 1;
157  }
158  return 0;
159  }
160 
167  iterator it = _items.lowerBound(val.first);
168  if (it == this->end() || !compareEqual(it->first, val.first))
169  return {_items.insert(val), true};
170  return {it, false};
171  }
172 
174  size_t size() const {
175  return _items.size();
176  }
177 
185  bool empty() const {
186  return _items.isEmpty();
187  }
188 
192  size_t count(const Key &key) {
193  int count_ = 0;
194  for (iterator it = this->begin(); it != this->end(); ++it) {
195  if (compareEqual(it->first, key))
196  ++count_;
197  }
198  return count_;
199  }
200 
201 private:
202  bool compareEqual(const Key &a, const Key &b) const {
203  return !_comp(a, b) && !_comp(b, a);
204  }
205 
206  TreeT _items;
207  CompFunc _comp;
208 };
209 
212 } // End of namespace Common
213 
214 #endif
iterator begin()
Definition: stablemap.h:57
Definition: util.h:213
size_t size() const
Definition: stablemap.h:174
iterator erase(iterator first, iterator last)
Definition: stablemap.h:144
Pair< Key, Val > value_type
Definition: stablemap.h:47
const_iterator end() const
Definition: stablemap.h:72
bool empty() const
Definition: stablemap.h:185
size_t erase(const Key &theKey)
Definition: stablemap.h:152
bool isEmpty() const
Definition: rb_tree.h:336
BasicIterator upperBound(const Key &key)
Definition: rb_tree.h:217
size_t count(const Key &key)
Definition: stablemap.h:192
BasicIterator lowerBound(const Key &key)
Definition: rb_tree.h:181
BasicIterator insert(const ValueType &val)
Definition: rb_tree.h:302
const_iterator begin() const
Definition: stablemap.h:67
typename TreeT::BasicIterator iterator
Definition: stablemap.h:48
Definition: stablemap.h:43
Pair< iterator, bool > insert(const value_type &val)
Definition: stablemap.h:166
iterator end()
Definition: stablemap.h:62
iterator find(const Key &theKey)
Definition: stablemap.h:104
iterator lower_bound(const Key &key)
Definition: stablemap.h:88
const_iterator find(const Key &theKey) const
Definition: stablemap.h:115
Definition: algorithm.h:29
Iterator< const Pair< Key, Val > &, const Pair< Key, Val > *> ConstIterator
Definition: rb_tree.h:123
Iterator< Pair< Key, Val > &, Pair< Key, Val > *> BasicIterator
Definition: rb_tree.h:122
Definition: rb_tree.h:32
const_iterator lower_bound(const Key &key) const
Definition: stablemap.h:80
iterator erase(iterator it)
Definition: stablemap.h:135
iterator upper_bound(const Key &key)
Definition: stablemap.h:96
BasicIterator erase(BasicIterator it)
Definition: rb_tree.h:250
void clear()
Definition: rb_tree.h:158
void clear()
Definition: stablemap.h:52
size_t size() const
Definition: rb_tree.h:327
Val & operator[](const Key &theKey)
Definition: stablemap.h:125
typename TreeT::ConstIterator const_iterator
Definition: stablemap.h:49
BasicIterator begin()
Definition: rb_tree.h:166
BasicIterator end()
Definition: rb_tree.h:172