1 ////////////////////////////////////////////////////////////////////////////////
2 // The Loki Library
3 // Copyright (c) 2001 by Andrei Alexandrescu
4 // This code accompanies the book:
5 // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
6 // Patterns Applied". Copyright (c) 2001. Addison-Wesley.
7 // Permission to use, copy, modify, distribute and sell this software for any
8 // purpose is hereby granted without fee, provided that the above copyright
9 // notice appear in all copies and that both that copyright notice and this
10 // permission notice appear in supporting documentation.
11 // The author or Addison-Welsey Longman make no representations about the
12 // suitability of this software for any purpose. It is provided "as is"
13 // without express or implied warranty.
14 ////////////////////////////////////////////////////////////////////////////////
15
16 // Last update: June 20, 2001
17
18 #ifndef ASSOCVECTOR_INC_
19 #define ASSOCVECTOR_INC_
20
21 #include <algorithm>
22 #include <functional>
23 #include <vector>
24 #include <utility>
25
26 namespace Loki
27 {
28 ////////////////////////////////////////////////////////////////////////////////
29 // class template AssocVectorCompare
30 // Used by AssocVector
31 ////////////////////////////////////////////////////////////////////////////////
32
33 namespace Private
34 {
35 template <class Value, class C>
36 class AssocVectorCompare : public C
37 {
38 typedef std::pair<typename C::first_argument_type, Value>
39 Data;
40 typedef typename C::first_argument_type first_argument_type;
41
42 public:
43 AssocVectorCompare()
44 {}
45
46 AssocVectorCompare(const C& src) : C(src)
47 {}
48
49 bool operator()(const first_argument_type& lhs,
50 const first_argument_type& rhs) const
51 { return C::operator()(lhs, rhs); }
52
53 bool operator()(const Data& lhs, const Data& rhs) const
54 { return operator()(lhs.first, rhs.first); }
55
56 bool operator()(const Data& lhs,
57 const first_argument_type& rhs) const
58 { return operator()(lhs.first, rhs); }
59
60 bool operator()(const first_argument_type& lhs,
61 const Data& rhs) const
62 { return operator()(lhs, rhs.first); }
63 };
64 }
65
66 ////////////////////////////////////////////////////////////////////////////////
67 // class template AssocVector
68 // An associative vector built as a syntactic drop-in replacement for std::map
69 // BEWARE: AssocVector doesn't respect all map's guarantees, the most important
70 // being:
71 // * iterators are invalidated by insert and erase operations
72 // * the complexity of insert/erase is O(N) not O(log N)
73 // * value_type is std::pair<K, V> not std::pair<const K, V>
74 // * iterators are random
75 ////////////////////////////////////////////////////////////////////////////////
76
77 template
78 <
79 class K,
80 class V,
81 class C = std::less<K>,
82 class A = std::allocator< std::pair<K, V> >
83 >
84 class AssocVector
85 : private std::vector< std::pair<K, V>, A >
86 , private Private::AssocVectorCompare<V, C>
87 {
88 typedef std::vector<std::pair<K, V>, A> Base;
89 typedef Private::AssocVectorCompare<V, C> MyCompare;
90
91 public:
92 typedef K key_type;
93 typedef V mapped_type;
94 typedef typename Base::value_type value_type;
95
96 typedef C key_compare;
97 typedef A allocator_type;
98 typedef typename A::reference reference;
99 typedef typename A::const_reference const_reference;
100 typedef typename Base::iterator iterator;
101 typedef typename Base::const_iterator const_iterator;
102 typedef typename Base::size_type size_type;
103 typedef typename Base::difference_type difference_type;
104 typedef typename A::pointer pointer;
105 typedef typename A::const_pointer const_pointer;
106 typedef typename Base::reverse_iterator reverse_iterator;
107 typedef typename Base::const_reverse_iterator const_reverse_iterator;
108
109 class value_compare
110 : public std::binary_function<value_type, value_type, bool>
111 , private key_compare
112 {
113 friend class AssocVector;
114
115 protected:
116 value_compare(key_compare pred) : key_compare(pred)
117 {}
118
119 public:
120 bool operator()(const value_type& lhs, const value_type& rhs) const
121 { return key_compare::operator()(lhs.first, rhs.first); }
122 };
123
124 // 23.3.1.1 construct/copy/destroy
125
126 explicit AssocVector(const key_compare& comp = key_compare(),
127 const A& alloc = A())
128 : Base(alloc), MyCompare(comp)
129 {}
130
131 template <class InputIterator>
132 AssocVector(InputIterator first, InputIterator last,
133 const key_compare& comp = key_compare(),
134 const A& alloc = A())
135 : Base(first, last, alloc), MyCompare(comp)
136 {
137 MyCompare& me = *this;
138 std::sort(begin(), end(), me);
139 }
140
141 AssocVector& operator=(const AssocVector& rhs)
142 { AssocVector(rhs).swap(*this); }
143
144 // iterators:
145 // The following are here because MWCW gets 'using' wrong
146 iterator begin() { return Base::begin(); }
147 const_iterator begin() const { return Base::begin(); }
148 iterator end() { return Base::end(); }
149 const_iterator end() const { return Base::end(); }
150 reverse_iterator rbegin() { return Base::rbegin(); }
151 const_reverse_iterator rbegin() const { return Base::rbegin(); }
152 reverse_iterator rend() { return Base::rend(); }
153 const_reverse_iterator rend() const { return Base::rend(); }
154
155 // capacity:
156 bool empty() const { return Base::empty(); }
157 size_type size() const { return Base::size(); }
158 size_type max_size() { return Base::max_size(); }
159
160 // 23.3.1.2 element access:
161 mapped_type& operator[](const key_type& key)
162 { return insert(value_type(key, mapped_type())).first->second; }
163
164 // modifiers:
165 std::pair<iterator, bool> insert(const value_type& val)
166 {
167 bool found(true);
168 iterator i(lower_bound(val.first));
169
170 if (i == end() || operator()(val.first, i->first))
171 {
172 i = Base::insert(i, val);
173 found = false;
174 }
175 return std::make_pair(i, !found);
176 }
177
178 iterator insert(iterator pos, const value_type& val)
179 {
180 if (pos != end() && operator()(*pos, val) &&
181 (pos == end() - 1 ||
182 !operator()(val, pos[1]) &&
183 operator()(pos[1], val)))
184 {
185 return Base::insert(pos, val);
186 }
187 return insert(val).first;
188 }
189
190 template <class InputIterator>
191 iterator insert(InputIterator first, InputIterator last)
192 { for (; first != last; ++first) insert(*first); }
193
194 void erase(iterator pos)
195 { Base::erase(pos); }
196
197 size_type erase(const key_type& k)
198 {
199 iterator i(find(k));
200 if (i == end()) return 0;
201 erase(i);
202 return 1;
203 }
204
205 void erase(iterator first, iterator last)
206 { Base::erase(first, last); }
207
208 void swap(AssocVector& other)
209 {
210 using namespace std;
211 Base::swap(other);
212 MyCompare& me = *this;
213 MyCompare& rhs = other;
214 swap(me, rhs);
215 }
216
217 void clear()
218 { Base::clear(); }
219
220 // observers:
221 key_compare key_comp() const
222 { return *this; }
223
224 value_compare value_comp() const
225 {
226 const key_compare& comp = *this;
227 return value_compare(comp);
228 }
229
230 // 23.3.1.3 map operations:
231 iterator find(const key_type& k)
232 {
233 iterator i(lower_bound(k));
234 if (i != end() && operator()(k, i->first))
235 {
236 i = end();
237 }
238 return i;
239 }
240
241 const_iterator find(const key_type& k) const
242 {
243 const_iterator i(lower_bound(k));
244 if (i != end() && operator()(k, i->first))
245 {
246 i = end();
247 }
248 return i;
249 }
250
251 size_type count(const key_type& k) const
252 { return find(k) != end(); }
253
254 iterator lower_bound(const key_type& k)
255 {
256 MyCompare& me = *this;
257 return std::lower_bound(begin(), end(), k, me);
258 }
259
260 const_iterator lower_bound(const key_type& k) const
261 {
262 const MyCompare& me = *this;
263 return std::lower_bound(begin(), end(), k, me);
264 }
265
266 iterator upper_bound(const key_type& k)
267 {
268 MyCompare& me = *this;
269 return std::upper_bound(begin(), end(), k, me);
270 }
271
272 const_iterator upper_bound(const key_type& k) const
273 {
274 const MyCompare& me = *this;
275 return std::upper_bound(begin(), end(), k, me);
276 }
277
278 std::pair<iterator, iterator> equal_range(const key_type& k)
279 {
280 MyCompare& me = *this;
281 return std::equal_range(begin(), end(), k, me);
282 }
283
284 std::pair<const_iterator, const_iterator> equal_range(
285 const key_type& k) const
286 {
287 const MyCompare& me = *this;
288 return std::equal_range(begin(), end(), k, me);
289 }
290
291 friend bool operator==(const AssocVector& lhs, const AssocVector& rhs)
292 {
293 const Base& me = lhs;
294 return me == rhs;
295 }
296
297 bool operator<(const AssocVector& rhs) const
298 {
299 const Base& me = *this;
300 const Base& yo = rhs;
301 return me < yo;
302 }
303
304 friend bool operator!=(const AssocVector& lhs, const AssocVector& rhs)
305 { return !(lhs == rhs); }
306
307 friend bool operator>(const AssocVector& lhs, const AssocVector& rhs)
308 { return rhs < lhs; }
309
310 friend bool operator>=(const AssocVector& lhs, const AssocVector& rhs)
311 { return !(lhs < rhs); }
312
313 friend bool operator<=(const AssocVector& lhs, const AssocVector& rhs)
314 { return !(rhs < lhs); }
315 };
316
317 // specialized algorithms:
318 template <class K, class V, class C, class A>
319 void swap(AssocVector<K, V, C, A>& lhs, AssocVector<K, V, C, A>& rhs)
320 { lhs.swap(rhs); }
321
322 } // namespace Loki
323
324 ////////////////////////////////////////////////////////////////////////////////
325 // Change log:
326 // May 20, 2001: change operator= - credit due to Cristoph Koegl
327 // June 11, 2001: remove paren in equal_range - credit due to Cristoph Koegl
328 // June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
329 ////////////////////////////////////////////////////////////////////////////////
330
331 #endif // ASSOCVECTOR_INC_