1 |
786 |
skrzyp |
// This file is part of the uSTL library, an STL implementation.
|
2 |
|
|
//
|
3 |
|
|
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
|
4 |
|
|
// This file is free software, distributed under the MIT License.
|
5 |
|
|
|
6 |
|
|
#ifndef UMULTIMAP_H_45743F516E02A87A3FCEA5024052A6F5
|
7 |
|
|
#define UMULTIMAP_H_45743F516E02A87A3FCEA5024052A6F5
|
8 |
|
|
|
9 |
|
|
#include "uvector.h"
|
10 |
|
|
#include "ufunction.h"
|
11 |
|
|
|
12 |
|
|
namespace ustl {
|
13 |
|
|
|
14 |
|
|
/// \class multimap umultimap.h ustl.h
|
15 |
|
|
/// \ingroup AssociativeContainers
|
16 |
|
|
///
|
17 |
|
|
/// \brief A sorted associative container that may container multiple entries for each key.
|
18 |
|
|
///
|
19 |
|
|
template <typename K, typename V>
|
20 |
|
|
class multimap : public vector<pair<K,V> > {
|
21 |
|
|
public:
|
22 |
|
|
typedef K key_type;
|
23 |
|
|
typedef V data_type;
|
24 |
|
|
typedef const K& const_key_ref;
|
25 |
|
|
typedef const V& const_data_ref;
|
26 |
|
|
typedef const multimap<K,V>& rcself_t;
|
27 |
|
|
typedef vector<pair<K,V> > base_class;
|
28 |
|
|
typedef typename base_class::value_type value_type;
|
29 |
|
|
typedef typename base_class::size_type size_type;
|
30 |
|
|
typedef typename base_class::pointer pointer;
|
31 |
|
|
typedef typename base_class::const_pointer const_pointer;
|
32 |
|
|
typedef typename base_class::reference reference;
|
33 |
|
|
typedef typename base_class::const_reference const_reference;
|
34 |
|
|
typedef typename base_class::const_iterator const_iterator;
|
35 |
|
|
typedef typename base_class::iterator iterator;
|
36 |
|
|
typedef typename base_class::reverse_iterator reverse_iterator;
|
37 |
|
|
typedef typename base_class::const_reverse_iterator const_reverse_iterator;
|
38 |
|
|
typedef pair<const_iterator,const_iterator> const_range_t;
|
39 |
|
|
typedef pair<iterator,iterator> range_t;
|
40 |
|
|
public:
|
41 |
|
|
inline multimap (void) : vector<pair<K,V> > () {}
|
42 |
|
|
explicit inline multimap (size_type n) : vector<pair<K,V> > (n) {}
|
43 |
|
|
inline multimap (rcself_t v) : vector<pair<K,V> > (v) {}
|
44 |
|
|
inline multimap (const_iterator i1, const_iterator i2) : vector<pair<K,V> > () { insert (i1, i2); }
|
45 |
|
|
inline rcself_t operator= (rcself_t v) { base_class::operator= (v); return (*this); }
|
46 |
|
|
inline size_type size (void) const { return (base_class::size()); }
|
47 |
|
|
inline iterator begin (void) { return (base_class::begin()); }
|
48 |
|
|
inline const_iterator begin (void) const { return (base_class::begin()); }
|
49 |
|
|
inline iterator end (void) { return (base_class::end()); }
|
50 |
|
|
inline const_iterator end (void) const { return (base_class::end()); }
|
51 |
|
|
inline void assign (const_iterator i1, const_iterator i2) { clear(); insert (i1, i2); }
|
52 |
|
|
inline size_type count (const_key_ref k) const { return (upper_bound(k) - lower_bound(k)); }
|
53 |
|
|
inline void push_back (const_reference v) { insert (v); }
|
54 |
|
|
inline const_range_t equal_range (const_key_ref k) const { return (make_pair (lower_bound(k), upper_bound(k))); }
|
55 |
|
|
inline range_t equal_range (const_key_ref k) { return (make_pair (const_cast<iterator>(lower_bound(k)), const_cast<iterator>(upper_bound(k)))); }
|
56 |
|
|
const_iterator lower_bound (const_key_ref k) const;
|
57 |
|
|
const_iterator upper_bound (const_key_ref k) const;
|
58 |
|
|
inline iterator insert (const_reference v);
|
59 |
|
|
void insert (const_iterator i1, const_iterator i2);
|
60 |
|
|
inline void erase (const_key_ref k) { erase (const_cast<iterator>(lower_bound(k)), const_cast<iterator>(upper_bound(k))); }
|
61 |
|
|
inline iterator erase (iterator ep) { return (base_class::erase (ep)); }
|
62 |
|
|
inline iterator erase (iterator ep1, iterator ep2) { return (base_class::erase (ep1, ep2)); }
|
63 |
|
|
inline void clear (void) { base_class::clear(); }
|
64 |
|
|
};
|
65 |
|
|
|
66 |
|
|
/// Returns an iterator to the first element with key value \p k.
|
67 |
|
|
template <typename K, typename V>
|
68 |
|
|
typename multimap<K,V>::const_iterator multimap<K,V>::lower_bound (const_key_ref k) const
|
69 |
|
|
{
|
70 |
|
|
const_iterator first (begin()), last (end());
|
71 |
|
|
while (first != last) {
|
72 |
|
|
const_iterator mid = advance (first, distance (first,last) / 2);
|
73 |
|
|
if (mid->first < k)
|
74 |
|
|
first = advance (mid, 1);
|
75 |
|
|
else
|
76 |
|
|
last = mid;
|
77 |
|
|
}
|
78 |
|
|
return (first);
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/// Returns an iterator to the first element with key value \p k.
|
82 |
|
|
template <typename K, typename V>
|
83 |
|
|
typename multimap<K,V>::const_iterator multimap<K,V>::upper_bound (const_key_ref k) const
|
84 |
|
|
{
|
85 |
|
|
const_iterator first (begin()), last (end());
|
86 |
|
|
while (first != last) {
|
87 |
|
|
const_iterator mid = advance (first, distance (first,last) / 2);
|
88 |
|
|
if (k < mid->first)
|
89 |
|
|
last = mid;
|
90 |
|
|
else
|
91 |
|
|
first = advance (mid, 1);
|
92 |
|
|
}
|
93 |
|
|
return (last);
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
/// Inserts the pair into the container.
|
97 |
|
|
template <typename K, typename V>
|
98 |
|
|
inline typename multimap<K,V>::iterator multimap<K,V>::insert (const_reference v)
|
99 |
|
|
{
|
100 |
|
|
iterator ip = const_cast<iterator> (upper_bound (v.first));
|
101 |
|
|
return (base_class::insert (ip, v));
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
/// Inserts elements from range [i1,i2) into the container.
|
105 |
|
|
template <typename K, typename V>
|
106 |
|
|
void multimap<K,V>::insert (const_iterator i1, const_iterator i2)
|
107 |
|
|
{
|
108 |
|
|
assert (i1 <= i2);
|
109 |
|
|
reserve (size() + distance (i1, i2));
|
110 |
|
|
for (; i1 != i2; ++i1)
|
111 |
|
|
insert (*i1);
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
} // namespace ustl
|
115 |
|
|
|
116 |
|
|
#endif
|