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