OSDN Git Service

2003-11-11 Doug Gregor <gregod@cs.rpi.edu>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_set.h
1 // Set implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 namespace __gnu_norm
67 {
68   // Forward declarations of operators < and ==, needed for friend declaration.
69   template<class _Key, class _Compare = less<_Key>, 
70            class _Alloc = allocator<_Key> >
71   class set;
72
73   template<class _Key, class _Compare, class _Alloc>
74     inline bool 
75     operator==(const set<_Key,_Compare,_Alloc>& __x, 
76                const set<_Key,_Compare,_Alloc>& __y);
77
78   template<class _Key, class _Compare, class _Alloc>
79     inline bool 
80     operator<(const set<_Key,_Compare,_Alloc>& __x, 
81               const set<_Key,_Compare,_Alloc>& __y);
82
83   template<class _Key, class _Compare, class _Alloc>
84     class set
85     {
86       // concept requirements
87       __glibcxx_class_requires(_Key, _SGIAssignableConcept)
88       __glibcxx_class_requires4(_Compare, bool, _Key, _Key, _BinaryFunctionConcept)
89         
90         public:
91       // typedefs:
92       typedef _Key     key_type;
93       typedef _Key     value_type;
94       typedef _Compare key_compare;
95       typedef _Compare value_compare;
96 private:
97   typedef _Rb_tree<key_type, value_type, 
98                   _Identity<value_type>, key_compare, _Alloc> _Rep_type;
99   _Rep_type _M_t;  // red-black tree representing set
100 public:
101   typedef typename _Rep_type::const_pointer pointer;
102   typedef typename _Rep_type::const_pointer const_pointer;
103   typedef typename _Rep_type::const_reference reference;
104   typedef typename _Rep_type::const_reference const_reference;
105   typedef typename _Rep_type::const_iterator iterator;
106   typedef typename _Rep_type::const_iterator const_iterator;
107   typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
108   typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
109   typedef typename _Rep_type::size_type size_type;
110   typedef typename _Rep_type::difference_type difference_type;
111   typedef typename _Rep_type::allocator_type allocator_type;
112
113   // allocation/deallocation
114
115   set() : _M_t(_Compare(), allocator_type()) {}
116   explicit set(const _Compare& __comp,
117                const allocator_type& __a = allocator_type())
118     : _M_t(__comp, __a) {}
119
120   template<class _InputIterator>
121   set(_InputIterator __first, _InputIterator __last)
122     : _M_t(_Compare(), allocator_type())
123     { _M_t.insert_unique(__first, __last); }
124
125   template<class _InputIterator>
126   set(_InputIterator __first, _InputIterator __last, const _Compare& __comp,
127       const allocator_type& __a = allocator_type())
128     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
129
130   set(const set<_Key,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {}
131   set<_Key,_Compare,_Alloc>& operator=(const set<_Key, _Compare, _Alloc>& __x)
132   { 
133     _M_t = __x._M_t; 
134     return *this;
135   }
136
137   // accessors:
138
139   key_compare key_comp() const { return _M_t.key_comp(); }
140   value_compare value_comp() const { return _M_t.key_comp(); }
141   allocator_type get_allocator() const { return _M_t.get_allocator(); }
142
143   iterator begin() const { return _M_t.begin(); }
144   iterator end() const { return _M_t.end(); }
145   reverse_iterator rbegin() const { return _M_t.rbegin(); } 
146   reverse_iterator rend() const { return _M_t.rend(); }
147   bool empty() const { return _M_t.empty(); }
148   size_type size() const { return _M_t.size(); }
149   size_type max_size() const { return _M_t.max_size(); }
150   void swap(set<_Key,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
151
152   // insert/erase
153   pair<iterator,bool> insert(const value_type& __x) { 
154     pair<typename _Rep_type::iterator, bool> __p = _M_t.insert_unique(__x); 
155     return pair<iterator, bool>(__p.first, __p.second);
156   }
157   iterator insert(iterator __position, const value_type& __x) {
158     typedef typename _Rep_type::iterator _Rep_iterator;
159     return _M_t.insert_unique((_Rep_iterator&)__position, __x);
160   }
161   template<class _InputIterator>
162   void insert(_InputIterator __first, _InputIterator __last) {
163     _M_t.insert_unique(__first, __last);
164   }
165   void erase(iterator __position) { 
166     typedef typename _Rep_type::iterator _Rep_iterator;
167     _M_t.erase((_Rep_iterator&)__position); 
168   }
169   size_type erase(const key_type& __x) { 
170     return _M_t.erase(__x); 
171   }
172   void erase(iterator __first, iterator __last) { 
173     typedef typename _Rep_type::iterator _Rep_iterator;
174     _M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last); 
175   }
176   void clear() { _M_t.clear(); }
177
178   // set operations:
179
180   size_type count(const key_type& __x) const {
181     return _M_t.find(__x) == _M_t.end() ? 0 : 1;
182   }
183
184   // _GLIBCXX_RESOLVE_LIB_DEFECTS
185   // 214.  set::find() missing const overload
186   iterator find(const key_type& __x) { return _M_t.find(__x); }
187   const_iterator find(const key_type& __x) const { return _M_t.find(__x); }
188   iterator lower_bound(const key_type& __x) {
189     return _M_t.lower_bound(__x);
190   }
191   const_iterator lower_bound(const key_type& __x) const {
192     return _M_t.lower_bound(__x);
193   }
194   iterator upper_bound(const key_type& __x) {
195     return _M_t.upper_bound(__x); 
196   }
197   const_iterator upper_bound(const key_type& __x) const {
198     return _M_t.upper_bound(__x); 
199   }
200   pair<iterator,iterator> equal_range(const key_type& __x) {
201     return _M_t.equal_range(__x);
202   }
203   pair<const_iterator,const_iterator> equal_range(const key_type& __x) const {
204     return _M_t.equal_range(__x);
205   }
206
207   template<class _K1, class _C1, class _A1>
208   friend bool operator== (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
209   template<class _K1, class _C1, class _A1>
210   friend bool operator< (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
211 };
212
213 template<class _Key, class _Compare, class _Alloc>
214 inline bool operator==(const set<_Key,_Compare,_Alloc>& __x, 
215                        const set<_Key,_Compare,_Alloc>& __y) {
216   return __x._M_t == __y._M_t;
217 }
218
219 template<class _Key, class _Compare, class _Alloc>
220 inline bool operator<(const set<_Key,_Compare,_Alloc>& __x, 
221                       const set<_Key,_Compare,_Alloc>& __y) {
222   return __x._M_t < __y._M_t;
223 }
224
225 template<class _Key, class _Compare, class _Alloc>
226 inline bool operator!=(const set<_Key,_Compare,_Alloc>& __x, 
227                        const set<_Key,_Compare,_Alloc>& __y) {
228   return !(__x == __y);
229 }
230
231 template<class _Key, class _Compare, class _Alloc>
232 inline bool operator>(const set<_Key,_Compare,_Alloc>& __x, 
233                       const set<_Key,_Compare,_Alloc>& __y) {
234   return __y < __x;
235 }
236
237 template<class _Key, class _Compare, class _Alloc>
238 inline bool operator<=(const set<_Key,_Compare,_Alloc>& __x, 
239                        const set<_Key,_Compare,_Alloc>& __y) {
240   return !(__y < __x);
241 }
242
243 template<class _Key, class _Compare, class _Alloc>
244 inline bool operator>=(const set<_Key,_Compare,_Alloc>& __x, 
245                        const set<_Key,_Compare,_Alloc>& __y) {
246   return !(__x < __y);
247 }
248
249 template<class _Key, class _Compare, class _Alloc>
250 inline void swap(set<_Key,_Compare,_Alloc>& __x, 
251                  set<_Key,_Compare,_Alloc>& __y) {
252   __x.swap(__y);
253 }
254
255 } // namespace __gnu_norm
256
257 #endif /* _SET_H */