13 template<
typename Tkey,
typename Tvalue,
typename Tcompare>
24 template<
class Tmap_iter,
class Tlist_iter,
class Tkey,
class Tvalue,
class Tcompare>
27 friend class MultiMap<Tkey, Tvalue, Tcompare>;
56 template<
class Tnon_const>
68 this->list_valid = (this->list_iter != this->map_iter->second.begin());
77 template<
class Tnon_const>
81 this->list_valid =
false;
92 assert(!this->map_iter->second.empty());
93 return this->list_valid ?
94 this->list_iter.operator*() :
95 this->map_iter->second.begin().operator*();
104 assert(!this->map_iter->second.empty());
105 return this->list_valid ?
106 this->list_iter.operator->() :
107 this->map_iter->second.begin().operator->();
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; }
114 const Tkey &GetKey()
const {
return this->map_iter->first; }
124 assert(!this->map_iter->second.empty());
125 if (this->list_valid) {
126 if (++this->list_iter == this->map_iter->second.end()) {
128 this->list_valid =
false;
131 this->list_iter = ++(this->map_iter->second.begin());
132 if (this->list_iter == this->map_iter->second.end()) {
135 this->list_valid =
true;
161 assert(!this->map_iter->second.empty());
162 if (!this->list_valid) {
164 this->list_iter = this->map_iter->second.end();
165 assert(!this->map_iter->second.empty());
168 this->list_valid = (--this->list_iter != this->map_iter->second.begin());
199 template<
class Tmap_iter1,
class Tlist_iter1,
class Tmap_iter2,
class Tlist_iter2,
class Tkey,
class Tvalue1,
class Tvalue2,
class Tcompare>
202 if (iter1.GetMapIter() != iter2.GetMapIter())
return false;
203 if (!iter1.ListValid())
return !iter2.ListValid();
204 return iter2.ListValid() ?
205 iter1.GetListIter() == iter2.GetListIter() :
false;
216 template<
class Tmap_iter1,
class Tlist_iter1,
class Tmap_iter2,
class Tlist_iter2,
class Tkey,
class Tvalue1,
class Tvalue2,
class Tcompare>
219 return !(iter1 == iter2);
230 template<
class Tmap_iter1,
class Tlist_iter1,
class Tmap_iter2,
class Tkey,
class Tvalue,
class Tcompare >
233 return !iter1.ListValid() && iter1.GetMapIter() == iter2;
242 template<
class Tmap_iter1,
class Tlist_iter1,
class Tmap_iter2,
class Tkey,
class Tvalue,
class Tcompare >
245 return iter1.ListValid() || iter1.GetMapIter() != iter2;
254 template<
class Tmap_iter1,
class Tlist_iter1,
class Tmap_iter2,
class Tkey,
class Tvalue,
class Tcompare >
257 return !iter1.ListValid() && iter1.GetMapIter() == iter2;
266 template<
class Tmap_iter1,
class Tlist_iter1,
class Tmap_iter2,
class Tkey,
class Tvalue,
class Tcompare >
269 return iter1.ListValid() || iter1.GetMapIter() != iter2;
280 template<
typename Tkey,
typename Tvalue,
typename Tcompare = std::less<Tkey> >
281 class MultiMap :
public std::map<Tkey, std::list<Tvalue>, Tcompare > {
283 typedef typename std::list<Tvalue> List;
284 typedef typename List::iterator ListIterator;
285 typedef typename List::const_iterator ConstListIterator;
287 typedef typename std::map<Tkey, List, Tcompare > Map;
288 typedef typename Map::iterator MapIterator;
289 typedef typename Map::const_iterator ConstMapIterator;
302 assert(!list.empty());
312 list.erase(list.begin());
313 if (list.empty()) this->Map::erase(it.
map_iter++);
323 void Insert(
const Tkey &key,
const Tvalue &val)
325 List &list = (*this)[key];
327 assert(!list.empty());
337 for (ConstMapIterator it = this->Map::begin(); it != this->Map::end(); ++it) {
338 ret += it->second.size();
359 MapIterator begin(this->lower_bound(key));
360 if (begin != this->Map::end() && begin->first == key) {
361 MapIterator end = begin;
362 return std::make_pair(begin, ++end);
364 return std::make_pair(begin, begin);
372 std::pair<const_iterator, const_iterator>
equal_range(
const Tkey &key)
const
374 ConstMapIterator begin(this->lower_bound(key));
375 if (begin != this->Map::end() && begin->first == key) {
376 ConstMapIterator end = begin;
377 return std::make_pair(begin, ++end);
379 return std::make_pair(begin, begin);
STL-style iterator for MultiMap.
bool list_valid
Flag to show that the iterator has just "walked" a step in the map.
Tmap_iter map_iter
Iterator pointing to the position of the current list of items with equal keys in the map.
Tvalue & operator*() const
Dereference operator.
MultiMapIterator(Tnon_const mi)
Constructor to allow possibly const iterators to be assigned from possibly non-const map iterators.
Self & operator++()
Prefix increment operator.
MultiMapIterator()
Simple, dangerous constructor to allow later assignment with operator=.
MultiMapIterator(Tmap_iter mi, Tlist_iter li)
Constructor to allow specifying an exact position in map and list.
Self & operator--()
Prefix decrement operator.
Tvalue * operator->() const
Same as operator*(), but returns a pointer.
Tlist_iter list_iter
Iterator pointing to current position in the current list of items with equal keys.
Self operator++(int)
Postfix increment operator.
Self operator--(int)
Postfix decrement operator.
Self & operator=(Tnon_const mi)
Assignment iterator like constructor with the same signature.
Hand-rolled multimap as map of lists.
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.
size_t MapSize() const
Count the number of ranges with equal keys in this MultiMap.
size_t size() const
Count all items in this MultiMap.
iterator erase(iterator it)
Erase the value pointed to by an iterator.
std::pair< iterator, iterator > equal_range(const Tkey &key)
Get a pair of iterators specifying a range of items with equal keys.
void Insert(const Tkey &key, const Tvalue &val)
Insert a value at the end of the range with the specified key.
bool operator!=(const MultiMapIterator< Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare > &iter1, const MultiMapIterator< Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare > &iter2)
Inverse of operator==().
bool operator==(const MultiMapIterator< Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare > &iter1, const MultiMapIterator< Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare > &iter2)
Compare two MultiMap iterators.
static uint size
The number of tiles on the map.