OSDN Git Service

2001-04-02 Phil Edwards <pme@sources.redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_set.h
1 /*
2  *
3  * Copyright (c) 1994
4  * Hewlett-Packard Company
5  *
6  * Permission to use, copy, modify, distribute and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appear in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation.  Hewlett-Packard Company makes no
11  * representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  *
15  * Copyright (c) 1996,1997
16  * Silicon Graphics Computer Systems, Inc.
17  *
18  * Permission to use, copy, modify, distribute and sell this software
19  * and its documentation for any purpose is hereby granted without fee,
20  * provided that the above copyright notice appear in all copies and
21  * that both that copyright notice and this permission notice appear
22  * in supporting documentation.  Silicon Graphics makes no
23  * representations about the suitability of this software for any
24  * purpose.  It is provided "as is" without express or implied warranty.
25  */
26
27 /* NOTE: This is an internal header file, included by other STL headers.
28  *   You should not attempt to use it directly.
29  */
30
31 #ifndef __SGI_STL_INTERNAL_SET_H
32 #define __SGI_STL_INTERNAL_SET_H
33
34 #include <bits/concept_check.h>
35
36 namespace std
37 {
38
39 // Forward declarations of operators < and ==, needed for friend declaration.
40
41 template <class _Key, class _Compare = less<_Key>,
42           class _Alloc = allocator<_Key> >
43 class set;
44
45 template <class _Key, class _Compare, class _Alloc>
46 inline bool operator==(const set<_Key,_Compare,_Alloc>& __x, 
47                        const set<_Key,_Compare,_Alloc>& __y);
48
49 template <class _Key, class _Compare, class _Alloc>
50 inline bool operator<(const set<_Key,_Compare,_Alloc>& __x, 
51                       const set<_Key,_Compare,_Alloc>& __y);
52
53
54 template <class _Key, class _Compare, class _Alloc>
55 class set
56 {
57   // concept requirements
58   glibcpp_class_requires(_Key, SGIAssignableConcept);
59   glibcpp_class_requires4(_Compare, bool, _Key, _Key, BinaryFunctionConcept);
60
61 public:
62   // typedefs:
63   typedef _Key     key_type;
64   typedef _Key     value_type;
65   typedef _Compare key_compare;
66   typedef _Compare value_compare;
67 private:
68   typedef _Rb_tree<key_type, value_type, 
69                   _Identity<value_type>, key_compare, _Alloc> _Rep_type;
70   _Rep_type _M_t;  // red-black tree representing set
71 public:
72   typedef typename _Rep_type::const_pointer pointer;
73   typedef typename _Rep_type::const_pointer const_pointer;
74   typedef typename _Rep_type::const_reference reference;
75   typedef typename _Rep_type::const_reference const_reference;
76   typedef typename _Rep_type::const_iterator iterator;
77   typedef typename _Rep_type::const_iterator const_iterator;
78   typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
79   typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
80   typedef typename _Rep_type::size_type size_type;
81   typedef typename _Rep_type::difference_type difference_type;
82   typedef typename _Rep_type::allocator_type allocator_type;
83
84   // allocation/deallocation
85
86   set() : _M_t(_Compare(), allocator_type()) {}
87   explicit set(const _Compare& __comp,
88                const allocator_type& __a = allocator_type())
89     : _M_t(__comp, __a) {}
90
91   template <class _InputIterator>
92   set(_InputIterator __first, _InputIterator __last)
93     : _M_t(_Compare(), allocator_type())
94     { _M_t.insert_unique(__first, __last); }
95
96   template <class _InputIterator>
97   set(_InputIterator __first, _InputIterator __last, const _Compare& __comp,
98       const allocator_type& __a = allocator_type())
99     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
100
101   set(const set<_Key,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {}
102   set<_Key,_Compare,_Alloc>& operator=(const set<_Key, _Compare, _Alloc>& __x)
103   { 
104     _M_t = __x._M_t; 
105     return *this;
106   }
107
108   // accessors:
109
110   key_compare key_comp() const { return _M_t.key_comp(); }
111   value_compare value_comp() const { return _M_t.key_comp(); }
112   allocator_type get_allocator() const { return _M_t.get_allocator(); }
113
114   iterator begin() const { return _M_t.begin(); }
115   iterator end() const { return _M_t.end(); }
116   reverse_iterator rbegin() const { return _M_t.rbegin(); } 
117   reverse_iterator rend() const { return _M_t.rend(); }
118   bool empty() const { return _M_t.empty(); }
119   size_type size() const { return _M_t.size(); }
120   size_type max_size() const { return _M_t.max_size(); }
121   void swap(set<_Key,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
122
123   // insert/erase
124   pair<iterator,bool> insert(const value_type& __x) { 
125     pair<typename _Rep_type::iterator, bool> __p = _M_t.insert_unique(__x); 
126     return pair<iterator, bool>(__p.first, __p.second);
127   }
128   iterator insert(iterator __position, const value_type& __x) {
129     typedef typename _Rep_type::iterator _Rep_iterator;
130     return _M_t.insert_unique((_Rep_iterator&)__position, __x);
131   }
132   template <class _InputIterator>
133   void insert(_InputIterator __first, _InputIterator __last) {
134     _M_t.insert_unique(__first, __last);
135   }
136   void erase(iterator __position) { 
137     typedef typename _Rep_type::iterator _Rep_iterator;
138     _M_t.erase((_Rep_iterator&)__position); 
139   }
140   size_type erase(const key_type& __x) { 
141     return _M_t.erase(__x); 
142   }
143   void erase(iterator __first, iterator __last) { 
144     typedef typename _Rep_type::iterator _Rep_iterator;
145     _M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last); 
146   }
147   void clear() { _M_t.clear(); }
148
149   // set operations:
150
151   iterator find(const key_type& __x) const { return _M_t.find(__x); }
152   size_type count(const key_type& __x) const {
153     return _M_t.find(__x) == _M_t.end() ? 0 : 1;
154   }
155   iterator lower_bound(const key_type& __x) const {
156     return _M_t.lower_bound(__x);
157   }
158   iterator upper_bound(const key_type& __x) const {
159     return _M_t.upper_bound(__x); 
160   }
161   pair<iterator,iterator> equal_range(const key_type& __x) const {
162     return _M_t.equal_range(__x);
163   }
164
165   template <class _K1, class _C1, class _A1>
166   friend bool operator== (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
167   template <class _K1, class _C1, class _A1>
168   friend bool operator< (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
169 };
170
171 template <class _Key, class _Compare, class _Alloc>
172 inline bool operator==(const set<_Key,_Compare,_Alloc>& __x, 
173                        const set<_Key,_Compare,_Alloc>& __y) {
174   return __x._M_t == __y._M_t;
175 }
176
177 template <class _Key, class _Compare, class _Alloc>
178 inline bool operator<(const set<_Key,_Compare,_Alloc>& __x, 
179                       const set<_Key,_Compare,_Alloc>& __y) {
180   return __x._M_t < __y._M_t;
181 }
182
183 template <class _Key, class _Compare, class _Alloc>
184 inline bool operator!=(const set<_Key,_Compare,_Alloc>& __x, 
185                        const set<_Key,_Compare,_Alloc>& __y) {
186   return !(__x == __y);
187 }
188
189 template <class _Key, class _Compare, class _Alloc>
190 inline bool operator>(const set<_Key,_Compare,_Alloc>& __x, 
191                       const set<_Key,_Compare,_Alloc>& __y) {
192   return __y < __x;
193 }
194
195 template <class _Key, class _Compare, class _Alloc>
196 inline bool operator<=(const set<_Key,_Compare,_Alloc>& __x, 
197                        const set<_Key,_Compare,_Alloc>& __y) {
198   return !(__y < __x);
199 }
200
201 template <class _Key, class _Compare, class _Alloc>
202 inline bool operator>=(const set<_Key,_Compare,_Alloc>& __x, 
203                        const set<_Key,_Compare,_Alloc>& __y) {
204   return !(__x < __y);
205 }
206
207 template <class _Key, class _Compare, class _Alloc>
208 inline void swap(set<_Key,_Compare,_Alloc>& __x, 
209                  set<_Key,_Compare,_Alloc>& __y) {
210   __x.swap(__y);
211 }
212
213 } // namespace std
214
215 #endif /* __SGI_STL_INTERNAL_SET_H */
216
217 // Local Variables:
218 // mode:C++
219 // End: