1 : // Set implementation -*- C++ -*-
2 :
3 : // Copyright (C) 2001, 2002, 2004, 2005, 2006 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 2, 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 : // 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 : // USA.
20 :
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.
29 :
30 : /*
31 : *
32 : * Copyright (c) 1994
33 : * Hewlett-Packard Company
34 : *
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.
42 : *
43 : *
44 : * Copyright (c) 1996,1997
45 : * Silicon Graphics Computer Systems, Inc.
46 : *
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.
54 : */
55 :
56 : /** @file stl_set.h
57 : * This is an internal header file, included by other library headers.
58 : * You should not attempt to use it directly.
59 : */
60 :
61 : #ifndef _SET_H
62 : #define _SET_H 1
63 :
64 : #include <bits/concept_check.h>
65 :
66 : _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
67 :
68 : /**
69 : * @brief A standard container made up of unique keys, which can be
70 : * retrieved in logarithmic time.
71 : *
72 : * @ingroup Containers
73 : * @ingroup Assoc_containers
74 : *
75 : * Meets the requirements of a <a href="tables.html#65">container</a>, a
76 : * <a href="tables.html#66">reversible container</a>, and an
77 : * <a href="tables.html#69">associative container</a> (using unique keys).
78 : *
79 : * Sets support bidirectional iterators.
80 : *
81 : * @param Key Type of key objects.
82 : * @param Compare Comparison function object type, defaults to less<Key>.
83 : * @param Alloc Allocator type, defaults to allocator<Key>.
84 : *
85 : * @if maint
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 : * @endif
90 : */
91 : template<class _Key, class _Compare = std::less<_Key>,
92 : class _Alloc = std::allocator<_Key> >
93 10360 : class set
94 : {
95 : // concept requirements
96 : typedef typename _Alloc::value_type _Alloc_value_type;
97 : __glibcxx_class_requires(_Key, _SGIAssignableConcept)
98 : __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
99 : _BinaryFunctionConcept)
100 : __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
101 :
102 : public:
103 : // typedefs:
104 : //@{
105 : /// Public typedefs.
106 : typedef _Key key_type;
107 : typedef _Key value_type;
108 : typedef _Compare key_compare;
109 : typedef _Compare value_compare;
110 : typedef _Alloc allocator_type;
111 : //@}
112 :
113 : private:
114 : typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type;
115 :
116 : typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
117 : key_compare, _Key_alloc_type> _Rep_type;
118 : _Rep_type _M_t; // red-black tree representing set
119 :
120 : public:
121 : //@{
122 : /// Iterator-related typedefs.
123 : typedef typename _Key_alloc_type::pointer pointer;
124 : typedef typename _Key_alloc_type::const_pointer const_pointer;
125 : typedef typename _Key_alloc_type::reference reference;
126 : typedef typename _Key_alloc_type::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 : /// Default constructor creates no elements.
140 : set()
141 15540 : : _M_t(_Compare(), allocator_type()) {}
142 :
143 : /**
144 : * @brief Default constructor creates no elements.
145 : *
146 : * @param comp Comparator to use.
147 : * @param a Allocator to use.
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<class _InputIterator>
164 : set(_InputIterator __first, _InputIterator __last)
165 : : _M_t(_Compare(), allocator_type())
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<class _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 : set(const set<_Key,_Compare,_Alloc>& __x)
194 : : _M_t(__x._M_t) { }
195 :
196 : /**
197 : * @brief Set assignment operator.
198 : * @param x A %set of identical element and allocator types.
199 : *
200 : * All the elements of @a x are copied, but unlike the copy constructor,
201 : * the allocator object is not copied.
202 : */
203 : set<_Key,_Compare,_Alloc>&
204 : operator=(const set<_Key, _Compare, _Alloc>& __x)
205 : {
206 : _M_t = __x._M_t;
207 : return *this;
208 : }
209 :
210 : // accessors:
211 :
212 : /// Returns the comparison object with which the %set was constructed.
213 : key_compare
214 : key_comp() const
215 : { return _M_t.key_comp(); }
216 : /// Returns the comparison object with which the %set was constructed.
217 : value_compare
218 : value_comp() const
219 : { return _M_t.key_comp(); }
220 : /// Returns the allocator object with which the %set was constructed.
221 : allocator_type
222 : get_allocator() const
223 : { return _M_t.get_allocator(); }
224 :
225 : /**
226 : * Returns a read/write iterator that points to the first element in the
227 : * %set. Iteration is done in ascending order according to the keys.
228 : */
229 : iterator
230 : begin() const
231 0 : { return _M_t.begin(); }
232 :
233 : /**
234 : * Returns a read/write iterator that points one past the last element in
235 : * the %set. Iteration is done in ascending order according to the keys.
236 : */
237 : iterator
238 : end() const
239 0 : { return _M_t.end(); }
240 :
241 : /**
242 : * Returns a read/write reverse iterator that points to the last element
243 : * in the %set. Iteration is done in descending order according to the
244 : * keys.
245 : */
246 : reverse_iterator
247 : rbegin() const
248 : { return _M_t.rbegin(); }
249 :
250 : /**
251 : * Returns a read-only (constant) reverse iterator that points to the
252 : * last pair in the %map. Iteration is done in descending order
253 : * according to the keys.
254 : */
255 : reverse_iterator
256 : rend() const
257 : { return _M_t.rend(); }
258 :
259 : /// Returns true if the %set is empty.
260 : bool
261 : empty() const
262 : { return _M_t.empty(); }
263 :
264 : /// Returns the size of the %set.
265 : size_type
266 : size() const
267 : { return _M_t.size(); }
268 :
269 : /// Returns the maximum size of the %set.
270 : size_type
271 : max_size() const
272 : { return _M_t.max_size(); }
273 :
274 : /**
275 : * @brief Swaps data with another %set.
276 : * @param x A %set of the same element and allocator types.
277 : *
278 : * This exchanges the elements between two sets in constant time.
279 : * (It is only swapping a pointer, an integer, and an instance of
280 : * the @c Compare type (which itself is often stateless and empty), so it
281 : * should be quite fast.)
282 : * Note that the global std::swap() function is specialized such that
283 : * std::swap(s1,s2) will feed to this function.
284 : */
285 : void
286 : swap(set<_Key,_Compare,_Alloc>& __x)
287 : { _M_t.swap(__x._M_t); }
288 :
289 : // insert/erase
290 : /**
291 : * @brief Attempts to insert an element into the %set.
292 : * @param x Element to be inserted.
293 : * @return A pair, of which the first element is an iterator that points
294 : * to the possibly inserted element, and the second is a bool
295 : * that is true if the element was actually inserted.
296 : *
297 : * This function attempts to insert an element into the %set. A %set
298 : * relies on unique keys and thus an element is only inserted if it is
299 : * not already present in the %set.
300 : *
301 : * Insertion requires logarithmic time.
302 : */
303 : std::pair<iterator,bool>
304 2268 : insert(const value_type& __x)
305 : {
306 2268 : std::pair<typename _Rep_type::iterator, bool> __p =
307 2268 : _M_t._M_insert_unique(__x);
308 2268 : return std::pair<iterator, bool>(__p.first, __p.second);
309 : }
310 :
311 : /**
312 : * @brief Attempts to insert an element into the %set.
313 : * @param position An iterator that serves as a hint as to where the
314 : * element should be inserted.
315 : * @param x Element to be inserted.
316 : * @return An iterator that points to the element with key of @a x (may
317 : * or may not be the element passed in).
318 : *
319 : * This function is not concerned about whether the insertion took place,
320 : * and thus does not return a boolean like the single-argument insert()
321 : * does. Note that the first parameter is only a hint and can
322 : * potentially improve the performance of the insertion process. A bad
323 : * hint would cause no gains in efficiency.
324 : *
325 : * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
326 : * for more on "hinting".
327 : *
328 : * Insertion requires logarithmic time (if the hint is not taken).
329 : */
330 : iterator
331 : insert(iterator __position, const value_type& __x)
332 : { return _M_t._M_insert_unique(__position, __x); }
333 :
334 : /**
335 : * @brief A template function that attemps to insert a range of elements.
336 : * @param first Iterator pointing to the start of the range to be
337 : * inserted.
338 : * @param last Iterator pointing to the end of the range.
339 : *
340 : * Complexity similar to that of the range constructor.
341 : */
342 : template<class _InputIterator>
343 : void
344 : insert(_InputIterator __first, _InputIterator __last)
345 : { _M_t._M_insert_unique(__first, __last); }
346 :
347 : /**
348 : * @brief Erases an element from a %set.
349 : * @param position An iterator pointing to the element to be erased.
350 : *
351 : * This function erases an element, pointed to by the given iterator,
352 : * from a %set. Note that this function only erases the element, and
353 : * that if the element is itself a pointer, the pointed-to memory is not
354 : * touched in any way. Managing the pointer is the user's responsibilty.
355 : */
356 : void
357 : erase(iterator __position)
358 : { _M_t.erase(__position); }
359 :
360 : /**
361 : * @brief Erases elements according to the provided key.
362 : * @param x Key of element to be erased.
363 : * @return The number of elements erased.
364 : *
365 : * This function erases all the elements located by the given key from
366 : * a %set.
367 : * Note that this function only erases the element, and that if
368 : * the element is itself a pointer, the pointed-to memory is not touched
369 : * in any way. Managing the pointer is the user's responsibilty.
370 : */
371 : size_type
372 : erase(const key_type& __x)
373 : { return _M_t.erase(__x); }
374 :
375 : /**
376 : * @brief Erases a [first,last) range of elements from a %set.
377 : * @param first Iterator pointing to the start of the range to be
378 : * erased.
379 : * @param last Iterator pointing to the end of the range to be erased.
380 : *
381 : * This function erases a sequence of elements from a %set.
382 : * Note that this function only erases the element, and that if
383 : * the element is itself a pointer, the pointed-to memory is not touched
384 : * in any way. Managing the pointer is the user's responsibilty.
385 : */
386 : void
387 : erase(iterator __first, iterator __last)
388 : { _M_t.erase(__first, __last); }
389 :
390 : /**
391 : * Erases all elements in a %set. Note that this function only erases
392 : * the elements, and that if the elements themselves are pointers, the
393 : * pointed-to memory is not touched in any way. Managing the pointer is
394 : * the user's responsibilty.
395 : */
396 : void
397 : clear()
398 : { _M_t.clear(); }
399 :
400 : // set operations:
401 :
402 : /**
403 : * @brief Finds the number of elements.
404 : * @param x Element to located.
405 : * @return Number of elements with specified key.
406 : *
407 : * This function only makes sense for multisets; for set the result will
408 : * either be 0 (not present) or 1 (present).
409 : */
410 : size_type
411 41107 : count(const key_type& __x) const
412 41107 : { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
413 :
414 : // _GLIBCXX_RESOLVE_LIB_DEFECTS
415 : // 214. set::find() missing const overload
416 : //@{
417 : /**
418 : * @brief Tries to locate an element in a %set.
419 : * @param x Element to be located.
420 : * @return Iterator pointing to sought-after element, or end() if not
421 : * found.
422 : *
423 : * This function takes a key and tries to locate the element with which
424 : * the key matches. If successful the function returns an iterator
425 : * pointing to the sought after element. If unsuccessful it returns the
426 : * past-the-end ( @c end() ) iterator.
427 : */
428 : iterator
429 : find(const key_type& __x)
430 : { return _M_t.find(__x); }
431 :
432 : const_iterator
433 : find(const key_type& __x) const
434 : { return _M_t.find(__x); }
435 : //@}
436 :
437 : //@{
438 : /**
439 : * @brief Finds the beginning of a subsequence matching given key.
440 : * @param x Key to be located.
441 : * @return Iterator pointing to first element equal to or greater
442 : * than key, or end().
443 : *
444 : * This function returns the first element of a subsequence of elements
445 : * that matches the given key. If unsuccessful it returns an iterator
446 : * pointing to the first element that has a greater value than given key
447 : * or end() if no such element exists.
448 : */
449 : iterator
450 : lower_bound(const key_type& __x)
451 : { return _M_t.lower_bound(__x); }
452 :
453 : const_iterator
454 : lower_bound(const key_type& __x) const
455 : { return _M_t.lower_bound(__x); }
456 : //@}
457 :
458 : //@{
459 : /**
460 : * @brief Finds the end of a subsequence matching given key.
461 : * @param x Key to be located.
462 : * @return Iterator pointing to the first element
463 : * greater than key, or end().
464 : */
465 : iterator
466 : upper_bound(const key_type& __x)
467 : { return _M_t.upper_bound(__x); }
468 :
469 : const_iterator
470 : upper_bound(const key_type& __x) const
471 : { return _M_t.upper_bound(__x); }
472 : //@}
473 :
474 : //@{
475 : /**
476 : * @brief Finds a subsequence matching given key.
477 : * @param x Key to be located.
478 : * @return Pair of iterators that possibly points to the subsequence
479 : * matching given key.
480 : *
481 : * This function is equivalent to
482 : * @code
483 : * std::make_pair(c.lower_bound(val),
484 : * c.upper_bound(val))
485 : * @endcode
486 : * (but is faster than making the calls separately).
487 : *
488 : * This function probably only makes sense for multisets.
489 : */
490 : std::pair<iterator, iterator>
491 : equal_range(const key_type& __x)
492 : { return _M_t.equal_range(__x); }
493 :
494 : std::pair<const_iterator, const_iterator>
495 : equal_range(const key_type& __x) const
496 : { return _M_t.equal_range(__x); }
497 : //@}
498 :
499 : template<class _K1, class _C1, class _A1>
500 : friend bool
501 : operator== (const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
502 :
503 : template<class _K1, class _C1, class _A1>
504 : friend bool
505 : operator< (const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
506 : };
507 :
508 :
509 : /**
510 : * @brief Set equality comparison.
511 : * @param x A %set.
512 : * @param y A %set of the same type as @a x.
513 : * @return True iff the size and elements of the sets are equal.
514 : *
515 : * This is an equivalence relation. It is linear in the size of the sets.
516 : * Sets are considered equivalent if their sizes are equal, and if
517 : * corresponding elements compare equal.
518 : */
519 : template<class _Key, class _Compare, class _Alloc>
520 : inline bool
521 : operator==(const set<_Key, _Compare, _Alloc>& __x,
522 : const set<_Key, _Compare, _Alloc>& __y)
523 : { return __x._M_t == __y._M_t; }
524 :
525 : /**
526 : * @brief Set ordering relation.
527 : * @param x A %set.
528 : * @param y A %set of the same type as @a x.
529 : * @return True iff @a x is lexicographically less than @a y.
530 : *
531 : * This is a total ordering relation. It is linear in the size of the
532 : * maps. The elements must be comparable with @c <.
533 : *
534 : * See std::lexicographical_compare() for how the determination is made.
535 : */
536 : template<class _Key, class _Compare, class _Alloc>
537 : inline bool
538 : operator<(const set<_Key, _Compare, _Alloc>& __x,
539 : const set<_Key, _Compare, _Alloc>& __y)
540 : { return __x._M_t < __y._M_t; }
541 :
542 : /// Returns !(x == y).
543 : template<class _Key, class _Compare, class _Alloc>
544 : inline bool
545 : operator!=(const set<_Key, _Compare, _Alloc>& __x,
546 : const set<_Key, _Compare, _Alloc>& __y)
547 : { return !(__x == __y); }
548 :
549 : /// Returns y < x.
550 : template<class _Key, class _Compare, class _Alloc>
551 : inline bool
552 : operator>(const set<_Key, _Compare, _Alloc>& __x,
553 : const set<_Key, _Compare, _Alloc>& __y)
554 : { return __y < __x; }
555 :
556 : /// Returns !(y < x)
557 : template<class _Key, class _Compare, class _Alloc>
558 : inline bool
559 : operator<=(const set<_Key, _Compare, _Alloc>& __x,
560 : const set<_Key, _Compare, _Alloc>& __y)
561 : { return !(__y < __x); }
562 :
563 : /// Returns !(x < y)
564 : template<class _Key, class _Compare, class _Alloc>
565 : inline bool
566 : operator>=(const set<_Key, _Compare, _Alloc>& __x,
567 : const set<_Key, _Compare, _Alloc>& __y)
568 : { return !(__x < __y); }
569 :
570 : /// See std::set::swap().
571 : template<class _Key, class _Compare, class _Alloc>
572 : inline void
573 : swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y)
574 : { __x.swap(__y); }
575 :
576 : _GLIBCXX_END_NESTED_NAMESPACE
577 :
578 : #endif /* _SET_H */
|