Line | % of fetches | Source |
---|---|---|
1 | // Set implementation -*- C++ -*- | |
2 | ||
3 | // Copyright (C) 2001-2015 Free Software Foundation, Inc. | |
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 | |
8 | // Free Software Foundation; either version 3, or (at your option) | |
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 | ||
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. | |
19 | ||
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/>. | |
24 | ||
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 | ||
51 | /** @file bits/stl_set.h | |
52 | * This is an internal header file, included by other library headers. | |
53 | * Do not attempt to use it directly. @headername{set} | |
54 | */ | |
55 | ||
56 | #ifndef _STL_SET_H | |
57 | #define _STL_SET_H 1 | |
58 | ||
59 | #include <bits/concept_check.h> | |
60 | #if __cplusplus >= 201103L | |
61 | #include <initializer_list> | |
62 | #endif | |
63 | ||
64 | namespace std _GLIBCXX_VISIBILITY(default) | |
65 | { | |
66 | _GLIBCXX_BEGIN_NAMESPACE_CONTAINER | |
67 | ||
68 | /** | |
69 | * @brief A standard container made up of unique keys, which can be | |
70 | * retrieved in logarithmic time. | |
71 | * | |
72 | * @ingroup associative_containers | |
73 | * | |
74 | * @tparam _Key Type of key objects. | |
75 | * @tparam _Compare Comparison function object type, defaults to less<_Key>. | |
76 | * @tparam _Alloc Allocator type, defaults to allocator<_Key>. | |
77 | * | |
78 | * Meets the requirements of a <a href="tables.html#65">container</a>, a | |
79 | * <a href="tables.html#66">reversible container</a>, and an | |
80 | * <a href="tables.html#69">associative container</a> (using unique keys). | |
81 | * | |
82 | * Sets support bidirectional iterators. | |
83 | * | |
84 | * The private tree data is declared exactly the same way for set and | |
85 | * multiset; the distinction is made entirely in how the tree functions are | |
86 | * called (*_unique versus *_equal, same as the standard). | |
87 | */ | |
88 | template<typename _Key, typename _Compare = std::less<_Key>, | |
89 | typename _Alloc = std::allocator<_Key> > | |
90 | class set | |
91 | { | |
92 | // concept requirements | |
93 | typedef typename _Alloc::value_type _Alloc_value_type; | |
94 | __glibcxx_class_requires(_Key, _SGIAssignableConcept) | |
95 | __glibcxx_class_requires4(_Compare, bool, _Key, _Key, | |
96 | _BinaryFunctionConcept) | |
97 | __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept) | |
98 | ||
99 | public: | |
100 | // typedefs: | |
101 | //@{ | |
102 | /// Public typedefs. | |
103 | typedef _Key key_type; | |
104 | typedef _Key value_type; | |
105 | typedef _Compare key_compare; | |
106 | typedef _Compare value_compare; | |
107 | typedef _Alloc allocator_type; | |
108 | //@} | |
109 | ||
110 | private: | |
111 | typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template | |
112 | rebind<_Key>::other _Key_alloc_type; | |
113 | ||
114 | typedef _Rb_tree<key_type, value_type, _Identity<value_type>, | |
115 | key_compare, _Key_alloc_type> _Rep_type; | |
116 | _Rep_type _M_t; // Red-black tree representing set. | |
117 | ||
118 | typedef __gnu_cxx::__alloc_traits<_Key_alloc_type> _Alloc_traits; | |
119 | ||
120 | public: | |
121 | //@{ | |
122 | /// Iterator-related typedefs. | |
123 | typedef typename _Alloc_traits::pointer pointer; | |
124 | typedef typename _Alloc_traits::const_pointer const_pointer; | |
125 | typedef typename _Alloc_traits::reference reference; | |
126 | typedef typename _Alloc_traits::const_reference const_reference; | |
127 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
128 | // DR 103. set::iterator is required to be modifiable, | |
129 | // but this allows modification of keys. | |
130 | typedef typename _Rep_type::const_iterator iterator; | |
131 | typedef typename _Rep_type::const_iterator const_iterator; | |
132 | typedef typename _Rep_type::const_reverse_iterator reverse_iterator; | |
133 | typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator; | |
134 | typedef typename _Rep_type::size_type size_type; | |
135 | typedef typename _Rep_type::difference_type difference_type; | |
136 | //@} | |
137 | ||
138 | // allocation/deallocation | |
139 | /** | |
140 | * @brief Default constructor creates no elements. | |
141 | */ | |
142 | set() | |
143 | #if __cplusplus >= 201103L | |
144 | noexcept(is_nothrow_default_constructible<allocator_type>::value) | |
145 | #endif | |
146 | : _M_t() { } | |
147 | ||
148 | /** | |
149 | * @brief Creates a %set with no elements. | |
150 | * @param __comp Comparator to use. | |
151 | * @param __a An allocator object. | |
152 | */ | |
153 | explicit | |
154 | set(const _Compare& __comp, | |
155 | const allocator_type& __a = allocator_type()) | |
156 | : _M_t(__comp, _Key_alloc_type(__a)) { } | |
157 | ||
158 | /** | |
159 | * @brief Builds a %set from a range. | |
160 | * @param __first An input iterator. | |
161 | * @param __last An input iterator. | |
162 | * | |
163 | * Create a %set consisting of copies of the elements from | |
164 | * [__first,__last). This is linear in N if the range is | |
165 | * already sorted, and NlogN otherwise (where N is | |
166 | * distance(__first,__last)). | |
167 | */ | |
168 | template<typename _InputIterator> | |
169 | set(_InputIterator __first, _InputIterator __last) | |
170 | : _M_t() | |
171 | { _M_t._M_insert_unique(__first, __last); } | |
172 | ||
173 | /** | |
174 | * @brief Builds a %set from a range. | |
175 | * @param __first An input iterator. | |
176 | * @param __last An input iterator. | |
177 | * @param __comp A comparison functor. | |
178 | * @param __a An allocator object. | |
179 | * | |
180 | * Create a %set consisting of copies of the elements from | |
181 | * [__first,__last). This is linear in N if the range is | |
182 | * already sorted, and NlogN otherwise (where N is | |
183 | * distance(__first,__last)). | |
184 | */ | |
185 | template<typename _InputIterator> | |
186 | set(_InputIterator __first, _InputIterator __last, | |
187 | const _Compare& __comp, | |
188 | const allocator_type& __a = allocator_type()) | |
189 | : _M_t(__comp, _Key_alloc_type(__a)) | |
190 | { _M_t._M_insert_unique(__first, __last); } | |
191 | ||
192 | /** | |
193 | * @brief %Set copy constructor. | |
194 | * @param __x A %set of identical element and allocator types. | |
195 | * | |
196 | * The newly-created %set uses a copy of the allocation object used | |
197 | * by @a __x. | |
198 | */ | |
199 | set(const set& __x) | |
200 | : _M_t(__x._M_t) { } | |
201 | ||
202 | #if __cplusplus >= 201103L | |
203 | /** | |
204 | * @brief %Set move constructor | |
205 | * @param __x A %set of identical element and allocator types. | |
206 | * | |
207 | * The newly-created %set contains the exact contents of @a x. | |
208 | * The contents of @a x are a valid, but unspecified %set. | |
209 | */ | |
210 | set(set&& __x) | |
211 | noexcept(is_nothrow_copy_constructible<_Compare>::value) | |
212 | : _M_t(std::move(__x._M_t)) { } | |
213 | ||
214 | /** | |
215 | * @brief Builds a %set from an initializer_list. | |
216 | * @param __l An initializer_list. | |
217 | * @param __comp A comparison functor. | |
218 | * @param __a An allocator object. | |
219 | * | |
220 | * Create a %set consisting of copies of the elements in the list. | |
221 | * This is linear in N if the list is already sorted, and NlogN | |
222 | * otherwise (where N is @a __l.size()). | |
223 | */ | |
224 | set(initializer_list<value_type> __l, | |
225 | const _Compare& __comp = _Compare(), | |
226 | const allocator_type& __a = allocator_type()) | |
227 | : _M_t(__comp, _Key_alloc_type(__a)) | |
228 | { _M_t._M_insert_unique(__l.begin(), __l.end()); } | |
229 | ||
230 | /// Allocator-extended default constructor. | |
231 | explicit | |
232 | set(const allocator_type& __a) | |
233 | : _M_t(_Compare(), _Key_alloc_type(__a)) { } | |
234 | ||
235 | /// Allocator-extended copy constructor. | |
236 | set(const set& __x, const allocator_type& __a) | |
237 | : _M_t(__x._M_t, _Key_alloc_type(__a)) { } | |
238 | ||
239 | /// Allocator-extended move constructor. | |
240 | set(set&& __x, const allocator_type& __a) | |
241 | noexcept(is_nothrow_copy_constructible<_Compare>::value | |
242 | && _Alloc_traits::_S_always_equal()) | |
243 | : _M_t(std::move(__x._M_t), _Key_alloc_type(__a)) { } | |
244 | ||
245 | /// Allocator-extended initialier-list constructor. | |
246 | set(initializer_list<value_type> __l, const allocator_type& __a) | |
247 | : _M_t(_Compare(), _Key_alloc_type(__a)) | |
248 | { _M_t._M_insert_unique(__l.begin(), __l.end()); } | |
249 | ||
250 | /// Allocator-extended range constructor. | |
251 | template<typename _InputIterator> | |
252 | set(_InputIterator __first, _InputIterator __last, | |
253 | const allocator_type& __a) | |
254 | : _M_t(_Compare(), _Key_alloc_type(__a)) | |
255 | { _M_t._M_insert_unique(__first, __last); } | |
256 | #endif | |
257 | ||
258 | /** | |
259 | * @brief %Set assignment operator. | |
260 | * @param __x A %set of identical element and allocator types. | |
261 | * | |
262 | * All the elements of @a __x are copied, but unlike the copy | |
263 | * constructor, the allocator object is not copied. | |
264 | */ | |
265 | set& | |
266 | operator=(const set& __x) | |
267 | { | |
268 | _M_t = __x._M_t; | |
269 | return *this; | |
270 | } | |
271 | ||
272 | #if __cplusplus >= 201103L | |
273 | /// Move assignment operator. | |
274 | set& | |
275 | operator=(set&&) = default; | |
276 | ||
277 | /** | |
278 | * @brief %Set list assignment operator. | |
279 | * @param __l An initializer_list. | |
280 | * | |
281 | * This function fills a %set with copies of the elements in the | |
282 | * initializer list @a __l. | |
283 | * | |
284 | * Note that the assignment completely changes the %set and | |
285 | * that the resulting %set's size is the same as the number | |
286 | * of elements assigned. Old data may be lost. | |
287 | */ | |
288 | set& | |
289 | operator=(initializer_list<value_type> __l) | |
290 | { | |
291 | _M_t._M_assign_unique(__l.begin(), __l.end()); | |
292 | return *this; | |
293 | } | |
294 | #endif | |
295 | ||
296 | // accessors: | |
297 | ||
298 | /// Returns the comparison object with which the %set was constructed. | |
299 | key_compare | |
300 | key_comp() const | |
301 | { return _M_t.key_comp(); } | |
302 | /// Returns the comparison object with which the %set was constructed. | |
303 | value_compare | |
304 | value_comp() const | |
305 | { return _M_t.key_comp(); } | |
306 | /// Returns the allocator object with which the %set was constructed. | |
307 | allocator_type | |
308 | get_allocator() const _GLIBCXX_NOEXCEPT | |
309 | { return allocator_type(_M_t.get_allocator()); } | |
310 | ||
311 | /** | |
312 | * Returns a read-only (constant) iterator that points to the first | |
313 | * element in the %set. Iteration is done in ascending order according | |
314 | * to the keys. | |
315 | */ | |
316 | iterator | |
317 | begin() const _GLIBCXX_NOEXCEPT | |
318 | { return _M_t.begin(); } | |
319 | ||
320 | /** | |
321 | * Returns a read-only (constant) iterator that points one past the last | |
322 | * element in the %set. Iteration is done in ascending order according | |
323 | * to the keys. | |
324 | */ | |
325 | iterator | |
326 | end() const _GLIBCXX_NOEXCEPT | |
327 | { return _M_t.end(); } | |
328 | ||
329 | /** | |
330 | * Returns a read-only (constant) iterator that points to the last | |
331 | * element in the %set. Iteration is done in descending order according | |
332 | * to the keys. | |
333 | */ | |
334 | reverse_iterator | |
335 | rbegin() const _GLIBCXX_NOEXCEPT | |
336 | { return _M_t.rbegin(); } | |
337 | ||
338 | /** | |
339 | * Returns a read-only (constant) reverse iterator that points to the | |
340 | * last pair in the %set. Iteration is done in descending order | |
341 | * according to the keys. | |
342 | */ | |
343 | reverse_iterator | |
344 | rend() const _GLIBCXX_NOEXCEPT | |
345 | { return _M_t.rend(); } | |
346 | ||
347 | #if __cplusplus >= 201103L | |
348 | /** | |
349 | * Returns a read-only (constant) iterator that points to the first | |
350 | * element in the %set. Iteration is done in ascending order according | |
351 | * to the keys. | |
352 | */ | |
353 | iterator | |
354 | cbegin() const noexcept | |
355 | { return _M_t.begin(); } | |
356 | ||
357 | /** | |
358 | * Returns a read-only (constant) iterator that points one past the last | |
359 | * element in the %set. Iteration is done in ascending order according | |
360 | * to the keys. | |
361 | */ | |
362 | iterator | |
363 | cend() const noexcept | |
364 | { return _M_t.end(); } | |
365 | ||
366 | /** | |
367 | * Returns a read-only (constant) iterator that points to the last | |
368 | * element in the %set. Iteration is done in descending order according | |
369 | * to the keys. | |
370 | */ | |
371 | reverse_iterator | |
372 | crbegin() const noexcept | |
373 | { return _M_t.rbegin(); } | |
374 | ||
375 | /** | |
376 | * Returns a read-only (constant) reverse iterator that points to the | |
377 | * last pair in the %set. Iteration is done in descending order | |
378 | * according to the keys. | |
379 | */ | |
380 | reverse_iterator | |
381 | crend() const noexcept | |
382 | { return _M_t.rend(); } | |
383 | #endif | |
384 | ||
385 | /// Returns true if the %set is empty. | |
386 | bool | |
387 | empty() const _GLIBCXX_NOEXCEPT | |
388 | { return _M_t.empty(); } | |
389 | ||
390 | /// Returns the size of the %set. | |
391 | size_type | |
392 | size() const _GLIBCXX_NOEXCEPT | |
393 | { return _M_t.size(); } | |
394 | ||
395 | /// Returns the maximum size of the %set. | |
396 | size_type | |
397 | max_size() const _GLIBCXX_NOEXCEPT | |
398 | { return _M_t.max_size(); } | |
399 | ||
400 | /** | |
401 | * @brief Swaps data with another %set. | |
402 | * @param __x A %set of the same element and allocator types. | |
403 | * | |
404 | * This exchanges the elements between two sets in constant | |
405 | * time. (It is only swapping a pointer, an integer, and an | |
406 | * instance of the @c Compare type (which itself is often | |
407 | * stateless and empty), so it should be quite fast.) Note | |
408 | * that the global std::swap() function is specialized such | |
409 | * that std::swap(s1,s2) will feed to this function. | |
410 | */ | |
411 | void | |
412 | swap(set& __x) | |
413 | #if __cplusplus >= 201103L | |
414 | noexcept(_Alloc_traits::_S_nothrow_swap()) | |
415 | #endif | |
416 | { _M_t.swap(__x._M_t); } | |
417 | ||
418 | // insert/erase | |
419 | #if __cplusplus >= 201103L | |
420 | /** | |
421 | * @brief Attempts to build and insert an element into the %set. | |
422 | * @param __args Arguments used to generate an element. | |
423 | * @return A pair, of which the first element is an iterator that points | |
424 | * to the possibly inserted element, and the second is a bool | |
425 | * that is true if the element was actually inserted. | |
426 | * | |
427 | * This function attempts to build and insert an element into the %set. | |
428 | * A %set relies on unique keys and thus an element is only inserted if | |
429 | * it is not already present in the %set. | |
430 | * | |
431 | * Insertion requires logarithmic time. | |
432 | */ | |
433 | template<typename... _Args> | |
434 | std::pair<iterator, bool> | |
435 | emplace(_Args&&... __args) | |
436 | { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); } | |
437 | ||
438 | /** | |
439 | * @brief Attempts to insert an element into the %set. | |
440 | * @param __pos An iterator that serves as a hint as to where the | |
441 | * element should be inserted. | |
442 | * @param __args Arguments used to generate the element to be | |
443 | * inserted. | |
444 | * @return An iterator that points to the element with key equivalent to | |
445 | * the one generated from @a __args (may or may not be the | |
446 | * element itself). | |
447 | * | |
448 | * This function is not concerned about whether the insertion took place, | |
449 | * and thus does not return a boolean like the single-argument emplace() | |
450 | * does. Note that the first parameter is only a hint and can | |
451 | * potentially improve the performance of the insertion process. A bad | |
452 | * hint would cause no gains in efficiency. | |
453 | * | |
454 | * For more on @a hinting, see: | |
455 | * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints | |
456 | * | |
457 | * Insertion requires logarithmic time (if the hint is not taken). | |
458 | */ | |
459 | template<typename... _Args> | |
460 | iterator | |
461 | emplace_hint(const_iterator __pos, _Args&&... __args) | |
462 | { | |
463 | return _M_t._M_emplace_hint_unique(__pos, | |
464 | std::forward<_Args>(__args)...); | |
465 | } | |
466 | #endif | |
467 | ||
468 | /** | |
469 | * @brief Attempts to insert an element into the %set. | |
470 | * @param __x Element to be inserted. | |
471 | * @return A pair, of which the first element is an iterator that points | |
472 | * to the possibly inserted element, and the second is a bool | |
473 | * that is true if the element was actually inserted. | |
474 | * | |
475 | * This function attempts to insert an element into the %set. A %set | |
476 | * relies on unique keys and thus an element is only inserted if it is | |
477 | * not already present in the %set. | |
478 | * | |
479 | * Insertion requires logarithmic time. | |
480 | */ | |
481 | std::pair<iterator, bool> | |
482 | insert(const value_type& __x) | |
483 | { | |
484 | std::pair<typename _Rep_type::iterator, bool> __p = | |
485 | _M_t._M_insert_unique(__x); | |
486 | return std::pair<iterator, bool>(__p.first, __p.second); | |
487 | } | |
488 | ||
489 | #if __cplusplus >= 201103L | |
490 | std::pair<iterator, bool> | |
491 | insert(value_type&& __x) | |
492 | { | |
493 | std::pair<typename _Rep_type::iterator, bool> __p = | |
494 | _M_t._M_insert_unique(std::move(__x)); | |
495 | return std::pair<iterator, bool>(__p.first, __p.second); | |
496 | } | |
497 | #endif | |
498 | ||
499 | /** | |
500 | * @brief Attempts to insert an element into the %set. | |
501 | * @param __position An iterator that serves as a hint as to where the | |
502 | * element should be inserted. | |
503 | * @param __x Element to be inserted. | |
504 | * @return An iterator that points to the element with key of | |
505 | * @a __x (may or may not be the element passed in). | |
506 | * | |
507 | * This function is not concerned about whether the insertion took place, | |
508 | * and thus does not return a boolean like the single-argument insert() | |
509 | * does. Note that the first parameter is only a hint and can | |
510 | * potentially improve the performance of the insertion process. A bad | |
511 | * hint would cause no gains in efficiency. | |
512 | * | |
513 | * For more on @a hinting, see: | |
514 | * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints | |
515 | * | |
516 | * Insertion requires logarithmic time (if the hint is not taken). | |
517 | */ | |
518 | iterator | |
519 | insert(const_iterator __position, const value_type& __x) | |
520 | { return _M_t._M_insert_unique_(__position, __x); } | |
521 | ||
522 | #if __cplusplus >= 201103L | |
523 | iterator | |
524 | insert(const_iterator __position, value_type&& __x) | |
525 | { return _M_t._M_insert_unique_(__position, std::move(__x)); } | |
526 | #endif | |
527 | ||
528 | /** | |
529 | * @brief A template function that attempts to insert a range | |
530 | * of elements. | |
531 | * @param __first Iterator pointing to the start of the range to be | |
532 | * inserted. | |
533 | * @param __last Iterator pointing to the end of the range. | |
534 | * | |
535 | * Complexity similar to that of the range constructor. | |
536 | */ | |
537 | template<typename _InputIterator> | |
538 | void | |
539 | insert(_InputIterator __first, _InputIterator __last) | |
540 | { _M_t._M_insert_unique(__first, __last); } | |
541 | ||
542 | #if __cplusplus >= 201103L | |
543 | /** | |
544 | * @brief Attempts to insert a list of elements into the %set. | |
545 | * @param __l A std::initializer_list<value_type> of elements | |
546 | * to be inserted. | |
547 | * | |
548 | * Complexity similar to that of the range constructor. | |
549 | */ | |
550 | void | |
551 | insert(initializer_list<value_type> __l) | |
552 | { this->insert(__l.begin(), __l.end()); } | |
553 | #endif | |
554 | ||
555 | #if __cplusplus >= 201103L | |
556 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
557 | // DR 130. Associative erase should return an iterator. | |
558 | /** | |
559 | * @brief Erases an element from a %set. | |
560 | * @param __position An iterator pointing to the element to be erased. | |
561 | * @return An iterator pointing to the element immediately following | |
562 | * @a __position prior to the element being erased. If no such | |
563 | * element exists, end() is returned. | |
564 | * | |
565 | * This function erases an element, pointed to by the given iterator, | |
566 | * from a %set. Note that this function only erases the element, and | |
567 | * that if the element is itself a pointer, the pointed-to memory is not | |
568 | * touched in any way. Managing the pointer is the user's | |
569 | * responsibility. | |
570 | */ | |
571 | _GLIBCXX_ABI_TAG_CXX11 | |
572 | iterator | |
573 | erase(const_iterator __position) | |
574 | { return _M_t.erase(__position); } | |
575 | #else | |
576 | /** | |
577 | * @brief Erases an element from a %set. | |
578 | * @param position An iterator pointing to the element to be erased. | |
579 | * | |
580 | * This function erases an element, pointed to by the given iterator, | |
581 | * from a %set. Note that this function only erases the element, and | |
582 | * that if the element is itself a pointer, the pointed-to memory is not | |
583 | * touched in any way. Managing the pointer is the user's | |
584 | * responsibility. | |
585 | */ | |
586 | void | |
587 | erase(iterator __position) | |
588 | { _M_t.erase(__position); } | |
589 | #endif | |
590 | ||
591 | /** | |
592 | * @brief Erases elements according to the provided key. | |
593 | * @param __x Key of element to be erased. | |
594 | * @return The number of elements erased. | |
595 | * | |
596 | * This function erases all the elements located by the given key from | |
597 | * a %set. | |
598 | * Note that this function only erases the element, and that if | |
599 | * the element is itself a pointer, the pointed-to memory is not touched | |
600 | * in any way. Managing the pointer is the user's responsibility. | |
601 | */ | |
602 | size_type | |
603 | erase(const key_type& __x) | |
604 | { return _M_t.erase(__x); } | |
605 | ||
606 | #if __cplusplus >= 201103L | |
607 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
608 | // DR 130. Associative erase should return an iterator. | |
609 | /** | |
610 | * @brief Erases a [__first,__last) range of elements from a %set. | |
611 | * @param __first Iterator pointing to the start of the range to be | |
612 | * erased. | |
613 | ||
614 | * @param __last Iterator pointing to the end of the range to | |
615 | * be erased. | |
616 | * @return The iterator @a __last. | |
617 | * | |
618 | * This function erases a sequence of elements from a %set. | |
619 | * Note that this function only erases the element, and that if | |
620 | * the element is itself a pointer, the pointed-to memory is not touched | |
621 | * in any way. Managing the pointer is the user's responsibility. | |
622 | */ | |
623 | _GLIBCXX_ABI_TAG_CXX11 | |
624 | iterator | |
625 | erase(const_iterator __first, const_iterator __last) | |
626 | { return _M_t.erase(__first, __last); } | |
627 | #else | |
628 | /** | |
629 | * @brief Erases a [first,last) range of elements from a %set. | |
630 | * @param __first Iterator pointing to the start of the range to be | |
631 | * erased. | |
632 | * @param __last Iterator pointing to the end of the range to | |
633 | * be erased. | |
634 | * | |
635 | * This function erases a sequence of elements from a %set. | |
636 | * Note that this function only erases the element, and that if | |
637 | * the element is itself a pointer, the pointed-to memory is not touched | |
638 | * in any way. Managing the pointer is the user's responsibility. | |
639 | */ | |
640 | void | |
641 | erase(iterator __first, iterator __last) | |
642 | { _M_t.erase(__first, __last); } | |
643 | #endif | |
644 | ||
645 | /** | |
646 | * Erases all elements in a %set. Note that this function only erases | |
647 | * the elements, and that if the elements themselves are pointers, the | |
648 | * pointed-to memory is not touched in any way. Managing the pointer is | |
649 | * the user's responsibility. | |
650 | */ | |
651 | void | |
652 | clear() _GLIBCXX_NOEXCEPT | |
653 | { _M_t.clear(); } | |
654 | ||
655 | // set operations: | |
656 | ||
657 | //@{ | |
658 | /** | |
659 | * @brief Finds the number of elements. | |
660 | * @param __x Element to located. | |
661 | * @return Number of elements with specified key. | |
662 | * | |
663 | * This function only makes sense for multisets; for set the result will | |
664 | * either be 0 (not present) or 1 (present). | |
665 | */ | |
666 | size_type | |
667 | count(const key_type& __x) const | |
668 | { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } | |
669 | ||
670 | #if __cplusplus > 201103L | |
671 | template<typename _Kt> | |
672 | auto | |
673 | count(const _Kt& __x) const | |
674 | -> decltype(_M_t._M_count_tr(__x)) | |
675 | { return _M_t._M_find_tr(__x) == _M_t.end() ? 0 : 1; } | |
676 | #endif | |
677 | //@} | |
678 | ||
679 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
680 | // 214. set::find() missing const overload | |
681 | //@{ | |
682 | /** | |
683 | * @brief Tries to locate an element in a %set. | |
684 | * @param __x Element to be located. | |
685 | * @return Iterator pointing to sought-after element, or end() if not | |
686 | * found. | |
687 | * | |
688 | * This function takes a key and tries to locate the element with which | |
689 | * the key matches. If successful the function returns an iterator | |
690 | * pointing to the sought after element. If unsuccessful it returns the | |
691 | * past-the-end ( @c end() ) iterator. | |
692 | */ | |
693 | iterator | |
694 | find(const key_type& __x) | |
695 | { return _M_t.find(__x); } | |
696 | ||
697 | const_iterator | |
698 | find(const key_type& __x) const | |
699 | { return _M_t.find(__x); } | |
700 | ||
701 | #if __cplusplus > 201103L | |
702 | template<typename _Kt> | |
703 | auto | |
704 | find(const _Kt& __x) | |
705 | -> decltype(iterator{_M_t._M_find_tr(__x)}) | |
706 | { return iterator{_M_t._M_find_tr(__x)}; } | |
707 | ||
708 | template<typename _Kt> | |
709 | auto | |
710 | find(const _Kt& __x) const | |
711 | -> decltype(const_iterator{_M_t._M_find_tr(__x)}) | |
712 | { return const_iterator{_M_t._M_find_tr(__x)}; } | |
713 | #endif | |
714 | //@} | |
715 | ||
716 | //@{ | |
717 | /** | |
718 | * @brief Finds the beginning of a subsequence matching given key. | |
719 | * @param __x Key to be located. | |
720 | * @return Iterator pointing to first element equal to or greater | |
721 | * than key, or end(). | |
722 | * | |
723 | * This function returns the first element of a subsequence of elements | |
724 | * that matches the given key. If unsuccessful it returns an iterator | |
725 | * pointing to the first element that has a greater value than given key | |
726 | * or end() if no such element exists. | |
727 | */ | |
728 | iterator | |
729 | lower_bound(const key_type& __x) | |
730 | { return _M_t.lower_bound(__x); } | |
731 | ||
732 | const_iterator | |
733 | lower_bound(const key_type& __x) const | |
734 | { return _M_t.lower_bound(__x); } | |
735 | ||
736 | #if __cplusplus > 201103L | |
737 | template<typename _Kt> | |
738 | auto | |
739 | lower_bound(const _Kt& __x) | |
740 | -> decltype(_M_t._M_lower_bound_tr(__x)) | |
741 | { return _M_t._M_lower_bound_tr(__x); } | |
742 | ||
743 | template<typename _Kt> | |
744 | auto | |
745 | lower_bound(const _Kt& __x) const | |
746 | -> decltype(_M_t._M_lower_bound_tr(__x)) | |
747 | { return _M_t._M_lower_bound_tr(__x); } | |
748 | #endif | |
749 | //@} | |
750 | ||
751 | //@{ | |
752 | /** | |
753 | * @brief Finds the end of a subsequence matching given key. | |
754 | * @param __x Key to be located. | |
755 | * @return Iterator pointing to the first element | |
756 | * greater than key, or end(). | |
757 | */ | |
758 | iterator | |
759 | upper_bound(const key_type& __x) | |
760 | { return _M_t.upper_bound(__x); } | |
761 | ||
762 | const_iterator | |
763 | upper_bound(const key_type& __x) const | |
764 | { return _M_t.upper_bound(__x); } | |
765 | ||
766 | #if __cplusplus > 201103L | |
767 | template<typename _Kt> | |
768 | auto | |
769 | upper_bound(const _Kt& __x) | |
770 | -> decltype(_M_t._M_upper_bound_tr(__x)) | |
771 | { return _M_t._M_upper_bound_tr(__x); } | |
772 | ||
773 | template<typename _Kt> | |
774 | auto | |
775 | upper_bound(const _Kt& __x) const | |
776 | -> decltype(_M_t._M_upper_bound_tr(__x)) | |
777 | { return _M_t._M_upper_bound_tr(__x); } | |
778 | #endif | |
779 | //@} | |
780 | ||
781 | //@{ | |
782 | /** | |
783 | * @brief Finds a subsequence matching given key. | |
784 | * @param __x Key to be located. | |
785 | * @return Pair of iterators that possibly points to the subsequence | |
786 | * matching given key. | |
787 | * | |
788 | * This function is equivalent to | |
789 | * @code | |
790 | * std::make_pair(c.lower_bound(val), | |
791 | * c.upper_bound(val)) | |
792 | * @endcode | |
793 | * (but is faster than making the calls separately). | |
794 | * | |
795 | * This function probably only makes sense for multisets. | |
796 | */ | |
797 | std::pair<iterator, iterator> | |
798 | equal_range(const key_type& __x) | |
799 | { return _M_t.equal_range(__x); } | |
800 | ||
801 | std::pair<const_iterator, const_iterator> | |
802 | equal_range(const key_type& __x) const | |
803 | { return _M_t.equal_range(__x); } | |
804 | ||
805 | #if __cplusplus > 201103L | |
806 | template<typename _Kt> | |
807 | auto | |
808 | equal_range(const _Kt& __x) | |
809 | -> decltype(_M_t._M_equal_range_tr(__x)) | |
810 | { return _M_t._M_equal_range_tr(__x); } | |
811 | ||
812 | template<typename _Kt> | |
813 | auto | |
814 | equal_range(const _Kt& __x) const | |
815 | -> decltype(_M_t._M_equal_range_tr(__x)) | |
816 | { return _M_t._M_equal_range_tr(__x); } | |
817 | #endif | |
818 | //@} | |
819 | ||
820 | template<typename _K1, typename _C1, typename _A1> | |
821 | friend bool | |
822 | operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&); | |
823 | ||
824 | template<typename _K1, typename _C1, typename _A1> | |
825 | friend bool | |
826 | operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&); | |
827 | }; | |
828 | ||
829 | ||
830 | /** | |
831 | * @brief Set equality comparison. | |
832 | * @param __x A %set. | |
833 | * @param __y A %set of the same type as @a x. | |
834 | * @return True iff the size and elements of the sets are equal. | |
835 | * | |
836 | * This is an equivalence relation. It is linear in the size of the sets. | |
837 | * Sets are considered equivalent if their sizes are equal, and if | |
838 | * corresponding elements compare equal. | |
839 | */ | |
840 | template<typename _Key, typename _Compare, typename _Alloc> | |
841 | inline bool | |
842 | operator==(const set<_Key, _Compare, _Alloc>& __x, | |
843 | const set<_Key, _Compare, _Alloc>& __y) | |
844 | { return __x._M_t == __y._M_t; } | |
845 | ||
846 | /** | |
847 | * @brief Set ordering relation. | |
848 | * @param __x A %set. | |
849 | * @param __y A %set of the same type as @a x. | |
850 | * @return True iff @a __x is lexicographically less than @a __y. | |
851 | * | |
852 | * This is a total ordering relation. It is linear in the size of the | |
853 | * sets. The elements must be comparable with @c <. | |
854 | * | |
855 | * See std::lexicographical_compare() for how the determination is made. | |
856 | */ | |
857 | template<typename _Key, typename _Compare, typename _Alloc> | |
858 | inline bool | |
859 | operator<(const set<_Key, _Compare, _Alloc>& __x, | |
860 | const set<_Key, _Compare, _Alloc>& __y) | |
861 | { return __x._M_t < __y._M_t; } | |
862 | ||
863 | /// Returns !(x == y). | |
864 | template<typename _Key, typename _Compare, typename _Alloc> | |
865 | inline bool | |
866 | operator!=(const set<_Key, _Compare, _Alloc>& __x, | |
867 | const set<_Key, _Compare, _Alloc>& __y) | |
868 | { return !(__x == __y); } | |
869 | ||
870 | /// Returns y < x. | |
871 | template<typename _Key, typename _Compare, typename _Alloc> | |
872 | inline bool | |
873 | operator>(const set<_Key, _Compare, _Alloc>& __x, | |
874 | const set<_Key, _Compare, _Alloc>& __y) | |
875 | { return __y < __x; } | |
876 | ||
877 | /// Returns !(y < x) | |
878 | template<typename _Key, typename _Compare, typename _Alloc> | |
879 | inline bool | |
880 | operator<=(const set<_Key, _Compare, _Alloc>& __x, | |
881 | const set<_Key, _Compare, _Alloc>& __y) | |
882 | { return !(__y < __x); } | |
883 | ||
884 | /// Returns !(x < y) | |
885 | template<typename _Key, typename _Compare, typename _Alloc> | |
886 | inline bool | |
887 | operator>=(const set<_Key, _Compare, _Alloc>& __x, | |
888 | const set<_Key, _Compare, _Alloc>& __y) | |
889 | { return !(__x < __y); } | |
890 | ||
891 | /// See std::set::swap(). | |
892 | template<typename _Key, typename _Compare, typename _Alloc> | |
893 | inline void | |
894 | swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y) | |
895 | { __x.swap(__y); } | |
896 | ||
897 | _GLIBCXX_END_NAMESPACE_CONTAINER | |
898 | } //namespace std | |
899 | #endif /* _STL_SET_H */ | |
900 |
Copyright (c) 2006-2012 Rogue Wave Software, Inc. All Rights Reserved.
Patents pending.