18 template<
typename Tkey,
typename Tvalue,
typename Tcompare>
29 template<
class Tmap_iter,
class Tlist_iter,
class Tkey,
class Tvalue,
class Tcompare>
32 friend class MultiMap<Tkey, Tvalue, Tcompare>;
61 template<
class Tnon_const>
73 this->list_valid = (this->list_iter != this->map_iter->second.begin());
82 template<
class Tnon_const>
86 this->list_valid =
false;
97 assert(!this->map_iter->second.empty());
98 return this->list_valid ?
99 this->list_iter.operator*() :
100 this->map_iter->second.begin().operator*();
109 assert(!this->map_iter->second.empty());
110 return this->list_valid ?
111 this->list_iter.operator->() :
112 this->map_iter->second.begin().operator->();
115 inline const Tmap_iter &GetMapIter()
const {
return this->
map_iter; }
116 inline const Tlist_iter &GetListIter()
const {
return this->
list_iter; }
117 inline bool ListValid()
const {
return this->
list_valid; }
119 const Tkey &GetKey()
const {
return this->map_iter->first; }
129 assert(!this->map_iter->second.empty());
130 if (this->list_valid) {
131 if (++this->list_iter == this->map_iter->second.end()) {
133 this->list_valid =
false;
136 this->list_iter = ++(this->map_iter->second.begin());
137 if (this->list_iter == this->map_iter->second.end()) {
140 this->list_valid =
true;
166 assert(!this->map_iter->second.empty());
167 if (!this->list_valid) {
169 this->list_iter = this->map_iter->second.end();
170 assert(!this->map_iter->second.empty());
173 this->list_valid = (--this->list_iter != this->map_iter->second.begin());
204 template<
class Tmap_iter1,
class Tlist_iter1,
class Tmap_iter2,
class Tlist_iter2,
class Tkey,
class Tvalue1,
class Tvalue2,
class Tcompare>
207 if (iter1.GetMapIter() != iter2.GetMapIter())
return false;
208 if (!iter1.ListValid())
return !iter2.ListValid();
209 return iter2.ListValid() ?
210 iter1.GetListIter() == iter2.GetListIter() :
false;
221 template<
class Tmap_iter1,
class Tlist_iter1,
class Tmap_iter2,
class Tlist_iter2,
class Tkey,
class Tvalue1,
class Tvalue2,
class Tcompare>
224 return !(iter1 == iter2);
235 template<
class Tmap_iter1,
class Tlist_iter1,
class Tmap_iter2,
class Tkey,
class Tvalue,
class Tcompare >
238 return !iter1.ListValid() && iter1.GetMapIter() == iter2;
247 template<
class Tmap_iter1,
class Tlist_iter1,
class Tmap_iter2,
class Tkey,
class Tvalue,
class Tcompare >
250 return iter1.ListValid() || iter1.GetMapIter() != iter2;
259 template<
class Tmap_iter1,
class Tlist_iter1,
class Tmap_iter2,
class Tkey,
class Tvalue,
class Tcompare >
262 return !iter1.ListValid() && iter1.GetMapIter() == iter2;
271 template<
class Tmap_iter1,
class Tlist_iter1,
class Tmap_iter2,
class Tkey,
class Tvalue,
class Tcompare >
274 return iter1.ListValid() || iter1.GetMapIter() != iter2;
285 template<
typename Tkey,
typename Tvalue,
typename Tcompare = std::less<Tkey> >
286 class MultiMap :
public std::map<Tkey, std::list<Tvalue>, Tcompare > {
288 typedef typename std::list<Tvalue> List;
289 typedef typename List::iterator ListIterator;
290 typedef typename List::const_iterator ConstListIterator;
292 typedef typename std::map<Tkey, List, Tcompare > Map;
293 typedef typename Map::iterator MapIterator;
294 typedef typename Map::const_iterator ConstMapIterator;
307 assert(!list.empty());
317 list.erase(list.begin());
318 if (list.empty()) this->Map::erase(it.
map_iter++);
328 void Insert(
const Tkey &key,
const Tvalue &val)
330 List &list = (*this)[key];
332 assert(!list.empty());
342 for (ConstMapIterator it = this->Map::begin(); it != this->Map::end(); ++it) {
343 ret += it->second.size();
354 return this->Map::size();
364 MapIterator begin(this->lower_bound(key));
365 if (begin != this->Map::end() && begin->first == key) {
366 MapIterator end = begin;
367 return std::make_pair(begin, ++end);
369 return std::make_pair(begin, begin);
377 std::pair<const_iterator, const_iterator>
equal_range(
const Tkey &key)
const 379 ConstMapIterator begin(this->lower_bound(key));
380 if (begin != this->Map::end() && begin->first == key) {
381 ConstMapIterator end = begin;
382 return std::make_pair(begin, ++end);
384 return std::make_pair(begin, begin);
Self & operator++()
Prefix increment operator.
STL-style iterator for MultiMap.
Self & operator=(Tnon_const mi)
Assignment iterator like constructor with the same signature.
Self & operator--()
Prefix decrement operator.
size_t size() const
Count all items in this MultiMap.
MultiMapIterator(Tnon_const mi)
Constructor to allow possibly const iterators to be assigned from possibly non-const map iterators...
bool list_valid
Flag to show that the iterator has just "walked" a step in the map.
Hand-rolled multimap as map of lists.
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==().
MultiMapIterator(Tmap_iter mi, Tlist_iter li)
Constructor to allow specifying an exact position in map and list.
Tvalue * operator->() const
Same as operator*(), but returns a pointer.
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.
std::pair< iterator, iterator > equal_range(const Tkey &key)
Get a pair of iterators specifying a range of items with equal keys.
Self operator--(int)
Postfix decrement operator.
Tmap_iter map_iter
Iterator pointing to the position of the current list of items with equal keys in the map...
iterator erase(iterator it)
Erase the value pointed to by an iterator.
size_t MapSize() const
Count the number of ranges with equal keys in this MultiMap.
Tlist_iter list_iter
Iterator pointing to current position in the current list of items with equal keys.
Self operator++(int)
Postfix increment operator.
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.
MultiMapIterator()
Simple, dangerous constructor to allow later assignment with operator=.
Tvalue & operator*() const
Dereference operator.
void Insert(const Tkey &key, const Tvalue &val)
Insert a value at the end of the range with the specified key.