]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libstdc++/include/debug/hash_multiset.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / libstdc++ / include / debug / hash_multiset.h
1 // Debugging hash_multiset implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2005, 2006
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 /** @file debug/hash_multiset.h
32  *  This file is a GNU debug extension to the Standard C++ Library.
33  */
34
35 #ifndef _GLIBCXX_DEBUG_HASH_MULTISET_H
36 #define _GLIBCXX_DEBUG_HASH_MULTISET_H 1
37
38 #include <debug/safe_sequence.h>
39 #include <debug/safe_iterator.h>
40
41 namespace __gnu_cxx
42 {
43 namespace __debug
44 {
45   template<typename _Value,
46            typename _HashFcn  = __gnu_cxx::hash<_Value>,
47            typename _EqualKey = std::equal_to<_Value>,
48            typename _Alloc =  std::allocator<_Value> >
49     class hash_multiset
50     : public _GLIBCXX_EXT::hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>,
51       public __gnu_debug::_Safe_sequence<hash_multiset<_Value, _HashFcn,
52                                                        _EqualKey, _Alloc> >
53     {
54       typedef _GLIBCXX_EXT:: hash_multiset<_Value,_HashFcn, _EqualKey,_Alloc>
55                                                         _Base;
56       typedef __gnu_debug::_Safe_sequence<hash_multiset> _Safe_base;
57
58   public:
59     typedef typename _Base::key_type                    key_type;
60     typedef typename _Base::value_type                  value_type;
61     typedef typename _Base::hasher                      hasher;
62     typedef typename _Base::key_equal                   key_equal;
63     typedef typename _Base::size_type                   size_type;
64     typedef typename _Base::difference_type             difference_type;
65     typedef typename _Base::pointer                     pointer;
66     typedef typename _Base::const_pointer               const_pointer;
67     typedef typename _Base::reference                   reference;
68     typedef typename _Base::const_reference             const_reference;
69
70     typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
71                                          hash_multiset> iterator;
72     typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
73                                          hash_multiset> const_iterator;
74
75     typedef typename _Base::allocator_type              allocator_type;
76
77     using _Base::hash_funct;
78     using _Base::key_eq;
79     using _Base::get_allocator;
80
81     hash_multiset() { }
82
83     explicit hash_multiset(size_type __n) : _Base(__n) { }
84
85     hash_multiset(size_type __n, const hasher& __hf) : _Base(__n, __hf) { }
86
87     hash_multiset(size_type __n, const hasher& __hf, const key_equal& __eql,
88                   const allocator_type& __a = allocator_type())
89     : _Base(__n, __hf, __eql, __a)
90     { }
91
92     template<typename _InputIterator>
93       hash_multiset(_InputIterator __f, _InputIterator __l)
94       : _Base(__gnu_debug::__check_valid_range(__f, __l), __l)
95       { }
96
97     template<typename _InputIterator>
98       hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n)
99       : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n)
100       { }
101
102     template<typename _InputIterator>
103       hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n,
104                     const hasher& __hf)
105       : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf)
106       { }
107
108     template<typename _InputIterator>
109       hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n,
110                     const hasher& __hf, const key_equal& __eql,
111                     const allocator_type& __a = allocator_type())
112       : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf,
113               __eql, __a)
114       { }
115
116     hash_multiset(const _Base& __x) : _Base(__x), _Safe_base() { }
117
118     using _Base::size;
119     using _Base::max_size;
120     using _Base::empty;
121
122     void
123     swap(hash_multiset& __x)
124     {
125       _Base::swap(__x);
126       this->_M_swap(__x);
127     }
128
129     iterator begin() const { return iterator(_Base::begin(), this); }
130     iterator end() const   { return iterator(_Base::end(),   this); }
131
132     iterator
133     insert(const value_type& __obj)
134     { return iterator(_Base::insert(__obj), this); }
135
136     template <typename _InputIterator>
137       void
138       insert(_InputIterator __first, _InputIterator __last)
139       {
140         __glibcxx_check_valid_range(__first, __last);
141         _Base::insert(__first.base(), __last.base());
142       }
143
144
145     iterator
146     insert_noresize(const value_type& __obj)
147     { return iterator(_Base::insert_noresize(__obj), this); }
148
149     iterator
150     find(const key_type& __key) const
151     { return iterator(_Base::find(__key), this); }
152
153     using _Base::count;
154
155     std::pair<iterator, iterator>
156     equal_range(const key_type& __key) const
157     {
158       typedef typename _Base::iterator _Base_iterator;
159       std::pair<_Base_iterator, _Base_iterator> __res =
160         _Base::equal_range(__key);
161       return std::make_pair(iterator(__res.first, this),
162                             iterator(__res.second, this));
163     }
164
165     size_type
166     erase(const key_type& __key)
167     {
168       size_type __count = 0;
169       std::pair<iterator, iterator> __victims = this->equal_range(__key);
170       while (__victims.first != __victims.second)
171         {
172           this->erase(__victims++);
173           ++__count;
174         }
175       return __count;
176     }
177
178     void
179     erase(iterator __it)
180     {
181       __glibcxx_check_erase(__it);
182       __it._M_invalidate();
183       _Base::erase(__it.base());
184     }
185
186     void
187     erase(iterator __first, iterator __last)
188     {
189       __glibcxx_check_erase_range(__first, __last);
190       for (iterator __tmp = __first; __tmp != __last;)
191         {
192           iterator __victim = __tmp++;
193           __victim._M_invalidate();
194         }
195       _Base::erase(__first.base(), __last.base());
196     }
197
198     void
199     clear()
200     {
201       _Base::clear();
202       this->_M_invalidate_all();
203     }
204
205     using _Base::resize;
206     using _Base::bucket_count;
207     using _Base::max_bucket_count;
208     using _Base::elems_in_bucket;
209
210     _Base&       _M_base()       { return *this; }
211     const _Base& _M_base() const { return *this; }
212
213   private:
214     void
215     _M_invalidate_all()
216     {
217       typedef typename _Base::const_iterator _Base_const_iterator;
218       typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
219       this->_M_invalidate_if(_Not_equal(_M_base().end()));
220     }
221   };
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 bool
231   operator!=(const hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>& __x,
232              const hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>& __y)
233   { return __x._M_base() != __y._M_base(); }
234
235 template<typename _Value, typename _HashFcn, typename _EqualKey, typename _Alloc>
236   inline void
237   swap(hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>& __x,
238        hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>& __y)
239   { __x.swap(__y); }
240 } // namespace __debug
241 } // namespace __gnu_cxx
242
243 #endif