ScummVM API documentation
multimap.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_MULTIMAP_H
23#define COMMON_MULTIMAP_H
24
25#include "common/util.h"
26#include "common/rb_tree.h"
27
28namespace Common {
29
33template<class Key, class Val, class CompFunc = Common::Less<Key> >
34class MultiMap {
35 using TreeT = RBTree<Pair<Key, Val>, Key, PairFirst<Key, Val>, CompFunc>;
36
37public:
39 using iterator = typename TreeT::BasicIterator;
40 using const_iterator = typename TreeT::ConstIterator;
41
45 void clear() {
46 _items.clear();
47 }
48
52 iterator begin() {
53 return _items.begin();
54 }
55
59 iterator end() {
60 return _items.end();
61 }
62
66 const_iterator begin() const {
67 return _items.begin();
68 }
69
73 const_iterator end() const {
74 return _items.end();
75 }
76
81 const_iterator lower_bound(const Key &key) const {
82 return _items.lowerBound(key);
83 }
84
85 iterator lower_bound(const Key &key) {
86 return _items.lowerBound(key);
87 }
88
89 iterator upper_bound(const Key &key) {
90 return _items.upperBound(key);
91 }
92
96 iterator find(const Key &theKey) {
97 iterator it = _items.lowerBound(theKey);
98 if (it != this->end() && compareEqual(it->first, theKey))
99 return it;
100 return this->end();
101 }
102
103 const_iterator find(const Key &theKey) const {
104 const_iterator it = _items.lowerBound(theKey);
105 if (it != this->end() && compareEqual(it->first, theKey))
106 return it;
107 return this->end();
108 }
109
113 iterator erase(iterator it) {
114 return _items.erase(it);
115 }
116
117 iterator erase(iterator first, iterator last) {
118 return _items.erase(first, last);
119 }
120
121 iterator erase(const Key &theKey) {
122 const iterator first = find(theKey);
123 iterator last = first;
124 while (last != this->end() && compareEqual(last->first, theKey))
125 ++last;
126 return _items.erase(first, last);
127 }
128
129 iterator insert(const value_type &val) {
130 return _items.insert(val);
131 }
132
136 size_t size() const {
137 return _items.size();
138 }
139
140 bool empty() const {
141 return _items.isEmpty();
142 }
143
147 size_t count(const Key &theKey) {
148 int count_ = 0;
149 for (iterator it = this->begin(); it != this->end(); ++it) {
150 if (compareEqual(it->first, theKey))
151 ++count_;
152 }
153 return count_;
154 }
155
156private:
157 bool compareEqual(const Key &a, const Key &b) {
158 return !_comp(a, b) && !_comp(b, a);
159 }
160
161 TreeT _items;
162 CompFunc _comp;
163};
164
165} // End of namespace Common
166
167#endif
Definition: multimap.h:34
const_iterator end() const
Definition: multimap.h:73
iterator end()
Definition: multimap.h:59
size_t size() const
Definition: multimap.h:136
const_iterator lower_bound(const Key &key) const
Definition: multimap.h:81
iterator erase(iterator it)
Definition: multimap.h:113
iterator find(const Key &theKey)
Definition: multimap.h:96
void clear()
Definition: multimap.h:45
size_t count(const Key &theKey)
Definition: multimap.h:147
const_iterator begin() const
Definition: multimap.h:66
iterator begin()
Definition: multimap.h:52
Definition: rb_tree.h:60
Iterator< ValueType &, ValueType * > BasicIterator
Definition: rb_tree.h:122
void clear()
Definition: rb_tree.h:158
BasicIterator upperBound(const Key &key)
Definition: rb_tree.h:217
BasicIterator erase(BasicIterator it)
Definition: rb_tree.h:250
BasicIterator lowerBound(const Key &key)
Definition: rb_tree.h:181
BasicIterator end()
Definition: rb_tree.h:172
bool isEmpty() const
Definition: rb_tree.h:336
BasicIterator begin()
Definition: rb_tree.h:166
BasicIterator insert(const ValueType &val)
Definition: rb_tree.h:302
size_t size() const
Definition: rb_tree.h:327
Iterator< const ValueType &, const ValueType * > ConstIterator
Definition: rb_tree.h:123
Definition: algorithm.h:29
Definition: rb_tree.h:32
Definition: util.h:178