# stl_set.h

Go to the documentation of this file.

    00001 // Set implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2001 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 __GLIBCPP_INTERNAL_SET_H
    00062 #define __GLIBCPP_INTERNAL_SET_H
    00063
    00064 #include <bits/concept_check.h>
    00065
    00066 namespace std
    00067 {
    00068
    00069 // Forward declarations of operators < and ==, needed for friend declaration.
    00070
    00071 template <class _Key, class _Compare = less<_Key>,
    00072           class _Alloc = allocator<_Key> >
    00073 class set;
    00074
    00075 template <class _Key, class _Compare, class _Alloc>
    00076 inline bool operator==(const set<_Key,_Compare,_Alloc>& __x,
    00077                        const set<_Key,_Compare,_Alloc>& __y);
    00078
    00079 template <class _Key, class _Compare, class _Alloc>
    00080 inline bool operator<(const set<_Key,_Compare,_Alloc>& __x,
    00081                       const set<_Key,_Compare,_Alloc>& __y);
    00082
    00083
    00084 template <class _Key, class _Compare, class _Alloc>
    00085 class set
    00086 {
    00087   // concept requirements
    00088   __glibcpp_class_requires(_Key, _SGIAssignableConcept)
    00089   __glibcpp_class_requires4(_Compare, bool, _Key, _Key, _BinaryFunctionConcept);
    00090
    00091 public:
    00092   // typedefs:
    00093   typedef _Key     key_type;
    00094   typedef _Key     value_type;
    00095   typedef _Compare key_compare;
    00096   typedef _Compare value_compare;
    00097 private:
    00098   typedef _Rb_tree<key_type, value_type,
    00099                   _Identity<value_type>, key_compare, _Alloc> _Rep_type;
    00100   _Rep_type _M_t;  // red-black tree representing set
    00101 public:
    00102   typedef typename _Rep_type::const_pointer pointer;
    00103   typedef typename _Rep_type::const_pointer const_pointer;
    00104   typedef typename _Rep_type::const_reference reference;
    00105   typedef typename _Rep_type::const_reference const_reference;
    00106   typedef typename _Rep_type::const_iterator iterator;
    00107   typedef typename _Rep_type::const_iterator const_iterator;
    00108   typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
    00109   typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
    00110   typedef typename _Rep_type::size_type size_type;
    00111   typedef typename _Rep_type::difference_type difference_type;
    00112   typedef typename _Rep_type::allocator_type allocator_type;
    00113
    00114   // allocation/deallocation
    00115
    00116   set() : _M_t(_Compare(), allocator_type()) {}
    00117   explicit set(const _Compare& __comp,
    00118                const allocator_type& __a = allocator_type())
    00119     : _M_t(__comp, __a) {}
    00120
    00121   template <class _InputIterator>
    00122   set(_InputIterator __first, _InputIterator __last)
    00123     : _M_t(_Compare(), allocator_type())
    00124     { _M_t.insert_unique(__first, __last); }
    00125
    00126   template <class _InputIterator>
    00127   set(_InputIterator __first, _InputIterator __last, const _Compare& __comp,
    00128       const allocator_type& __a = allocator_type())
    00129     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
    00130
    00131   set(const set<_Key,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {}
    00132   set<_Key,_Compare,_Alloc>& operator=(const set<_Key, _Compare, _Alloc>& __x)
    00133   {
    00134     _M_t = __x._M_t;
    00135     return *this;
    00136   }
    00137
    00138   // accessors:
    00139
    00140   key_compare key_comp() const { return _M_t.key_comp(); }
    00141   value_compare value_comp() const { return _M_t.key_comp(); }
    00142   allocator_type get_allocator() const { return _M_t.get_allocator(); }
    00143
    00144   iterator begin() const { return _M_t.begin(); }
    00145   iterator end() const { return _M_t.end(); }
    00146   reverse_iterator rbegin() const { return _M_t.rbegin(); }
    00147   reverse_iterator rend() const { return _M_t.rend(); }
    00148   bool empty() const { return _M_t.empty(); }
    00149   size_type size() const { return _M_t.size(); }
    00150   size_type max_size() const { return _M_t.max_size(); }
    00151   void swap(set<_Key,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
    00152
    00153   // insert/erase
    00154   pair<iterator,bool> insert(const value_type& __x) {
    00155     pair<typename _Rep_type::iterator, bool> __p = _M_t.insert_unique(__x);
    00156     return pair<iterator, bool>(__p.first, __p.second);
    00157   }
    00158   iterator insert(iterator __position, const value_type& __x) {
    00159     typedef typename _Rep_type::iterator _Rep_iterator;
    00160     return _M_t.insert_unique((_Rep_iterator&)__position, __x);
    00161   }
    00162   template <class _InputIterator>
    00163   void insert(_InputIterator __first, _InputIterator __last) {
    00164     _M_t.insert_unique(__first, __last);
    00165   }
    00166   void erase(iterator __position) {
    00167     typedef typename _Rep_type::iterator _Rep_iterator;
    00168     _M_t.erase((_Rep_iterator&)__position);
    00169   }
    00170   size_type erase(const key_type& __x) {
    00171     return _M_t.erase(__x);
    00172   }
    00173   void erase(iterator __first, iterator __last) {
    00174     typedef typename _Rep_type::iterator _Rep_iterator;
    00175     _M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last);
    00176   }
    00177   void clear() { _M_t.clear(); }
    00178
    00179   // set operations:
    00180
    00181   size_type count(const key_type& __x) const {
    00182     return _M_t.find(__x) == _M_t.end() ? 0 : 1;
    00183   }
    00184
    00185 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00186 //214.  set::find() missing const overload
    00187   iterator find(const key_type& __x) { return _M_t.find(__x); }
    00188   const_iterator find(const key_type& __x) const { return _M_t.find(__x); }
    00189   iterator lower_bound(const key_type& __x) {
    00190     return _M_t.lower_bound(__x);
    00191   }
    00192   const_iterator lower_bound(const key_type& __x) const {
    00193     return _M_t.lower_bound(__x);
    00194   }
    00195   iterator upper_bound(const key_type& __x) {
    00196     return _M_t.upper_bound(__x);
    00197   }
    00198   const_iterator upper_bound(const key_type& __x) const {
    00199     return _M_t.upper_bound(__x);
    00200   }
    00201   pair<iterator,iterator> equal_range(const key_type& __x) {
    00202     return _M_t.equal_range(__x);
    00203   }
    00204   pair<const_iterator,const_iterator> equal_range(const key_type& __x) const {
    00205     return _M_t.equal_range(__x);
    00206   }
    00207 #else
    00208   iterator find(const key_type& __x) const { return _M_t.find(__x); }
    00209   iterator lower_bound(const key_type& __x) const {
    00210     return _M_t.lower_bound(__x);
    00211   }
    00212   iterator upper_bound(const key_type& __x) const {
    00213     return _M_t.upper_bound(__x);
    00214   }
    00215   pair<iterator,iterator> equal_range(const key_type& __x) const {
    00216     return _M_t.equal_range(__x);
    00217   }
    00218 #endif
    00219
    00220   template <class _K1, class _C1, class _A1>
    00221   friend bool operator== (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
    00222   template <class _K1, class _C1, class _A1>
    00223   friend bool operator< (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
    00224 };
    00225
    00226 template <class _Key, class _Compare, class _Alloc>
    00227 inline bool operator==(const set<_Key,_Compare,_Alloc>& __x,
    00228                        const set<_Key,_Compare,_Alloc>& __y) {
    00229   return __x._M_t == __y._M_t;
    00230 }
    00231
    00232 template <class _Key, class _Compare, class _Alloc>
    00233 inline bool operator<(const set<_Key,_Compare,_Alloc>& __x,
    00234                       const set<_Key,_Compare,_Alloc>& __y) {
    00235   return __x._M_t < __y._M_t;
    00236 }
    00237
    00238 template <class _Key, class _Compare, class _Alloc>
    00239 inline bool operator!=(const set<_Key,_Compare,_Alloc>& __x,
    00240                        const set<_Key,_Compare,_Alloc>& __y) {
    00241   return !(__x == __y);
    00242 }
    00243
    00244 template <class _Key, class _Compare, class _Alloc>
    00245 inline bool operator>(const set<_Key,_Compare,_Alloc>& __x,
    00246                       const set<_Key,_Compare,_Alloc>& __y) {
    00247   return __y < __x;
    00248 }
    00249
    00250 template <class _Key, class _Compare, class _Alloc>
    00251 inline bool operator<=(const set<_Key,_Compare,_Alloc>& __x,
    00252                        const set<_Key,_Compare,_Alloc>& __y) {
    00253   return !(__y < __x);
    00254 }
    00255
    00256 template <class _Key, class _Compare, class _Alloc>
    00257 inline bool operator>=(const set<_Key,_Compare,_Alloc>& __x,
    00258                        const set<_Key,_Compare,_Alloc>& __y) {
    00259   return !(__x < __y);
    00260 }
    00261
    00262 template <class _Key, class _Compare, class _Alloc>
    00263 inline void swap(set<_Key,_Compare,_Alloc>& __x,
    00264                  set<_Key,_Compare,_Alloc>& __y) {
    00265   __x.swap(__y);
    00266 }
    00267
    00268 } // namespace std
    00269
    00270 #endif /* __GLIBCPP_INTERNAL_SET_H */
    00271
    00272 // Local Variables:
    00273 // mode:C++
    00274 // End: