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 UMAP_H_45643F516E02A87A3DCEA5024052A6F5
|
7 |
|
|
#define UMAP_H_45643F516E02A87A3DCEA5024052A6F5
|
8 |
|
|
|
9 |
|
|
#include "uvector.h"
|
10 |
|
|
#include "ufunction.h"
|
11 |
|
|
|
12 |
|
|
namespace ustl {
|
13 |
|
|
|
14 |
|
|
/// \class map umap.h ustl.h
|
15 |
|
|
/// \ingroup AssociativeContainers
|
16 |
|
|
///
|
17 |
|
|
/// \brief A sorted associative container of pair<K,V>
|
18 |
|
|
///
|
19 |
|
|
template <typename K, typename V>
|
20 |
|
|
class map : 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 map<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 |
|
|
typedef pair<iterator,bool> insertrv_t;
|
41 |
|
|
public:
|
42 |
|
|
inline map (void) : vector<pair<K,V> > () {}
|
43 |
|
|
explicit inline map (size_type n) : vector<pair<K,V> > (n) {}
|
44 |
|
|
inline map (rcself_t v) : vector<pair<K,V> > (v) {}
|
45 |
|
|
inline map (const_iterator i1, const_iterator i2) : vector<pair<K,V> >() { insert (i1, i2); }
|
46 |
|
|
inline rcself_t operator= (rcself_t v) { base_class::operator= (v); return (*this); }
|
47 |
|
|
inline const_data_ref operator[] (const_key_ref i) const;
|
48 |
|
|
data_type& operator[] (const_key_ref i);
|
49 |
|
|
inline size_type size (void) const { return (base_class::size()); }
|
50 |
|
|
inline iterator begin (void) { return (base_class::begin()); }
|
51 |
|
|
inline const_iterator begin (void) const { return (base_class::begin()); }
|
52 |
|
|
inline iterator end (void) { return (base_class::end()); }
|
53 |
|
|
inline const_iterator end (void) const { return (base_class::end()); }
|
54 |
|
|
inline void assign (const_iterator i1, const_iterator i2) { clear(); insert (i1, i2); }
|
55 |
|
|
inline void push_back (const_reference v) { insert (v); }
|
56 |
|
|
inline const_iterator find (const_key_ref k) const;
|
57 |
|
|
inline iterator find (const_key_ref k) { return (const_cast<iterator> (const_cast<rcself_t>(*this).find (k))); }
|
58 |
|
|
inline const_iterator find_data (const_data_ref v, const_iterator first = NULL, const_iterator last = NULL) const;
|
59 |
|
|
inline iterator find_data (const_data_ref v, iterator first = NULL, iterator last = NULL);
|
60 |
|
|
insertrv_t insert (const_reference v);
|
61 |
|
|
void insert (const_iterator i1, const_iterator i2);
|
62 |
|
|
inline void erase (const_key_ref k);
|
63 |
|
|
inline iterator erase (iterator ep) { return (base_class::erase (ep)); }
|
64 |
|
|
inline iterator erase (iterator ep1, iterator ep2) { return (base_class::erase (ep1, ep2)); }
|
65 |
|
|
inline void clear (void) { base_class::clear(); }
|
66 |
|
|
private:
|
67 |
|
|
const_iterator lower_bound (const_key_ref k) const;
|
68 |
|
|
inline iterator lower_bound (const_key_ref k) { return (const_cast<iterator>(const_cast<rcself_t>(*this).lower_bound (k))); }
|
69 |
|
|
};
|
70 |
|
|
|
71 |
|
|
template <typename K, typename V>
|
72 |
|
|
typename map<K,V>::const_iterator map<K,V>::lower_bound (const_key_ref k) const
|
73 |
|
|
{
|
74 |
|
|
const_iterator first (begin()), last (end());
|
75 |
|
|
while (first != last) {
|
76 |
|
|
const_iterator mid = advance (first, distance (first,last) / 2);
|
77 |
|
|
if (mid->first < k)
|
78 |
|
|
first = advance (mid, 1);
|
79 |
|
|
else
|
80 |
|
|
last = mid;
|
81 |
|
|
}
|
82 |
|
|
return (first);
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
/// Returns the pair<K,V> where K = \p k.
|
86 |
|
|
template <typename K, typename V>
|
87 |
|
|
inline typename map<K,V>::const_iterator map<K,V>::find (const_key_ref k) const
|
88 |
|
|
{
|
89 |
|
|
const_iterator i = lower_bound (k);
|
90 |
|
|
return ((i < end() && k < i->first) ? end() : i);
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
/// Returns the pair<K,V> where V = \p v, occuring in range [first,last).
|
94 |
|
|
template <typename K, typename V>
|
95 |
|
|
inline typename map<K,V>::const_iterator map<K,V>::find_data (const_data_ref v, const_iterator first, const_iterator last) const
|
96 |
|
|
{
|
97 |
|
|
if (!first) first = begin();
|
98 |
|
|
if (!last) last = end();
|
99 |
|
|
for (; first != last && first->second != v; ++first) ;
|
100 |
|
|
return (first);
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
/// Returns the pair<K,V> where V = \p v, occuring in range [first,last).
|
104 |
|
|
template <typename K, typename V>
|
105 |
|
|
inline typename map<K,V>::iterator map<K,V>::find_data (const_data_ref v, iterator first, iterator last)
|
106 |
|
|
{
|
107 |
|
|
return (const_cast<iterator> (find_data (v, const_cast<const_iterator>(first), const_cast<const_iterator>(last))));
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
/// Returns data associated with key \p k.
|
111 |
|
|
template <typename K, typename V>
|
112 |
|
|
inline const typename map<K,V>::data_type& map<K,V>::operator[] (const_key_ref k) const
|
113 |
|
|
{
|
114 |
|
|
assert (find(k) != end() && "operator[] const can not insert non-existent keys");
|
115 |
|
|
return (find(k)->second);
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
/// Returns data associated with key \p k.
|
119 |
|
|
template <typename K, typename V>
|
120 |
|
|
typename map<K,V>::data_type& map<K,V>::operator[] (const_key_ref k)
|
121 |
|
|
{
|
122 |
|
|
iterator ip = lower_bound (k);
|
123 |
|
|
if (ip == end() || k < ip->first)
|
124 |
|
|
ip = base_class::insert (ip, make_pair (k, V()));
|
125 |
|
|
return (ip->second);
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
/// Inserts the pair into the container.
|
129 |
|
|
template <typename K, typename V>
|
130 |
|
|
typename map<K,V>::insertrv_t map<K,V>::insert (const_reference v)
|
131 |
|
|
{
|
132 |
|
|
iterator ip = lower_bound (v.first);
|
133 |
|
|
bool bInserted = ip == end() || v.first < ip->first;
|
134 |
|
|
if (bInserted)
|
135 |
|
|
ip = base_class::insert (ip, v);
|
136 |
|
|
return (make_pair (ip, bInserted));
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
/// Inserts elements from range [i1,i2) into the container.
|
140 |
|
|
template <typename K, typename V>
|
141 |
|
|
void map<K,V>::insert (const_iterator i1, const_iterator i2)
|
142 |
|
|
{
|
143 |
|
|
assert (i1 <= i2);
|
144 |
|
|
reserve (size() + distance (i1, i2));
|
145 |
|
|
for (; i1 != i2; ++i1)
|
146 |
|
|
insert (*i1);
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
/// Erases the element with key value \p k.
|
150 |
|
|
template <typename K, typename V>
|
151 |
|
|
inline void map<K,V>::erase (const_key_ref k)
|
152 |
|
|
{
|
153 |
|
|
iterator ip = find (k);
|
154 |
|
|
if (ip != end())
|
155 |
|
|
erase (ip);
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
} // namespace ustl
|
159 |
|
|
|
160 |
|
|
#endif
|