|
| 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 <concept_checks.h> |
| 35 | + |
| 36 | +__STL_BEGIN_NAMESPACE |
| 37 | + |
| 38 | +#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) |
| 39 | +#pragma set woff 1174 |
| 40 | +#pragma set woff 1375 |
| 41 | +#endif |
| 42 | + |
| 43 | +// Forward declarations of operators < and ==, needed for friend declaration. |
| 44 | + |
| 45 | +template <class _Key, class _Compare __STL_DEPENDENT_DEFAULT_TMPL(less<_Key>), |
| 46 | + class _Alloc = __STL_DEFAULT_ALLOCATOR(_Key) > |
| 47 | +class set; |
| 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 | +template <class _Key, class _Compare, class _Alloc> |
| 54 | +inline bool operator<(const set<_Key,_Compare,_Alloc>& __x, |
| 55 | + const set<_Key,_Compare,_Alloc>& __y); |
| 56 | + |
| 57 | + |
| 58 | +// Set默认递增排序 |
| 59 | +template <class _Key, class _Compare, class _Alloc> |
| 60 | +class set { |
| 61 | + // requirements: |
| 62 | + |
| 63 | + __STL_CLASS_REQUIRES(_Key, _Assignable); |
| 64 | + __STL_CLASS_BINARY_FUNCTION_CHECK(_Compare, bool, _Key, _Key); |
| 65 | + |
| 66 | +public: |
| 67 | + // typedefs: |
| 68 | + |
| 69 | + typedef _Key key_type; |
| 70 | + typedef _Key value_type; |
| 71 | + // key_compare与value_compare使用同一个函数 |
| 72 | + typedef _Compare key_compare; |
| 73 | + typedef _Compare value_compare; |
| 74 | +private: |
| 75 | + typedef _Rb_tree<key_type, value_type, |
| 76 | + _Identity<value_type>, key_compare, _Alloc> _Rep_type; |
| 77 | + // 使用红黑树表现set |
| 78 | + _Rep_type _M_t; // red-black tree representing set |
| 79 | +public: |
| 80 | + typedef typename _Rep_type::const_pointer pointer; |
| 81 | + typedef typename _Rep_type::const_pointer const_pointer; |
| 82 | + typedef typename _Rep_type::const_reference reference; |
| 83 | + typedef typename _Rep_type::const_reference const_reference; |
| 84 | + // iterator定义为const,不允许写入操作 |
| 85 | + typedef typename _Rep_type::const_iterator iterator; |
| 86 | + typedef typename _Rep_type::const_iterator const_iterator; |
| 87 | + typedef typename _Rep_type::const_reverse_iterator reverse_iterator; |
| 88 | + typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator; |
| 89 | + typedef typename _Rep_type::size_type size_type; |
| 90 | + typedef typename _Rep_type::difference_type difference_type; |
| 91 | + typedef typename _Rep_type::allocator_type allocator_type; |
| 92 | + |
| 93 | + // allocation/deallocation |
| 94 | + // set使用红黑树的insert_unique,不允许重复键值 |
| 95 | + set() : _M_t(_Compare(), allocator_type()) {} |
| 96 | + explicit set(const _Compare& __comp, |
| 97 | + const allocator_type& __a = allocator_type()) |
| 98 | + : _M_t(__comp, __a) {} |
| 99 | + |
| 100 | +#ifdef __STL_MEMBER_TEMPLATES |
| 101 | + template <class _InputIterator> |
| 102 | + set(_InputIterator __first, _InputIterator __last) |
| 103 | + : _M_t(_Compare(), allocator_type()) |
| 104 | + { _M_t.insert_unique(__first, __last); } |
| 105 | + |
| 106 | + template <class _InputIterator> |
| 107 | + set(_InputIterator __first, _InputIterator __last, const _Compare& __comp, |
| 108 | + const allocator_type& __a = allocator_type()) |
| 109 | + : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); } |
| 110 | +#else |
| 111 | + set(const value_type* __first, const value_type* __last) |
| 112 | + : _M_t(_Compare(), allocator_type()) |
| 113 | + { _M_t.insert_unique(__first, __last); } |
| 114 | + |
| 115 | + set(const value_type* __first, |
| 116 | + const value_type* __last, const _Compare& __comp, |
| 117 | + const allocator_type& __a = allocator_type()) |
| 118 | + : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); } |
| 119 | + |
| 120 | + set(const_iterator __first, const_iterator __last) |
| 121 | + : _M_t(_Compare(), allocator_type()) |
| 122 | + { _M_t.insert_unique(__first, __last); } |
| 123 | + |
| 124 | + set(const_iterator __first, const_iterator __last, const _Compare& __comp, |
| 125 | + const allocator_type& __a = allocator_type()) |
| 126 | + : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); } |
| 127 | +#endif /* __STL_MEMBER_TEMPLATES */ |
| 128 | + |
| 129 | + set(const set<_Key,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {} |
| 130 | + set<_Key,_Compare,_Alloc>& operator=(const set<_Key, _Compare, _Alloc>& __x) |
| 131 | + { |
| 132 | + _M_t = __x._M_t; |
| 133 | + return *this; |
| 134 | + } |
| 135 | + |
| 136 | + // accessors: |
| 137 | + // 调用红黑树实现 |
| 138 | + key_compare key_comp() const { return _M_t.key_comp(); } |
| 139 | + // value_compare一样调用key_compare |
| 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 | +#ifdef __STL_MEMBER_TEMPLATES |
| 162 | + template <class _InputIterator> |
| 163 | + void insert(_InputIterator __first, _InputIterator __last) { |
| 164 | + _M_t.insert_unique(__first, __last); |
| 165 | + } |
| 166 | +#else |
| 167 | + void insert(const_iterator __first, const_iterator __last) { |
| 168 | + _M_t.insert_unique(__first, __last); |
| 169 | + } |
| 170 | + void insert(const value_type* __first, const value_type* __last) { |
| 171 | + _M_t.insert_unique(__first, __last); |
| 172 | + } |
| 173 | +#endif /* __STL_MEMBER_TEMPLATES */ |
| 174 | + void erase(iterator __position) { |
| 175 | + typedef typename _Rep_type::iterator _Rep_iterator; |
| 176 | + _M_t.erase((_Rep_iterator&)__position); |
| 177 | + } |
| 178 | + size_type erase(const key_type& __x) { |
| 179 | + return _M_t.erase(__x); |
| 180 | + } |
| 181 | + void erase(iterator __first, iterator __last) { |
| 182 | + typedef typename _Rep_type::iterator _Rep_iterator; |
| 183 | + _M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last); |
| 184 | + } |
| 185 | + void clear() { _M_t.clear(); } |
| 186 | + |
| 187 | + // set operations: |
| 188 | + |
| 189 | + iterator find(const key_type& __x) const { return _M_t.find(__x); } |
| 190 | + size_type count(const key_type& __x) const { |
| 191 | + return _M_t.find(__x) == _M_t.end() ? 0 : 1; |
| 192 | + } |
| 193 | + iterator lower_bound(const key_type& __x) const { |
| 194 | + return _M_t.lower_bound(__x); |
| 195 | + } |
| 196 | + iterator upper_bound(const key_type& __x) const { |
| 197 | + return _M_t.upper_bound(__x); |
| 198 | + } |
| 199 | + pair<iterator,iterator> equal_range(const key_type& __x) const { |
| 200 | + return _M_t.equal_range(__x); |
| 201 | + } |
| 202 | + |
| 203 | +#ifdef __STL_TEMPLATE_FRIENDS |
| 204 | + template <class _K1, class _C1, class _A1> |
| 205 | + friend bool operator== (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&); |
| 206 | + template <class _K1, class _C1, class _A1> |
| 207 | + friend bool operator< (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&); |
| 208 | +#else /* __STL_TEMPLATE_FRIENDS */ |
| 209 | + friend bool __STD_QUALIFIER |
| 210 | + // __STL_NULL_TMPL_ARGS定义为<> |
| 211 | + operator== __STL_NULL_TMPL_ARGS (const set&, const set&); |
| 212 | + friend bool __STD_QUALIFIER |
| 213 | + operator< __STL_NULL_TMPL_ARGS (const set&, const set&); |
| 214 | +#endif /* __STL_TEMPLATE_FRIENDS */ |
| 215 | +}; |
| 216 | + |
| 217 | +template <class _Key, class _Compare, class _Alloc> |
| 218 | +inline bool operator==(const set<_Key,_Compare,_Alloc>& __x, |
| 219 | + const set<_Key,_Compare,_Alloc>& __y) { |
| 220 | + return __x._M_t == __y._M_t; |
| 221 | +} |
| 222 | + |
| 223 | +template <class _Key, class _Compare, class _Alloc> |
| 224 | +inline bool operator<(const set<_Key,_Compare,_Alloc>& __x, |
| 225 | + const set<_Key,_Compare,_Alloc>& __y) { |
| 226 | + return __x._M_t < __y._M_t; |
| 227 | +} |
| 228 | + |
| 229 | +#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER |
| 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 !(__x == __y); |
| 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 !(__y < __x); |
| 247 | +} |
| 248 | + |
| 249 | +template <class _Key, class _Compare, class _Alloc> |
| 250 | +inline bool operator>=(const set<_Key,_Compare,_Alloc>& __x, |
| 251 | + const set<_Key,_Compare,_Alloc>& __y) { |
| 252 | + return !(__x < __y); |
| 253 | +} |
| 254 | + |
| 255 | +template <class _Key, class _Compare, class _Alloc> |
| 256 | +inline void swap(set<_Key,_Compare,_Alloc>& __x, |
| 257 | + set<_Key,_Compare,_Alloc>& __y) { |
| 258 | + __x.swap(__y); |
| 259 | +} |
| 260 | + |
| 261 | +#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */ |
| 262 | + |
| 263 | +#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) |
| 264 | +#pragma reset woff 1174 |
| 265 | +#pragma reset woff 1375 |
| 266 | +#endif |
| 267 | + |
| 268 | +__STL_END_NAMESPACE |
| 269 | + |
| 270 | +#endif /* __SGI_STL_INTERNAL_SET_H */ |
| 271 | + |
| 272 | +// Local Variables: |
| 273 | +// mode:C++ |
| 274 | +// End: |
0 commit comments