]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_set.h
Add parallel mode.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_set.h
1 // Set implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 * @if maint
87 * The private tree data is declared exactly the same way for set and
88 * multiset; the distinction is made entirely in how the tree functions are
89 * called (*_unique versus *_equal, same as the standard).
90 * @endif
91 */
92 template<class _Key, class _Compare = std::less<_Key>,
93 class _Alloc = std::allocator<_Key> >
94 class set
95 {
96 // concept requirements
97 typedef typename _Alloc::value_type _Alloc_value_type;
98 __glibcxx_class_requires(_Key, _SGIAssignableConcept)
99 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
100 _BinaryFunctionConcept)
101 __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
102
103 public:
104 // typedefs:
105 //@{
106 /// Public typedefs.
107 typedef _Key key_type;
108 typedef _Key value_type;
109 typedef _Compare key_compare;
110 typedef _Compare value_compare;
111 typedef _Alloc allocator_type;
112 //@}
113
114 private:
115 typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type;
116
117 typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
118 key_compare, _Key_alloc_type> _Rep_type;
119 _Rep_type _M_t; // red-black tree representing set
120
121 public:
122 //@{
123 /// Iterator-related typedefs.
124 typedef typename _Key_alloc_type::pointer pointer;
125 typedef typename _Key_alloc_type::const_pointer const_pointer;
126 typedef typename _Key_alloc_type::reference reference;
127 typedef typename _Key_alloc_type::const_reference const_reference;
128 // _GLIBCXX_RESOLVE_LIB_DEFECTS
129 // DR 103. set::iterator is required to be modifiable,
130 // but this allows modification of keys.
131 typedef typename _Rep_type::const_iterator iterator;
132 typedef typename _Rep_type::const_iterator const_iterator;
133 typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
134 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
135 typedef typename _Rep_type::size_type size_type;
136 typedef typename _Rep_type::difference_type difference_type;
137 //@}
138
139 // allocation/deallocation
140 /// Default constructor creates no elements.
141 set()
142 : _M_t(_Compare(), allocator_type()) {}
143
144 /**
145 * @brief Default constructor creates no elements.
146 *
147 * @param comp Comparator to use.
148 * @param a Allocator to use.
149 */
150 explicit
151 set(const _Compare& __comp,
152 const allocator_type& __a = allocator_type())
153 : _M_t(__comp, __a) {}
154
155 /**
156 * @brief Builds a %set from a range.
157 * @param first An input iterator.
158 * @param last An input iterator.
159 *
160 * Create a %set consisting of copies of the elements from [first,last).
161 * This is linear in N if the range is already sorted, and NlogN
162 * otherwise (where N is distance(first,last)).
163 */
164 template<class _InputIterator>
165 set(_InputIterator __first, _InputIterator __last)
166 : _M_t(_Compare(), allocator_type())
167 { _M_t._M_insert_unique(__first, __last); }
168
169 /**
170 * @brief Builds a %set from a range.
171 * @param first An input iterator.
172 * @param last An input iterator.
173 * @param comp A comparison functor.
174 * @param a An allocator object.
175 *
176 * Create a %set consisting of copies of the elements from [first,last).
177 * This is linear in N if the range is already sorted, and NlogN
178 * otherwise (where N is distance(first,last)).
179 */
180 template<class _InputIterator>
181 set(_InputIterator __first, _InputIterator __last,
182 const _Compare& __comp,
183 const allocator_type& __a = allocator_type())
184 : _M_t(__comp, __a)
185 { _M_t._M_insert_unique(__first, __last); }
186
187 /**
188 * @brief Set copy constructor.
189 * @param x A %set of identical element and allocator types.
190 *
191 * The newly-created %set uses a copy of the allocation object used
192 * by @a x.
193 */
194 set(const set<_Key,_Compare,_Alloc>& __x)
195 : _M_t(__x._M_t) { }
196
197 /**
198 * @brief Set assignment operator.
199 * @param x A %set of identical element and allocator types.
200 *
201 * All the elements of @a x are copied, but unlike the copy constructor,
202 * the allocator object is not copied.
203 */
204 set<_Key,_Compare,_Alloc>&
205 operator=(const set<_Key, _Compare, _Alloc>& __x)
206 {
207 _M_t = __x._M_t;
208 return *this;
209 }
210
211 // accessors:
212
213 /// Returns the comparison object with which the %set was constructed.
214 key_compare
215 key_comp() const
216 { return _M_t.key_comp(); }
217 /// Returns the comparison object with which the %set was constructed.
218 value_compare
219 value_comp() const
220 { return _M_t.key_comp(); }
221 /// Returns the allocator object with which the %set was constructed.
222 allocator_type
223 get_allocator() const
224 { return _M_t.get_allocator(); }
225
226 /**
227 * Returns a read/write iterator that points to the first element in the
228 * %set. Iteration is done in ascending order according to the keys.
229 */
230 iterator
231 begin() const
232 { return _M_t.begin(); }
233
234 /**
235 * Returns a read/write iterator that points one past the last element in
236 * the %set. Iteration is done in ascending order according to the keys.
237 */
238 iterator
239 end() const
240 { return _M_t.end(); }
241
242 /**
243 * Returns a read/write reverse iterator that points to the last element
244 * in the %set. Iteration is done in descending order according to the
245 * keys.
246 */
247 reverse_iterator
248 rbegin() const
249 { return _M_t.rbegin(); }
250
251 /**
252 * Returns a read-only (constant) reverse iterator that points to the
253 * last pair in the %map. Iteration is done in descending order
254 * according to the keys.
255 */
256 reverse_iterator
257 rend() const
258 { return _M_t.rend(); }
259
260 /// Returns true if the %set is empty.
261 bool
262 empty() const
263 { return _M_t.empty(); }
264
265 /// Returns the size of the %set.
266 size_type
267 size() const
268 { return _M_t.size(); }
269
270 /// Returns the maximum size of the %set.
271 size_type
272 max_size() const
273 { return _M_t.max_size(); }
274
275 /**
276 * @brief Swaps data with another %set.
277 * @param x A %set of the same element and allocator types.
278 *
279 * This exchanges the elements between two sets in constant time.
280 * (It is only swapping a pointer, an integer, and an instance of
281 * the @c Compare type (which itself is often stateless and empty), so it
282 * should be quite fast.)
283 * Note that the global std::swap() function is specialized such that
284 * std::swap(s1,s2) will feed to this function.
285 */
286 void
287 swap(set<_Key,_Compare,_Alloc>& __x)
288 { _M_t.swap(__x._M_t); }
289
290 // insert/erase
291 /**
292 * @brief Attempts to insert an element into the %set.
293 * @param x Element to be inserted.
294 * @return A pair, of which the first element is an iterator that points
295 * to the possibly inserted element, and the second is a bool
296 * that is true if the element was actually inserted.
297 *
298 * This function attempts to insert an element into the %set. A %set
299 * relies on unique keys and thus an element is only inserted if it is
300 * not already present in the %set.
301 *
302 * Insertion requires logarithmic time.
303 */
304 std::pair<iterator,bool>
305 insert(const value_type& __x)
306 {
307 std::pair<typename _Rep_type::iterator, bool> __p =
308 _M_t._M_insert_unique(__x);
309 return std::pair<iterator, bool>(__p.first, __p.second);
310 }
311
312 /**
313 * @brief Attempts to insert an element into the %set.
314 * @param position An iterator that serves as a hint as to where the
315 * element should be inserted.
316 * @param x Element to be inserted.
317 * @return An iterator that points to the element with key of @a x (may
318 * or may not be the element passed in).
319 *
320 * This function is not concerned about whether the insertion took place,
321 * and thus does not return a boolean like the single-argument insert()
322 * does. Note that the first parameter is only a hint and can
323 * potentially improve the performance of the insertion process. A bad
324 * hint would cause no gains in efficiency.
325 *
326 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
327 * for more on "hinting".
328 *
329 * Insertion requires logarithmic time (if the hint is not taken).
330 */
331 iterator
332 insert(iterator __position, const value_type& __x)
333 { return _M_t._M_insert_unique_(__position, __x); }
334
335 /**
336 * @brief A template function that attemps to insert a range of elements.
337 * @param first Iterator pointing to the start of the range to be
338 * inserted.
339 * @param last Iterator pointing to the end of the range.
340 *
341 * Complexity similar to that of the range constructor.
342 */
343 template<class _InputIterator>
344 void
345 insert(_InputIterator __first, _InputIterator __last)
346 { _M_t._M_insert_unique(__first, __last); }
347
348 /**
349 * @brief Erases an element from a %set.
350 * @param position An iterator pointing to the element to be erased.
351 *
352 * This function erases an element, pointed to by the given iterator,
353 * from a %set. Note that this function only erases the element, and
354 * that if the element is itself a pointer, the pointed-to memory is not
355 * touched in any way. Managing the pointer is the user's responsibilty.
356 */
357 void
358 erase(iterator __position)
359 { _M_t.erase(__position); }
360
361 /**
362 * @brief Erases elements according to the provided key.
363 * @param x Key of element to be erased.
364 * @return The number of elements erased.
365 *
366 * This function erases all the elements located by the given key from
367 * a %set.
368 * Note that this function only erases the element, and that if
369 * the element is itself a pointer, the pointed-to memory is not touched
370 * in any way. Managing the pointer is the user's responsibilty.
371 */
372 size_type
373 erase(const key_type& __x)
374 { return _M_t.erase(__x); }
375
376 /**
377 * @brief Erases a [first,last) range of elements from a %set.
378 * @param first Iterator pointing to the start of the range to be
379 * erased.
380 * @param last Iterator pointing to the end of the range to be erased.
381 *
382 * This function erases a sequence of elements from a %set.
383 * Note that this function only erases the element, and that if
384 * the element is itself a pointer, the pointed-to memory is not touched
385 * in any way. Managing the pointer is the user's responsibilty.
386 */
387 void
388 erase(iterator __first, iterator __last)
389 { _M_t.erase(__first, __last); }
390
391 /**
392 * Erases all elements in a %set. Note that this function only erases
393 * the elements, and that if the elements themselves are pointers, the
394 * pointed-to memory is not touched in any way. Managing the pointer is
395 * the user's responsibilty.
396 */
397 void
398 clear()
399 { _M_t.clear(); }
400
401 // set operations:
402
403 /**
404 * @brief Finds the number of elements.
405 * @param x Element to located.
406 * @return Number of elements with specified key.
407 *
408 * This function only makes sense for multisets; for set the result will
409 * either be 0 (not present) or 1 (present).
410 */
411 size_type
412 count(const key_type& __x) const
413 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
414
415 // _GLIBCXX_RESOLVE_LIB_DEFECTS
416 // 214. set::find() missing const overload
417 //@{
418 /**
419 * @brief Tries to locate an element in a %set.
420 * @param x Element to be located.
421 * @return Iterator pointing to sought-after element, or end() if not
422 * found.
423 *
424 * This function takes a key and tries to locate the element with which
425 * the key matches. If successful the function returns an iterator
426 * pointing to the sought after element. If unsuccessful it returns the
427 * past-the-end ( @c end() ) iterator.
428 */
429 iterator
430 find(const key_type& __x)
431 { return _M_t.find(__x); }
432
433 const_iterator
434 find(const key_type& __x) const
435 { return _M_t.find(__x); }
436 //@}
437
438 //@{
439 /**
440 * @brief Finds the beginning of a subsequence matching given key.
441 * @param x Key to be located.
442 * @return Iterator pointing to first element equal to or greater
443 * than key, or end().
444 *
445 * This function returns the first element of a subsequence of elements
446 * that matches the given key. If unsuccessful it returns an iterator
447 * pointing to the first element that has a greater value than given key
448 * or end() if no such element exists.
449 */
450 iterator
451 lower_bound(const key_type& __x)
452 { return _M_t.lower_bound(__x); }
453
454 const_iterator
455 lower_bound(const key_type& __x) const
456 { return _M_t.lower_bound(__x); }
457 //@}
458
459 //@{
460 /**
461 * @brief Finds the end of a subsequence matching given key.
462 * @param x Key to be located.
463 * @return Iterator pointing to the first element
464 * greater than key, or end().
465 */
466 iterator
467 upper_bound(const key_type& __x)
468 { return _M_t.upper_bound(__x); }
469
470 const_iterator
471 upper_bound(const key_type& __x) const
472 { return _M_t.upper_bound(__x); }
473 //@}
474
475 //@{
476 /**
477 * @brief Finds a subsequence matching given key.
478 * @param x Key to be located.
479 * @return Pair of iterators that possibly points to the subsequence
480 * matching given key.
481 *
482 * This function is equivalent to
483 * @code
484 * std::make_pair(c.lower_bound(val),
485 * c.upper_bound(val))
486 * @endcode
487 * (but is faster than making the calls separately).
488 *
489 * This function probably only makes sense for multisets.
490 */
491 std::pair<iterator, iterator>
492 equal_range(const key_type& __x)
493 { return _M_t.equal_range(__x); }
494
495 std::pair<const_iterator, const_iterator>
496 equal_range(const key_type& __x) const
497 { return _M_t.equal_range(__x); }
498 //@}
499
500 template<class _K1, class _C1, class _A1>
501 friend bool
502 operator== (const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
503
504 template<class _K1, class _C1, class _A1>
505 friend bool
506 operator< (const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
507 };
508
509
510 /**
511 * @brief Set equality comparison.
512 * @param x A %set.
513 * @param y A %set of the same type as @a x.
514 * @return True iff the size and elements of the sets are equal.
515 *
516 * This is an equivalence relation. It is linear in the size of the sets.
517 * Sets are considered equivalent if their sizes are equal, and if
518 * corresponding elements compare equal.
519 */
520 template<class _Key, class _Compare, class _Alloc>
521 inline bool
522 operator==(const set<_Key, _Compare, _Alloc>& __x,
523 const set<_Key, _Compare, _Alloc>& __y)
524 { return __x._M_t == __y._M_t; }
525
526 /**
527 * @brief Set ordering relation.
528 * @param x A %set.
529 * @param y A %set of the same type as @a x.
530 * @return True iff @a x is lexicographically less than @a y.
531 *
532 * This is a total ordering relation. It is linear in the size of the
533 * maps. The elements must be comparable with @c <.
534 *
535 * See std::lexicographical_compare() for how the determination is made.
536 */
537 template<class _Key, class _Compare, class _Alloc>
538 inline bool
539 operator<(const set<_Key, _Compare, _Alloc>& __x,
540 const set<_Key, _Compare, _Alloc>& __y)
541 { return __x._M_t < __y._M_t; }
542
543 /// Returns !(x == y).
544 template<class _Key, class _Compare, class _Alloc>
545 inline bool
546 operator!=(const set<_Key, _Compare, _Alloc>& __x,
547 const set<_Key, _Compare, _Alloc>& __y)
548 { return !(__x == __y); }
549
550 /// Returns y < x.
551 template<class _Key, class _Compare, class _Alloc>
552 inline bool
553 operator>(const set<_Key, _Compare, _Alloc>& __x,
554 const set<_Key, _Compare, _Alloc>& __y)
555 { return __y < __x; }
556
557 /// Returns !(y < x)
558 template<class _Key, class _Compare, class _Alloc>
559 inline bool
560 operator<=(const set<_Key, _Compare, _Alloc>& __x,
561 const set<_Key, _Compare, _Alloc>& __y)
562 { return !(__y < __x); }
563
564 /// Returns !(x < y)
565 template<class _Key, class _Compare, class _Alloc>
566 inline bool
567 operator>=(const set<_Key, _Compare, _Alloc>& __x,
568 const set<_Key, _Compare, _Alloc>& __y)
569 { return !(__x < __y); }
570
571 /// See std::set::swap().
572 template<class _Key, class _Compare, class _Alloc>
573 inline void
574 swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y)
575 { __x.swap(__y); }
576
577 _GLIBCXX_END_NESTED_NAMESPACE
578
579 #endif /* _STL_SET_H */