1 // Set implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
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 2, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 #include <bits/concept_check.h>
68 // Forward declarations of operators < and ==, needed for friend declaration.
69 template<class _Key, class _Compare = less<_Key>,
70 class _Alloc = allocator<_Key> >
73 template<class _Key, class _Compare, class _Alloc>
75 operator==(const set<_Key,_Compare,_Alloc>& __x,
76 const set<_Key,_Compare,_Alloc>& __y);
78 template<class _Key, class _Compare, class _Alloc>
80 operator<(const set<_Key,_Compare,_Alloc>& __x,
81 const set<_Key,_Compare,_Alloc>& __y);
84 * @brief A standard container made up of unique keys, which can be
85 * retrieved in logarithmic time.
88 * @ingroup Assoc_containers
90 * Meets the requirements of a <a href="tables.html#65">container</a>, a
91 * <a href="tables.html#66">reversible container</a>, and an
92 * <a href="tables.html#69">associative container</a> (using unique keys).
94 * Sets support bidirectional iterators.
96 * @param Key Type of key objects.
97 * @param Compare Comparison function object type, defaults to less<Key>.
98 * @param Alloc Allocator type, defaults to allocator<Key>.
101 * The private tree data is declared exactly the same way for set and
102 * multiset; the distinction is made entirely in how the tree functions are
103 * called (*_unique versus *_equal, same as the standard).
106 template<class _Key, class _Compare, class _Alloc>
109 // concept requirements
110 __glibcxx_class_requires(_Key, _SGIAssignableConcept)
111 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
112 _BinaryFunctionConcept)
118 typedef _Key key_type;
119 typedef _Key value_type;
120 typedef _Compare key_compare;
121 typedef _Compare value_compare;
125 typedef _Rb_tree<key_type, value_type,
126 _Identity<value_type>, key_compare, _Alloc> _Rep_type;
127 _Rep_type _M_t; // red-black tree representing set
130 /// Iterator-related typedefs.
131 typedef typename _Alloc::pointer pointer;
132 typedef typename _Alloc::const_pointer const_pointer;
133 typedef typename _Alloc::reference reference;
134 typedef typename _Alloc::const_reference const_reference;
135 typedef typename _Rep_type::const_iterator iterator;
136 typedef typename _Rep_type::const_iterator const_iterator;
137 typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
138 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
139 typedef typename _Rep_type::size_type size_type;
140 typedef typename _Rep_type::difference_type difference_type;
141 typedef typename _Rep_type::allocator_type allocator_type;
144 // allocation/deallocation
145 /// Default constructor creates no elements.
147 : _M_t(_Compare(), allocator_type()) {}
150 * @brief Default constructor creates no elements.
152 * @param comp Comparator to use.
153 * @param a Allocator to use.
155 explicit set(const _Compare& __comp,
156 const allocator_type& __a = allocator_type())
157 : _M_t(__comp, __a) {}
160 * @brief Builds a %set from a range.
161 * @param first An input iterator.
162 * @param last An input iterator.
164 * Create a %set consisting of copies of the elements from [first,last).
165 * This is linear in N if the range is already sorted, and NlogN
166 * otherwise (where N is distance(first,last)).
168 template<class _InputIterator>
169 set(_InputIterator __first, _InputIterator __last)
170 : _M_t(_Compare(), allocator_type())
171 { _M_t.insert_unique(__first, __last); }
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.
180 * Create a %set consisting of copies of the elements from [first,last).
181 * This is linear in N if the range is already sorted, and NlogN
182 * otherwise (where N is distance(first,last)).
184 template<class _InputIterator>
185 set(_InputIterator __first, _InputIterator __last,
186 const _Compare& __comp,
187 const allocator_type& __a = allocator_type())
189 { _M_t.insert_unique(__first, __last); }
192 * @brief Set copy constructor.
193 * @param x A %set of identical element and allocator types.
195 * The newly-created %set uses a copy of the allocation object used
198 set(const set<_Key,_Compare,_Alloc>& __x)
202 * @brief Set assignment operator.
203 * @param x A %set of identical element and allocator types.
205 * All the elements of @a x are copied, but unlike the copy constructor,
206 * the allocator object is not copied.
208 set<_Key,_Compare,_Alloc>&
209 operator=(const set<_Key, _Compare, _Alloc>& __x)
217 /// Returns the comparison object with which the %set was constructed.
220 { return _M_t.key_comp(); }
221 /// Returns the comparison object with which the %set was constructed.
224 { return _M_t.key_comp(); }
225 /// Returns the allocator object with which the %set was constructed.
227 get_allocator() const
228 { return _M_t.get_allocator(); }
231 * Returns a read/write iterator that points to the first element in the
232 * %set. Iteration is done in ascending order according to the keys.
236 { return _M_t.begin(); }
239 * Returns a read/write iterator that points one past the last element in
240 * the %set. Iteration is done in ascending order according to the keys.
244 { return _M_t.end(); }
247 * Returns a read/write reverse iterator that points to the last element
248 * in the %set. Iteration is done in descending order according to the
253 { return _M_t.rbegin(); }
256 * Returns a read-only (constant) reverse iterator that points to the
257 * last pair in the %map. Iteration is done in descending order
258 * according to the keys.
262 { return _M_t.rend(); }
264 /// Returns true if the %set is empty.
267 { return _M_t.empty(); }
269 /// Returns the size of the %set.
272 { return _M_t.size(); }
274 /// Returns the maximum size of the %set.
277 { return _M_t.max_size(); }
280 * @brief Swaps data with another %set.
281 * @param x A %set of the same element and allocator types.
283 * This exchanges the elements between two sets in constant time.
284 * (It is only swapping a pointer, an integer, and an instance of
285 * the @c Compare type (which itself is often stateless and empty), so it
286 * should be quite fast.)
287 * Note that the global std::swap() function is specialized such that
288 * std::swap(s1,s2) will feed to this function.
291 swap(set<_Key,_Compare,_Alloc>& __x)
292 { _M_t.swap(__x._M_t); }
296 * @brief Attempts to insert an element into the %set.
297 * @param x Element to be inserted.
298 * @return A pair, of which the first element is an iterator that points
299 * to the possibly inserted element, and the second is a bool
300 * that is true if the element was actually inserted.
302 * This function attempts to insert an element into the %set. A %set
303 * relies on unique keys and thus an element is only inserted if it is
304 * not already present in the %set.
306 * Insertion requires logarithmic time.
309 insert(const value_type& __x)
311 pair<typename _Rep_type::iterator, bool> __p = _M_t.insert_unique(__x);
312 return pair<iterator, bool>(__p.first, __p.second);
316 * @brief Attempts to insert an element into the %set.
317 * @param position An iterator that serves as a hint as to where the
318 * element should be inserted.
319 * @param x Element to be inserted.
320 * @return An iterator that points to the element with key of @a x (may
321 * or may not be the element passed in).
323 * This function is not concerned about whether the insertion took place,
324 * and thus does not return a boolean like the single-argument insert()
325 * does. Note that the first parameter is only a hint and can
326 * potentially improve the performance of the insertion process. A bad
327 * hint would cause no gains in efficiency.
329 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
330 * for more on "hinting".
332 * Insertion requires logarithmic time (if the hint is not taken).
335 insert(iterator __position, const value_type& __x)
337 typedef typename _Rep_type::iterator _Rep_iterator;
338 return _M_t.insert_unique((_Rep_iterator&)__position, __x);
342 * @brief A template function that attemps to insert a range of elements.
343 * @param first Iterator pointing to the start of the range to be
345 * @param last Iterator pointing to the end of the range.
347 * Complexity similar to that of the range constructor.
349 template<class _InputIterator>
351 insert(_InputIterator __first, _InputIterator __last)
352 { _M_t.insert_unique(__first, __last); }
355 * @brief Erases an element from a %set.
356 * @param position An iterator pointing to the element to be erased.
358 * This function erases an element, pointed to by the given iterator,
359 * from a %set. Note that this function only erases the element, and
360 * that if the element is itself a pointer, the pointed-to memory is not
361 * touched in any way. Managing the pointer is the user's responsibilty.
364 erase(iterator __position)
366 typedef typename _Rep_type::iterator _Rep_iterator;
367 _M_t.erase((_Rep_iterator&)__position);
371 * @brief Erases elements according to the provided key.
372 * @param x Key of element to be erased.
373 * @return The number of elements erased.
375 * This function erases all the elements located by the given key from
377 * Note that this function only erases the element, and that if
378 * the element is itself a pointer, the pointed-to memory is not touched
379 * in any way. Managing the pointer is the user's responsibilty.
382 erase(const key_type& __x) { return _M_t.erase(__x); }
385 * @brief Erases a [first,last) range of elements from a %set.
386 * @param first Iterator pointing to the start of the range to be
388 * @param last Iterator pointing to the end of the range to be erased.
390 * This function erases a sequence of elements from a %set.
391 * Note that this function only erases the element, and that if
392 * the element is itself a pointer, the pointed-to memory is not touched
393 * in any way. Managing the pointer is the user's responsibilty.
396 erase(iterator __first, iterator __last)
398 typedef typename _Rep_type::iterator _Rep_iterator;
399 _M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last);
403 * Erases all elements in a %set. Note that this function only erases
404 * the elements, and that if the elements themselves are pointers, the
405 * pointed-to memory is not touched in any way. Managing the pointer is
406 * the user's responsibilty.
415 * @brief Finds the number of elements.
416 * @param x Element to located.
417 * @return Number of elements with specified key.
419 * This function only makes sense for multisets; for set the result will
420 * either be 0 (not present) or 1 (present).
423 count(const key_type& __x) const
424 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
426 // _GLIBCXX_RESOLVE_LIB_DEFECTS
427 // 214. set::find() missing const overload
430 * @brief Tries to locate an element in a %set.
431 * @param x Element to be located.
432 * @return Iterator pointing to sought-after element, or end() if not
435 * This function takes a key and tries to locate the element with which
436 * the key matches. If successful the function returns an iterator
437 * pointing to the sought after element. If unsuccessful it returns the
438 * past-the-end ( @c end() ) iterator.
441 find(const key_type& __x)
442 { return _M_t.find(__x); }
445 find(const key_type& __x) const
446 { return _M_t.find(__x); }
451 * @brief Finds the beginning of a subsequence matching given key.
452 * @param x Key to be located.
453 * @return Iterator pointing to first element equal to or greater
454 * than key, or end().
456 * This function returns the first element of a subsequence of elements
457 * that matches the given key. If unsuccessful it returns an iterator
458 * pointing to the first element that has a greater value than given key
459 * or end() if no such element exists.
462 lower_bound(const key_type& __x)
463 { return _M_t.lower_bound(__x); }
466 lower_bound(const key_type& __x) const
467 { return _M_t.lower_bound(__x); }
472 * @brief Finds the end of a subsequence matching given key.
473 * @param x Key to be located.
474 * @return Iterator pointing to the first element
475 * greater than key, or end().
478 upper_bound(const key_type& __x)
479 { return _M_t.upper_bound(__x); }
482 upper_bound(const key_type& __x) const
483 { return _M_t.upper_bound(__x); }
488 * @brief Finds a subsequence matching given key.
489 * @param x Key to be located.
490 * @return Pair of iterators that possibly points to the subsequence
491 * matching given key.
493 * This function is equivalent to
495 * std::make_pair(c.lower_bound(val),
496 * c.upper_bound(val))
498 * (but is faster than making the calls separately).
500 * This function probably only makes sense for multisets.
502 pair<iterator,iterator>
503 equal_range(const key_type& __x)
504 { return _M_t.equal_range(__x); }
506 pair<const_iterator,const_iterator>
507 equal_range(const key_type& __x) const
508 { return _M_t.equal_range(__x); }
511 template<class _K1, class _C1, class _A1>
513 operator== (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
515 template<class _K1, class _C1, class _A1>
517 operator< (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
522 * @brief Set equality comparison.
524 * @param y A %set of the same type as @a x.
525 * @return True iff the size and elements of the sets are equal.
527 * This is an equivalence relation. It is linear in the size of the sets.
528 * Sets are considered equivalent if their sizes are equal, and if
529 * corresponding elements compare equal.
531 template<class _Key, class _Compare, class _Alloc>
533 operator==(const set<_Key,_Compare,_Alloc>& __x,
534 const set<_Key,_Compare,_Alloc>& __y)
535 { return __x._M_t == __y._M_t; }
538 * @brief Set ordering relation.
540 * @param y A %set of the same type as @a x.
541 * @return True iff @a x is lexicographically less than @a y.
543 * This is a total ordering relation. It is linear in the size of the
544 * maps. The elements must be comparable with @c <.
546 * See std::lexicographical_compare() for how the determination is made.
548 template<class _Key, class _Compare, class _Alloc>
550 operator<(const set<_Key,_Compare,_Alloc>& __x,
551 const set<_Key,_Compare,_Alloc>& __y)
552 { return __x._M_t < __y._M_t; }
554 /// Returns !(x == y).
555 template<class _Key, class _Compare, class _Alloc>
557 operator!=(const set<_Key,_Compare,_Alloc>& __x,
558 const set<_Key,_Compare,_Alloc>& __y)
559 { return !(__x == __y); }
562 template<class _Key, class _Compare, class _Alloc>
564 operator>(const set<_Key,_Compare,_Alloc>& __x,
565 const set<_Key,_Compare,_Alloc>& __y)
566 { return __y < __x; }
569 template<class _Key, class _Compare, class _Alloc>
571 operator<=(const set<_Key,_Compare,_Alloc>& __x,
572 const set<_Key,_Compare,_Alloc>& __y)
573 { return !(__y < __x); }
576 template<class _Key, class _Compare, class _Alloc>
578 operator>=(const set<_Key,_Compare,_Alloc>& __x,
579 const set<_Key,_Compare,_Alloc>& __y)
580 { return !(__x < __y); }
582 /// See std::set::swap().
583 template<class _Key, class _Compare, class _Alloc>
585 swap(set<_Key,_Compare,_Alloc>& __x, set<_Key,_Compare,_Alloc>& __y)
588 } // namespace __gnu_norm