]>
Commit | Line | Data |
---|---|---|
42526146 PE |
1 | // Map implementation -*- C++ -*- |
2 | ||
12ffa228 BK |
3 | // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, |
4 | // 2011 Free Software Foundation, Inc. | |
42526146 PE |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
42526146 PE |
10 | // any later version. |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
748086b7 JJ |
17 | // Under Section 7 of GPL version 3, you are granted additional |
18 | // permissions described in the GCC Runtime Library Exception, version | |
19 | // 3.1, as published by the Free Software Foundation. | |
42526146 | 20 | |
748086b7 JJ |
21 | // You should have received a copy of the GNU General Public License and |
22 | // a copy of the GCC Runtime Library Exception along with this program; | |
23 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | // <http://www.gnu.org/licenses/>. | |
42526146 | 25 | |
725dc051 BK |
26 | /* |
27 | * | |
28 | * Copyright (c) 1994 | |
29 | * Hewlett-Packard Company | |
30 | * | |
31 | * Permission to use, copy, modify, distribute and sell this software | |
32 | * and its documentation for any purpose is hereby granted without fee, | |
33 | * provided that the above copyright notice appear in all copies and | |
34 | * that both that copyright notice and this permission notice appear | |
35 | * in supporting documentation. Hewlett-Packard Company makes no | |
36 | * representations about the suitability of this software for any | |
37 | * purpose. It is provided "as is" without express or implied warranty. | |
38 | * | |
39 | * | |
40 | * Copyright (c) 1996,1997 | |
41 | * Silicon Graphics Computer Systems, Inc. | |
42 | * | |
43 | * Permission to use, copy, modify, distribute and sell this software | |
44 | * and its documentation for any purpose is hereby granted without fee, | |
45 | * provided that the above copyright notice appear in all copies and | |
46 | * that both that copyright notice and this permission notice appear | |
47 | * in supporting documentation. Silicon Graphics makes no | |
48 | * representations about the suitability of this software for any | |
49 | * purpose. It is provided "as is" without express or implied warranty. | |
50 | */ | |
51 | ||
f910786b | 52 | /** @file bits/stl_map.h |
729e3d3f | 53 | * This is an internal header file, included by other library headers. |
f910786b | 54 | * Do not attempt to use it directly. @headername{map} |
725dc051 BK |
55 | */ |
56 | ||
046d30f4 PC |
57 | #ifndef _STL_MAP_H |
58 | #define _STL_MAP_H 1 | |
725dc051 | 59 | |
8b5f07a2 | 60 | #include <bits/functexcept.h> |
30a20a1e | 61 | #include <bits/concept_check.h> |
09357846 | 62 | #include <initializer_list> |
725dc051 | 63 | |
12ffa228 BK |
64 | namespace std _GLIBCXX_VISIBILITY(default) |
65 | { | |
66 | _GLIBCXX_BEGIN_NAMESPACE_CONTAINER | |
3cbc7af0 | 67 | |
224a45d0 | 68 | /** |
3971a4d2 PE |
69 | * @brief A standard container made up of (key,value) pairs, which can be |
70 | * retrieved based on a key, in logarithmic time. | |
224a45d0 | 71 | * |
aac2878e | 72 | * @ingroup associative_containers |
224a45d0 | 73 | * |
3971a4d2 PE |
74 | * Meets the requirements of a <a href="tables.html#65">container</a>, a |
75 | * <a href="tables.html#66">reversible container</a>, and an | |
76 | * <a href="tables.html#69">associative container</a> (using unique keys). | |
77 | * For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the | |
78 | * value_type is std::pair<const Key,T>. | |
224a45d0 | 79 | * |
3971a4d2 | 80 | * Maps support bidirectional iterators. |
224a45d0 | 81 | * |
3971a4d2 PE |
82 | * The private tree data is declared exactly the same way for map and |
83 | * multimap; the distinction is made entirely in how the tree functions are | |
84 | * called (*_unique versus *_equal, same as the standard). | |
3971a4d2 | 85 | */ |
6323b34e PC |
86 | template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>, |
87 | typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > > | |
3971a4d2 | 88 | class map |
f6592a9e | 89 | { |
737ab798 | 90 | public: |
f6592a9e PC |
91 | typedef _Key key_type; |
92 | typedef _Tp mapped_type; | |
6323b34e | 93 | typedef std::pair<const _Key, _Tp> value_type; |
f6592a9e | 94 | typedef _Compare key_compare; |
4fd20a8f PC |
95 | typedef _Alloc allocator_type; |
96 | ||
97 | private: | |
98 | // concept requirements | |
99 | typedef typename _Alloc::value_type _Alloc_value_type; | |
100 | __glibcxx_class_requires(_Tp, _SGIAssignableConcept) | |
101 | __glibcxx_class_requires4(_Compare, bool, _Key, _Key, | |
102 | _BinaryFunctionConcept) | |
103 | __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept) | |
ed6814f7 | 104 | |
4fd20a8f | 105 | public: |
f6592a9e | 106 | class value_compare |
6323b34e | 107 | : public std::binary_function<value_type, value_type, bool> |
3971a4d2 | 108 | { |
4fd20a8f | 109 | friend class map<_Key, _Tp, _Compare, _Alloc>; |
3971a4d2 | 110 | protected: |
f6592a9e | 111 | _Compare comp; |
ed6814f7 | 112 | |
f6592a9e | 113 | value_compare(_Compare __c) |
737ab798 | 114 | : comp(__c) { } |
ed6814f7 | 115 | |
3971a4d2 | 116 | public: |
f6592a9e PC |
117 | bool operator()(const value_type& __x, const value_type& __y) const |
118 | { return comp(__x.first, __y.first); } | |
3971a4d2 | 119 | }; |
ed6814f7 | 120 | |
f6592a9e | 121 | private: |
4312e020 | 122 | /// This turns a red-black tree into a [multi]map. |
4fd20a8f PC |
123 | typedef typename _Alloc::template rebind<value_type>::other |
124 | _Pair_alloc_type; | |
125 | ||
126 | typedef _Rb_tree<key_type, value_type, _Select1st<value_type>, | |
127 | key_compare, _Pair_alloc_type> _Rep_type; | |
128 | ||
4312e020 | 129 | /// The actual tree structure. |
f6592a9e | 130 | _Rep_type _M_t; |
ed6814f7 | 131 | |
f6592a9e PC |
132 | public: |
133 | // many of these are specified differently in ISO, but the following are | |
134 | // "functionally equivalent" | |
4fd20a8f PC |
135 | typedef typename _Pair_alloc_type::pointer pointer; |
136 | typedef typename _Pair_alloc_type::const_pointer const_pointer; | |
137 | typedef typename _Pair_alloc_type::reference reference; | |
138 | typedef typename _Pair_alloc_type::const_reference const_reference; | |
7338fc64 PC |
139 | typedef typename _Rep_type::iterator iterator; |
140 | typedef typename _Rep_type::const_iterator const_iterator; | |
141 | typedef typename _Rep_type::size_type size_type; | |
142 | typedef typename _Rep_type::difference_type difference_type; | |
143 | typedef typename _Rep_type::reverse_iterator reverse_iterator; | |
144 | typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator; | |
ed6814f7 | 145 | |
f6592a9e PC |
146 | // [23.3.1.1] construct/copy/destroy |
147 | // (get_allocator() is normally listed in this section, but seems to have | |
148 | // been accidentally omitted in the printed standard) | |
149 | /** | |
150 | * @brief Default constructor creates no elements. | |
151 | */ | |
152 | map() | |
78b36b70 | 153 | : _M_t() { } |
ed6814f7 | 154 | |
f6592a9e | 155 | /** |
78b36b70 PC |
156 | * @brief Creates a %map with no elements. |
157 | * @param comp A comparison object. | |
158 | * @param a An allocator object. | |
f6592a9e PC |
159 | */ |
160 | explicit | |
78b36b70 PC |
161 | map(const _Compare& __comp, |
162 | const allocator_type& __a = allocator_type()) | |
3971a4d2 | 163 | : _M_t(__comp, __a) { } |
ed6814f7 | 164 | |
f6592a9e | 165 | /** |
78b36b70 | 166 | * @brief %Map copy constructor. |
f6592a9e PC |
167 | * @param x A %map of identical element and allocator types. |
168 | * | |
78b36b70 PC |
169 | * The newly-created %map uses a copy of the allocation object |
170 | * used by @a x. | |
f6592a9e PC |
171 | */ |
172 | map(const map& __x) | |
3971a4d2 | 173 | : _M_t(__x._M_t) { } |
ed6814f7 | 174 | |
78b36b70 PC |
175 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
176 | /** | |
177 | * @brief %Map move constructor. | |
178 | * @param x A %map of identical element and allocator types. | |
179 | * | |
180 | * The newly-created %map contains the exact contents of @a x. | |
181 | * The contents of @a x are a valid, but unspecified %map. | |
182 | */ | |
183 | map(map&& __x) | |
5f1fd346 | 184 | : _M_t(std::move(__x._M_t)) { } |
988499f4 JM |
185 | |
186 | /** | |
187 | * @brief Builds a %map from an initializer_list. | |
188 | * @param l An initializer_list. | |
189 | * @param comp A comparison object. | |
190 | * @param a An allocator object. | |
191 | * | |
192 | * Create a %map consisting of copies of the elements in the | |
193 | * initializer_list @a l. | |
194 | * This is linear in N if the range is already sorted, and NlogN | |
195 | * otherwise (where N is @a l.size()). | |
196 | */ | |
197 | map(initializer_list<value_type> __l, | |
198 | const _Compare& __c = _Compare(), | |
199 | const allocator_type& __a = allocator_type()) | |
b798df05 | 200 | : _M_t(__c, __a) |
988499f4 | 201 | { _M_t._M_insert_unique(__l.begin(), __l.end()); } |
78b36b70 PC |
202 | #endif |
203 | ||
f6592a9e PC |
204 | /** |
205 | * @brief Builds a %map from a range. | |
206 | * @param first An input iterator. | |
207 | * @param last An input iterator. | |
208 | * | |
209 | * Create a %map consisting of copies of the elements from [first,last). | |
210 | * This is linear in N if the range is already sorted, and NlogN | |
211 | * otherwise (where N is distance(first,last)). | |
212 | */ | |
78b36b70 | 213 | template<typename _InputIterator> |
f6592a9e | 214 | map(_InputIterator __first, _InputIterator __last) |
78b36b70 | 215 | : _M_t() |
42a27024 | 216 | { _M_t._M_insert_unique(__first, __last); } |
ed6814f7 | 217 | |
f6592a9e PC |
218 | /** |
219 | * @brief Builds a %map from a range. | |
220 | * @param first An input iterator. | |
221 | * @param last An input iterator. | |
222 | * @param comp A comparison functor. | |
223 | * @param a An allocator object. | |
224 | * | |
225 | * Create a %map consisting of copies of the elements from [first,last). | |
226 | * This is linear in N if the range is already sorted, and NlogN | |
227 | * otherwise (where N is distance(first,last)). | |
228 | */ | |
78b36b70 | 229 | template<typename _InputIterator> |
f6592a9e | 230 | map(_InputIterator __first, _InputIterator __last, |
78b36b70 PC |
231 | const _Compare& __comp, |
232 | const allocator_type& __a = allocator_type()) | |
f6592a9e | 233 | : _M_t(__comp, __a) |
42a27024 | 234 | { _M_t._M_insert_unique(__first, __last); } |
ed6814f7 | 235 | |
45f388bb BK |
236 | // FIXME There is no dtor declared, but we should have something |
237 | // generated by Doxygen. I don't know what tags to add to this | |
238 | // paragraph to make that happen: | |
f6592a9e PC |
239 | /** |
240 | * The dtor only erases the elements, and note that if the elements | |
241 | * themselves are pointers, the pointed-to memory is not touched in any | |
28dac70a | 242 | * way. Managing the pointer is the user's responsibility. |
f6592a9e | 243 | */ |
ed6814f7 | 244 | |
f6592a9e | 245 | /** |
78b36b70 | 246 | * @brief %Map assignment operator. |
f6592a9e PC |
247 | * @param x A %map of identical element and allocator types. |
248 | * | |
249 | * All the elements of @a x are copied, but unlike the copy constructor, | |
250 | * the allocator object is not copied. | |
251 | */ | |
252 | map& | |
253 | operator=(const map& __x) | |
254 | { | |
255 | _M_t = __x._M_t; | |
256 | return *this; | |
257 | } | |
ed6814f7 | 258 | |
78b36b70 PC |
259 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
260 | /** | |
261 | * @brief %Map move assignment operator. | |
262 | * @param x A %map of identical element and allocator types. | |
263 | * | |
264 | * The contents of @a x are moved into this map (without copying). | |
265 | * @a x is a valid, but unspecified %map. | |
266 | */ | |
267 | map& | |
268 | operator=(map&& __x) | |
269 | { | |
0462fd5e PC |
270 | // NB: DR 1204. |
271 | // NB: DR 675. | |
272 | this->clear(); | |
273 | this->swap(__x); | |
78b36b70 PC |
274 | return *this; |
275 | } | |
988499f4 JM |
276 | |
277 | /** | |
278 | * @brief %Map list assignment operator. | |
279 | * @param l An initializer_list. | |
280 | * | |
281 | * This function fills a %map with copies of the elements in the | |
282 | * initializer list @a l. | |
283 | * | |
284 | * Note that the assignment completely changes the %map and | |
285 | * that the resulting %map's size is the same as the number | |
286 | * of elements assigned. Old data may be lost. | |
287 | */ | |
288 | map& | |
289 | operator=(initializer_list<value_type> __l) | |
290 | { | |
291 | this->clear(); | |
292 | this->insert(__l.begin(), __l.end()); | |
293 | return *this; | |
294 | } | |
78b36b70 PC |
295 | #endif |
296 | ||
f6592a9e PC |
297 | /// Get a copy of the memory allocation object. |
298 | allocator_type | |
299 | get_allocator() const | |
300 | { return _M_t.get_allocator(); } | |
ed6814f7 | 301 | |
f6592a9e PC |
302 | // iterators |
303 | /** | |
ed6814f7 | 304 | * Returns a read/write iterator that points to the first pair in the |
f6592a9e PC |
305 | * %map. |
306 | * Iteration is done in ascending order according to the keys. | |
307 | */ | |
308 | iterator | |
309 | begin() | |
310 | { return _M_t.begin(); } | |
ed6814f7 | 311 | |
f6592a9e PC |
312 | /** |
313 | * Returns a read-only (constant) iterator that points to the first pair | |
314 | * in the %map. Iteration is done in ascending order according to the | |
315 | * keys. | |
316 | */ | |
317 | const_iterator | |
318 | begin() const | |
319 | { return _M_t.begin(); } | |
ed6814f7 | 320 | |
f6592a9e | 321 | /** |
45f388bb BK |
322 | * Returns a read/write iterator that points one past the last |
323 | * pair in the %map. Iteration is done in ascending order | |
324 | * according to the keys. | |
f6592a9e PC |
325 | */ |
326 | iterator | |
327 | end() | |
328 | { return _M_t.end(); } | |
ed6814f7 | 329 | |
f6592a9e PC |
330 | /** |
331 | * Returns a read-only (constant) iterator that points one past the last | |
332 | * pair in the %map. Iteration is done in ascending order according to | |
333 | * the keys. | |
334 | */ | |
335 | const_iterator | |
336 | end() const | |
337 | { return _M_t.end(); } | |
ed6814f7 | 338 | |
f6592a9e PC |
339 | /** |
340 | * Returns a read/write reverse iterator that points to the last pair in | |
341 | * the %map. Iteration is done in descending order according to the | |
342 | * keys. | |
343 | */ | |
344 | reverse_iterator | |
345 | rbegin() | |
346 | { return _M_t.rbegin(); } | |
ed6814f7 | 347 | |
f6592a9e PC |
348 | /** |
349 | * Returns a read-only (constant) reverse iterator that points to the | |
350 | * last pair in the %map. Iteration is done in descending order | |
351 | * according to the keys. | |
352 | */ | |
353 | const_reverse_iterator | |
354 | rbegin() const | |
355 | { return _M_t.rbegin(); } | |
ed6814f7 | 356 | |
f6592a9e PC |
357 | /** |
358 | * Returns a read/write reverse iterator that points to one before the | |
359 | * first pair in the %map. Iteration is done in descending order | |
360 | * according to the keys. | |
361 | */ | |
362 | reverse_iterator | |
363 | rend() | |
364 | { return _M_t.rend(); } | |
ed6814f7 | 365 | |
f6592a9e PC |
366 | /** |
367 | * Returns a read-only (constant) reverse iterator that points to one | |
368 | * before the first pair in the %map. Iteration is done in descending | |
369 | * order according to the keys. | |
370 | */ | |
371 | const_reverse_iterator | |
372 | rend() const | |
373 | { return _M_t.rend(); } | |
ed6814f7 | 374 | |
0cd50f89 PC |
375 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
376 | /** | |
377 | * Returns a read-only (constant) iterator that points to the first pair | |
378 | * in the %map. Iteration is done in ascending order according to the | |
379 | * keys. | |
380 | */ | |
381 | const_iterator | |
382 | cbegin() const | |
383 | { return _M_t.begin(); } | |
384 | ||
385 | /** | |
386 | * Returns a read-only (constant) iterator that points one past the last | |
387 | * pair in the %map. Iteration is done in ascending order according to | |
388 | * the keys. | |
389 | */ | |
390 | const_iterator | |
391 | cend() const | |
392 | { return _M_t.end(); } | |
393 | ||
394 | /** | |
395 | * Returns a read-only (constant) reverse iterator that points to the | |
396 | * last pair in the %map. Iteration is done in descending order | |
397 | * according to the keys. | |
398 | */ | |
399 | const_reverse_iterator | |
400 | crbegin() const | |
401 | { return _M_t.rbegin(); } | |
402 | ||
403 | /** | |
404 | * Returns a read-only (constant) reverse iterator that points to one | |
405 | * before the first pair in the %map. Iteration is done in descending | |
406 | * order according to the keys. | |
407 | */ | |
408 | const_reverse_iterator | |
409 | crend() const | |
410 | { return _M_t.rend(); } | |
411 | #endif | |
412 | ||
f6592a9e PC |
413 | // capacity |
414 | /** Returns true if the %map is empty. (Thus begin() would equal | |
415 | * end().) | |
416 | */ | |
417 | bool | |
418 | empty() const | |
419 | { return _M_t.empty(); } | |
ed6814f7 | 420 | |
f6592a9e PC |
421 | /** Returns the size of the %map. */ |
422 | size_type | |
423 | size() const | |
424 | { return _M_t.size(); } | |
ed6814f7 | 425 | |
f6592a9e PC |
426 | /** Returns the maximum size of the %map. */ |
427 | size_type | |
428 | max_size() const | |
429 | { return _M_t.max_size(); } | |
ed6814f7 | 430 | |
f6592a9e PC |
431 | // [23.3.1.2] element access |
432 | /** | |
433 | * @brief Subscript ( @c [] ) access to %map data. | |
434 | * @param k The key for which data should be retrieved. | |
435 | * @return A reference to the data of the (key,data) %pair. | |
436 | * | |
45f388bb BK |
437 | * Allows for easy lookup with the subscript ( @c [] ) |
438 | * operator. Returns data associated with the key specified in | |
439 | * subscript. If the key does not exist, a pair with that key | |
440 | * is created using default values, which is then returned. | |
f6592a9e PC |
441 | * |
442 | * Lookup requires logarithmic time. | |
443 | */ | |
444 | mapped_type& | |
445 | operator[](const key_type& __k) | |
446 | { | |
447 | // concept requirements | |
448 | __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>) | |
ed6814f7 | 449 | |
f6592a9e PC |
450 | iterator __i = lower_bound(__k); |
451 | // __i->first is greater than or equivalent to __k. | |
452 | if (__i == end() || key_comp()(__k, (*__i).first)) | |
83042fca | 453 | __i = insert(__i, value_type(__k, mapped_type())); |
f6592a9e PC |
454 | return (*__i).second; |
455 | } | |
ed6814f7 | 456 | |
e6a05448 PC |
457 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
458 | mapped_type& | |
459 | operator[](key_type&& __k) | |
460 | { | |
461 | // concept requirements | |
462 | __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>) | |
463 | ||
464 | iterator __i = lower_bound(__k); | |
465 | // __i->first is greater than or equivalent to __k. | |
466 | if (__i == end() || key_comp()(__k, (*__i).first)) | |
467 | __i = insert(__i, std::make_pair(std::move(__k), mapped_type())); | |
468 | return (*__i).second; | |
469 | } | |
470 | #endif | |
471 | ||
8b5f07a2 PC |
472 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
473 | // DR 464. Suggestion for new member functions in standard containers. | |
474 | /** | |
475 | * @brief Access to %map data. | |
476 | * @param k The key for which data should be retrieved. | |
e677187e | 477 | * @return A reference to the data whose key is equivalent to @a k, if |
76c6705b | 478 | * such a data is present in the %map. |
8b5f07a2 PC |
479 | * @throw std::out_of_range If no such data is present. |
480 | */ | |
481 | mapped_type& | |
482 | at(const key_type& __k) | |
483 | { | |
484 | iterator __i = lower_bound(__k); | |
485 | if (__i == end() || key_comp()(__k, (*__i).first)) | |
486 | __throw_out_of_range(__N("map::at")); | |
487 | return (*__i).second; | |
488 | } | |
489 | ||
490 | const mapped_type& | |
491 | at(const key_type& __k) const | |
492 | { | |
493 | const_iterator __i = lower_bound(__k); | |
494 | if (__i == end() || key_comp()(__k, (*__i).first)) | |
495 | __throw_out_of_range(__N("map::at")); | |
496 | return (*__i).second; | |
497 | } | |
498 | ||
f6592a9e PC |
499 | // modifiers |
500 | /** | |
501 | * @brief Attempts to insert a std::pair into the %map. | |
45f388bb BK |
502 | |
503 | * @param x Pair to be inserted (see std::make_pair for easy creation | |
504 | * of pairs). | |
505 | ||
506 | * @return A pair, of which the first element is an iterator that | |
507 | * points to the possibly inserted pair, and the second is | |
508 | * a bool that is true if the pair was actually inserted. | |
f6592a9e PC |
509 | * |
510 | * This function attempts to insert a (key, value) %pair into the %map. | |
511 | * A %map relies on unique keys and thus a %pair is only inserted if its | |
512 | * first element (the key) is not already present in the %map. | |
513 | * | |
514 | * Insertion requires logarithmic time. | |
515 | */ | |
eb9af792 | 516 | std::pair<iterator, bool> |
f6592a9e | 517 | insert(const value_type& __x) |
42a27024 | 518 | { return _M_t._M_insert_unique(__x); } |
ed6814f7 | 519 | |
e6a05448 PC |
520 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
521 | template<typename _Pair, typename = typename | |
522 | std::enable_if<std::is_convertible<_Pair, | |
523 | value_type>::value>::type> | |
524 | std::pair<iterator, bool> | |
525 | insert(_Pair&& __x) | |
526 | { return _M_t._M_insert_unique(std::forward<_Pair>(__x)); } | |
527 | #endif | |
528 | ||
6010fae7 | 529 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
09357846 JM |
530 | /** |
531 | * @brief Attempts to insert a list of std::pairs into the %map. | |
532 | * @param list A std::initializer_list<value_type> of pairs to be | |
533 | * inserted. | |
534 | * | |
535 | * Complexity similar to that of the range constructor. | |
09357846 JM |
536 | */ |
537 | void | |
6010fae7 | 538 | insert(std::initializer_list<value_type> __list) |
7606bd11 | 539 | { insert(__list.begin(), __list.end()); } |
6010fae7 | 540 | #endif |
09357846 | 541 | |
f6592a9e PC |
542 | /** |
543 | * @brief Attempts to insert a std::pair into the %map. | |
544 | * @param position An iterator that serves as a hint as to where the | |
545 | * pair should be inserted. | |
45f388bb BK |
546 | * @param x Pair to be inserted (see std::make_pair for easy creation |
547 | * of pairs). | |
f6592a9e PC |
548 | * @return An iterator that points to the element with key of @a x (may |
549 | * or may not be the %pair passed in). | |
550 | * | |
45f388bb BK |
551 | |
552 | * This function is not concerned about whether the insertion | |
553 | * took place, and thus does not return a boolean like the | |
554 | * single-argument insert() does. Note that the first | |
555 | * parameter is only a hint and can potentially improve the | |
556 | * performance of the insertion process. A bad hint would | |
557 | * cause no gains in efficiency. | |
f6592a9e | 558 | * |
45f388bb | 559 | * See |
a40fff0e | 560 | * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html |
2a60a9f6 | 561 | * for more on @a hinting. |
f6592a9e PC |
562 | * |
563 | * Insertion requires logarithmic time (if the hint is not taken). | |
564 | */ | |
565 | iterator | |
7606bd11 PC |
566 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
567 | insert(const_iterator __position, const value_type& __x) | |
568 | #else | |
eb9af792 | 569 | insert(iterator __position, const value_type& __x) |
7606bd11 | 570 | #endif |
eb9af792 | 571 | { return _M_t._M_insert_unique_(__position, __x); } |
ed6814f7 | 572 | |
e6a05448 PC |
573 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
574 | template<typename _Pair, typename = typename | |
575 | std::enable_if<std::is_convertible<_Pair, | |
576 | value_type>::value>::type> | |
577 | iterator | |
578 | insert(const_iterator __position, _Pair&& __x) | |
579 | { return _M_t._M_insert_unique_(__position, | |
580 | std::forward<_Pair>(__x)); } | |
581 | #endif | |
582 | ||
f6592a9e | 583 | /** |
28dac70a | 584 | * @brief Template function that attempts to insert a range of elements. |
f6592a9e PC |
585 | * @param first Iterator pointing to the start of the range to be |
586 | * inserted. | |
587 | * @param last Iterator pointing to the end of the range. | |
588 | * | |
589 | * Complexity similar to that of the range constructor. | |
590 | */ | |
78b36b70 | 591 | template<typename _InputIterator> |
f6592a9e PC |
592 | void |
593 | insert(_InputIterator __first, _InputIterator __last) | |
42a27024 | 594 | { _M_t._M_insert_unique(__first, __last); } |
ed6814f7 | 595 | |
c105751c ESR |
596 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
597 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
598 | // DR 130. Associative erase should return an iterator. | |
599 | /** | |
600 | * @brief Erases an element from a %map. | |
601 | * @param position An iterator pointing to the element to be erased. | |
602 | * @return An iterator pointing to the element immediately following | |
603 | * @a position prior to the element being erased. If no such | |
604 | * element exists, end() is returned. | |
605 | * | |
606 | * This function erases an element, pointed to by the given | |
607 | * iterator, from a %map. Note that this function only erases | |
608 | * the element, and that if the element is itself a pointer, | |
609 | * the pointed-to memory is not touched in any way. Managing | |
610 | * the pointer is the user's responsibility. | |
611 | */ | |
612 | iterator | |
7606bd11 | 613 | erase(const_iterator __position) |
c105751c ESR |
614 | { return _M_t.erase(__position); } |
615 | #else | |
f6592a9e PC |
616 | /** |
617 | * @brief Erases an element from a %map. | |
618 | * @param position An iterator pointing to the element to be erased. | |
619 | * | |
45f388bb BK |
620 | * This function erases an element, pointed to by the given |
621 | * iterator, from a %map. Note that this function only erases | |
622 | * the element, and that if the element is itself a pointer, | |
623 | * the pointed-to memory is not touched in any way. Managing | |
28dac70a | 624 | * the pointer is the user's responsibility. |
f6592a9e | 625 | */ |
3971a4d2 | 626 | void |
f6592a9e PC |
627 | erase(iterator __position) |
628 | { _M_t.erase(__position); } | |
c105751c | 629 | #endif |
ed6814f7 | 630 | |
f6592a9e PC |
631 | /** |
632 | * @brief Erases elements according to the provided key. | |
633 | * @param x Key of element to be erased. | |
634 | * @return The number of elements erased. | |
635 | * | |
636 | * This function erases all the elements located by the given key from | |
637 | * a %map. | |
638 | * Note that this function only erases the element, and that if | |
639 | * the element is itself a pointer, the pointed-to memory is not touched | |
28dac70a | 640 | * in any way. Managing the pointer is the user's responsibility. |
f6592a9e PC |
641 | */ |
642 | size_type | |
643 | erase(const key_type& __x) | |
644 | { return _M_t.erase(__x); } | |
ed6814f7 | 645 | |
c105751c ESR |
646 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
647 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
648 | // DR 130. Associative erase should return an iterator. | |
649 | /** | |
650 | * @brief Erases a [first,last) range of elements from a %map. | |
651 | * @param first Iterator pointing to the start of the range to be | |
652 | * erased. | |
653 | * @param last Iterator pointing to the end of the range to be erased. | |
654 | * @return The iterator @a last. | |
655 | * | |
656 | * This function erases a sequence of elements from a %map. | |
657 | * Note that this function only erases the element, and that if | |
658 | * the element is itself a pointer, the pointed-to memory is not touched | |
659 | * in any way. Managing the pointer is the user's responsibility. | |
660 | */ | |
661 | iterator | |
7606bd11 | 662 | erase(const_iterator __first, const_iterator __last) |
c105751c ESR |
663 | { return _M_t.erase(__first, __last); } |
664 | #else | |
f6592a9e PC |
665 | /** |
666 | * @brief Erases a [first,last) range of elements from a %map. | |
667 | * @param first Iterator pointing to the start of the range to be | |
668 | * erased. | |
669 | * @param last Iterator pointing to the end of the range to be erased. | |
670 | * | |
671 | * This function erases a sequence of elements from a %map. | |
672 | * Note that this function only erases the element, and that if | |
673 | * the element is itself a pointer, the pointed-to memory is not touched | |
28dac70a | 674 | * in any way. Managing the pointer is the user's responsibility. |
f6592a9e PC |
675 | */ |
676 | void | |
677 | erase(iterator __first, iterator __last) | |
678 | { _M_t.erase(__first, __last); } | |
c105751c | 679 | #endif |
ed6814f7 | 680 | |
f6592a9e PC |
681 | /** |
682 | * @brief Swaps data with another %map. | |
683 | * @param x A %map of the same element and allocator types. | |
684 | * | |
45f388bb BK |
685 | * This exchanges the elements between two maps in constant |
686 | * time. (It is only swapping a pointer, an integer, and an | |
687 | * instance of the @c Compare type (which itself is often | |
688 | * stateless and empty), so it should be quite fast.) Note | |
689 | * that the global std::swap() function is specialized such | |
690 | * that std::swap(m1,m2) will feed to this function. | |
f6592a9e PC |
691 | */ |
692 | void | |
693 | swap(map& __x) | |
694 | { _M_t.swap(__x._M_t); } | |
ed6814f7 | 695 | |
f6592a9e | 696 | /** |
45f388bb BK |
697 | * Erases all elements in a %map. Note that this function only |
698 | * erases the elements, and that if the elements themselves are | |
699 | * pointers, the pointed-to memory is not touched in any way. | |
28dac70a | 700 | * Managing the pointer is the user's responsibility. |
f6592a9e PC |
701 | */ |
702 | void | |
703 | clear() | |
704 | { _M_t.clear(); } | |
ed6814f7 | 705 | |
f6592a9e PC |
706 | // observers |
707 | /** | |
708 | * Returns the key comparison object out of which the %map was | |
709 | * constructed. | |
710 | */ | |
711 | key_compare | |
712 | key_comp() const | |
713 | { return _M_t.key_comp(); } | |
ed6814f7 | 714 | |
f6592a9e PC |
715 | /** |
716 | * Returns a value comparison object, built from the key comparison | |
717 | * object out of which the %map was constructed. | |
718 | */ | |
719 | value_compare | |
720 | value_comp() const | |
721 | { return value_compare(_M_t.key_comp()); } | |
ed6814f7 | 722 | |
f6592a9e PC |
723 | // [23.3.1.3] map operations |
724 | /** | |
725 | * @brief Tries to locate an element in a %map. | |
726 | * @param x Key of (key, value) %pair to be located. | |
727 | * @return Iterator pointing to sought-after element, or end() if not | |
728 | * found. | |
729 | * | |
730 | * This function takes a key and tries to locate the element with which | |
731 | * the key matches. If successful the function returns an iterator | |
732 | * pointing to the sought after %pair. If unsuccessful it returns the | |
733 | * past-the-end ( @c end() ) iterator. | |
734 | */ | |
735 | iterator | |
736 | find(const key_type& __x) | |
737 | { return _M_t.find(__x); } | |
ed6814f7 | 738 | |
f6592a9e PC |
739 | /** |
740 | * @brief Tries to locate an element in a %map. | |
741 | * @param x Key of (key, value) %pair to be located. | |
742 | * @return Read-only (constant) iterator pointing to sought-after | |
743 | * element, or end() if not found. | |
744 | * | |
745 | * This function takes a key and tries to locate the element with which | |
746 | * the key matches. If successful the function returns a constant | |
747 | * iterator pointing to the sought after %pair. If unsuccessful it | |
748 | * returns the past-the-end ( @c end() ) iterator. | |
749 | */ | |
750 | const_iterator | |
751 | find(const key_type& __x) const | |
752 | { return _M_t.find(__x); } | |
ed6814f7 | 753 | |
f6592a9e PC |
754 | /** |
755 | * @brief Finds the number of elements with given key. | |
756 | * @param x Key of (key, value) pairs to be located. | |
757 | * @return Number of elements with specified key. | |
758 | * | |
759 | * This function only makes sense for multimaps; for map the result will | |
760 | * either be 0 (not present) or 1 (present). | |
761 | */ | |
762 | size_type | |
763 | count(const key_type& __x) const | |
764 | { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } | |
ed6814f7 | 765 | |
f6592a9e PC |
766 | /** |
767 | * @brief Finds the beginning of a subsequence matching given key. | |
768 | * @param x Key of (key, value) pair to be located. | |
769 | * @return Iterator pointing to first element equal to or greater | |
770 | * than key, or end(). | |
771 | * | |
772 | * This function returns the first element of a subsequence of elements | |
773 | * that matches the given key. If unsuccessful it returns an iterator | |
774 | * pointing to the first element that has a greater value than given key | |
775 | * or end() if no such element exists. | |
776 | */ | |
777 | iterator | |
778 | lower_bound(const key_type& __x) | |
779 | { return _M_t.lower_bound(__x); } | |
ed6814f7 | 780 | |
f6592a9e PC |
781 | /** |
782 | * @brief Finds the beginning of a subsequence matching given key. | |
783 | * @param x Key of (key, value) pair to be located. | |
784 | * @return Read-only (constant) iterator pointing to first element | |
785 | * equal to or greater than key, or end(). | |
786 | * | |
787 | * This function returns the first element of a subsequence of elements | |
788 | * that matches the given key. If unsuccessful it returns an iterator | |
789 | * pointing to the first element that has a greater value than given key | |
790 | * or end() if no such element exists. | |
791 | */ | |
792 | const_iterator | |
793 | lower_bound(const key_type& __x) const | |
794 | { return _M_t.lower_bound(__x); } | |
ed6814f7 | 795 | |
f6592a9e PC |
796 | /** |
797 | * @brief Finds the end of a subsequence matching given key. | |
798 | * @param x Key of (key, value) pair to be located. | |
799 | * @return Iterator pointing to the first element | |
800 | * greater than key, or end(). | |
801 | */ | |
802 | iterator | |
803 | upper_bound(const key_type& __x) | |
804 | { return _M_t.upper_bound(__x); } | |
ed6814f7 | 805 | |
f6592a9e PC |
806 | /** |
807 | * @brief Finds the end of a subsequence matching given key. | |
808 | * @param x Key of (key, value) pair to be located. | |
809 | * @return Read-only (constant) iterator pointing to first iterator | |
810 | * greater than key, or end(). | |
811 | */ | |
812 | const_iterator | |
813 | upper_bound(const key_type& __x) const | |
814 | { return _M_t.upper_bound(__x); } | |
ed6814f7 | 815 | |
f6592a9e PC |
816 | /** |
817 | * @brief Finds a subsequence matching given key. | |
818 | * @param x Key of (key, value) pairs to be located. | |
819 | * @return Pair of iterators that possibly points to the subsequence | |
820 | * matching given key. | |
821 | * | |
822 | * This function is equivalent to | |
823 | * @code | |
824 | * std::make_pair(c.lower_bound(val), | |
825 | * c.upper_bound(val)) | |
826 | * @endcode | |
827 | * (but is faster than making the calls separately). | |
828 | * | |
829 | * This function probably only makes sense for multimaps. | |
830 | */ | |
4fd20a8f | 831 | std::pair<iterator, iterator> |
f6592a9e PC |
832 | equal_range(const key_type& __x) |
833 | { return _M_t.equal_range(__x); } | |
ed6814f7 | 834 | |
f6592a9e PC |
835 | /** |
836 | * @brief Finds a subsequence matching given key. | |
837 | * @param x Key of (key, value) pairs to be located. | |
838 | * @return Pair of read-only (constant) iterators that possibly points | |
839 | * to the subsequence matching given key. | |
840 | * | |
841 | * This function is equivalent to | |
842 | * @code | |
843 | * std::make_pair(c.lower_bound(val), | |
844 | * c.upper_bound(val)) | |
845 | * @endcode | |
846 | * (but is faster than making the calls separately). | |
847 | * | |
848 | * This function probably only makes sense for multimaps. | |
849 | */ | |
4fd20a8f | 850 | std::pair<const_iterator, const_iterator> |
f6592a9e PC |
851 | equal_range(const key_type& __x) const |
852 | { return _M_t.equal_range(__x); } | |
ed6814f7 | 853 | |
78b36b70 | 854 | template<typename _K1, typename _T1, typename _C1, typename _A1> |
737ab798 | 855 | friend bool |
78b36b70 PC |
856 | operator==(const map<_K1, _T1, _C1, _A1>&, |
857 | const map<_K1, _T1, _C1, _A1>&); | |
737ab798 | 858 | |
78b36b70 | 859 | template<typename _K1, typename _T1, typename _C1, typename _A1> |
737ab798 | 860 | friend bool |
78b36b70 PC |
861 | operator<(const map<_K1, _T1, _C1, _A1>&, |
862 | const map<_K1, _T1, _C1, _A1>&); | |
f6592a9e | 863 | }; |
ed6814f7 | 864 | |
3971a4d2 PE |
865 | /** |
866 | * @brief Map equality comparison. | |
867 | * @param x A %map. | |
868 | * @param y A %map of the same type as @a x. | |
869 | * @return True iff the size and elements of the maps are equal. | |
fd58f127 | 870 | * |
3971a4d2 PE |
871 | * This is an equivalence relation. It is linear in the size of the |
872 | * maps. Maps are considered equivalent if their sizes are equal, | |
873 | * and if corresponding elements compare equal. | |
874 | */ | |
78b36b70 | 875 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
3971a4d2 | 876 | inline bool |
4fd20a8f PC |
877 | operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x, |
878 | const map<_Key, _Tp, _Compare, _Alloc>& __y) | |
3971a4d2 | 879 | { return __x._M_t == __y._M_t; } |
ed6814f7 | 880 | |
3971a4d2 PE |
881 | /** |
882 | * @brief Map ordering relation. | |
883 | * @param x A %map. | |
884 | * @param y A %map of the same type as @a x. | |
9536ca34 | 885 | * @return True iff @a x is lexicographically less than @a y. |
fd58f127 | 886 | * |
3971a4d2 PE |
887 | * This is a total ordering relation. It is linear in the size of the |
888 | * maps. The elements must be comparable with @c <. | |
fd58f127 | 889 | * |
9536ca34 | 890 | * See std::lexicographical_compare() for how the determination is made. |
3971a4d2 | 891 | */ |
78b36b70 | 892 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
3971a4d2 | 893 | inline bool |
4fd20a8f PC |
894 | operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x, |
895 | const map<_Key, _Tp, _Compare, _Alloc>& __y) | |
3971a4d2 | 896 | { return __x._M_t < __y._M_t; } |
ed6814f7 | 897 | |
3971a4d2 | 898 | /// Based on operator== |
78b36b70 | 899 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
3971a4d2 | 900 | inline bool |
4fd20a8f PC |
901 | operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x, |
902 | const map<_Key, _Tp, _Compare, _Alloc>& __y) | |
3971a4d2 | 903 | { return !(__x == __y); } |
ed6814f7 | 904 | |
3971a4d2 | 905 | /// Based on operator< |
78b36b70 | 906 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
3971a4d2 | 907 | inline bool |
4fd20a8f PC |
908 | operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x, |
909 | const map<_Key, _Tp, _Compare, _Alloc>& __y) | |
3971a4d2 | 910 | { return __y < __x; } |
ed6814f7 | 911 | |
3971a4d2 | 912 | /// Based on operator< |
78b36b70 | 913 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
3971a4d2 | 914 | inline bool |
4fd20a8f PC |
915 | operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x, |
916 | const map<_Key, _Tp, _Compare, _Alloc>& __y) | |
3971a4d2 | 917 | { return !(__y < __x); } |
ed6814f7 | 918 | |
3971a4d2 | 919 | /// Based on operator< |
78b36b70 | 920 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
3971a4d2 | 921 | inline bool |
4fd20a8f PC |
922 | operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x, |
923 | const map<_Key, _Tp, _Compare, _Alloc>& __y) | |
3971a4d2 | 924 | { return !(__x < __y); } |
ed6814f7 | 925 | |
3971a4d2 | 926 | /// See std::map::swap(). |
78b36b70 | 927 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
3971a4d2 | 928 | inline void |
4fd20a8f PC |
929 | swap(map<_Key, _Tp, _Compare, _Alloc>& __x, |
930 | map<_Key, _Tp, _Compare, _Alloc>& __y) | |
3971a4d2 | 931 | { __x.swap(__y); } |
3cbc7af0 | 932 | |
12ffa228 BK |
933 | _GLIBCXX_END_NAMESPACE_CONTAINER |
934 | } // namespace std | |
725dc051 | 935 | |
046d30f4 | 936 | #endif /* _STL_MAP_H */ |