1 : // Set implementation -*- C++ -*-
2 :
3 : // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 : // Free Software Foundation, Inc.
5 : //
6 : // This file is part of the GNU ISO C++ Library. This library is free
7 : // software; you can redistribute it and/or modify it under the
8 : // terms of the GNU General Public License as published by the
9 : // Free Software Foundation; either version 2, or (at your option)
10 : // any later version.
11 :
12 : // This library is distributed in the hope that it will be useful,
13 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : // GNU General Public License for more details.
16 :
17 : // You should have received a copy of the GNU General Public License along
18 : // with this library; see the file COPYING. If not, write to the Free
19 : // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 : // USA.
21 :
22 : // As a special exception, you may use this file as part of a free software
23 : // library without restriction. Specifically, if other files instantiate
24 : // templates or use macros or inline functions from this file, or you compile
25 : // this file and link it with other files to produce an executable, this
26 : // file does not by itself cause the resulting executable to be covered by
27 : // the GNU General Public License. This exception does not however
28 : // invalidate any other reasons why the executable file might be covered by
29 : // the GNU General Public License.
30 :
31 : /*
32 : *
33 : * Copyright (c) 1994
34 : * Hewlett-Packard Company
35 : *
36 : * Permission to use, copy, modify, distribute and sell this software
37 : * and its documentation for any purpose is hereby granted without fee,
38 : * provided that the above copyright notice appear in all copies and
39 : * that both that copyright notice and this permission notice appear
40 : * in supporting documentation. Hewlett-Packard Company makes no
41 : * representations about the suitability of this software for any
42 : * purpose. It is provided "as is" without express or implied warranty.
43 : *
44 : *
45 : * Copyright (c) 1996,1997
46 : * Silicon Graphics Computer Systems, Inc.
47 : *
48 : * Permission to use, copy, modify, distribute and sell this software
49 : * and its documentation for any purpose is hereby granted without fee,
50 : * provided that the above copyright notice appear in all copies and
51 : * that both that copyright notice and this permission notice appear
52 : * in supporting documentation. Silicon Graphics makes no
53 : * representations about the suitability of this software for any
54 : * purpose. It is provided "as is" without express or implied warranty.
55 : */
56 :
57 : /** @file stl_set.h
58 : * This is an internal header file, included by other library headers.
59 : * You should not attempt to use it directly.
60 : */
61 :
62 : #ifndef _STL_SET_H
63 : #define _STL_SET_H 1
64 :
65 : #include <bits/concept_check.h>
66 :
67 : _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
68 :
69 : /**
70 : * @brief A standard container made up of unique keys, which can be
71 : * retrieved in logarithmic time.
72 : *
73 : * @ingroup Containers
74 : * @ingroup Assoc_containers
75 : *
76 : * Meets the requirements of a <a href="tables.html#65">container</a>, a
77 : * <a href="tables.html#66">reversible container</a>, and an
78 : * <a href="tables.html#69">associative container</a> (using unique keys).
79 : *
80 : * Sets support bidirectional iterators.
81 : *
82 : * @param Key Type of key objects.
83 : * @param Compare Comparison function object type, defaults to less<Key>.
84 : * @param Alloc Allocator type, defaults to allocator<Key>.
85 : *
86 : * The private tree data is declared exactly the same way for set and
87 : * multiset; the distinction is made entirely in how the tree functions are
88 : * called (*_unique versus *_equal, same as the standard).
89 : */
90 : template<typename _Key, typename _Compare = std::less<_Key>,
91 : typename _Alloc = std::allocator<_Key> >
92 : class set
93 1308625 : {
94 : // concept requirements
95 : typedef typename _Alloc::value_type _Alloc_value_type;
96 : __glibcxx_class_requires(_Key, _SGIAssignableConcept)
97 : __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
98 : _BinaryFunctionConcept)
99 : __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
100 :
101 : public:
102 : // typedefs:
103 : //@{
104 : /// Public typedefs.
105 : typedef _Key key_type;
106 : typedef _Key value_type;
107 : typedef _Compare key_compare;
108 : typedef _Compare value_compare;
109 : typedef _Alloc allocator_type;
110 : //@}
111 :
112 : private:
113 : typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type;
114 :
115 : typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
116 : key_compare, _Key_alloc_type> _Rep_type;
117 : _Rep_type _M_t; // Red-black tree representing set.
118 :
119 : public:
120 : //@{
121 : /// Iterator-related typedefs.
122 : typedef typename _Key_alloc_type::pointer pointer;
123 : typedef typename _Key_alloc_type::const_pointer const_pointer;
124 : typedef typename _Key_alloc_type::reference reference;
125 : typedef typename _Key_alloc_type::const_reference const_reference;
126 : // _GLIBCXX_RESOLVE_LIB_DEFECTS
127 : // DR 103. set::iterator is required to be modifiable,
128 : // but this allows modification of keys.
129 : typedef typename _Rep_type::const_iterator iterator;
130 : typedef typename _Rep_type::const_iterator const_iterator;
131 : typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
132 : typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
133 : typedef typename _Rep_type::size_type size_type;
134 : typedef typename _Rep_type::difference_type difference_type;
135 : //@}
136 :
137 : // allocation/deallocation
138 : /**
139 : * @brief Default constructor creates no elements.
140 : */
141 277740 : set()
142 277740 : : _M_t() { }
143 :
144 : /**
145 : * @brief Creates a %set with no elements.
146 : * @param comp Comparator to use.
147 : * @param a An allocator object.
148 : */
149 : explicit
150 : set(const _Compare& __comp,
151 : const allocator_type& __a = allocator_type())
152 : : _M_t(__comp, __a) { }
153 :
154 : /**
155 : * @brief Builds a %set from a range.
156 : * @param first An input iterator.
157 : * @param last An input iterator.
158 : *
159 : * Create a %set consisting of copies of the elements from [first,last).
160 : * This is linear in N if the range is already sorted, and NlogN
161 : * otherwise (where N is distance(first,last)).
162 : */
163 : template<typename _InputIterator>
164 : set(_InputIterator __first, _InputIterator __last)
165 : : _M_t()
166 : { _M_t._M_insert_unique(__first, __last); }
167 :
168 : /**
169 : * @brief Builds a %set from a range.
170 : * @param first An input iterator.
171 : * @param last An input iterator.
172 : * @param comp A comparison functor.
173 : * @param a An allocator object.
174 : *
175 : * Create a %set consisting of copies of the elements from [first,last).
176 : * This is linear in N if the range is already sorted, and NlogN
177 : * otherwise (where N is distance(first,last)).
178 : */
179 : template<typename _InputIterator>
180 : set(_InputIterator __first, _InputIterator __last,
181 : const _Compare& __comp,
182 : const allocator_type& __a = allocator_type())
183 : : _M_t(__comp, __a)
184 : { _M_t._M_insert_unique(__first, __last); }
185 :
186 : /**
187 : * @brief %Set copy constructor.
188 : * @param x A %set of identical element and allocator types.
189 : *
190 : * The newly-created %set uses a copy of the allocation object used
191 : * by @a x.
192 : */
193 925151 : set(const set& __x)
194 925151 : : _M_t(__x._M_t) { }
195 :
196 : #ifdef __GXX_EXPERIMENTAL_CXX0X__
197 : /**
198 : * @brief %Set move constructor
199 : * @param x A %set of identical element and allocator types.
200 : *
201 : * The newly-created %set contains the exact contents of @a x.
202 : * The contents of @a x are a valid, but unspecified %set.
203 : */
204 : set(set&& __x)
205 : : _M_t(std::forward<_Rep_type>(__x._M_t)) { }
206 : #endif
207 :
208 : /**
209 : * @brief %Set assignment operator.
210 : * @param x A %set of identical element and allocator types.
211 : *
212 : * All the elements of @a x are copied, but unlike the copy constructor,
213 : * the allocator object is not copied.
214 : */
215 : set&
216 80 : operator=(const set& __x)
217 : {
218 80 : _M_t = __x._M_t;
219 80 : return *this;
220 : }
221 :
222 : #ifdef __GXX_EXPERIMENTAL_CXX0X__
223 : /**
224 : * @brief %Set move assignment operator.
225 : * @param x A %set of identical element and allocator types.
226 : *
227 : * The contents of @a x are moved into this %set (without copying).
228 : * @a x is a valid, but unspecified %set.
229 : */
230 : set&
231 : operator=(set&& __x)
232 : {
233 : // NB: DR 675.
234 : this->clear();
235 : this->swap(__x);
236 : return *this;
237 : }
238 : #endif
239 :
240 : // accessors:
241 :
242 : /// Returns the comparison object with which the %set was constructed.
243 : key_compare
244 : key_comp() const
245 : { return _M_t.key_comp(); }
246 : /// Returns the comparison object with which the %set was constructed.
247 : value_compare
248 : value_comp() const
249 : { return _M_t.key_comp(); }
250 : /// Returns the allocator object with which the %set was constructed.
251 : allocator_type
252 : get_allocator() const
253 : { return _M_t.get_allocator(); }
254 :
255 : /**
256 : * Returns a read-only (constant) iterator that points to the first
257 : * element in the %set. Iteration is done in ascending order according
258 : * to the keys.
259 : */
260 : iterator
261 468023 : begin() const
262 468023 : { return _M_t.begin(); }
263 :
264 : /**
265 : * Returns a read-only (constant) iterator that points one past the last
266 : * element in the %set. Iteration is done in ascending order according
267 : * to the keys.
268 : */
269 : iterator
270 1576796 : end() const
271 1576796 : { return _M_t.end(); }
272 :
273 : /**
274 : * Returns a read-only (constant) iterator that points to the last
275 : * element in the %set. Iteration is done in descending order according
276 : * to the keys.
277 : */
278 : reverse_iterator
279 : rbegin() const
280 : { return _M_t.rbegin(); }
281 :
282 : /**
283 : * Returns a read-only (constant) reverse iterator that points to the
284 : * last pair in the %set. Iteration is done in descending order
285 : * according to the keys.
286 : */
287 : reverse_iterator
288 : rend() const
289 : { return _M_t.rend(); }
290 :
291 : #ifdef __GXX_EXPERIMENTAL_CXX0X__
292 : /**
293 : * Returns a read-only (constant) iterator that points to the first
294 : * element in the %set. Iteration is done in ascending order according
295 : * to the keys.
296 : */
297 : iterator
298 : cbegin() const
299 : { return _M_t.begin(); }
300 :
301 : /**
302 : * Returns a read-only (constant) iterator that points one past the last
303 : * element in the %set. Iteration is done in ascending order according
304 : * to the keys.
305 : */
306 : iterator
307 : cend() const
308 : { return _M_t.end(); }
309 :
310 : /**
311 : * Returns a read-only (constant) iterator that points to the last
312 : * element in the %set. Iteration is done in descending order according
313 : * to the keys.
314 : */
315 : reverse_iterator
316 : crbegin() const
317 : { return _M_t.rbegin(); }
318 :
319 : /**
320 : * Returns a read-only (constant) reverse iterator that points to the
321 : * last pair in the %set. Iteration is done in descending order
322 : * according to the keys.
323 : */
324 : reverse_iterator
325 : crend() const
326 : { return _M_t.rend(); }
327 : #endif
328 :
329 : /// Returns true if the %set is empty.
330 : bool
331 338386 : empty() const
332 338386 : { return _M_t.empty(); }
333 :
334 : /// Returns the size of the %set.
335 : size_type
336 84631 : size() const
337 84631 : { return _M_t.size(); }
338 :
339 : /// Returns the maximum size of the %set.
340 : size_type
341 : max_size() const
342 : { return _M_t.max_size(); }
343 :
344 : /**
345 : * @brief Swaps data with another %set.
346 : * @param x A %set of the same element and allocator types.
347 : *
348 : * This exchanges the elements between two sets in constant time.
349 : * (It is only swapping a pointer, an integer, and an instance of
350 : * the @c Compare type (which itself is often stateless and empty), so it
351 : * should be quite fast.)
352 : * Note that the global std::swap() function is specialized such that
353 : * std::swap(s1,s2) will feed to this function.
354 : */
355 : void
356 : #ifdef __GXX_EXPERIMENTAL_CXX0X__
357 : swap(set&& __x)
358 : #else
359 : swap(set& __x)
360 : #endif
361 : { _M_t.swap(__x._M_t); }
362 :
363 : // insert/erase
364 : /**
365 : * @brief Attempts to insert an element into the %set.
366 : * @param x Element to be inserted.
367 : * @return A pair, of which the first element is an iterator that points
368 : * to the possibly inserted element, and the second is a bool
369 : * that is true if the element was actually inserted.
370 : *
371 : * This function attempts to insert an element into the %set. A %set
372 : * relies on unique keys and thus an element is only inserted if it is
373 : * not already present in the %set.
374 : *
375 : * Insertion requires logarithmic time.
376 : */
377 : std::pair<iterator, bool>
378 1422360 : insert(const value_type& __x)
379 : {
380 : std::pair<typename _Rep_type::iterator, bool> __p =
381 1422360 : _M_t._M_insert_unique(__x);
382 1422360 : return std::pair<iterator, bool>(__p.first, __p.second);
383 : }
384 :
385 : /**
386 : * @brief Attempts to insert an element into the %set.
387 : * @param position An iterator that serves as a hint as to where the
388 : * element should be inserted.
389 : * @param x Element to be inserted.
390 : * @return An iterator that points to the element with key of @a x (may
391 : * or may not be the element passed in).
392 : *
393 : * This function is not concerned about whether the insertion took place,
394 : * and thus does not return a boolean like the single-argument insert()
395 : * does. Note that the first parameter is only a hint and can
396 : * potentially improve the performance of the insertion process. A bad
397 : * hint would cause no gains in efficiency.
398 : *
399 : * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
400 : * for more on "hinting".
401 : *
402 : * Insertion requires logarithmic time (if the hint is not taken).
403 : */
404 : iterator
405 107613 : insert(iterator __position, const value_type& __x)
406 107613 : { return _M_t._M_insert_unique_(__position, __x); }
407 :
408 : /**
409 : * @brief A template function that attempts to insert a range of elements.
410 : * @param first Iterator pointing to the start of the range to be
411 : * inserted.
412 : * @param last Iterator pointing to the end of the range.
413 : *
414 : * Complexity similar to that of the range constructor.
415 : */
416 : template<typename _InputIterator>
417 : void
418 : insert(_InputIterator __first, _InputIterator __last)
419 : { _M_t._M_insert_unique(__first, __last); }
420 :
421 : /**
422 : * @brief Erases an element from a %set.
423 : * @param position An iterator pointing to the element to be erased.
424 : *
425 : * This function erases an element, pointed to by the given iterator,
426 : * from a %set. Note that this function only erases the element, and
427 : * that if the element is itself a pointer, the pointed-to memory is not
428 : * touched in any way. Managing the pointer is the user's responsibility.
429 : */
430 : void
431 129 : erase(iterator __position)
432 129 : { _M_t.erase(__position); }
433 :
434 : /**
435 : * @brief Erases elements according to the provided key.
436 : * @param x Key of element to be erased.
437 : * @return The number of elements erased.
438 : *
439 : * This function erases all the elements located by the given key from
440 : * a %set.
441 : * Note that this function only erases the element, and that if
442 : * the element is itself a pointer, the pointed-to memory is not touched
443 : * in any way. Managing the pointer is the user's responsibility.
444 : */
445 : size_type
446 9 : erase(const key_type& __x)
447 9 : { return _M_t.erase(__x); }
448 :
449 : /**
450 : * @brief Erases a [first,last) range of elements from a %set.
451 : * @param first Iterator pointing to the start of the range to be
452 : * erased.
453 : * @param last Iterator pointing to the end of the range to be erased.
454 : *
455 : * This function erases a sequence of elements from a %set.
456 : * Note that this function only erases the element, and that if
457 : * the element is itself a pointer, the pointed-to memory is not touched
458 : * in any way. Managing the pointer is the user's responsibility.
459 : */
460 : void
461 : erase(iterator __first, iterator __last)
462 : { _M_t.erase(__first, __last); }
463 :
464 : /**
465 : * Erases all elements in a %set. Note that this function only erases
466 : * the elements, and that if the elements themselves are pointers, the
467 : * pointed-to memory is not touched in any way. Managing the pointer is
468 : * the user's responsibility.
469 : */
470 : void
471 84600 : clear()
472 84600 : { _M_t.clear(); }
473 :
474 : // set operations:
475 :
476 : /**
477 : * @brief Finds the number of elements.
478 : * @param x Element to located.
479 : * @return Number of elements with specified key.
480 : *
481 : * This function only makes sense for multisets; for set the result will
482 : * either be 0 (not present) or 1 (present).
483 : */
484 : size_type
485 : count(const key_type& __x) const
486 : { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
487 :
488 : // _GLIBCXX_RESOLVE_LIB_DEFECTS
489 : // 214. set::find() missing const overload
490 : //@{
491 : /**
492 : * @brief Tries to locate an element in a %set.
493 : * @param x Element to be located.
494 : * @return Iterator pointing to sought-after element, or end() if not
495 : * found.
496 : *
497 : * This function takes a key and tries to locate the element with which
498 : * the key matches. If successful the function returns an iterator
499 : * pointing to the sought after element. If unsuccessful it returns the
500 : * past-the-end ( @c end() ) iterator.
501 : */
502 : iterator
503 39 : find(const key_type& __x)
504 39 : { return _M_t.find(__x); }
505 :
506 : const_iterator
507 16 : find(const key_type& __x) const
508 16 : { return _M_t.find(__x); }
509 : //@}
510 :
511 : //@{
512 : /**
513 : * @brief Finds the beginning of a subsequence matching given key.
514 : * @param x Key to be located.
515 : * @return Iterator pointing to first element equal to or greater
516 : * than key, or end().
517 : *
518 : * This function returns the first element of a subsequence of elements
519 : * that matches the given key. If unsuccessful it returns an iterator
520 : * pointing to the first element that has a greater value than given key
521 : * or end() if no such element exists.
522 : */
523 : iterator
524 : lower_bound(const key_type& __x)
525 : { return _M_t.lower_bound(__x); }
526 :
527 : const_iterator
528 : lower_bound(const key_type& __x) const
529 : { return _M_t.lower_bound(__x); }
530 : //@}
531 :
532 : //@{
533 : /**
534 : * @brief Finds the end of a subsequence matching given key.
535 : * @param x Key to be located.
536 : * @return Iterator pointing to the first element
537 : * greater than key, or end().
538 : */
539 : iterator
540 : upper_bound(const key_type& __x)
541 : { return _M_t.upper_bound(__x); }
542 :
543 : const_iterator
544 : upper_bound(const key_type& __x) const
545 : { return _M_t.upper_bound(__x); }
546 : //@}
547 :
548 : //@{
549 : /**
550 : * @brief Finds a subsequence matching given key.
551 : * @param x Key to be located.
552 : * @return Pair of iterators that possibly points to the subsequence
553 : * matching given key.
554 : *
555 : * This function is equivalent to
556 : * @code
557 : * std::make_pair(c.lower_bound(val),
558 : * c.upper_bound(val))
559 : * @endcode
560 : * (but is faster than making the calls separately).
561 : *
562 : * This function probably only makes sense for multisets.
563 : */
564 : std::pair<iterator, iterator>
565 : equal_range(const key_type& __x)
566 : { return _M_t.equal_range(__x); }
567 :
568 : std::pair<const_iterator, const_iterator>
569 : equal_range(const key_type& __x) const
570 : { return _M_t.equal_range(__x); }
571 : //@}
572 :
573 : template<typename _K1, typename _C1, typename _A1>
574 : friend bool
575 : operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
576 :
577 : template<typename _K1, typename _C1, typename _A1>
578 : friend bool
579 : operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
580 : };
581 :
582 :
583 : /**
584 : * @brief Set equality comparison.
585 : * @param x A %set.
586 : * @param y A %set of the same type as @a x.
587 : * @return True iff the size and elements of the sets are equal.
588 : *
589 : * This is an equivalence relation. It is linear in the size of the sets.
590 : * Sets are considered equivalent if their sizes are equal, and if
591 : * corresponding elements compare equal.
592 : */
593 : template<typename _Key, typename _Compare, typename _Alloc>
594 : inline bool
595 : operator==(const set<_Key, _Compare, _Alloc>& __x,
596 4 : const set<_Key, _Compare, _Alloc>& __y)
597 4 : { return __x._M_t == __y._M_t; }
598 :
599 : /**
600 : * @brief Set ordering relation.
601 : * @param x A %set.
602 : * @param y A %set of the same type as @a x.
603 : * @return True iff @a x is lexicographically less than @a y.
604 : *
605 : * This is a total ordering relation. It is linear in the size of the
606 : * maps. The elements must be comparable with @c <.
607 : *
608 : * See std::lexicographical_compare() for how the determination is made.
609 : */
610 : template<typename _Key, typename _Compare, typename _Alloc>
611 : inline bool
612 : operator<(const set<_Key, _Compare, _Alloc>& __x,
613 : const set<_Key, _Compare, _Alloc>& __y)
614 : { return __x._M_t < __y._M_t; }
615 :
616 : /// Returns !(x == y).
617 : template<typename _Key, typename _Compare, typename _Alloc>
618 : inline bool
619 : operator!=(const set<_Key, _Compare, _Alloc>& __x,
620 : const set<_Key, _Compare, _Alloc>& __y)
621 : { return !(__x == __y); }
622 :
623 : /// Returns y < x.
624 : template<typename _Key, typename _Compare, typename _Alloc>
625 : inline bool
626 : operator>(const set<_Key, _Compare, _Alloc>& __x,
627 : const set<_Key, _Compare, _Alloc>& __y)
628 : { return __y < __x; }
629 :
630 : /// Returns !(y < x)
631 : template<typename _Key, typename _Compare, typename _Alloc>
632 : inline bool
633 : operator<=(const set<_Key, _Compare, _Alloc>& __x,
634 : const set<_Key, _Compare, _Alloc>& __y)
635 : { return !(__y < __x); }
636 :
637 : /// Returns !(x < y)
638 : template<typename _Key, typename _Compare, typename _Alloc>
639 : inline bool
640 : operator>=(const set<_Key, _Compare, _Alloc>& __x,
641 : const set<_Key, _Compare, _Alloc>& __y)
642 : { return !(__x < __y); }
643 :
644 : /// See std::set::swap().
645 : template<typename _Key, typename _Compare, typename _Alloc>
646 : inline void
647 : swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y)
648 : { __x.swap(__y); }
649 :
650 : #ifdef __GXX_EXPERIMENTAL_CXX0X__
651 : template<typename _Key, typename _Compare, typename _Alloc>
652 : inline void
653 : swap(set<_Key, _Compare, _Alloc>&& __x, set<_Key, _Compare, _Alloc>& __y)
654 : { __x.swap(__y); }
655 :
656 : template<typename _Key, typename _Compare, typename _Alloc>
657 : inline void
658 : swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>&& __y)
659 : { __x.swap(__y); }
660 : #endif
661 :
662 : _GLIBCXX_END_NESTED_NAMESPACE
663 :
664 : #endif /* _STL_SET_H */
|