| 1 | // Debugging hash_multiset implementation -*- C++ -*- |
|---|
| 2 | |
|---|
| 3 | // Copyright (C) 2003 |
|---|
| 4 | // Free Software Foundation, Inc. |
|---|
| 5 | // |
|---|
| 6 | // This file is part of the GNU ISO C++ Library. This library is free |
|---|
| 7 | // software; you can redistribute it and/or modify it under the |
|---|
| 8 | // terms of the GNU General Public License as published by the |
|---|
| 9 | // Free Software Foundation; either version 2, or (at your option) |
|---|
| 10 | // any later version. |
|---|
| 11 | |
|---|
| 12 | // This library is distributed in the hope that it will be useful, |
|---|
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | // GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | // You should have received a copy of the GNU General Public License along |
|---|
| 18 | // with this library; see the file COPYING. If not, write to the Free |
|---|
| 19 | // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
|---|
| 20 | // USA. |
|---|
| 21 | |
|---|
| 22 | // As a special exception, you may use this file as part of a free software |
|---|
| 23 | // library without restriction. Specifically, if other files instantiate |
|---|
| 24 | // templates or use macros or inline functions from this file, or you compile |
|---|
| 25 | // this file and link it with other files to produce an executable, this |
|---|
| 26 | // file does not by itself cause the resulting executable to be covered by |
|---|
| 27 | // the GNU General Public License. This exception does not however |
|---|
| 28 | // invalidate any other reasons why the executable file might be covered by |
|---|
| 29 | // the GNU General Public License. |
|---|
| 30 | |
|---|
| 31 | #ifndef _GLIBCXX_DEBUG_HASH_MULTISET_H |
|---|
| 32 | #define _GLIBCXX_DEBUG_HASH_MULTISET_H 1 |
|---|
| 33 | |
|---|
| 34 | #include <debug/safe_sequence.h> |
|---|
| 35 | #include <debug/safe_iterator.h> |
|---|
| 36 | |
|---|
| 37 | namespace __gnu_debug_def |
|---|
| 38 | { |
|---|
| 39 | template<typename _Value, |
|---|
| 40 | typename _HashFcn = __gnu_cxx::hash<_Value>, |
|---|
| 41 | typename _EqualKey = std::equal_to<_Value>, |
|---|
| 42 | typename _Alloc = std::allocator<_Value> > |
|---|
| 43 | class hash_multiset |
|---|
| 44 | : public __gnu_cxx::hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>, |
|---|
| 45 | public __gnu_debug::_Safe_sequence<hash_multiset<_Value, _HashFcn, |
|---|
| 46 | _EqualKey, _Alloc> > |
|---|
| 47 | { |
|---|
| 48 | typedef __gnu_cxx:: hash_multiset<_Value,_HashFcn, _EqualKey,_Alloc> |
|---|
| 49 | _Base; |
|---|
| 50 | typedef __gnu_debug::_Safe_sequence<hash_multiset> _Safe_base; |
|---|
| 51 | |
|---|
| 52 | public: |
|---|
| 53 | typedef typename _Base::key_type key_type; |
|---|
| 54 | typedef typename _Base::value_type value_type; |
|---|
| 55 | typedef typename _Base::hasher hasher; |
|---|
| 56 | typedef typename _Base::key_equal key_equal; |
|---|
| 57 | typedef typename _Base::size_type size_type; |
|---|
| 58 | typedef typename _Base::difference_type difference_type; |
|---|
| 59 | typedef typename _Base::pointer pointer; |
|---|
| 60 | typedef typename _Base::const_pointer const_pointer; |
|---|
| 61 | typedef typename _Base::reference reference; |
|---|
| 62 | typedef typename _Base::const_reference const_reference; |
|---|
| 63 | |
|---|
| 64 | typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, |
|---|
| 65 | hash_multiset> iterator; |
|---|
| 66 | typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, |
|---|
| 67 | hash_multiset> const_iterator; |
|---|
| 68 | |
|---|
| 69 | typedef typename _Base::allocator_type allocator_type; |
|---|
| 70 | |
|---|
| 71 | using _Base::hash_funct; |
|---|
| 72 | using _Base::key_eq; |
|---|
| 73 | using _Base::get_allocator; |
|---|
| 74 | |
|---|
| 75 | hash_multiset() { } |
|---|
| 76 | |
|---|
| 77 | explicit hash_multiset(size_type __n) : _Base(__n) { } |
|---|
| 78 | |
|---|
| 79 | hash_multiset(size_type __n, const hasher& __hf) : _Base(__n, __hf) { } |
|---|
| 80 | |
|---|
| 81 | hash_multiset(size_type __n, const hasher& __hf, const key_equal& __eql, |
|---|
| 82 | const allocator_type& __a = allocator_type()) |
|---|
| 83 | : _Base(__n, __hf, __eql, __a) |
|---|
| 84 | { } |
|---|
| 85 | |
|---|
| 86 | template<typename _InputIterator> |
|---|
| 87 | hash_multiset(_InputIterator __f, _InputIterator __l) |
|---|
| 88 | : _Base(__gnu_debug::__check_valid_range(__f, __l), __l) |
|---|
| 89 | { } |
|---|
| 90 | |
|---|
| 91 | template<typename _InputIterator> |
|---|
| 92 | hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n) |
|---|
| 93 | : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n) |
|---|
| 94 | { } |
|---|
| 95 | |
|---|
| 96 | template<typename _InputIterator> |
|---|
| 97 | hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n, |
|---|
| 98 | const hasher& __hf) |
|---|
| 99 | : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf) |
|---|
| 100 | { } |
|---|
| 101 | |
|---|
| 102 | template<typename _InputIterator> |
|---|
| 103 | hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n, |
|---|
| 104 | const hasher& __hf, const key_equal& __eql, |
|---|
| 105 | const allocator_type& __a = allocator_type()) |
|---|
| 106 | : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf, |
|---|
| 107 | __eql, __a) |
|---|
| 108 | { } |
|---|
| 109 | |
|---|
| 110 | hash_multiset(const _Base& __x) : _Base(__x), _Safe_base() { } |
|---|
| 111 | |
|---|
| 112 | using _Base::size; |
|---|
| 113 | using _Base::max_size; |
|---|
| 114 | using _Base::empty; |
|---|
| 115 | |
|---|
| 116 | void |
|---|
| 117 | swap(hash_multiset& __x) |
|---|
| 118 | { |
|---|
| 119 | _Base::swap(__x); |
|---|
| 120 | this->_M_swap(__x); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | iterator begin() const { return iterator(_Base::begin(), this); } |
|---|
| 124 | iterator end() const { return iterator(_Base::end(), this); } |
|---|
| 125 | |
|---|
| 126 | iterator |
|---|
| 127 | insert(const value_type& __obj) |
|---|
| 128 | { return iterator(_Base::insert(__obj), this); } |
|---|
| 129 | |
|---|
| 130 | template <typename _InputIterator> |
|---|
| 131 | void |
|---|
| 132 | insert(_InputIterator __first, _InputIterator __last) |
|---|
| 133 | { |
|---|
| 134 | __glibcxx_check_valid_range(__first, __last); |
|---|
| 135 | _Base::insert(__first.base(), __last.base()); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | iterator |
|---|
| 140 | insert_noresize(const value_type& __obj) |
|---|
| 141 | { return iterator(_Base::insert_noresize(__obj), this); } |
|---|
| 142 | |
|---|
| 143 | iterator |
|---|
| 144 | find(const key_type& __key) const |
|---|
| 145 | { return iterator(_Base::find(__key), this); } |
|---|
| 146 | |
|---|
| 147 | using _Base::count; |
|---|
| 148 | |
|---|
| 149 | std::pair<iterator, iterator> |
|---|
| 150 | equal_range(const key_type& __key) const |
|---|
| 151 | { |
|---|
| 152 | typedef typename _Base::iterator _Base_iterator; |
|---|
| 153 | std::pair<_Base_iterator, _Base_iterator> __res = |
|---|
| 154 | _Base::equal_range(__key); |
|---|
| 155 | return std::make_pair(iterator(__res.first, this), |
|---|
| 156 | iterator(__res.second, this)); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | size_type |
|---|
| 160 | erase(const key_type& __key) |
|---|
| 161 | { |
|---|
| 162 | size_type __count = 0; |
|---|
| 163 | std::pair<iterator, iterator> __victims = this->equal_range(__key); |
|---|
| 164 | while (__victims.first != __victims.second) |
|---|
| 165 | { |
|---|
| 166 | this->erase(__victims++); |
|---|
| 167 | ++__count; |
|---|
| 168 | } |
|---|
| 169 | return __count; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | void |
|---|
| 173 | erase(iterator __it) |
|---|
| 174 | { |
|---|
| 175 | __glibcxx_check_erase(__it); |
|---|
| 176 | __it._M_invalidate(); |
|---|
| 177 | _Base::erase(__it.base()); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | void |
|---|
| 181 | erase(iterator __first, iterator __last) |
|---|
| 182 | { |
|---|
| 183 | __glibcxx_check_erase_range(__first, __last); |
|---|
| 184 | for (iterator __tmp = __first; __tmp != __last;) |
|---|
| 185 | { |
|---|
| 186 | iterator __victim = __tmp++; |
|---|
| 187 | __victim._M_invalidate(); |
|---|
| 188 | } |
|---|
| 189 | _Base::erase(__first.base(), __last.base()); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | void |
|---|
| 193 | clear() |
|---|
| 194 | { |
|---|
| 195 | _Base::clear(); |
|---|
| 196 | this->_M_invalidate_all(); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | using _Base::resize; |
|---|
| 200 | using _Base::bucket_count; |
|---|
| 201 | using _Base::max_bucket_count; |
|---|
| 202 | using _Base::elems_in_bucket; |
|---|
| 203 | |
|---|
| 204 | _Base& _M_base() { return *this; } |
|---|
| 205 | const _Base& _M_base() const { return *this; } |
|---|
| 206 | |
|---|
| 207 | private: |
|---|
| 208 | void |
|---|
| 209 | _M_invalidate_all() |
|---|
| 210 | { |
|---|
| 211 | typedef typename _Base::const_iterator _Base_const_iterator; |
|---|
| 212 | typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal; |
|---|
| 213 | this->_M_invalidate_if(_Not_equal(_M_base().end())); |
|---|
| 214 | } |
|---|
| 215 | }; |
|---|
| 216 | |
|---|
| 217 | template<typename _Value, typename _HashFcn, typename _EqualKey, typename _Alloc> |
|---|
| 218 | inline bool |
|---|
| 219 | operator==(const hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>& __x, |
|---|
| 220 | const hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>& __y) |
|---|
| 221 | { return __x._M_base() == __y._M_base(); } |
|---|
| 222 | |
|---|
| 223 | template<typename _Value, typename _HashFcn, typename _EqualKey, typename _Alloc> |
|---|
| 224 | inline bool |
|---|
| 225 | operator!=(const hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>& __x, |
|---|
| 226 | const hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>& __y) |
|---|
| 227 | { return __x._M_base() != __y._M_base(); } |
|---|
| 228 | |
|---|
| 229 | template<typename _Value, typename _HashFcn, typename _EqualKey, typename _Alloc> |
|---|
| 230 | inline void |
|---|
| 231 | swap(hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>& __x, |
|---|
| 232 | hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>& __y) |
|---|
| 233 | { __x.swap(__y); } |
|---|
| 234 | } // namespace __gnu_debug_def |
|---|
| 235 | |
|---|
| 236 | #endif |
|---|