1 // Multiset implementation -*- C++ -*- 2 3 // Copyright (C) 2001 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 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_multiset.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 __GLIBCPP_INTERNAL_MULTISET_H 62 #define __GLIBCPP_INTERNAL_MULTISET_H 63 64 #include <bits/concept_check.h> 65 66 namespace std 67 { 68 69 // Forward declaration of operators < and ==, needed for friend declaration. 70 71 template <class _Key, class _Compare = less<_Key>, 72 class _Alloc = allocator<_Key> > 73 class multiset; 74 75 template <class _Key, class _Compare, class _Alloc> 76 inline bool operator==(const multiset<_Key,_Compare,_Alloc>& __x, 77 const multiset<_Key,_Compare,_Alloc>& __y); 78 79 template <class _Key, class _Compare, class _Alloc> 80 inline bool operator<(const multiset<_Key,_Compare,_Alloc>& __x, 81 const multiset<_Key,_Compare,_Alloc>& __y); 82 83 template <class _Key, class _Compare, class _Alloc> 84 class multiset 85 { 86 // concept requirements 87 __glibcpp_class_requires(_Key, _SGIAssignableConcept) 88 __glibcpp_class_requires4(_Compare, bool, _Key, _Key, _BinaryFunctionConcept); 89 90 public: 91 92 // typedefs: 93 94 typedef _Key key_type; 95 typedef _Key value_type; 96 typedef _Compare key_compare; 97 typedef _Compare value_compare; 98 private: 99 typedef _Rb_tree<key_type, value_type, 100 _Identity<value_type>, key_compare, _Alloc> _Rep_type; 101 _Rep_type _M_t; // red-black tree representing multiset 102 public: 103 typedef typename _Rep_type::const_pointer pointer; 104 typedef typename _Rep_type::const_pointer const_pointer; 105 typedef typename _Rep_type::const_reference reference; 106 typedef typename _Rep_type::const_reference const_reference; 107 typedef typename _Rep_type::const_iterator iterator; 108 typedef typename _Rep_type::const_iterator const_iterator; 109 typedef typename _Rep_type::const_reverse_iterator reverse_iterator; 110 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator; 111 typedef typename _Rep_type::size_type size_type; 112 typedef typename _Rep_type::difference_type difference_type; 113 typedef typename _Rep_type::allocator_type allocator_type; 114 115 // allocation/deallocation 116 117 multiset() : _M_t(_Compare(), allocator_type()) {} 118 explicit multiset(const _Compare& __comp, 119 const allocator_type& __a = allocator_type()) 120 : _M_t(__comp, __a) {} 121 122 template <class _InputIterator> 123 multiset(_InputIterator __first, _InputIterator __last) 124 : _M_t(_Compare(), allocator_type()) 125 { _M_t.insert_equal(__first, __last); } 126 127 template <class _InputIterator> 128 multiset(_InputIterator __first, _InputIterator __last, 129 const _Compare& __comp, 130 const allocator_type& __a = allocator_type()) 131 : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); } 132 133 multiset(const multiset<_Key,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {} 134 135 multiset<_Key,_Compare,_Alloc>& 136 operator=(const multiset<_Key,_Compare,_Alloc>& __x) { 137 _M_t = __x._M_t; 138 return *this; 139 } 140 141 // accessors: 142 143 key_compare key_comp() const { return _M_t.key_comp(); } 144 value_compare value_comp() const { return _M_t.key_comp(); } 145 allocator_type get_allocator() const { return _M_t.get_allocator(); } 146 147 iterator begin() const { return _M_t.begin(); } 148 iterator end() const { return _M_t.end(); } 149 reverse_iterator rbegin() const { return _M_t.rbegin(); } 150 reverse_iterator rend() const { return _M_t.rend(); } 151 bool empty() const { return _M_t.empty(); } 152 size_type size() const { return _M_t.size(); } 153 size_type max_size() const { return _M_t.max_size(); } 154 void swap(multiset<_Key,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); } 155 156 // insert/erase 157 iterator insert(const value_type& __x) { 158 return _M_t.insert_equal(__x); 159 } 160 iterator insert(iterator __position, const value_type& __x) { 161 typedef typename _Rep_type::iterator _Rep_iterator; 162 return _M_t.insert_equal((_Rep_iterator&)__position, __x); 163 } 164 165 template <class _InputIterator> 166 void insert(_InputIterator __first, _InputIterator __last) { 167 _M_t.insert_equal(__first, __last); 168 } 169 void erase(iterator __position) { 170 typedef typename _Rep_type::iterator _Rep_iterator; 171 _M_t.erase((_Rep_iterator&)__position); 172 } 173 size_type erase(const key_type& __x) { 174 return _M_t.erase(__x); 175 } 176 void erase(iterator __first, iterator __last) { 177 typedef typename _Rep_type::iterator _Rep_iterator; 178 _M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last); 179 } 180 void clear() { _M_t.clear(); } 181 182 // multiset operations: 183 184 size_type count(const key_type& __x) const { return _M_t.count(__x); } 185 186 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 187 //214. set::find() missing const overload 188 iterator find(const key_type& __x) { return _M_t.find(__x); } 189 const_iterator find(const key_type& __x) const { return _M_t.find(__x); } 190 iterator lower_bound(const key_type& __x) { 191 return _M_t.lower_bound(__x); 192 } 193 const_iterator lower_bound(const key_type& __x) const { 194 return _M_t.lower_bound(__x); 195 } 196 iterator upper_bound(const key_type& __x) { 197 return _M_t.upper_bound(__x); 198 } 199 const_iterator upper_bound(const key_type& __x) const { 200 return _M_t.upper_bound(__x); 201 } 202 pair<iterator,iterator> equal_range(const key_type& __x) { 203 return _M_t.equal_range(__x); 204 } 205 pair<const_iterator,const_iterator> equal_range(const key_type& __x) const { 206 return _M_t.equal_range(__x); 207 } 208 #else 209 iterator find(const key_type& __x) const { return _M_t.find(__x); } 210 iterator lower_bound(const key_type& __x) const { 211 return _M_t.lower_bound(__x); 212 } 213 iterator upper_bound(const key_type& __x) const { 214 return _M_t.upper_bound(__x); 215 } 216 pair<iterator,iterator> equal_range(const key_type& __x) const { 217 return _M_t.equal_range(__x); 218 } 219 #endif 220 221 template <class _K1, class _C1, class _A1> 222 friend bool operator== (const multiset<_K1,_C1,_A1>&, 223 const multiset<_K1,_C1,_A1>&); 224 template <class _K1, class _C1, class _A1> 225 friend bool operator< (const multiset<_K1,_C1,_A1>&, 226 const multiset<_K1,_C1,_A1>&); 227 }; 228 229 template <class _Key, class _Compare, class _Alloc> 230 inline bool operator==(const multiset<_Key,_Compare,_Alloc>& __x, 231 const multiset<_Key,_Compare,_Alloc>& __y) { 232 return __x._M_t == __y._M_t; 233 } 234 235 template <class _Key, class _Compare, class _Alloc> 236 inline bool operator<(const multiset<_Key,_Compare,_Alloc>& __x, 237 const multiset<_Key,_Compare,_Alloc>& __y) { 238 return __x._M_t < __y._M_t; 239 } 240 241 template <class _Key, class _Compare, class _Alloc> 242 inline bool operator!=(const multiset<_Key,_Compare,_Alloc>& __x, 243 const multiset<_Key,_Compare,_Alloc>& __y) { 244 return !(__x == __y); 245 } 246 247 template <class _Key, class _Compare, class _Alloc> 248 inline bool operator>(const multiset<_Key,_Compare,_Alloc>& __x, 249 const multiset<_Key,_Compare,_Alloc>& __y) { 250 return __y < __x; 251 } 252 253 template <class _Key, class _Compare, class _Alloc> 254 inline bool operator<=(const multiset<_Key,_Compare,_Alloc>& __x, 255 const multiset<_Key,_Compare,_Alloc>& __y) { 256 return !(__y < __x); 257 } 258 259 template <class _Key, class _Compare, class _Alloc> 260 inline bool operator>=(const multiset<_Key,_Compare,_Alloc>& __x, 261 const multiset<_Key,_Compare,_Alloc>& __y) { 262 return !(__x < __y); 263 } 264 265 template <class _Key, class _Compare, class _Alloc> 266 inline void swap(multiset<_Key,_Compare,_Alloc>& __x, 267 multiset<_Key,_Compare,_Alloc>& __y) { 268 __x.swap(__y); 269 } 270 271 } // namespace std 272 273 #endif /* __GLIBCPP_INTERNAL_MULTISET_H */ 274 275 // Local Variables: 276 // mode:C++ 277 // End: 278