1
////////////////////////////////////////////////////////////////////////////////
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-Wesley 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
////////////////////////////////////////////////////////////////////////////////
16
#ifndef ASSOCVECTOR_INC_
17
#define ASSOCVECTOR_INC_
26
////////////////////////////////////////////////////////////////////////////////
27
// class template AssocVectorCompare
28
// Used by AssocVector
29
////////////////////////////////////////////////////////////////////////////////
33
template <class Value, class C>
34
class AssocVectorCompare : public C
36
typedef std::pair<typename C::first_argument_type, Value>
38
typedef typename C::first_argument_type first_argument_type;
44
AssocVectorCompare(const C& src) : C(src)
47
bool operator()(const first_argument_type& lhs,
48
const first_argument_type& rhs) const
49
{ return C::operator()(lhs, rhs); }
51
bool operator()(const Data& lhs, const Data& rhs) const
52
{ return operator()(lhs.first, rhs.first); }
54
bool operator()(const Data& lhs,
55
const first_argument_type& rhs) const
56
{ return operator()(lhs.first, rhs); }
58
bool operator()(const first_argument_type& lhs,
59
const Data& rhs) const
60
{ return operator()(lhs, rhs.first); }
64
////////////////////////////////////////////////////////////////////////////////
65
// class template AssocVector
66
// An associative vector built as a syntactic drop-in replacement for std::map
67
// BEWARE: AssocVector doesn't respect all map's guarantees, the most important
69
// * iterators are invalidated by insert and erase operations
70
// * the complexity of insert/erase is O(N) not O(log N)
71
// * value_type is std::pair<K, V> not std::pair<const K, V>
72
// * iterators are random
73
////////////////////////////////////////////////////////////////////////////////
79
class C = std::less<K>,
80
class A = std::allocator< std::pair<K, V> >
83
: private std::vector< std::pair<K, V>, A >
84
, private Private::AssocVectorCompare<V, C>
86
typedef std::vector<std::pair<K, V>, A> Base;
87
typedef Private::AssocVectorCompare<V, C> MyCompare;
91
typedef V mapped_type;
92
typedef typename Base::value_type value_type;
94
typedef C key_compare;
95
typedef A allocator_type;
96
typedef typename A::reference reference;
97
typedef typename A::const_reference const_reference;
98
typedef typename Base::iterator iterator;
99
typedef typename Base::const_iterator const_iterator;
100
typedef typename Base::size_type size_type;
101
typedef typename Base::difference_type difference_type;
102
typedef typename A::pointer pointer;
103
typedef typename A::const_pointer const_pointer;
104
typedef typename Base::reverse_iterator reverse_iterator;
105
typedef typename Base::const_reverse_iterator const_reverse_iterator;
108
: public std::binary_function<value_type, value_type, bool>
109
, private key_compare
111
friend class AssocVector;
114
value_compare(key_compare pred) : key_compare(pred)
118
bool operator()(const value_type& lhs, const value_type& rhs) const
119
{ return key_compare::operator()(lhs.first, rhs.first); }
122
// 23.3.1.1 construct/copy/destroy
124
explicit AssocVector(const key_compare& comp = key_compare(),
125
const A& alloc = A())
126
: Base(alloc), MyCompare(comp)
129
template <class InputIterator>
130
AssocVector(InputIterator first, InputIterator last,
131
const key_compare& comp = key_compare(),
132
const A& alloc = A())
133
: Base(first, last, alloc), MyCompare(comp)
135
MyCompare& me = *this;
136
std::sort(begin(), end(), me);
139
AssocVector& operator=(const AssocVector& rhs)
141
AssocVector(rhs).swap(*this);
146
// The following are here because MWCW gets 'using' wrong
147
iterator begin() { return Base::begin(); }
148
const_iterator begin() const { return Base::begin(); }
149
iterator end() { return Base::end(); }
150
const_iterator end() const { return Base::end(); }
151
reverse_iterator rbegin() { return Base::rbegin(); }
152
const_reverse_iterator rbegin() const { return Base::rbegin(); }
153
reverse_iterator rend() { return Base::rend(); }
154
const_reverse_iterator rend() const { return Base::rend(); }
157
bool empty() const { return Base::empty(); }
158
size_type size() const { return Base::size(); }
159
size_type max_size() { return Base::max_size(); }
161
// 23.3.1.2 element access:
162
mapped_type& operator[](const key_type& key)
163
{ return insert(value_type(key, mapped_type())).first->second; }
166
std::pair<iterator, bool> insert(const value_type& val)
169
iterator i(lower_bound(val.first));
171
if (i == end() || this->operator()(val.first, i->first))
173
i = Base::insert(i, val);
176
return std::make_pair(i, !found);
179
iterator insert(iterator pos, const value_type& val)
181
if (pos != end() && this->operator()(*pos, val) &&
183
!this->operator()(val, pos[1]) &&
184
this->operator()(pos[1], val)))
186
return Base::insert(pos, val);
188
return insert(val).first;
191
template <class InputIterator>
192
void insert(InputIterator first, InputIterator last)
193
{ for (; first != last; ++first) insert(*first); }
195
void erase(iterator pos)
196
{ Base::erase(pos); }
198
size_type erase(const key_type& k)
201
if (i == end()) return 0;
206
void erase(iterator first, iterator last)
207
{ Base::erase(first, last); }
209
void swap(AssocVector& other)
213
MyCompare& me = *this;
214
MyCompare& rhs = other;
222
key_compare key_comp() const
225
value_compare value_comp() const
227
const key_compare& comp = *this;
228
return value_compare(comp);
231
// 23.3.1.3 map operations:
232
iterator find(const key_type& k)
234
iterator i(lower_bound(k));
235
if (i != end() && this->operator()(k, i->first))
242
const_iterator find(const key_type& k) const
244
const_iterator i(lower_bound(k));
245
if (i != end() && this->operator()(k, i->first))
252
size_type count(const key_type& k) const
253
{ return find(k) != end(); }
255
iterator lower_bound(const key_type& k)
257
MyCompare& me = *this;
258
return std::lower_bound(begin(), end(), k, me);
261
const_iterator lower_bound(const key_type& k) const
263
const MyCompare& me = *this;
264
return std::lower_bound(begin(), end(), k, me);
267
iterator upper_bound(const key_type& k)
269
MyCompare& me = *this;
270
return std::upper_bound(begin(), end(), k, me);
273
const_iterator upper_bound(const key_type& k) const
275
const MyCompare& me = *this;
276
return std::upper_bound(begin(), end(), k, me);
279
std::pair<iterator, iterator> equal_range(const key_type& k)
281
MyCompare& me = *this;
282
return std::equal_range(begin(), end(), k, me);
285
std::pair<const_iterator, const_iterator> equal_range(
286
const key_type& k) const
288
const MyCompare& me = *this;
289
return std::equal_range(begin(), end(), k, me);
292
friend bool operator==(const AssocVector& lhs, const AssocVector& rhs)
294
const Base& me = lhs;
298
bool operator<(const AssocVector& rhs) const
300
const Base& me = *this;
301
const Base& yo = rhs;
305
friend bool operator!=(const AssocVector& lhs, const AssocVector& rhs)
306
{ return !(lhs == rhs); }
308
friend bool operator>(const AssocVector& lhs, const AssocVector& rhs)
309
{ return rhs < lhs; }
311
friend bool operator>=(const AssocVector& lhs, const AssocVector& rhs)
312
{ return !(lhs < rhs); }
314
friend bool operator<=(const AssocVector& lhs, const AssocVector& rhs)
315
{ return !(rhs < lhs); }
318
// specialized algorithms:
319
template <class K, class V, class C, class A>
320
void swap(AssocVector<K, V, C, A>& lhs, AssocVector<K, V, C, A>& rhs)
325
////////////////////////////////////////////////////////////////////////////////
327
// May 20, 2001: change operator= - credit due to Cristoph Koegl
328
// June 11, 2001: remove paren in equal_range - credit due to Cristoph Koegl
329
// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
330
// January 22, 2002: fixed operator= - credit due to Tom Hyer
331
// June 25, 2002: fixed template insert() - credit due to Robert Minsk
332
// June 27, 2002: fixed member swap() - credit due to David Brookman
333
// February 2, 2003: fixed dependent names - credit due to Rani Sharoni
334
////////////////////////////////////////////////////////////////////////////////
336
#endif // ASSOCVECTOR_INC_