stl_set.h

Go to the documentation of this file.
00001 // Set implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /*
00031  *
00032  * Copyright (c) 1994
00033  * Hewlett-Packard Company
00034  *
00035  * Permission to use, copy, modify, distribute and sell this software
00036  * and its documentation for any purpose is hereby granted without fee,
00037  * provided that the above copyright notice appear in all copies and
00038  * that both that copyright notice and this permission notice appear
00039  * in supporting documentation.  Hewlett-Packard Company makes no
00040  * representations about the suitability of this software for any
00041  * purpose.  It is provided "as is" without express or implied warranty.
00042  *
00043  *
00044  * Copyright (c) 1996,1997
00045  * Silicon Graphics Computer Systems, Inc.
00046  *
00047  * Permission to use, copy, modify, distribute and sell this software
00048  * and its documentation for any purpose is hereby granted without fee,
00049  * provided that the above copyright notice appear in all copies and
00050  * that both that copyright notice and this permission notice appear
00051  * in supporting documentation.  Silicon Graphics makes no
00052  * representations about the suitability of this software for any
00053  * purpose.  It is provided "as is" without express or implied warranty.
00054  */
00055 
00056 /** @file stl_set.h
00057  *  This is an internal header file, included by other library headers.
00058  *  You should not attempt to use it directly.
00059  */
00060 
00061 #ifndef _SET_H
00062 #define _SET_H 1
00063 
00064 #include <bits/concept_check.h>
00065 
00066 namespace _GLIBCXX_STD
00067 {
00068   // Forward declarations of operators < and ==, needed for friend declaration.
00069   template<class _Key, class _Compare = std::less<_Key>,
00070        class _Alloc = std::allocator<_Key> >
00071     class set;
00072 
00073   template<class _Key, class _Compare, class _Alloc>
00074     inline bool
00075     operator==(const set<_Key, _Compare, _Alloc>& __x,
00076            const set<_Key, _Compare, _Alloc>& __y);
00077 
00078   template<class _Key, class _Compare, class _Alloc>
00079     inline bool
00080     operator<(const set<_Key, _Compare, _Alloc>& __x,
00081           const set<_Key, _Compare, _Alloc>& __y);
00082 
00083   /**
00084    *  @brief A standard container made up of unique keys, which can be
00085    *  retrieved in logarithmic time.
00086    *
00087    *  @ingroup Containers
00088    *  @ingroup Assoc_containers
00089    *
00090    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
00091    *  <a href="tables.html#66">reversible container</a>, and an
00092    *  <a href="tables.html#69">associative container</a> (using unique keys).
00093    *
00094    *  Sets support bidirectional iterators.
00095    *
00096    *  @param  Key  Type of key objects.
00097    *  @param  Compare  Comparison function object type, defaults to less<Key>.
00098    *  @param  Alloc  Allocator type, defaults to allocator<Key>.
00099    *
00100    *  @if maint
00101    *  The private tree data is declared exactly the same way for set and
00102    *  multiset; the distinction is made entirely in how the tree functions are
00103    *  called (*_unique versus *_equal, same as the standard).
00104    *  @endif
00105   */
00106   template<class _Key, class _Compare, class _Alloc>
00107     class set
00108     {
00109       // concept requirements
00110       typedef typename _Alloc::value_type                   _Alloc_value_type;
00111       __glibcxx_class_requires(_Key, _SGIAssignableConcept)
00112       __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
00113                 _BinaryFunctionConcept)
00114       __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)  
00115 
00116     public:
00117       // typedefs:
00118       //@{
00119       /// Public typedefs.
00120       typedef _Key     key_type;
00121       typedef _Key     value_type;
00122       typedef _Compare key_compare;
00123       typedef _Compare value_compare;
00124       typedef _Alloc   allocator_type;
00125       //@}
00126 
00127     private:
00128       typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type;
00129 
00130       typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
00131                key_compare, _Key_alloc_type> _Rep_type;
00132       _Rep_type _M_t;  // red-black tree representing set
00133 
00134     public:
00135       //@{
00136       ///  Iterator-related typedefs.
00137       typedef typename _Key_alloc_type::pointer             pointer;
00138       typedef typename _Key_alloc_type::const_pointer       const_pointer;
00139       typedef typename _Key_alloc_type::reference           reference;
00140       typedef typename _Key_alloc_type::const_reference     const_reference;
00141       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00142       // DR 103. set::iterator is required to be modifiable,
00143       // but this allows modification of keys.
00144       typedef typename _Rep_type::const_iterator            iterator;
00145       typedef typename _Rep_type::const_iterator            const_iterator;
00146       typedef typename _Rep_type::const_reverse_iterator    reverse_iterator;
00147       typedef typename _Rep_type::const_reverse_iterator    const_reverse_iterator;
00148       typedef typename _Rep_type::size_type                 size_type;
00149       typedef typename _Rep_type::difference_type           difference_type;
00150       //@}
00151 
00152       // allocation/deallocation
00153       ///  Default constructor creates no elements.
00154       set()
00155       : _M_t(_Compare(), allocator_type()) {}
00156 
00157       /**
00158        *  @brief  Default constructor creates no elements.
00159        *
00160        *  @param  comp  Comparator to use.
00161        *  @param  a  Allocator to use.
00162        */
00163       explicit
00164       set(const _Compare& __comp,
00165       const allocator_type& __a = allocator_type())
00166       : _M_t(__comp, __a) {}
00167 
00168       /**
00169        *  @brief  Builds a %set from a range.
00170        *  @param  first  An input iterator.
00171        *  @param  last  An input iterator.
00172        *
00173        *  Create a %set consisting of copies of the elements from [first,last).
00174        *  This is linear in N if the range is already sorted, and NlogN
00175        *  otherwise (where N is distance(first,last)).
00176        */
00177       template<class _InputIterator>
00178         set(_InputIterator __first, _InputIterator __last)
00179         : _M_t(_Compare(), allocator_type())
00180         { _M_t.insert_unique(__first, __last); }
00181 
00182       /**
00183        *  @brief  Builds a %set from a range.
00184        *  @param  first  An input iterator.
00185        *  @param  last  An input iterator.
00186        *  @param  comp  A comparison functor.
00187        *  @param  a  An allocator object.
00188        *
00189        *  Create a %set consisting of copies of the elements from [first,last).
00190        *  This is linear in N if the range is already sorted, and NlogN
00191        *  otherwise (where N is distance(first,last)).
00192        */
00193       template<class _InputIterator>
00194         set(_InputIterator __first, _InputIterator __last,
00195         const _Compare& __comp,
00196         const allocator_type& __a = allocator_type())
00197     : _M_t(__comp, __a)
00198         { _M_t.insert_unique(__first, __last); }
00199 
00200       /**
00201        *  @brief  Set copy constructor.
00202        *  @param  x  A %set of identical element and allocator types.
00203        *
00204        *  The newly-created %set uses a copy of the allocation object used
00205        *  by @a x.
00206        */
00207       set(const set<_Key,_Compare,_Alloc>& __x)
00208       : _M_t(__x._M_t) { }
00209 
00210       /**
00211        *  @brief  Set assignment operator.
00212        *  @param  x  A %set of identical element and allocator types.
00213        *
00214        *  All the elements of @a x are copied, but unlike the copy constructor,
00215        *  the allocator object is not copied.
00216        */
00217       set<_Key,_Compare,_Alloc>&
00218       operator=(const set<_Key, _Compare, _Alloc>& __x)
00219       {
00220     _M_t = __x._M_t;
00221     return *this;
00222       }
00223 
00224       // accessors:
00225 
00226       ///  Returns the comparison object with which the %set was constructed.
00227       key_compare
00228       key_comp() const
00229       { return _M_t.key_comp(); }
00230       ///  Returns the comparison object with which the %set was constructed.
00231       value_compare
00232       value_comp() const
00233       { return _M_t.key_comp(); }
00234       ///  Returns the allocator object with which the %set was constructed.
00235       allocator_type
00236       get_allocator() const
00237       { return _M_t.get_allocator(); }
00238 
00239       /**
00240        *  Returns a read/write iterator that points to the first element in the
00241        *  %set.  Iteration is done in ascending order according to the keys.
00242        */
00243       iterator
00244       begin() const
00245       { return _M_t.begin(); }
00246 
00247       /**
00248        *  Returns a read/write iterator that points one past the last element in
00249        *  the %set.  Iteration is done in ascending order according to the keys.
00250        */
00251       iterator
00252       end() const
00253       { return _M_t.end(); }
00254 
00255       /**
00256        *  Returns a read/write reverse iterator that points to the last element
00257        *  in the %set.  Iteration is done in descending order according to the
00258        *  keys.
00259        */
00260       reverse_iterator
00261       rbegin() const
00262       { return _M_t.rbegin(); }
00263 
00264       /**
00265        *  Returns a read-only (constant) reverse iterator that points to the
00266        *  last pair in the %map.  Iteration is done in descending order
00267        *  according to the keys.
00268        */
00269       reverse_iterator
00270       rend() const
00271       { return _M_t.rend(); }
00272 
00273       ///  Returns true if the %set is empty.
00274       bool
00275       empty() const
00276       { return _M_t.empty(); }
00277 
00278       ///  Returns the size of the %set.
00279       size_type
00280       size() const
00281       { return _M_t.size(); }
00282 
00283       ///  Returns the maximum size of the %set.
00284       size_type
00285       max_size() const
00286       { return _M_t.max_size(); }
00287 
00288       /**
00289        *  @brief  Swaps data with another %set.
00290        *  @param  x  A %set of the same element and allocator types.
00291        *
00292        *  This exchanges the elements between two sets in constant time.
00293        *  (It is only swapping a pointer, an integer, and an instance of
00294        *  the @c Compare type (which itself is often stateless and empty), so it
00295        *  should be quite fast.)
00296        *  Note that the global std::swap() function is specialized such that
00297        *  std::swap(s1,s2) will feed to this function.
00298        */
00299       void
00300       swap(set<_Key,_Compare,_Alloc>& __x)
00301       { _M_t.swap(__x._M_t); }
00302 
00303       // insert/erase
00304       /**
00305        *  @brief Attempts to insert an element into the %set.
00306        *  @param  x  Element to be inserted.
00307        *  @return  A pair, of which the first element is an iterator that points
00308        *           to the possibly inserted element, and the second is a bool
00309        *           that is true if the element was actually inserted.
00310        *
00311        *  This function attempts to insert an element into the %set.  A %set
00312        *  relies on unique keys and thus an element is only inserted if it is
00313        *  not already present in the %set.
00314        *
00315        *  Insertion requires logarithmic time.
00316        */
00317       std::pair<iterator,bool>
00318       insert(const value_type& __x)
00319       {
00320     std::pair<typename _Rep_type::iterator, bool> __p =
00321       _M_t.insert_unique(__x);
00322     return std::pair<iterator, bool>(__p.first, __p.second);
00323       }
00324 
00325       /**
00326        *  @brief Attempts to insert an element into the %set.
00327        *  @param  position  An iterator that serves as a hint as to where the
00328        *                    element should be inserted.
00329        *  @param  x  Element to be inserted.
00330        *  @return  An iterator that points to the element with key of @a x (may
00331        *           or may not be the element passed in).
00332        *
00333        *  This function is not concerned about whether the insertion took place,
00334        *  and thus does not return a boolean like the single-argument insert()
00335        *  does.  Note that the first parameter is only a hint and can
00336        *  potentially improve the performance of the insertion process.  A bad
00337        *  hint would cause no gains in efficiency.
00338        *
00339        *  See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
00340        *  for more on "hinting".
00341        *
00342        *  Insertion requires logarithmic time (if the hint is not taken).
00343        */
00344       iterator
00345       insert(iterator __position, const value_type& __x)
00346       { return _M_t.insert_unique(__position, __x); }
00347 
00348       /**
00349        *  @brief A template function that attemps to insert a range of elements.
00350        *  @param  first  Iterator pointing to the start of the range to be
00351        *                 inserted.
00352        *  @param  last  Iterator pointing to the end of the range.
00353        *
00354        *  Complexity similar to that of the range constructor.
00355        */
00356       template<class _InputIterator>
00357         void
00358         insert(_InputIterator __first, _InputIterator __last)
00359         { _M_t.insert_unique(__first, __last); }
00360 
00361       /**
00362        *  @brief Erases an element from a %set.
00363        *  @param  position  An iterator pointing to the element to be erased.
00364        *
00365        *  This function erases an element, pointed to by the given iterator,
00366        *  from a %set.  Note that this function only erases the element, and
00367        *  that if the element is itself a pointer, the pointed-to memory is not
00368        *  touched in any way.  Managing the pointer is the user's responsibilty.
00369        */
00370       void
00371       erase(iterator __position)
00372       { _M_t.erase(__position); }
00373 
00374       /**
00375        *  @brief Erases elements according to the provided key.
00376        *  @param  x  Key of element to be erased.
00377        *  @return  The number of elements erased.
00378        *
00379        *  This function erases all the elements located by the given key from
00380        *  a %set.
00381        *  Note that this function only erases the element, and that if
00382        *  the element is itself a pointer, the pointed-to memory is not touched
00383        *  in any way.  Managing the pointer is the user's responsibilty.
00384        */
00385       size_type
00386       erase(const key_type& __x)
00387       { return _M_t.erase(__x); }
00388 
00389       /**
00390        *  @brief Erases a [first,last) range of elements from a %set.
00391        *  @param  first  Iterator pointing to the start of the range to be
00392        *                 erased.
00393        *  @param  last  Iterator pointing to the end of the range to be erased.
00394        *
00395        *  This function erases a sequence of elements from a %set.
00396        *  Note that this function only erases the element, and that if
00397        *  the element is itself a pointer, the pointed-to memory is not touched
00398        *  in any way.  Managing the pointer is the user's responsibilty.
00399        */
00400       void
00401       erase(iterator __first, iterator __last)
00402       { _M_t.erase(__first, __last); }
00403 
00404       /**
00405        *  Erases all elements in a %set.  Note that this function only erases
00406        *  the elements, and that if the elements themselves are pointers, the
00407        *  pointed-to memory is not touched in any way.  Managing the pointer is
00408        *  the user's responsibilty.
00409        */
00410       void
00411       clear()
00412       { _M_t.clear(); }
00413 
00414       // set operations:
00415 
00416       /**
00417        *  @brief  Finds the number of elements.
00418        *  @param  x  Element to located.
00419        *  @return  Number of elements with specified key.
00420        *
00421        *  This function only makes sense for multisets; for set the result will
00422        *  either be 0 (not present) or 1 (present).
00423        */
00424       size_type
00425       count(const key_type& __x) const
00426       { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
00427 
00428       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00429       // 214.  set::find() missing const overload
00430       //@{
00431       /**
00432        *  @brief Tries to locate an element in a %set.
00433        *  @param  x  Element to be located.
00434        *  @return  Iterator pointing to sought-after element, or end() if not
00435        *           found.
00436        *
00437        *  This function takes a key and tries to locate the element with which
00438        *  the key matches.  If successful the function returns an iterator
00439        *  pointing to the sought after element.  If unsuccessful it returns the
00440        *  past-the-end ( @c end() ) iterator.
00441        */
00442       iterator
00443       find(const key_type& __x)
00444       { return _M_t.find(__x); }
00445 
00446       const_iterator
00447       find(const key_type& __x) const
00448       { return _M_t.find(__x); }
00449       //@}
00450 
00451       //@{
00452       /**
00453        *  @brief Finds the beginning of a subsequence matching given key.
00454        *  @param  x  Key to be located.
00455        *  @return  Iterator pointing to first element equal to or greater
00456        *           than key, or end().
00457        *
00458        *  This function returns the first element of a subsequence of elements
00459        *  that matches the given key.  If unsuccessful it returns an iterator
00460        *  pointing to the first element that has a greater value than given key
00461        *  or end() if no such element exists.
00462        */
00463       iterator
00464       lower_bound(const key_type& __x)
00465       { return _M_t.lower_bound(__x); }
00466 
00467       const_iterator
00468       lower_bound(const key_type& __x) const
00469       { return _M_t.lower_bound(__x); }
00470       //@}
00471 
00472       //@{
00473       /**
00474        *  @brief Finds the end of a subsequence matching given key.
00475        *  @param  x  Key to be located.
00476        *  @return Iterator pointing to the first element
00477        *          greater than key, or end().
00478        */
00479       iterator
00480       upper_bound(const key_type& __x)
00481       { return _M_t.upper_bound(__x); }
00482 
00483       const_iterator
00484       upper_bound(const key_type& __x) const
00485       { return _M_t.upper_bound(__x); }
00486       //@}
00487 
00488       //@{
00489       /**
00490        *  @brief Finds a subsequence matching given key.
00491        *  @param  x  Key to be located.
00492        *  @return  Pair of iterators that possibly points to the subsequence
00493        *           matching given key.
00494        *
00495        *  This function is equivalent to
00496        *  @code
00497        *    std::make_pair(c.lower_bound(val),
00498        *                   c.upper_bound(val))
00499        *  @endcode
00500        *  (but is faster than making the calls separately).
00501        *
00502        *  This function probably only makes sense for multisets.
00503        */
00504       std::pair<iterator, iterator>
00505       equal_range(const key_type& __x)
00506       { return _M_t.equal_range(__x); }
00507 
00508       std::pair<const_iterator, const_iterator>
00509       equal_range(const key_type& __x) const
00510       { return _M_t.equal_range(__x); }
00511       //@}
00512 
00513       template<class _K1, class _C1, class _A1>
00514         friend bool
00515         operator== (const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
00516 
00517       template<class _K1, class _C1, class _A1>
00518         friend bool
00519         operator< (const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
00520     };
00521 
00522 
00523   /**
00524    *  @brief  Set equality comparison.
00525    *  @param  x  A %set.
00526    *  @param  y  A %set of the same type as @a x.
00527    *  @return  True iff the size and elements of the sets are equal.
00528    *
00529    *  This is an equivalence relation.  It is linear in the size of the sets.
00530    *  Sets are considered equivalent if their sizes are equal, and if
00531    *  corresponding elements compare equal.
00532   */
00533   template<class _Key, class _Compare, class _Alloc>
00534     inline bool
00535     operator==(const set<_Key, _Compare, _Alloc>& __x,
00536            const set<_Key, _Compare, _Alloc>& __y)
00537     { return __x._M_t == __y._M_t; }
00538 
00539   /**
00540    *  @brief  Set ordering relation.
00541    *  @param  x  A %set.
00542    *  @param  y  A %set of the same type as @a x.
00543    *  @return  True iff @a x is lexicographically less than @a y.
00544    *
00545    *  This is a total ordering relation.  It is linear in the size of the
00546    *  maps.  The elements must be comparable with @c <.
00547    *
00548    *  See std::lexicographical_compare() for how the determination is made.
00549   */
00550   template<class _Key, class _Compare, class _Alloc>
00551     inline bool
00552     operator<(const set<_Key, _Compare, _Alloc>& __x,
00553           const set<_Key, _Compare, _Alloc>& __y)
00554     { return __x._M_t < __y._M_t; }
00555 
00556   ///  Returns !(x == y).
00557   template<class _Key, class _Compare, class _Alloc>
00558     inline bool
00559     operator!=(const set<_Key, _Compare, _Alloc>& __x,
00560            const set<_Key, _Compare, _Alloc>& __y)
00561     { return !(__x == __y); }
00562 
00563   ///  Returns y < x.
00564   template<class _Key, class _Compare, class _Alloc>
00565     inline bool
00566     operator>(const set<_Key, _Compare, _Alloc>& __x,
00567           const set<_Key, _Compare, _Alloc>& __y)
00568     { return __y < __x; }
00569 
00570   ///  Returns !(y < x)
00571   template<class _Key, class _Compare, class _Alloc>
00572     inline bool
00573     operator<=(const set<_Key, _Compare, _Alloc>& __x,
00574            const set<_Key, _Compare, _Alloc>& __y)
00575     { return !(__y < __x); }
00576 
00577   ///  Returns !(x < y)
00578   template<class _Key, class _Compare, class _Alloc>
00579     inline bool
00580     operator>=(const set<_Key, _Compare, _Alloc>& __x,
00581            const set<_Key, _Compare, _Alloc>& __y)
00582     { return !(__x < __y); }
00583 
00584   /// See std::set::swap().
00585   template<class _Key, class _Compare, class _Alloc>
00586     inline void
00587     swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y)
00588     { __x.swap(__y); }
00589 
00590 } // namespace std
00591 
00592 #endif /* _SET_H */

Generated on Tue Dec 2 03:59:29 2008 for libstdc++ by  doxygen 1.5.7.1