OpenTTD Source: src/core/multimap.hpp Source File

OpenTTD Source 15.0-RC2

multimap.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef MULTIMAP_HPP
11#define MULTIMAP_HPP
12
13template <typename Tkey, typename Tvalue, typename Tcompare>
14class MultiMap;
15
24template <class Tmap_iter, class Tlist_iter, class Tkey, class Tvalue, class Tcompare>
25 class MultiMapIterator {
26protected:
27 friend class MultiMap<Tkey, Tvalue, Tcompare>;
28 typedef MultiMapIterator<Tmap_iter, Tlist_iter, Tkey, Tvalue, Tcompare> Self;
29
30 Tlist_iter list_iter;
31 Tmap_iter map_iter;
32
42 bool list_valid;
43
44public:
48 MultiMapIterator() : list_valid(false) {}
49
56 template <class Tnon_const>
57 MultiMapIterator(Tnon_const mi) : map_iter(mi), list_valid(false) {}
58
66 MultiMapIterator(Tmap_iter mi, Tlist_iter li) : list_iter(li), map_iter(mi)
67 {
68 this->list_valid = (this->list_iter != this->map_iter->second.begin());
69 }
70
77 template <class Tnon_const>
78 Self &operator=(Tnon_const mi)
79 {
80 this->map_iter = mi;
81 this->list_valid = false;
82 return *this;
83 }
84
90 Tvalue &operator*() const
91 {
92 assert(!this->map_iter->second.empty());
93 return this->list_valid ?
94 this->list_iter.operator*() :
95 this->map_iter->second.begin().operator*();
96 }
97
102 Tvalue *operator->() const
103 {
104 assert(!this->map_iter->second.empty());
105 return this->list_valid ?
106 this->list_iter.operator->() :
107 this->map_iter->second.begin().operator->();
108 }
109
110 inline const Tmap_iter &GetMapIter() const { return this->map_iter; }
111 inline const Tlist_iter &GetListIter() const { return this->list_iter; }
112 inline bool ListValid() const { return this->list_valid; }
113
114 const Tkey &GetKey() const { return this->map_iter->first; }
115
122 Self &operator++()
123 {
124 assert(!this->map_iter->second.empty());
125 if (this->list_valid) {
126 if (++this->list_iter == this->map_iter->second.end()) {
127 ++this->map_iter;
128 this->list_valid = false;
129 }
130 } else {
131 this->list_iter = ++(this->map_iter->second.begin());
132 if (this->list_iter == this->map_iter->second.end()) {
133 ++this->map_iter;
134 } else {
135 this->list_valid = true;
136 }
137 }
138 return *this;
139 }
140
147 Self operator++(int)
148 {
149 Self tmp = *this;
150 this->operator++();
151 return tmp;
152 }
153
159 Self &operator--()
160 {
161 assert(!this->map_iter->second.empty());
162 if (!this->list_valid) {
163 --this->map_iter;
164 this->list_iter = this->map_iter->second.end();
165 assert(!this->map_iter->second.empty());
166 }
167
168 this->list_valid = (--this->list_iter != this->map_iter->second.begin());
169 return *this;
170 }
171
178 Self operator--(int)
179 {
180 Self tmp = *this;
181 this->operator--();
182 return tmp;
183 }
194 template <class Tmap_iter_other, class Tlist_iter_other, class Tvalue_other>
195 bool operator==(const MultiMapIterator<Tmap_iter_other, Tlist_iter_other, Tkey, Tvalue_other, Tcompare> &other) const
196 {
197 if (this->GetMapIter() != other.GetMapIter()) return false;
198 if (!this->ListValid()) return !other.ListValid();
199 return other.ListValid() ?
200 this->GetListIter() == other.GetListIter() : false;
201 }
202
210 template <class Titer>
211 bool operator==(const Titer &iter) const
212 {
213 return !this->ListValid() && this->GetMapIter() == iter;
214 }
215};
216
224template <typename Tkey, typename Tvalue, typename Tcompare = std::less<Tkey> >
225 class MultiMap : public std::map<Tkey, std::list<Tvalue>, Tcompare > {
226public:
227 typedef typename std::list<Tvalue> List;
228 typedef typename List::iterator ListIterator;
229 typedef typename List::const_iterator ConstListIterator;
230
231 typedef typename std::map<Tkey, List, Tcompare > Map;
232 typedef typename Map::iterator MapIterator;
233 typedef typename Map::const_iterator ConstMapIterator;
234
235 typedef MultiMapIterator<MapIterator, ListIterator, Tkey, Tvalue, Tcompare> iterator;
236 typedef MultiMapIterator<ConstMapIterator, ConstListIterator, Tkey, const Tvalue, Tcompare> const_iterator;
237
243 iterator erase(iterator it)
244 {
245 List &list = it.map_iter->second;
246 assert(!list.empty());
247 if (it.list_valid) {
248 it.list_iter = list.erase(it.list_iter);
249 /* This can't be the first list element as otherwise list_valid would have
250 * to be false. So the list cannot be empty here. */
251 if (it.list_iter == list.end()) {
252 ++it.map_iter;
253 it.list_valid = false;
254 }
255 } else {
256 list.erase(list.begin());
257 if (list.empty()) this->Map::erase(it.map_iter++);
258 }
259 return it;
260 }
261
267 void Insert(const Tkey &key, const Tvalue &val)
268 {
269 List &list = (*this)[key];
270 list.push_back(val);
271 assert(!list.empty());
272 }
273
278 size_t size() const
279 {
280 size_t ret = 0;
281 for (ConstMapIterator it = this->Map::begin(); it != this->Map::end(); ++it) {
282 ret += it->second.size();
283 }
284 return ret;
285 }
286
291 size_t MapSize() const
292 {
293 return this->Map::size();
294 }
295
301 std::pair<iterator, iterator> equal_range(const Tkey &key)
302 {
303 MapIterator begin(this->lower_bound(key));
304 if (begin != this->Map::end() && begin->first == key) {
305 MapIterator end = begin;
306 return std::make_pair(begin, ++end);
307 }
308 return std::make_pair(begin, begin);
309 }
310
316 std::pair<const_iterator, const_iterator> equal_range(const Tkey &key) const
317 {
318 ConstMapIterator begin(this->lower_bound(key));
319 if (begin != this->Map::end() && begin->first == key) {
320 ConstMapIterator end = begin;
321 return std::make_pair(begin, ++end);
322 }
323 return std::make_pair(begin, begin);
324 }
325};
326
327#endif /* MULTIMAP_HPP */
MultiMapIterator
STL-style iterator for MultiMap.
Definition multimap.hpp:25
MultiMapIterator::list_valid
bool list_valid
Flag to show that the iterator has just "walked" a step in the map.
Definition multimap.hpp:42
MultiMapIterator::map_iter
Tmap_iter map_iter
Iterator pointing to the position of the current list of items with equal keys in the map.
Definition multimap.hpp:31
MultiMapIterator::operator++
Self & operator++()
Prefix increment operator.
Definition multimap.hpp:122
MultiMapIterator::MultiMapIterator
MultiMapIterator(Tnon_const mi)
Constructor to allow possibly const iterators to be assigned from possibly non-const map iterators.
Definition multimap.hpp:57
MultiMapIterator::MultiMapIterator
MultiMapIterator()
Simple, dangerous constructor to allow later assignment with operator=.
Definition multimap.hpp:48
MultiMapIterator::operator=
Self & operator=(Tnon_const mi)
Assignment iterator like constructor with the same signature.
Definition multimap.hpp:78
MultiMapIterator::MultiMapIterator
MultiMapIterator(Tmap_iter mi, Tlist_iter li)
Constructor to allow specifying an exact position in map and list.
Definition multimap.hpp:66
MultiMapIterator::operator==
bool operator==(const MultiMapIterator< Tmap_iter_other, Tlist_iter_other, Tkey, Tvalue_other, Tcompare > &other) const
Compare two MultiMap iterators.
Definition multimap.hpp:195
MultiMapIterator::operator==
bool operator==(const Titer &iter) const
Check if a MultiMap iterator is at the begin of a list pointed to by the given map iterator.
Definition multimap.hpp:211
MultiMapIterator::operator--
Self & operator--()
Prefix decrement operator.
Definition multimap.hpp:159
MultiMapIterator::list_iter
Tlist_iter list_iter
Iterator pointing to current position in the current list of items with equal keys.
Definition multimap.hpp:30
MultiMapIterator::operator->
Tvalue * operator->() const
Same as operator*(), but returns a pointer.
Definition multimap.hpp:102
MultiMapIterator::operator++
Self operator++(int)
Postfix increment operator.
Definition multimap.hpp:147
MultiMapIterator::operator*
Tvalue & operator*() const
Dereference operator.
Definition multimap.hpp:90
MultiMapIterator::operator--
Self operator--(int)
Postfix decrement operator.
Definition multimap.hpp:178
MultiMap
Hand-rolled multimap as map of lists.
Definition multimap.hpp:225
MultiMap::equal_range
std::pair< const_iterator, const_iterator > equal_range(const Tkey &key) const
Get a pair of constant iterators specifying a range of items with equal keys.
Definition multimap.hpp:316
MultiMap::MapSize
size_t MapSize() const
Count the number of ranges with equal keys in this MultiMap.
Definition multimap.hpp:291
MultiMap::size
size_t size() const
Count all items in this MultiMap.
Definition multimap.hpp:278
MultiMap::erase
iterator erase(iterator it)
Erase the value pointed to by an iterator.
Definition multimap.hpp:243
MultiMap::equal_range
std::pair< iterator, iterator > equal_range(const Tkey &key)
Get a pair of iterators specifying a range of items with equal keys.
Definition multimap.hpp:301
MultiMap::Insert
void Insert(const Tkey &key, const Tvalue &val)
Insert a value at the end of the range with the specified key.
Definition multimap.hpp:267
Map::size
static uint size
The number of tiles on the map.
Definition map_func.h:239