]>
Commit | Line | Data |
---|---|---|
1d61409b | 1 | // Map implementation -*- C++ -*- |
2 | ||
f1717362 | 3 | // Copyright (C) 2001-2016 Free Software Foundation, Inc. |
1d61409b | 4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
6bc9506f | 8 | // Free Software Foundation; either version 3, or (at your option) |
1d61409b | 9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
6bc9506f | 16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
1d61409b | 19 | |
6bc9506f | 20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
1d61409b | 24 | |
1d487aca | 25 | /* |
26 | * | |
27 | * Copyright (c) 1994 | |
28 | * Hewlett-Packard Company | |
29 | * | |
30 | * Permission to use, copy, modify, distribute and sell this software | |
31 | * and its documentation for any purpose is hereby granted without fee, | |
32 | * provided that the above copyright notice appear in all copies and | |
33 | * that both that copyright notice and this permission notice appear | |
34 | * in supporting documentation. Hewlett-Packard Company makes no | |
35 | * representations about the suitability of this software for any | |
36 | * purpose. It is provided "as is" without express or implied warranty. | |
37 | * | |
38 | * | |
39 | * Copyright (c) 1996,1997 | |
40 | * Silicon Graphics Computer Systems, Inc. | |
41 | * | |
42 | * Permission to use, copy, modify, distribute and sell this software | |
43 | * and its documentation for any purpose is hereby granted without fee, | |
44 | * provided that the above copyright notice appear in all copies and | |
45 | * that both that copyright notice and this permission notice appear | |
46 | * in supporting documentation. Silicon Graphics makes no | |
47 | * representations about the suitability of this software for any | |
48 | * purpose. It is provided "as is" without express or implied warranty. | |
49 | */ | |
50 | ||
5846aeac | 51 | /** @file bits/stl_map.h |
7a472ef1 | 52 | * This is an internal header file, included by other library headers. |
5846aeac | 53 | * Do not attempt to use it directly. @headername{map} |
1d487aca | 54 | */ |
55 | ||
f142a34a | 56 | #ifndef _STL_MAP_H |
57 | #define _STL_MAP_H 1 | |
1d487aca | 58 | |
e227b159 | 59 | #include <bits/functexcept.h> |
40b55285 | 60 | #include <bits/concept_check.h> |
0c8766b1 | 61 | #if __cplusplus >= 201103L |
f82f1250 | 62 | #include <initializer_list> |
6b9314a8 | 63 | #include <tuple> |
fbb4cdfc | 64 | #endif |
1d487aca | 65 | |
2948dd21 | 66 | namespace std _GLIBCXX_VISIBILITY(default) |
67 | { | |
68 | _GLIBCXX_BEGIN_NAMESPACE_CONTAINER | |
1069247d | 69 | |
f344810f | 70 | /** |
32bb2cd7 | 71 | * @brief A standard container made up of (key,value) pairs, which can be |
72 | * retrieved based on a key, in logarithmic time. | |
f344810f | 73 | * |
48e3f567 | 74 | * @ingroup associative_containers |
f344810f | 75 | * |
7184845c | 76 | * @tparam _Key Type of key objects. |
77 | * @tparam _Tp Type of mapped objects. | |
78 | * @tparam _Compare Comparison function object type, defaults to less<_Key>. | |
79 | * @tparam _Alloc Allocator type, defaults to | |
80 | * allocator<pair<const _Key, _Tp>. | |
81 | * | |
32bb2cd7 | 82 | * Meets the requirements of a <a href="tables.html#65">container</a>, a |
83 | * <a href="tables.html#66">reversible container</a>, and an | |
84 | * <a href="tables.html#69">associative container</a> (using unique keys). | |
85 | * For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the | |
86 | * value_type is std::pair<const Key,T>. | |
f344810f | 87 | * |
32bb2cd7 | 88 | * Maps support bidirectional iterators. |
f344810f | 89 | * |
32bb2cd7 | 90 | * The private tree data is declared exactly the same way for map and |
91 | * multimap; the distinction is made entirely in how the tree functions are | |
92 | * called (*_unique versus *_equal, same as the standard). | |
32bb2cd7 | 93 | */ |
be7e699b | 94 | template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>, |
95 | typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > > | |
32bb2cd7 | 96 | class map |
b9156b5c | 97 | { |
62ba3194 | 98 | public: |
b9156b5c | 99 | typedef _Key key_type; |
100 | typedef _Tp mapped_type; | |
be7e699b | 101 | typedef std::pair<const _Key, _Tp> value_type; |
b9156b5c | 102 | typedef _Compare key_compare; |
171a395a | 103 | typedef _Alloc allocator_type; |
104 | ||
105 | private: | |
106 | // concept requirements | |
107 | typedef typename _Alloc::value_type _Alloc_value_type; | |
108 | __glibcxx_class_requires(_Tp, _SGIAssignableConcept) | |
109 | __glibcxx_class_requires4(_Compare, bool, _Key, _Key, | |
110 | _BinaryFunctionConcept) | |
111 | __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept) | |
bae9b8af | 112 | |
171a395a | 113 | public: |
b9156b5c | 114 | class value_compare |
be7e699b | 115 | : public std::binary_function<value_type, value_type, bool> |
32bb2cd7 | 116 | { |
171a395a | 117 | friend class map<_Key, _Tp, _Compare, _Alloc>; |
32bb2cd7 | 118 | protected: |
b9156b5c | 119 | _Compare comp; |
bae9b8af | 120 | |
b9156b5c | 121 | value_compare(_Compare __c) |
62ba3194 | 122 | : comp(__c) { } |
bae9b8af | 123 | |
32bb2cd7 | 124 | public: |
b9156b5c | 125 | bool operator()(const value_type& __x, const value_type& __y) const |
126 | { return comp(__x.first, __y.first); } | |
32bb2cd7 | 127 | }; |
bae9b8af | 128 | |
b9156b5c | 129 | private: |
0aeadebf | 130 | /// This turns a red-black tree into a [multi]map. |
709dc991 | 131 | typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template |
132 | rebind<value_type>::other _Pair_alloc_type; | |
171a395a | 133 | |
134 | typedef _Rb_tree<key_type, value_type, _Select1st<value_type>, | |
135 | key_compare, _Pair_alloc_type> _Rep_type; | |
136 | ||
0aeadebf | 137 | /// The actual tree structure. |
b9156b5c | 138 | _Rep_type _M_t; |
bae9b8af | 139 | |
709dc991 | 140 | typedef __gnu_cxx::__alloc_traits<_Pair_alloc_type> _Alloc_traits; |
141 | ||
b9156b5c | 142 | public: |
143 | // many of these are specified differently in ISO, but the following are | |
144 | // "functionally equivalent" | |
709dc991 | 145 | typedef typename _Alloc_traits::pointer pointer; |
146 | typedef typename _Alloc_traits::const_pointer const_pointer; | |
147 | typedef typename _Alloc_traits::reference reference; | |
148 | typedef typename _Alloc_traits::const_reference const_reference; | |
cdb016e8 | 149 | typedef typename _Rep_type::iterator iterator; |
150 | typedef typename _Rep_type::const_iterator const_iterator; | |
151 | typedef typename _Rep_type::size_type size_type; | |
152 | typedef typename _Rep_type::difference_type difference_type; | |
153 | typedef typename _Rep_type::reverse_iterator reverse_iterator; | |
154 | typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator; | |
bae9b8af | 155 | |
b9156b5c | 156 | // [23.3.1.1] construct/copy/destroy |
fc04fffc | 157 | // (get_allocator() is also listed in this section) |
158 | ||
b9156b5c | 159 | /** |
160 | * @brief Default constructor creates no elements. | |
161 | */ | |
162 | map() | |
96177d13 | 163 | #if __cplusplus >= 201103L |
164 | noexcept(is_nothrow_default_constructible<allocator_type>::value) | |
165 | #endif | |
707bff49 | 166 | : _M_t() { } |
bae9b8af | 167 | |
b9156b5c | 168 | /** |
707bff49 | 169 | * @brief Creates a %map with no elements. |
e12e4f3b | 170 | * @param __comp A comparison object. |
171 | * @param __a An allocator object. | |
b9156b5c | 172 | */ |
173 | explicit | |
707bff49 | 174 | map(const _Compare& __comp, |
175 | const allocator_type& __a = allocator_type()) | |
852b4cc2 | 176 | : _M_t(__comp, _Pair_alloc_type(__a)) { } |
bae9b8af | 177 | |
b9156b5c | 178 | /** |
707bff49 | 179 | * @brief %Map copy constructor. |
e12e4f3b | 180 | * @param __x A %map of identical element and allocator types. |
b9156b5c | 181 | * |
707bff49 | 182 | * The newly-created %map uses a copy of the allocation object |
e12e4f3b | 183 | * used by @a __x. |
b9156b5c | 184 | */ |
185 | map(const map& __x) | |
32bb2cd7 | 186 | : _M_t(__x._M_t) { } |
bae9b8af | 187 | |
0c8766b1 | 188 | #if __cplusplus >= 201103L |
707bff49 | 189 | /** |
190 | * @brief %Map move constructor. | |
e12e4f3b | 191 | * @param __x A %map of identical element and allocator types. |
707bff49 | 192 | * |
e12e4f3b | 193 | * The newly-created %map contains the exact contents of @a __x. |
194 | * The contents of @a __x are a valid, but unspecified %map. | |
707bff49 | 195 | */ |
196 | map(map&& __x) | |
c95bf15b | 197 | noexcept(is_nothrow_copy_constructible<_Compare>::value) |
d01a2eb2 | 198 | : _M_t(std::move(__x._M_t)) { } |
b23fdac1 | 199 | |
200 | /** | |
201 | * @brief Builds a %map from an initializer_list. | |
e12e4f3b | 202 | * @param __l An initializer_list. |
203 | * @param __comp A comparison object. | |
204 | * @param __a An allocator object. | |
b23fdac1 | 205 | * |
206 | * Create a %map consisting of copies of the elements in the | |
e12e4f3b | 207 | * initializer_list @a __l. |
b23fdac1 | 208 | * This is linear in N if the range is already sorted, and NlogN |
e12e4f3b | 209 | * otherwise (where N is @a __l.size()). |
b23fdac1 | 210 | */ |
211 | map(initializer_list<value_type> __l, | |
e12e4f3b | 212 | const _Compare& __comp = _Compare(), |
b23fdac1 | 213 | const allocator_type& __a = allocator_type()) |
852b4cc2 | 214 | : _M_t(__comp, _Pair_alloc_type(__a)) |
b23fdac1 | 215 | { _M_t._M_insert_unique(__l.begin(), __l.end()); } |
709dc991 | 216 | |
217 | /// Allocator-extended default constructor. | |
218 | explicit | |
219 | map(const allocator_type& __a) | |
220 | : _M_t(_Compare(), _Pair_alloc_type(__a)) { } | |
221 | ||
222 | /// Allocator-extended copy constructor. | |
223 | map(const map& __m, const allocator_type& __a) | |
224 | : _M_t(__m._M_t, _Pair_alloc_type(__a)) { } | |
225 | ||
226 | /// Allocator-extended move constructor. | |
227 | map(map&& __m, const allocator_type& __a) | |
228 | noexcept(is_nothrow_copy_constructible<_Compare>::value | |
229 | && _Alloc_traits::_S_always_equal()) | |
230 | : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { } | |
231 | ||
232 | /// Allocator-extended initialier-list constructor. | |
233 | map(initializer_list<value_type> __l, const allocator_type& __a) | |
234 | : _M_t(_Compare(), _Pair_alloc_type(__a)) | |
235 | { _M_t._M_insert_unique(__l.begin(), __l.end()); } | |
236 | ||
237 | /// Allocator-extended range constructor. | |
238 | template<typename _InputIterator> | |
239 | map(_InputIterator __first, _InputIterator __last, | |
240 | const allocator_type& __a) | |
241 | : _M_t(_Compare(), _Pair_alloc_type(__a)) | |
242 | { _M_t._M_insert_unique(__first, __last); } | |
707bff49 | 243 | #endif |
244 | ||
b9156b5c | 245 | /** |
246 | * @brief Builds a %map from a range. | |
e12e4f3b | 247 | * @param __first An input iterator. |
248 | * @param __last An input iterator. | |
b9156b5c | 249 | * |
e12e4f3b | 250 | * Create a %map consisting of copies of the elements from |
251 | * [__first,__last). This is linear in N if the range is | |
252 | * already sorted, and NlogN otherwise (where N is | |
253 | * distance(__first,__last)). | |
b9156b5c | 254 | */ |
707bff49 | 255 | template<typename _InputIterator> |
b9156b5c | 256 | map(_InputIterator __first, _InputIterator __last) |
707bff49 | 257 | : _M_t() |
8474dcf5 | 258 | { _M_t._M_insert_unique(__first, __last); } |
bae9b8af | 259 | |
b9156b5c | 260 | /** |
261 | * @brief Builds a %map from a range. | |
e12e4f3b | 262 | * @param __first An input iterator. |
263 | * @param __last An input iterator. | |
264 | * @param __comp A comparison functor. | |
265 | * @param __a An allocator object. | |
b9156b5c | 266 | * |
e12e4f3b | 267 | * Create a %map consisting of copies of the elements from |
268 | * [__first,__last). This is linear in N if the range is | |
269 | * already sorted, and NlogN otherwise (where N is | |
270 | * distance(__first,__last)). | |
b9156b5c | 271 | */ |
707bff49 | 272 | template<typename _InputIterator> |
b9156b5c | 273 | map(_InputIterator __first, _InputIterator __last, |
707bff49 | 274 | const _Compare& __comp, |
275 | const allocator_type& __a = allocator_type()) | |
852b4cc2 | 276 | : _M_t(__comp, _Pair_alloc_type(__a)) |
8474dcf5 | 277 | { _M_t._M_insert_unique(__first, __last); } |
bae9b8af | 278 | |
10c73e3f | 279 | // FIXME There is no dtor declared, but we should have something |
280 | // generated by Doxygen. I don't know what tags to add to this | |
281 | // paragraph to make that happen: | |
b9156b5c | 282 | /** |
283 | * The dtor only erases the elements, and note that if the elements | |
284 | * themselves are pointers, the pointed-to memory is not touched in any | |
9fc1117c | 285 | * way. Managing the pointer is the user's responsibility. |
b9156b5c | 286 | */ |
bae9b8af | 287 | |
b9156b5c | 288 | /** |
707bff49 | 289 | * @brief %Map assignment operator. |
e12e4f3b | 290 | * @param __x A %map of identical element and allocator types. |
b9156b5c | 291 | * |
e12e4f3b | 292 | * All the elements of @a __x are copied, but unlike the copy |
293 | * constructor, the allocator object is not copied. | |
b9156b5c | 294 | */ |
295 | map& | |
296 | operator=(const map& __x) | |
297 | { | |
298 | _M_t = __x._M_t; | |
299 | return *this; | |
300 | } | |
bae9b8af | 301 | |
0c8766b1 | 302 | #if __cplusplus >= 201103L |
39e522ee | 303 | /// Move assignment operator. |
707bff49 | 304 | map& |
39e522ee | 305 | operator=(map&&) = default; |
b23fdac1 | 306 | |
307 | /** | |
308 | * @brief %Map list assignment operator. | |
e12e4f3b | 309 | * @param __l An initializer_list. |
b23fdac1 | 310 | * |
311 | * This function fills a %map with copies of the elements in the | |
e12e4f3b | 312 | * initializer list @a __l. |
b23fdac1 | 313 | * |
314 | * Note that the assignment completely changes the %map and | |
315 | * that the resulting %map's size is the same as the number | |
316 | * of elements assigned. Old data may be lost. | |
317 | */ | |
318 | map& | |
319 | operator=(initializer_list<value_type> __l) | |
320 | { | |
39e522ee | 321 | _M_t._M_assign_unique(__l.begin(), __l.end()); |
b23fdac1 | 322 | return *this; |
323 | } | |
707bff49 | 324 | #endif |
325 | ||
b9156b5c | 326 | /// Get a copy of the memory allocation object. |
327 | allocator_type | |
7cd718fd | 328 | get_allocator() const _GLIBCXX_NOEXCEPT |
852b4cc2 | 329 | { return allocator_type(_M_t.get_allocator()); } |
bae9b8af | 330 | |
b9156b5c | 331 | // iterators |
332 | /** | |
bae9b8af | 333 | * Returns a read/write iterator that points to the first pair in the |
b9156b5c | 334 | * %map. |
335 | * Iteration is done in ascending order according to the keys. | |
336 | */ | |
337 | iterator | |
7cd718fd | 338 | begin() _GLIBCXX_NOEXCEPT |
b9156b5c | 339 | { return _M_t.begin(); } |
bae9b8af | 340 | |
b9156b5c | 341 | /** |
342 | * Returns a read-only (constant) iterator that points to the first pair | |
343 | * in the %map. Iteration is done in ascending order according to the | |
344 | * keys. | |
345 | */ | |
346 | const_iterator | |
7cd718fd | 347 | begin() const _GLIBCXX_NOEXCEPT |
b9156b5c | 348 | { return _M_t.begin(); } |
bae9b8af | 349 | |
b9156b5c | 350 | /** |
10c73e3f | 351 | * Returns a read/write iterator that points one past the last |
352 | * pair in the %map. Iteration is done in ascending order | |
353 | * according to the keys. | |
b9156b5c | 354 | */ |
355 | iterator | |
7cd718fd | 356 | end() _GLIBCXX_NOEXCEPT |
b9156b5c | 357 | { return _M_t.end(); } |
bae9b8af | 358 | |
b9156b5c | 359 | /** |
360 | * Returns a read-only (constant) iterator that points one past the last | |
361 | * pair in the %map. Iteration is done in ascending order according to | |
362 | * the keys. | |
363 | */ | |
364 | const_iterator | |
7cd718fd | 365 | end() const _GLIBCXX_NOEXCEPT |
b9156b5c | 366 | { return _M_t.end(); } |
bae9b8af | 367 | |
b9156b5c | 368 | /** |
369 | * Returns a read/write reverse iterator that points to the last pair in | |
370 | * the %map. Iteration is done in descending order according to the | |
371 | * keys. | |
372 | */ | |
373 | reverse_iterator | |
7cd718fd | 374 | rbegin() _GLIBCXX_NOEXCEPT |
b9156b5c | 375 | { return _M_t.rbegin(); } |
bae9b8af | 376 | |
b9156b5c | 377 | /** |
378 | * Returns a read-only (constant) reverse iterator that points to the | |
379 | * last pair in the %map. Iteration is done in descending order | |
380 | * according to the keys. | |
381 | */ | |
382 | const_reverse_iterator | |
7cd718fd | 383 | rbegin() const _GLIBCXX_NOEXCEPT |
b9156b5c | 384 | { return _M_t.rbegin(); } |
bae9b8af | 385 | |
b9156b5c | 386 | /** |
387 | * Returns a read/write reverse iterator that points to one before the | |
388 | * first pair in the %map. Iteration is done in descending order | |
389 | * according to the keys. | |
390 | */ | |
391 | reverse_iterator | |
7cd718fd | 392 | rend() _GLIBCXX_NOEXCEPT |
b9156b5c | 393 | { return _M_t.rend(); } |
bae9b8af | 394 | |
b9156b5c | 395 | /** |
396 | * Returns a read-only (constant) reverse iterator that points to one | |
397 | * before the first pair in the %map. Iteration is done in descending | |
398 | * order according to the keys. | |
399 | */ | |
400 | const_reverse_iterator | |
7cd718fd | 401 | rend() const _GLIBCXX_NOEXCEPT |
b9156b5c | 402 | { return _M_t.rend(); } |
bae9b8af | 403 | |
0c8766b1 | 404 | #if __cplusplus >= 201103L |
c40b2e37 | 405 | /** |
406 | * Returns a read-only (constant) iterator that points to the first pair | |
407 | * in the %map. Iteration is done in ascending order according to the | |
408 | * keys. | |
409 | */ | |
410 | const_iterator | |
7cd718fd | 411 | cbegin() const noexcept |
c40b2e37 | 412 | { return _M_t.begin(); } |
413 | ||
414 | /** | |
415 | * Returns a read-only (constant) iterator that points one past the last | |
416 | * pair in the %map. Iteration is done in ascending order according to | |
417 | * the keys. | |
418 | */ | |
419 | const_iterator | |
7cd718fd | 420 | cend() const noexcept |
c40b2e37 | 421 | { return _M_t.end(); } |
422 | ||
423 | /** | |
424 | * Returns a read-only (constant) reverse iterator that points to the | |
425 | * last pair in the %map. Iteration is done in descending order | |
426 | * according to the keys. | |
427 | */ | |
428 | const_reverse_iterator | |
7cd718fd | 429 | crbegin() const noexcept |
c40b2e37 | 430 | { return _M_t.rbegin(); } |
431 | ||
432 | /** | |
433 | * Returns a read-only (constant) reverse iterator that points to one | |
434 | * before the first pair in the %map. Iteration is done in descending | |
435 | * order according to the keys. | |
436 | */ | |
437 | const_reverse_iterator | |
7cd718fd | 438 | crend() const noexcept |
c40b2e37 | 439 | { return _M_t.rend(); } |
440 | #endif | |
441 | ||
b9156b5c | 442 | // capacity |
443 | /** Returns true if the %map is empty. (Thus begin() would equal | |
444 | * end().) | |
445 | */ | |
446 | bool | |
7cd718fd | 447 | empty() const _GLIBCXX_NOEXCEPT |
b9156b5c | 448 | { return _M_t.empty(); } |
bae9b8af | 449 | |
b9156b5c | 450 | /** Returns the size of the %map. */ |
451 | size_type | |
7cd718fd | 452 | size() const _GLIBCXX_NOEXCEPT |
b9156b5c | 453 | { return _M_t.size(); } |
bae9b8af | 454 | |
b9156b5c | 455 | /** Returns the maximum size of the %map. */ |
456 | size_type | |
7cd718fd | 457 | max_size() const _GLIBCXX_NOEXCEPT |
b9156b5c | 458 | { return _M_t.max_size(); } |
bae9b8af | 459 | |
b9156b5c | 460 | // [23.3.1.2] element access |
461 | /** | |
462 | * @brief Subscript ( @c [] ) access to %map data. | |
e12e4f3b | 463 | * @param __k The key for which data should be retrieved. |
b9156b5c | 464 | * @return A reference to the data of the (key,data) %pair. |
465 | * | |
10c73e3f | 466 | * Allows for easy lookup with the subscript ( @c [] ) |
467 | * operator. Returns data associated with the key specified in | |
468 | * subscript. If the key does not exist, a pair with that key | |
469 | * is created using default values, which is then returned. | |
b9156b5c | 470 | * |
471 | * Lookup requires logarithmic time. | |
472 | */ | |
473 | mapped_type& | |
474 | operator[](const key_type& __k) | |
475 | { | |
476 | // concept requirements | |
477 | __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>) | |
bae9b8af | 478 | |
b9156b5c | 479 | iterator __i = lower_bound(__k); |
480 | // __i->first is greater than or equivalent to __k. | |
481 | if (__i == end() || key_comp()(__k, (*__i).first)) | |
0c8766b1 | 482 | #if __cplusplus >= 201103L |
6b9314a8 | 483 | __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct, |
484 | std::tuple<const key_type&>(__k), | |
485 | std::tuple<>()); | |
486 | #else | |
7b7cb326 | 487 | __i = insert(__i, value_type(__k, mapped_type())); |
6b9314a8 | 488 | #endif |
b9156b5c | 489 | return (*__i).second; |
490 | } | |
bae9b8af | 491 | |
0c8766b1 | 492 | #if __cplusplus >= 201103L |
3e469200 | 493 | mapped_type& |
494 | operator[](key_type&& __k) | |
495 | { | |
496 | // concept requirements | |
497 | __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>) | |
498 | ||
499 | iterator __i = lower_bound(__k); | |
500 | // __i->first is greater than or equivalent to __k. | |
501 | if (__i == end() || key_comp()(__k, (*__i).first)) | |
6b9314a8 | 502 | __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct, |
503 | std::forward_as_tuple(std::move(__k)), | |
504 | std::tuple<>()); | |
3e469200 | 505 | return (*__i).second; |
506 | } | |
507 | #endif | |
508 | ||
e227b159 | 509 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
510 | // DR 464. Suggestion for new member functions in standard containers. | |
511 | /** | |
512 | * @brief Access to %map data. | |
e12e4f3b | 513 | * @param __k The key for which data should be retrieved. |
514 | * @return A reference to the data whose key is equivalent to @a __k, if | |
193716a1 | 515 | * such a data is present in the %map. |
e227b159 | 516 | * @throw std::out_of_range If no such data is present. |
517 | */ | |
518 | mapped_type& | |
519 | at(const key_type& __k) | |
520 | { | |
521 | iterator __i = lower_bound(__k); | |
522 | if (__i == end() || key_comp()(__k, (*__i).first)) | |
523 | __throw_out_of_range(__N("map::at")); | |
524 | return (*__i).second; | |
525 | } | |
526 | ||
527 | const mapped_type& | |
528 | at(const key_type& __k) const | |
529 | { | |
530 | const_iterator __i = lower_bound(__k); | |
531 | if (__i == end() || key_comp()(__k, (*__i).first)) | |
532 | __throw_out_of_range(__N("map::at")); | |
533 | return (*__i).second; | |
534 | } | |
535 | ||
b9156b5c | 536 | // modifiers |
0c8766b1 | 537 | #if __cplusplus >= 201103L |
6b9314a8 | 538 | /** |
539 | * @brief Attempts to build and insert a std::pair into the %map. | |
540 | * | |
541 | * @param __args Arguments used to generate a new pair instance (see | |
542 | * std::piecewise_contruct for passing arguments to each | |
543 | * part of the pair constructor). | |
544 | * | |
545 | * @return A pair, of which the first element is an iterator that points | |
546 | * to the possibly inserted pair, and the second is a bool that | |
547 | * is true if the pair was actually inserted. | |
548 | * | |
549 | * This function attempts to build and insert a (key, value) %pair into | |
550 | * the %map. | |
551 | * A %map relies on unique keys and thus a %pair is only inserted if its | |
552 | * first element (the key) is not already present in the %map. | |
553 | * | |
554 | * Insertion requires logarithmic time. | |
555 | */ | |
556 | template<typename... _Args> | |
557 | std::pair<iterator, bool> | |
558 | emplace(_Args&&... __args) | |
559 | { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); } | |
560 | ||
561 | /** | |
562 | * @brief Attempts to build and insert a std::pair into the %map. | |
563 | * | |
564 | * @param __pos An iterator that serves as a hint as to where the pair | |
565 | * should be inserted. | |
566 | * @param __args Arguments used to generate a new pair instance (see | |
567 | * std::piecewise_contruct for passing arguments to each | |
568 | * part of the pair constructor). | |
569 | * @return An iterator that points to the element with key of the | |
570 | * std::pair built from @a __args (may or may not be that | |
571 | * std::pair). | |
572 | * | |
573 | * This function is not concerned about whether the insertion took place, | |
574 | * and thus does not return a boolean like the single-argument emplace() | |
575 | * does. | |
576 | * Note that the first parameter is only a hint and can potentially | |
577 | * improve the performance of the insertion process. A bad hint would | |
578 | * cause no gains in efficiency. | |
579 | * | |
580 | * See | |
0698cdf1 | 581 | * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints |
6b9314a8 | 582 | * for more on @a hinting. |
583 | * | |
584 | * Insertion requires logarithmic time (if the hint is not taken). | |
585 | */ | |
586 | template<typename... _Args> | |
587 | iterator | |
588 | emplace_hint(const_iterator __pos, _Args&&... __args) | |
589 | { | |
590 | return _M_t._M_emplace_hint_unique(__pos, | |
591 | std::forward<_Args>(__args)...); | |
592 | } | |
593 | #endif | |
a65c36dc | 594 | |
ed562371 | 595 | #if __cplusplus > 201402L |
a65c36dc | 596 | #define __cpp_lib_map_try_emplace 201411 |
ed562371 | 597 | /** |
598 | * @brief Attempts to build and insert a std::pair into the %map. | |
599 | * | |
600 | * @param __k Key to use for finding a possibly existing pair in | |
601 | * the map. | |
602 | * @param __args Arguments used to generate the .second for a new pair | |
603 | * instance. | |
604 | * | |
605 | * @return A pair, of which the first element is an iterator that points | |
606 | * to the possibly inserted pair, and the second is a bool that | |
607 | * is true if the pair was actually inserted. | |
608 | * | |
609 | * This function attempts to build and insert a (key, value) %pair into | |
610 | * the %map. | |
611 | * A %map relies on unique keys and thus a %pair is only inserted if its | |
612 | * first element (the key) is not already present in the %map. | |
613 | * If a %pair is not inserted, this function has no effect. | |
614 | * | |
615 | * Insertion requires logarithmic time. | |
616 | */ | |
617 | template <typename... _Args> | |
618 | pair<iterator, bool> | |
619 | try_emplace(const key_type& __k, _Args&&... __args) | |
620 | { | |
621 | iterator __i = lower_bound(__k); | |
622 | if (__i == end() || key_comp()(__k, (*__i).first)) | |
623 | { | |
624 | __i = emplace_hint(__i, std::piecewise_construct, | |
625 | std::forward_as_tuple(__k), | |
626 | std::forward_as_tuple( | |
627 | std::forward<_Args>(__args)...)); | |
628 | return {__i, true}; | |
629 | } | |
630 | return {__i, false}; | |
631 | } | |
632 | ||
633 | // move-capable overload | |
634 | template <typename... _Args> | |
635 | pair<iterator, bool> | |
636 | try_emplace(key_type&& __k, _Args&&... __args) | |
637 | { | |
638 | iterator __i = lower_bound(__k); | |
639 | if (__i == end() || key_comp()(__k, (*__i).first)) | |
640 | { | |
641 | __i = emplace_hint(__i, std::piecewise_construct, | |
642 | std::forward_as_tuple(std::move(__k)), | |
643 | std::forward_as_tuple( | |
644 | std::forward<_Args>(__args)...)); | |
645 | return {__i, true}; | |
646 | } | |
647 | return {__i, false}; | |
648 | } | |
6b9314a8 | 649 | |
ed562371 | 650 | /** |
651 | * @brief Attempts to build and insert a std::pair into the %map. | |
652 | * | |
653 | * @param __hint An iterator that serves as a hint as to where the | |
654 | * pair should be inserted. | |
655 | * @param __k Key to use for finding a possibly existing pair in | |
656 | * the map. | |
657 | * @param __args Arguments used to generate the .second for a new pair | |
658 | * instance. | |
659 | * @return An iterator that points to the element with key of the | |
660 | * std::pair built from @a __args (may or may not be that | |
661 | * std::pair). | |
662 | * | |
663 | * This function is not concerned about whether the insertion took place, | |
664 | * and thus does not return a boolean like the single-argument | |
665 | * try_emplace() does. However, if insertion did not take place, | |
666 | * this function has no effect. | |
667 | * Note that the first parameter is only a hint and can potentially | |
668 | * improve the performance of the insertion process. A bad hint would | |
669 | * cause no gains in efficiency. | |
670 | * | |
671 | * See | |
672 | * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints | |
673 | * for more on @a hinting. | |
674 | * | |
675 | * Insertion requires logarithmic time (if the hint is not taken). | |
676 | */ | |
677 | template <typename... _Args> | |
678 | iterator | |
679 | try_emplace(const_iterator __hint, const key_type& __k, | |
680 | _Args&&... __args) | |
681 | { | |
682 | iterator __i; | |
683 | auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k); | |
684 | if (__true_hint.second) | |
685 | __i = emplace_hint(iterator(__true_hint.second), | |
686 | std::piecewise_construct, | |
687 | std::forward_as_tuple(__k), | |
688 | std::forward_as_tuple( | |
689 | std::forward<_Args>(__args)...)); | |
690 | else | |
691 | __i = iterator(__true_hint.first); | |
692 | return __i; | |
693 | } | |
694 | ||
695 | // move-capable overload | |
696 | template <typename... _Args> | |
697 | iterator | |
698 | try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args) | |
699 | { | |
700 | iterator __i; | |
701 | auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k); | |
702 | if (__true_hint.second) | |
703 | __i = emplace_hint(iterator(__true_hint.second), | |
704 | std::piecewise_construct, | |
705 | std::forward_as_tuple(std::move(__k)), | |
706 | std::forward_as_tuple( | |
707 | std::forward<_Args>(__args)...)); | |
708 | else | |
709 | __i = iterator(__true_hint.first); | |
710 | return __i; | |
711 | } | |
712 | #endif | |
a65c36dc | 713 | |
b9156b5c | 714 | /** |
715 | * @brief Attempts to insert a std::pair into the %map. | |
10c73e3f | 716 | |
e12e4f3b | 717 | * @param __x Pair to be inserted (see std::make_pair for easy |
718 | * creation of pairs). | |
719 | * | |
10c73e3f | 720 | * @return A pair, of which the first element is an iterator that |
721 | * points to the possibly inserted pair, and the second is | |
722 | * a bool that is true if the pair was actually inserted. | |
b9156b5c | 723 | * |
724 | * This function attempts to insert a (key, value) %pair into the %map. | |
725 | * A %map relies on unique keys and thus a %pair is only inserted if its | |
726 | * first element (the key) is not already present in the %map. | |
727 | * | |
728 | * Insertion requires logarithmic time. | |
729 | */ | |
fea3abe2 | 730 | std::pair<iterator, bool> |
b9156b5c | 731 | insert(const value_type& __x) |
8474dcf5 | 732 | { return _M_t._M_insert_unique(__x); } |
bae9b8af | 733 | |
0c8766b1 | 734 | #if __cplusplus >= 201103L |
3e469200 | 735 | template<typename _Pair, typename = typename |
4f2884de | 736 | std::enable_if<std::is_constructible<value_type, |
737 | _Pair&&>::value>::type> | |
3e469200 | 738 | std::pair<iterator, bool> |
739 | insert(_Pair&& __x) | |
740 | { return _M_t._M_insert_unique(std::forward<_Pair>(__x)); } | |
741 | #endif | |
742 | ||
0c8766b1 | 743 | #if __cplusplus >= 201103L |
f82f1250 | 744 | /** |
745 | * @brief Attempts to insert a list of std::pairs into the %map. | |
e12e4f3b | 746 | * @param __list A std::initializer_list<value_type> of pairs to be |
747 | * inserted. | |
f82f1250 | 748 | * |
749 | * Complexity similar to that of the range constructor. | |
f82f1250 | 750 | */ |
751 | void | |
b26a4c3c | 752 | insert(std::initializer_list<value_type> __list) |
3b607c40 | 753 | { insert(__list.begin(), __list.end()); } |
b26a4c3c | 754 | #endif |
f82f1250 | 755 | |
b9156b5c | 756 | /** |
757 | * @brief Attempts to insert a std::pair into the %map. | |
e12e4f3b | 758 | * @param __position An iterator that serves as a hint as to where the |
b9156b5c | 759 | * pair should be inserted. |
e12e4f3b | 760 | * @param __x Pair to be inserted (see std::make_pair for easy creation |
761 | * of pairs). | |
762 | * @return An iterator that points to the element with key of | |
763 | * @a __x (may or may not be the %pair passed in). | |
b9156b5c | 764 | * |
10c73e3f | 765 | |
766 | * This function is not concerned about whether the insertion | |
767 | * took place, and thus does not return a boolean like the | |
768 | * single-argument insert() does. Note that the first | |
769 | * parameter is only a hint and can potentially improve the | |
770 | * performance of the insertion process. A bad hint would | |
771 | * cause no gains in efficiency. | |
b9156b5c | 772 | * |
10c73e3f | 773 | * See |
0698cdf1 | 774 | * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints |
72117d76 | 775 | * for more on @a hinting. |
b9156b5c | 776 | * |
777 | * Insertion requires logarithmic time (if the hint is not taken). | |
778 | */ | |
779 | iterator | |
0c8766b1 | 780 | #if __cplusplus >= 201103L |
3b607c40 | 781 | insert(const_iterator __position, const value_type& __x) |
782 | #else | |
fea3abe2 | 783 | insert(iterator __position, const value_type& __x) |
3b607c40 | 784 | #endif |
fea3abe2 | 785 | { return _M_t._M_insert_unique_(__position, __x); } |
bae9b8af | 786 | |
0c8766b1 | 787 | #if __cplusplus >= 201103L |
3e469200 | 788 | template<typename _Pair, typename = typename |
4f2884de | 789 | std::enable_if<std::is_constructible<value_type, |
790 | _Pair&&>::value>::type> | |
3e469200 | 791 | iterator |
792 | insert(const_iterator __position, _Pair&& __x) | |
793 | { return _M_t._M_insert_unique_(__position, | |
794 | std::forward<_Pair>(__x)); } | |
795 | #endif | |
796 | ||
b9156b5c | 797 | /** |
9fc1117c | 798 | * @brief Template function that attempts to insert a range of elements. |
e12e4f3b | 799 | * @param __first Iterator pointing to the start of the range to be |
800 | * inserted. | |
801 | * @param __last Iterator pointing to the end of the range. | |
b9156b5c | 802 | * |
803 | * Complexity similar to that of the range constructor. | |
804 | */ | |
707bff49 | 805 | template<typename _InputIterator> |
b9156b5c | 806 | void |
807 | insert(_InputIterator __first, _InputIterator __last) | |
8474dcf5 | 808 | { _M_t._M_insert_unique(__first, __last); } |
bae9b8af | 809 | |
ed562371 | 810 | #if __cplusplus > 201402L |
a65c36dc | 811 | #define __cpp_lib_map_insertion 201411 |
ed562371 | 812 | /** |
813 | * @brief Attempts to insert or assign a std::pair into the %map. | |
814 | * @param __k Key to use for finding a possibly existing pair in | |
815 | * the map. | |
816 | * @param __obj Argument used to generate the .second for a pair | |
817 | * instance. | |
818 | * | |
819 | * @return A pair, of which the first element is an iterator that | |
820 | * points to the possibly inserted pair, and the second is | |
821 | * a bool that is true if the pair was actually inserted. | |
822 | * | |
823 | * This function attempts to insert a (key, value) %pair into the %map. | |
824 | * A %map relies on unique keys and thus a %pair is only inserted if its | |
825 | * first element (the key) is not already present in the %map. | |
826 | * If the %pair was already in the %map, the .second of the %pair | |
827 | * is assigned from __obj. | |
828 | * | |
829 | * Insertion requires logarithmic time. | |
830 | */ | |
831 | template <typename _Obj> | |
832 | pair<iterator, bool> | |
833 | insert_or_assign(const key_type& __k, _Obj&& __obj) | |
834 | { | |
835 | iterator __i = lower_bound(__k); | |
836 | if (__i == end() || key_comp()(__k, (*__i).first)) | |
837 | { | |
838 | __i = emplace_hint(__i, std::piecewise_construct, | |
839 | std::forward_as_tuple(__k), | |
840 | std::forward_as_tuple( | |
841 | std::forward<_Obj>(__obj))); | |
842 | return {__i, true}; | |
843 | } | |
844 | (*__i).second = std::forward<_Obj>(__obj); | |
845 | return {__i, false}; | |
846 | } | |
847 | ||
848 | // move-capable overload | |
849 | template <typename _Obj> | |
850 | pair<iterator, bool> | |
851 | insert_or_assign(key_type&& __k, _Obj&& __obj) | |
852 | { | |
853 | iterator __i = lower_bound(__k); | |
854 | if (__i == end() || key_comp()(__k, (*__i).first)) | |
855 | { | |
856 | __i = emplace_hint(__i, std::piecewise_construct, | |
857 | std::forward_as_tuple(std::move(__k)), | |
858 | std::forward_as_tuple( | |
859 | std::forward<_Obj>(__obj))); | |
860 | return {__i, true}; | |
861 | } | |
862 | (*__i).second = std::forward<_Obj>(__obj); | |
863 | return {__i, false}; | |
864 | } | |
865 | ||
866 | /** | |
867 | * @brief Attempts to insert or assign a std::pair into the %map. | |
868 | * @param __hint An iterator that serves as a hint as to where the | |
869 | * pair should be inserted. | |
870 | * @param __k Key to use for finding a possibly existing pair in | |
871 | * the map. | |
872 | * @param __obj Argument used to generate the .second for a pair | |
873 | * instance. | |
874 | * | |
875 | * @return An iterator that points to the element with key of | |
876 | * @a __x (may or may not be the %pair passed in). | |
877 | * | |
878 | * This function attempts to insert a (key, value) %pair into the %map. | |
879 | * A %map relies on unique keys and thus a %pair is only inserted if its | |
880 | * first element (the key) is not already present in the %map. | |
881 | * If the %pair was already in the %map, the .second of the %pair | |
882 | * is assigned from __obj. | |
883 | * | |
884 | * Insertion requires logarithmic time. | |
885 | */ | |
886 | template <typename _Obj> | |
887 | iterator | |
888 | insert_or_assign(const_iterator __hint, | |
889 | const key_type& __k, _Obj&& __obj) | |
890 | { | |
891 | iterator __i; | |
892 | auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k); | |
893 | if (__true_hint.second) | |
894 | { | |
895 | return emplace_hint(iterator(__true_hint.second), | |
896 | std::piecewise_construct, | |
897 | std::forward_as_tuple(__k), | |
898 | std::forward_as_tuple( | |
899 | std::forward<_Obj>(__obj))); | |
900 | } | |
901 | __i = iterator(__true_hint.first); | |
902 | (*__i).second = std::forward<_Obj>(__obj); | |
903 | return __i; | |
904 | } | |
905 | ||
906 | // move-capable overload | |
907 | template <typename _Obj> | |
908 | iterator | |
909 | insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj) | |
910 | { | |
911 | iterator __i; | |
912 | auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k); | |
913 | if (__true_hint.second) | |
914 | { | |
915 | return emplace_hint(iterator(__true_hint.second), | |
916 | std::piecewise_construct, | |
917 | std::forward_as_tuple(std::move(__k)), | |
918 | std::forward_as_tuple( | |
919 | std::forward<_Obj>(__obj))); | |
920 | } | |
921 | __i = iterator(__true_hint.first); | |
922 | (*__i).second = std::forward<_Obj>(__obj); | |
923 | return __i; | |
924 | } | |
925 | #endif | |
926 | ||
0c8766b1 | 927 | #if __cplusplus >= 201103L |
200f291d | 928 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
929 | // DR 130. Associative erase should return an iterator. | |
930 | /** | |
931 | * @brief Erases an element from a %map. | |
e12e4f3b | 932 | * @param __position An iterator pointing to the element to be erased. |
200f291d | 933 | * @return An iterator pointing to the element immediately following |
934 | * @a position prior to the element being erased. If no such | |
935 | * element exists, end() is returned. | |
936 | * | |
937 | * This function erases an element, pointed to by the given | |
938 | * iterator, from a %map. Note that this function only erases | |
939 | * the element, and that if the element is itself a pointer, | |
940 | * the pointed-to memory is not touched in any way. Managing | |
941 | * the pointer is the user's responsibility. | |
942 | */ | |
943 | iterator | |
3b607c40 | 944 | erase(const_iterator __position) |
200f291d | 945 | { return _M_t.erase(__position); } |
8683d45d | 946 | |
8544d95d | 947 | // LWG 2059 |
948 | _GLIBCXX_ABI_TAG_CXX11 | |
8683d45d | 949 | iterator |
950 | erase(iterator __position) | |
951 | { return _M_t.erase(__position); } | |
200f291d | 952 | #else |
b9156b5c | 953 | /** |
954 | * @brief Erases an element from a %map. | |
e12e4f3b | 955 | * @param __position An iterator pointing to the element to be erased. |
b9156b5c | 956 | * |
10c73e3f | 957 | * This function erases an element, pointed to by the given |
958 | * iterator, from a %map. Note that this function only erases | |
959 | * the element, and that if the element is itself a pointer, | |
960 | * the pointed-to memory is not touched in any way. Managing | |
9fc1117c | 961 | * the pointer is the user's responsibility. |
b9156b5c | 962 | */ |
32bb2cd7 | 963 | void |
b9156b5c | 964 | erase(iterator __position) |
965 | { _M_t.erase(__position); } | |
200f291d | 966 | #endif |
bae9b8af | 967 | |
b9156b5c | 968 | /** |
969 | * @brief Erases elements according to the provided key. | |
e12e4f3b | 970 | * @param __x Key of element to be erased. |
b9156b5c | 971 | * @return The number of elements erased. |
972 | * | |
973 | * This function erases all the elements located by the given key from | |
974 | * a %map. | |
975 | * Note that this function only erases the element, and that if | |
976 | * the element is itself a pointer, the pointed-to memory is not touched | |
9fc1117c | 977 | * in any way. Managing the pointer is the user's responsibility. |
b9156b5c | 978 | */ |
979 | size_type | |
980 | erase(const key_type& __x) | |
981 | { return _M_t.erase(__x); } | |
bae9b8af | 982 | |
0c8766b1 | 983 | #if __cplusplus >= 201103L |
200f291d | 984 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
985 | // DR 130. Associative erase should return an iterator. | |
986 | /** | |
987 | * @brief Erases a [first,last) range of elements from a %map. | |
e12e4f3b | 988 | * @param __first Iterator pointing to the start of the range to be |
989 | * erased. | |
990 | * @param __last Iterator pointing to the end of the range to | |
991 | * be erased. | |
992 | * @return The iterator @a __last. | |
200f291d | 993 | * |
994 | * This function erases a sequence of elements from a %map. | |
995 | * Note that this function only erases the element, and that if | |
996 | * the element is itself a pointer, the pointed-to memory is not touched | |
997 | * in any way. Managing the pointer is the user's responsibility. | |
998 | */ | |
999 | iterator | |
3b607c40 | 1000 | erase(const_iterator __first, const_iterator __last) |
200f291d | 1001 | { return _M_t.erase(__first, __last); } |
1002 | #else | |
b9156b5c | 1003 | /** |
e12e4f3b | 1004 | * @brief Erases a [__first,__last) range of elements from a %map. |
1005 | * @param __first Iterator pointing to the start of the range to be | |
1006 | * erased. | |
1007 | * @param __last Iterator pointing to the end of the range to | |
1008 | * be erased. | |
b9156b5c | 1009 | * |
1010 | * This function erases a sequence of elements from a %map. | |
1011 | * Note that this function only erases the element, and that if | |
1012 | * the element is itself a pointer, the pointed-to memory is not touched | |
9fc1117c | 1013 | * in any way. Managing the pointer is the user's responsibility. |
b9156b5c | 1014 | */ |
1015 | void | |
1016 | erase(iterator __first, iterator __last) | |
1017 | { _M_t.erase(__first, __last); } | |
200f291d | 1018 | #endif |
bae9b8af | 1019 | |
b9156b5c | 1020 | /** |
1021 | * @brief Swaps data with another %map. | |
e12e4f3b | 1022 | * @param __x A %map of the same element and allocator types. |
b9156b5c | 1023 | * |
10c73e3f | 1024 | * This exchanges the elements between two maps in constant |
1025 | * time. (It is only swapping a pointer, an integer, and an | |
1026 | * instance of the @c Compare type (which itself is often | |
1027 | * stateless and empty), so it should be quite fast.) Note | |
1028 | * that the global std::swap() function is specialized such | |
1029 | * that std::swap(m1,m2) will feed to this function. | |
b9156b5c | 1030 | */ |
1031 | void | |
1032 | swap(map& __x) | |
02769f1f | 1033 | _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value) |
b9156b5c | 1034 | { _M_t.swap(__x._M_t); } |
bae9b8af | 1035 | |
b9156b5c | 1036 | /** |
10c73e3f | 1037 | * Erases all elements in a %map. Note that this function only |
1038 | * erases the elements, and that if the elements themselves are | |
1039 | * pointers, the pointed-to memory is not touched in any way. | |
9fc1117c | 1040 | * Managing the pointer is the user's responsibility. |
b9156b5c | 1041 | */ |
1042 | void | |
7cd718fd | 1043 | clear() _GLIBCXX_NOEXCEPT |
b9156b5c | 1044 | { _M_t.clear(); } |
bae9b8af | 1045 | |
b9156b5c | 1046 | // observers |
1047 | /** | |
1048 | * Returns the key comparison object out of which the %map was | |
1049 | * constructed. | |
1050 | */ | |
1051 | key_compare | |
1052 | key_comp() const | |
1053 | { return _M_t.key_comp(); } | |
bae9b8af | 1054 | |
b9156b5c | 1055 | /** |
1056 | * Returns a value comparison object, built from the key comparison | |
1057 | * object out of which the %map was constructed. | |
1058 | */ | |
1059 | value_compare | |
1060 | value_comp() const | |
1061 | { return value_compare(_M_t.key_comp()); } | |
bae9b8af | 1062 | |
b9156b5c | 1063 | // [23.3.1.3] map operations |
fcb2e07a | 1064 | |
1065 | //@{ | |
b9156b5c | 1066 | /** |
1067 | * @brief Tries to locate an element in a %map. | |
e12e4f3b | 1068 | * @param __x Key of (key, value) %pair to be located. |
b9156b5c | 1069 | * @return Iterator pointing to sought-after element, or end() if not |
1070 | * found. | |
1071 | * | |
1072 | * This function takes a key and tries to locate the element with which | |
1073 | * the key matches. If successful the function returns an iterator | |
1074 | * pointing to the sought after %pair. If unsuccessful it returns the | |
1075 | * past-the-end ( @c end() ) iterator. | |
1076 | */ | |
fcb2e07a | 1077 | |
b9156b5c | 1078 | iterator |
1079 | find(const key_type& __x) | |
1080 | { return _M_t.find(__x); } | |
bae9b8af | 1081 | |
fcb2e07a | 1082 | #if __cplusplus > 201103L |
1083 | template<typename _Kt> | |
1084 | auto | |
1085 | find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x)) | |
1086 | { return _M_t._M_find_tr(__x); } | |
1087 | #endif | |
1088 | //@} | |
1089 | ||
1090 | //@{ | |
b9156b5c | 1091 | /** |
1092 | * @brief Tries to locate an element in a %map. | |
e12e4f3b | 1093 | * @param __x Key of (key, value) %pair to be located. |
b9156b5c | 1094 | * @return Read-only (constant) iterator pointing to sought-after |
1095 | * element, or end() if not found. | |
1096 | * | |
1097 | * This function takes a key and tries to locate the element with which | |
1098 | * the key matches. If successful the function returns a constant | |
1099 | * iterator pointing to the sought after %pair. If unsuccessful it | |
1100 | * returns the past-the-end ( @c end() ) iterator. | |
1101 | */ | |
fcb2e07a | 1102 | |
b9156b5c | 1103 | const_iterator |
1104 | find(const key_type& __x) const | |
1105 | { return _M_t.find(__x); } | |
bae9b8af | 1106 | |
fcb2e07a | 1107 | #if __cplusplus > 201103L |
1108 | template<typename _Kt> | |
1109 | auto | |
1110 | find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x)) | |
1111 | { return _M_t._M_find_tr(__x); } | |
1112 | #endif | |
1113 | //@} | |
1114 | ||
1115 | //@{ | |
b9156b5c | 1116 | /** |
1117 | * @brief Finds the number of elements with given key. | |
e12e4f3b | 1118 | * @param __x Key of (key, value) pairs to be located. |
b9156b5c | 1119 | * @return Number of elements with specified key. |
1120 | * | |
1121 | * This function only makes sense for multimaps; for map the result will | |
1122 | * either be 0 (not present) or 1 (present). | |
1123 | */ | |
1124 | size_type | |
1125 | count(const key_type& __x) const | |
1126 | { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } | |
bae9b8af | 1127 | |
fcb2e07a | 1128 | #if __cplusplus > 201103L |
1129 | template<typename _Kt> | |
1130 | auto | |
1131 | count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x)) | |
1132 | { return _M_t._M_find_tr(__x) == _M_t.end() ? 0 : 1; } | |
1133 | #endif | |
1134 | //@} | |
1135 | ||
1136 | //@{ | |
b9156b5c | 1137 | /** |
1138 | * @brief Finds the beginning of a subsequence matching given key. | |
e12e4f3b | 1139 | * @param __x Key of (key, value) pair to be located. |
b9156b5c | 1140 | * @return Iterator pointing to first element equal to or greater |
1141 | * than key, or end(). | |
1142 | * | |
1143 | * This function returns the first element of a subsequence of elements | |
1144 | * that matches the given key. If unsuccessful it returns an iterator | |
1145 | * pointing to the first element that has a greater value than given key | |
1146 | * or end() if no such element exists. | |
1147 | */ | |
1148 | iterator | |
1149 | lower_bound(const key_type& __x) | |
1150 | { return _M_t.lower_bound(__x); } | |
bae9b8af | 1151 | |
fcb2e07a | 1152 | #if __cplusplus > 201103L |
1153 | template<typename _Kt> | |
1154 | auto | |
1155 | lower_bound(const _Kt& __x) | |
1156 | -> decltype(_M_t._M_lower_bound_tr(__x)) | |
1157 | { return _M_t._M_lower_bound_tr(__x); } | |
1158 | #endif | |
1159 | //@} | |
1160 | ||
1161 | //@{ | |
b9156b5c | 1162 | /** |
1163 | * @brief Finds the beginning of a subsequence matching given key. | |
e12e4f3b | 1164 | * @param __x Key of (key, value) pair to be located. |
b9156b5c | 1165 | * @return Read-only (constant) iterator pointing to first element |
1166 | * equal to or greater than key, or end(). | |
1167 | * | |
1168 | * This function returns the first element of a subsequence of elements | |
1169 | * that matches the given key. If unsuccessful it returns an iterator | |
1170 | * pointing to the first element that has a greater value than given key | |
1171 | * or end() if no such element exists. | |
1172 | */ | |
1173 | const_iterator | |
1174 | lower_bound(const key_type& __x) const | |
1175 | { return _M_t.lower_bound(__x); } | |
bae9b8af | 1176 | |
fcb2e07a | 1177 | #if __cplusplus > 201103L |
1178 | template<typename _Kt> | |
1179 | auto | |
1180 | lower_bound(const _Kt& __x) const | |
1181 | -> decltype(_M_t._M_lower_bound_tr(__x)) | |
1182 | { return _M_t._M_lower_bound_tr(__x); } | |
1183 | #endif | |
1184 | //@} | |
1185 | ||
1186 | //@{ | |
b9156b5c | 1187 | /** |
1188 | * @brief Finds the end of a subsequence matching given key. | |
e12e4f3b | 1189 | * @param __x Key of (key, value) pair to be located. |
b9156b5c | 1190 | * @return Iterator pointing to the first element |
1191 | * greater than key, or end(). | |
1192 | */ | |
1193 | iterator | |
1194 | upper_bound(const key_type& __x) | |
1195 | { return _M_t.upper_bound(__x); } | |
bae9b8af | 1196 | |
fcb2e07a | 1197 | #if __cplusplus > 201103L |
1198 | template<typename _Kt> | |
1199 | auto | |
1200 | upper_bound(const _Kt& __x) | |
1201 | -> decltype(_M_t._M_upper_bound_tr(__x)) | |
1202 | { return _M_t._M_upper_bound_tr(__x); } | |
1203 | #endif | |
1204 | //@} | |
1205 | ||
1206 | //@{ | |
b9156b5c | 1207 | /** |
1208 | * @brief Finds the end of a subsequence matching given key. | |
e12e4f3b | 1209 | * @param __x Key of (key, value) pair to be located. |
b9156b5c | 1210 | * @return Read-only (constant) iterator pointing to first iterator |
1211 | * greater than key, or end(). | |
1212 | */ | |
1213 | const_iterator | |
1214 | upper_bound(const key_type& __x) const | |
1215 | { return _M_t.upper_bound(__x); } | |
bae9b8af | 1216 | |
fcb2e07a | 1217 | #if __cplusplus > 201103L |
1218 | template<typename _Kt> | |
1219 | auto | |
1220 | upper_bound(const _Kt& __x) const | |
1221 | -> decltype(_M_t._M_upper_bound_tr(__x)) | |
1222 | { return _M_t._M_upper_bound_tr(__x); } | |
1223 | #endif | |
1224 | //@} | |
1225 | ||
1226 | //@{ | |
b9156b5c | 1227 | /** |
1228 | * @brief Finds a subsequence matching given key. | |
e12e4f3b | 1229 | * @param __x Key of (key, value) pairs to be located. |
b9156b5c | 1230 | * @return Pair of iterators that possibly points to the subsequence |
1231 | * matching given key. | |
1232 | * | |
1233 | * This function is equivalent to | |
1234 | * @code | |
1235 | * std::make_pair(c.lower_bound(val), | |
1236 | * c.upper_bound(val)) | |
1237 | * @endcode | |
1238 | * (but is faster than making the calls separately). | |
1239 | * | |
1240 | * This function probably only makes sense for multimaps. | |
1241 | */ | |
171a395a | 1242 | std::pair<iterator, iterator> |
b9156b5c | 1243 | equal_range(const key_type& __x) |
1244 | { return _M_t.equal_range(__x); } | |
bae9b8af | 1245 | |
fcb2e07a | 1246 | #if __cplusplus > 201103L |
1247 | template<typename _Kt> | |
1248 | auto | |
1249 | equal_range(const _Kt& __x) | |
1250 | -> decltype(_M_t._M_equal_range_tr(__x)) | |
1251 | { return _M_t._M_equal_range_tr(__x); } | |
1252 | #endif | |
1253 | //@} | |
1254 | ||
1255 | //@{ | |
b9156b5c | 1256 | /** |
1257 | * @brief Finds a subsequence matching given key. | |
e12e4f3b | 1258 | * @param __x Key of (key, value) pairs to be located. |
b9156b5c | 1259 | * @return Pair of read-only (constant) iterators that possibly points |
1260 | * to the subsequence matching given key. | |
1261 | * | |
1262 | * This function is equivalent to | |
1263 | * @code | |
1264 | * std::make_pair(c.lower_bound(val), | |
1265 | * c.upper_bound(val)) | |
1266 | * @endcode | |
1267 | * (but is faster than making the calls separately). | |
1268 | * | |
1269 | * This function probably only makes sense for multimaps. | |
1270 | */ | |
171a395a | 1271 | std::pair<const_iterator, const_iterator> |
b9156b5c | 1272 | equal_range(const key_type& __x) const |
1273 | { return _M_t.equal_range(__x); } | |
bae9b8af | 1274 | |
fcb2e07a | 1275 | #if __cplusplus > 201103L |
1276 | template<typename _Kt> | |
1277 | auto | |
1278 | equal_range(const _Kt& __x) const | |
1279 | -> decltype(_M_t._M_equal_range_tr(__x)) | |
1280 | { return _M_t._M_equal_range_tr(__x); } | |
1281 | #endif | |
1282 | //@} | |
1283 | ||
707bff49 | 1284 | template<typename _K1, typename _T1, typename _C1, typename _A1> |
62ba3194 | 1285 | friend bool |
707bff49 | 1286 | operator==(const map<_K1, _T1, _C1, _A1>&, |
1287 | const map<_K1, _T1, _C1, _A1>&); | |
62ba3194 | 1288 | |
707bff49 | 1289 | template<typename _K1, typename _T1, typename _C1, typename _A1> |
62ba3194 | 1290 | friend bool |
707bff49 | 1291 | operator<(const map<_K1, _T1, _C1, _A1>&, |
1292 | const map<_K1, _T1, _C1, _A1>&); | |
b9156b5c | 1293 | }; |
bae9b8af | 1294 | |
32bb2cd7 | 1295 | /** |
1296 | * @brief Map equality comparison. | |
e12e4f3b | 1297 | * @param __x A %map. |
1298 | * @param __y A %map of the same type as @a x. | |
32bb2cd7 | 1299 | * @return True iff the size and elements of the maps are equal. |
ca33b868 | 1300 | * |
32bb2cd7 | 1301 | * This is an equivalence relation. It is linear in the size of the |
1302 | * maps. Maps are considered equivalent if their sizes are equal, | |
1303 | * and if corresponding elements compare equal. | |
1304 | */ | |
707bff49 | 1305 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
32bb2cd7 | 1306 | inline bool |
171a395a | 1307 | operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x, |
1308 | const map<_Key, _Tp, _Compare, _Alloc>& __y) | |
32bb2cd7 | 1309 | { return __x._M_t == __y._M_t; } |
bae9b8af | 1310 | |
32bb2cd7 | 1311 | /** |
1312 | * @brief Map ordering relation. | |
e12e4f3b | 1313 | * @param __x A %map. |
1314 | * @param __y A %map of the same type as @a x. | |
78314996 | 1315 | * @return True iff @a x is lexicographically less than @a y. |
ca33b868 | 1316 | * |
32bb2cd7 | 1317 | * This is a total ordering relation. It is linear in the size of the |
1318 | * maps. The elements must be comparable with @c <. | |
ca33b868 | 1319 | * |
78314996 | 1320 | * See std::lexicographical_compare() for how the determination is made. |
32bb2cd7 | 1321 | */ |
707bff49 | 1322 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
32bb2cd7 | 1323 | inline bool |
171a395a | 1324 | operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x, |
1325 | const map<_Key, _Tp, _Compare, _Alloc>& __y) | |
32bb2cd7 | 1326 | { return __x._M_t < __y._M_t; } |
bae9b8af | 1327 | |
32bb2cd7 | 1328 | /// Based on operator== |
707bff49 | 1329 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
32bb2cd7 | 1330 | inline bool |
171a395a | 1331 | operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x, |
1332 | const map<_Key, _Tp, _Compare, _Alloc>& __y) | |
32bb2cd7 | 1333 | { return !(__x == __y); } |
bae9b8af | 1334 | |
32bb2cd7 | 1335 | /// Based on operator< |
707bff49 | 1336 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
32bb2cd7 | 1337 | inline bool |
171a395a | 1338 | operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x, |
1339 | const map<_Key, _Tp, _Compare, _Alloc>& __y) | |
32bb2cd7 | 1340 | { return __y < __x; } |
bae9b8af | 1341 | |
32bb2cd7 | 1342 | /// Based on operator< |
707bff49 | 1343 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
32bb2cd7 | 1344 | inline bool |
171a395a | 1345 | operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x, |
1346 | const map<_Key, _Tp, _Compare, _Alloc>& __y) | |
32bb2cd7 | 1347 | { return !(__y < __x); } |
bae9b8af | 1348 | |
32bb2cd7 | 1349 | /// Based on operator< |
707bff49 | 1350 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
32bb2cd7 | 1351 | inline bool |
171a395a | 1352 | operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x, |
1353 | const map<_Key, _Tp, _Compare, _Alloc>& __y) | |
32bb2cd7 | 1354 | { return !(__x < __y); } |
bae9b8af | 1355 | |
32bb2cd7 | 1356 | /// See std::map::swap(). |
707bff49 | 1357 | template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> |
32bb2cd7 | 1358 | inline void |
171a395a | 1359 | swap(map<_Key, _Tp, _Compare, _Alloc>& __x, |
1360 | map<_Key, _Tp, _Compare, _Alloc>& __y) | |
02769f1f | 1361 | _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y))) |
32bb2cd7 | 1362 | { __x.swap(__y); } |
1069247d | 1363 | |
2948dd21 | 1364 | _GLIBCXX_END_NAMESPACE_CONTAINER |
1365 | } // namespace std | |
1d487aca | 1366 | |
f142a34a | 1367 | #endif /* _STL_MAP_H */ |