stl_set.h

Go to the documentation of this file.
00001 // Set implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 
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 = less<_Key>,
00070        class _Alloc = 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 
00106   template<class _Key, class _Compare, class _Alloc>
00107     class set
00108     {
00109       // concept requirements
00110       __glibcxx_class_requires(_Key, _SGIAssignableConcept)
00111       __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
00112                 _BinaryFunctionConcept)
00113 
00114     public:
00115       // typedefs:
00117 
00118       typedef _Key     key_type;
00119       typedef _Key     value_type;
00120       typedef _Compare key_compare;
00121       typedef _Compare value_compare;
00123 
00124     private:
00125       typedef _Rb_tree<key_type, value_type,
00126                _Identity<value_type>, key_compare, _Alloc> _Rep_type;
00127       _Rep_type _M_t;  // red-black tree representing set
00128     public:
00130 
00131       typedef typename _Alloc::pointer pointer;
00132       typedef typename _Alloc::const_pointer const_pointer;
00133       typedef typename _Alloc::reference reference;
00134       typedef typename _Alloc::const_reference const_reference;
00135       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00136       // DR 103. set::iterator is required to be modifiable,
00137       // but this allows modification of keys.
00138       typedef typename _Rep_type::const_iterator iterator;
00139       typedef typename _Rep_type::const_iterator const_iterator;
00140       typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
00141       typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
00142       typedef typename _Rep_type::size_type size_type;
00143       typedef typename _Rep_type::difference_type difference_type;
00144       typedef typename _Rep_type::allocator_type allocator_type;
00146 
00147       // allocation/deallocation
00149       set()
00150       : _M_t(_Compare(), allocator_type()) {}
00151 
00158       explicit set(const _Compare& __comp,
00159            const allocator_type& __a = allocator_type())
00160       : _M_t(__comp, __a) {}
00161 
00171       template<class _InputIterator>
00172         set(_InputIterator __first, _InputIterator __last)
00173         : _M_t(_Compare(), allocator_type())
00174         { _M_t.insert_unique(__first, __last); }
00175 
00187       template<class _InputIterator>
00188         set(_InputIterator __first, _InputIterator __last,
00189         const _Compare& __comp,
00190         const allocator_type& __a = allocator_type())
00191     : _M_t(__comp, __a)
00192         { _M_t.insert_unique(__first, __last); }
00193 
00201       set(const set<_Key,_Compare,_Alloc>& __x)
00202       : _M_t(__x._M_t) { }
00203 
00211       set<_Key,_Compare,_Alloc>&
00212       operator=(const set<_Key, _Compare, _Alloc>& __x)
00213       {
00214     _M_t = __x._M_t;
00215     return *this;
00216       }
00217 
00218       // accessors:
00219 
00221       key_compare
00222       key_comp() const
00223       { return _M_t.key_comp(); }
00225       value_compare
00226       value_comp() const
00227       { return _M_t.key_comp(); }
00229       allocator_type
00230       get_allocator() const
00231       { return _M_t.get_allocator(); }
00232 
00237       iterator
00238       begin() const
00239       { return _M_t.begin(); }
00240 
00245       iterator
00246       end() const
00247       { return _M_t.end(); }
00248 
00254       reverse_iterator
00255       rbegin() const
00256       { return _M_t.rbegin(); }
00257 
00263       reverse_iterator
00264       rend() const
00265       { return _M_t.rend(); }
00266 
00268       bool
00269       empty() const
00270       { return _M_t.empty(); }
00271 
00273       size_type
00274       size() const
00275       { return _M_t.size(); }
00276 
00278       size_type
00279       max_size() const
00280       { return _M_t.max_size(); }
00281 
00293       void
00294       swap(set<_Key,_Compare,_Alloc>& __x)
00295       { _M_t.swap(__x._M_t); }
00296 
00297       // insert/erase
00311       pair<iterator,bool>
00312       insert(const value_type& __x)
00313       {
00314     pair<typename _Rep_type::iterator, bool> __p = _M_t.insert_unique(__x);
00315     return pair<iterator, bool>(__p.first, __p.second);
00316       }
00317 
00337       iterator
00338       insert(iterator __position, const value_type& __x)
00339       {
00340     typedef typename _Rep_type::iterator _Rep_iterator;
00341     return _M_t.insert_unique((_Rep_iterator&)__position, __x);
00342       }
00343 
00352       template<class _InputIterator>
00353       void
00354       insert(_InputIterator __first, _InputIterator __last)
00355       { _M_t.insert_unique(__first, __last); }
00356 
00366       void
00367       erase(iterator __position)
00368       {
00369     typedef typename _Rep_type::iterator _Rep_iterator;
00370     _M_t.erase((_Rep_iterator&)__position);
00371       }
00372 
00384       size_type
00385       erase(const key_type& __x) { return _M_t.erase(__x); }
00386 
00398       void
00399       erase(iterator __first, iterator __last)
00400       {
00401     typedef typename _Rep_type::iterator _Rep_iterator;
00402     _M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last);
00403       }
00404 
00411       void
00412       clear()
00413       { _M_t.clear(); }
00414 
00415       // set operations:
00416 
00425       size_type
00426       count(const key_type& __x) const
00427       { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
00428 
00429       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00430       // 214.  set::find() missing const overload
00432 
00443       iterator
00444       find(const key_type& __x)
00445       { return _M_t.find(__x); }
00446 
00447       const_iterator
00448       find(const key_type& __x) const
00449       { return _M_t.find(__x); }
00451 
00453 
00464       iterator
00465       lower_bound(const key_type& __x)
00466       { return _M_t.lower_bound(__x); }
00467 
00468       const_iterator
00469       lower_bound(const key_type& __x) const
00470       { return _M_t.lower_bound(__x); }
00472 
00474 
00480       iterator
00481       upper_bound(const key_type& __x)
00482       { return _M_t.upper_bound(__x); }
00483 
00484       const_iterator
00485       upper_bound(const key_type& __x) const
00486       { return _M_t.upper_bound(__x); }
00488 
00490 
00505       pair<iterator,iterator>
00506       equal_range(const key_type& __x)
00507       { return _M_t.equal_range(__x); }
00508 
00509       pair<const_iterator,const_iterator>
00510       equal_range(const key_type& __x) const
00511       { return _M_t.equal_range(__x); }
00513 
00514       template<class _K1, class _C1, class _A1>
00515         friend bool
00516         operator== (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
00517 
00518       template<class _K1, class _C1, class _A1>
00519         friend bool
00520         operator< (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
00521     };
00522 
00523 
00534   template<class _Key, class _Compare, class _Alloc>
00535     inline bool
00536     operator==(const set<_Key,_Compare,_Alloc>& __x,
00537            const set<_Key,_Compare,_Alloc>& __y)
00538     { return __x._M_t == __y._M_t; }
00539 
00551   template<class _Key, class _Compare, class _Alloc>
00552     inline bool
00553     operator<(const set<_Key,_Compare,_Alloc>& __x,
00554           const set<_Key,_Compare,_Alloc>& __y)
00555     { return __x._M_t < __y._M_t; }
00556 
00558   template<class _Key, class _Compare, class _Alloc>
00559     inline bool
00560     operator!=(const set<_Key,_Compare,_Alloc>& __x,
00561            const set<_Key,_Compare,_Alloc>& __y)
00562     { return !(__x == __y); }
00563 
00565   template<class _Key, class _Compare, class _Alloc>
00566     inline bool
00567     operator>(const set<_Key,_Compare,_Alloc>& __x,
00568           const set<_Key,_Compare,_Alloc>& __y)
00569     { return __y < __x; }
00570 
00572   template<class _Key, class _Compare, class _Alloc>
00573     inline bool
00574     operator<=(const set<_Key,_Compare,_Alloc>& __x,
00575            const set<_Key,_Compare,_Alloc>& __y)
00576     { return !(__y < __x); }
00577 
00579   template<class _Key, class _Compare, class _Alloc>
00580     inline bool
00581     operator>=(const set<_Key,_Compare,_Alloc>& __x,
00582            const set<_Key,_Compare,_Alloc>& __y)
00583     { return !(__x < __y); }
00584 
00586   template<class _Key, class _Compare, class _Alloc>
00587     inline void
00588     swap(set<_Key,_Compare,_Alloc>& __x, set<_Key,_Compare,_Alloc>& __y)
00589     { __x.swap(__y); }
00590 
00591 } // namespace std
00592 
00593 #endif /* _SET_H */

Generated on Tue Jan 30 17:31:55 2007 for GNU C++ STL by doxygen 1.3.6