Debian Codegraph @

kiwi/AssocVector.h

raw
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-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 ////////////////////////////////////////////////////////////////////////////////
15 // Updated 2019 by Matthieu Dartiailh for C++11 compliancy
16 ////////////////////////////////////////////////////////////////////////////////
17 #pragma once
18
19 // $Id: AssocVector.h 765 2006-10-18 13:55:32Z syntheticpp $
20
21
22 #include <algorithm>
23 #include <functional>
24 #include <vector>
25 #include <utility>
26
27 namespace Loki
28 {
29 ////////////////////////////////////////////////////////////////////////////////
30 // class template AssocVectorCompare
31 // Used by AssocVector
32 ////////////////////////////////////////////////////////////////////////////////
33
34 namespace Private
35 {
36 template <class Value, class C, class K>
37 class AssocVectorCompare : public C
38 {
39 typedef std::pair<K, Value>
40 Data;
41 typedef K first_argument_type;
42
43 public:
44 AssocVectorCompare()
45 {}
46
47 AssocVectorCompare(const C& src) : C(src)
48 {}
49
50 bool operator()(const first_argument_type& lhs,
51 const first_argument_type& rhs) const
52 { return C::operator()(lhs, rhs); }
53
54 bool operator()(const Data& lhs, const Data& rhs) const
55 { return operator()(lhs.first, rhs.first); }
56
57 bool operator()(const Data& lhs,
58 const first_argument_type& rhs) const
59 { return operator()(lhs.first, rhs); }
60
61 bool operator()(const first_argument_type& lhs,
62 const Data& rhs) const
63 { return operator()(lhs, rhs.first); }
64 };
65 }
66
67 ////////////////////////////////////////////////////////////////////////////////
68 // class template AssocVector
69 // An associative vector built as a syntactic drop-in replacement for std::map
70 // BEWARE: AssocVector doesn't respect all map's guarantees, the most important
71 // being:
72 // * iterators are invalidated by insert and erase operations
73 // * the complexity of insert/erase is O(N) not O(log N)
74 // * value_type is std::pair<K, V> not std::pair<const K, V>
75 // * iterators are random
76 ////////////////////////////////////////////////////////////////////////////////
77
78
79 template
80 <
81 class K,
82 class V,
83 class C = std::less<K>,
84 class A = std::allocator< std::pair<K, V> >
85 >
86 class AssocVector
87 : private std::vector< std::pair<K, V>, A >
88 , private Private::AssocVectorCompare<V, C, K>
89 {
90 typedef std::vector<std::pair<K, V>, A> Base;
91 typedef Private::AssocVectorCompare<V, C, K> MyCompare;
92
93 public:
94 typedef K key_type;
95 typedef V mapped_type;
96 typedef typename Base::value_type value_type;
97
98 typedef C key_compare;
99 typedef A allocator_type;
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 Base::reverse_iterator reverse_iterator;
105 typedef typename Base::const_reverse_iterator const_reverse_iterator;
106
107 class value_compare
108 : public std::function<bool(value_type, value_type)>
109 , private key_compare
110 {
111 friend class AssocVector;
112
113 protected:
114 value_compare(key_compare pred) : key_compare(pred)
115 {}
116
117 public:
118 bool operator()(const value_type& lhs, const value_type& rhs) const
119 { return key_compare::operator()(lhs.first, rhs.first); }
120 };
121
122 // 23.3.1.1 construct/copy/destroy
123
124 explicit AssocVector(const key_compare& comp = key_compare(),
125 const A& alloc = A())
126 : Base(alloc), MyCompare(comp)
127 {}
128
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)
134 {
135 MyCompare& me = *this;
136 std::sort(begin(), end(), me);
137 }
138
139 AssocVector(const AssocVector& rhs): Base(rhs), MyCompare(rhs) {}
140
141 AssocVector(AssocVector&& rhs) noexcept
142 : Base(std::move(rhs)), MyCompare(std::move(rhs))
143 {}
144
145 AssocVector& operator=(const AssocVector& rhs)
146 {
147 AssocVector(rhs).swap(*this);
148 return *this;
149 }
150
151 AssocVector& operator=(AssocVector&& rhs) noexcept
152 {
153 AssocVector(std::move(rhs)).swap(*this);
154 return *this;
155 }
156
157 // iterators:
158 // The following are here because MWCW gets 'using' wrong
159 iterator begin() { return Base::begin(); }
160 const_iterator begin() const { return Base::begin(); }
161 iterator end() { return Base::end(); }
162 const_iterator end() const { return Base::end(); }
163 reverse_iterator rbegin() { return Base::rbegin(); }
164 const_reverse_iterator rbegin() const { return Base::rbegin(); }
165 reverse_iterator rend() { return Base::rend(); }
166 const_reverse_iterator rend() const { return Base::rend(); }
167
168 // capacity:
169 bool empty() const { return Base::empty(); }
170 size_type size() const { return Base::size(); }
171 size_type max_size() { return Base::max_size(); }
172
173 // 23.3.1.2 element access:
174 mapped_type& operator[](const key_type& key)
175 { return insert(value_type(key, mapped_type())).first->second; }
176
177 // modifiers:
178 std::pair<iterator, bool> insert(const value_type& val)
179 {
180 bool found(true);
181 iterator i(lower_bound(val.first));
182
183 if (i == end() || this->operator()(val.first, i->first))
184 {
185 i = Base::insert(i, val);
186 found = false;
187 }
188 return std::make_pair(i, !found);
189 }
190 //Section [23.1.2], Table 69
191 //http://developer.apple.com/documentation/DeveloperTools/gcc-3.3/libstdc++/23_containers/howto.html#4
192 iterator insert(iterator pos, const value_type& val)
193 {
194 if( (pos == begin() || this->operator()(*(pos-1),val)) &&
195 (pos == end() || this->operator()(val, *pos)) )
196 {
197 return Base::insert(pos, val);
198 }
199 return insert(val).first;
200 }
201
202 template <class InputIterator>
203 void insert(InputIterator first, InputIterator last)
204 { for (; first != last; ++first) insert(*first); }
205
206 void erase(iterator pos)
207 { Base::erase(pos); }
208
209 size_type erase(const key_type& k)
210 {
211 iterator i(find(k));
212 if (i == end()) return 0;
213 erase(i);
214 return 1;
215 }
216
217 void erase(iterator first, iterator last)
218 { Base::erase(first, last); }
219
220 void swap(AssocVector& other)
221 {
222 Base::swap(other);
223 MyCompare& me = *this;
224 MyCompare& rhs = other;
225 std::swap(me, rhs);
226 }
227
228 void clear()
229 { Base::clear(); }
230
231 // observers:
232 key_compare key_comp() const
233 { return *this; }
234
235 value_compare value_comp() const
236 {
237 const key_compare& comp = *this;
238 return value_compare(comp);
239 }
240
241 // 23.3.1.3 map operations:
242 iterator find(const key_type& k)
243 {
244 iterator i(lower_bound(k));
245 if (i != end() && this->operator()(k, i->first))
246 {
247 i = end();
248 }
249 return i;
250 }
251
252 const_iterator find(const key_type& k) const
253 {
254 const_iterator i(lower_bound(k));
255 if (i != end() && this->operator()(k, i->first))
256 {
257 i = end();
258 }
259 return i;
260 }
261
262 size_type count(const key_type& k) const
263 { return find(k) != end(); }
264
265 iterator lower_bound(const key_type& k)
266 {
267 MyCompare& me = *this;
268 return std::lower_bound(begin(), end(), k, me);
269 }
270
271 const_iterator lower_bound(const key_type& k) const
272 {
273 const MyCompare& me = *this;
274 return std::lower_bound(begin(), end(), k, me);
275 }
276
277 iterator upper_bound(const key_type& k)
278 {
279 MyCompare& me = *this;
280 return std::upper_bound(begin(), end(), k, me);
281 }
282
283 const_iterator upper_bound(const key_type& k) const
284 {
285 const MyCompare& me = *this;
286 return std::upper_bound(begin(), end(), k, me);
287 }
288
289 std::pair<iterator, iterator> equal_range(const key_type& k)
290 {
291 MyCompare& me = *this;
292 return std::equal_range(begin(), end(), k, me);
293 }
294
295 std::pair<const_iterator, const_iterator> equal_range(
296 const key_type& k) const
297 {
298 const MyCompare& me = *this;
299 return std::equal_range(begin(), end(), k, me);
300 }
301
302 template <class K1, class V1, class C1, class A1>
303 friend bool operator==(const AssocVector<K1, V1, C1, A1>& lhs,
304 const AssocVector<K1, V1, C1, A1>& rhs);
305
306 bool operator<(const AssocVector& rhs) const
307 {
308 const Base& me = *this;
309 const Base& yo = rhs;
310 return me < yo;
311 }
312
313 template <class K1, class V1, class C1, class A1>
314 friend bool operator!=(const AssocVector<K1, V1, C1, A1>& lhs,
315 const AssocVector<K1, V1, C1, A1>& rhs);
316
317 template <class K1, class V1, class C1, class A1>
318 friend bool operator>(const AssocVector<K1, V1, C1, A1>& lhs,
319 const AssocVector<K1, V1, C1, A1>& rhs);
320
321 template <class K1, class V1, class C1, class A1>
322 friend bool operator>=(const AssocVector<K1, V1, C1, A1>& lhs,
323 const AssocVector<K1, V1, C1, A1>& rhs);
324
325 template <class K1, class V1, class C1, class A1>
326 friend bool operator<=(const AssocVector<K1, V1, C1, A1>& lhs,
327 const AssocVector<K1, V1, C1, A1>& rhs);
328 };
329
330 template <class K, class V, class C, class A>
331 inline bool operator==(const AssocVector<K, V, C, A>& lhs,
332 const AssocVector<K, V, C, A>& rhs)
333 {
334 const std::vector<std::pair<K, V>, A>& me = lhs;
335 return me == rhs;
336 }
337
338 template <class K, class V, class C, class A>
339 inline bool operator!=(const AssocVector<K, V, C, A>& lhs,
340 const AssocVector<K, V, C, A>& rhs)
341 { return !(lhs == rhs); }
342
343 template <class K, class V, class C, class A>
344 inline bool operator>(const AssocVector<K, V, C, A>& lhs,
345 const AssocVector<K, V, C, A>& rhs)
346 { return rhs < lhs; }
347
348 template <class K, class V, class C, class A>
349 inline bool operator>=(const AssocVector<K, V, C, A>& lhs,
350 const AssocVector<K, V, C, A>& rhs)
351 { return !(lhs < rhs); }
352
353 template <class K, class V, class C, class A>
354 inline bool operator<=(const AssocVector<K, V, C, A>& lhs,
355 const AssocVector<K, V, C, A>& rhs)
356 { return !(rhs < lhs); }
357
358
359 // specialized algorithms:
360 template <class K, class V, class C, class A>
361 void swap(AssocVector<K, V, C, A>& lhs, AssocVector<K, V, C, A>& rhs)
362 { lhs.swap(rhs); }
363
364 } // namespace Loki