OSDN Git Service

4f27c3f375dfcf1f12b235b6e3ed1a98caf539cf
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / debug / unordered_set
1 // Debugging unordered_set/unordered_multiset implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /** @file debug/unordered_set
27  *  This file is a GNU debug extension to the Standard C++ Library.
28  */
29
30 #ifndef _GLIBCXX_DEBUG_UNORDERED_SET
31 #define _GLIBCXX_DEBUG_UNORDERED_SET 1
32
33 #ifdef __GXX_EXPERIMENTAL_CXX0X__
34 # include <unordered_set>
35 #else
36 # include <c++0x_warning.h>
37 #endif
38
39 #include <debug/safe_sequence.h>
40 #include <debug/safe_iterator.h>
41 #include <initializer_list>
42
43 namespace std
44 {
45 namespace __debug
46 {
47   /// Class std::unordered_set with safety/checking/debug instrumentation.
48   template<typename _Value,
49            typename _Hash = std::hash<_Value>,
50            typename _Pred = std::equal_to<_Value>,
51            typename _Alloc = std::allocator<_Value> >
52     class unordered_set
53     : public _GLIBCXX_STD_D::unordered_set<_Value, _Hash, _Pred, _Alloc>,
54       public __gnu_debug::_Safe_sequence<unordered_set<_Value, _Hash,
55                                                        _Pred, _Alloc> >
56     {
57       typedef _GLIBCXX_STD_D::unordered_set<_Value, _Hash,
58                                             _Pred, _Alloc> _Base;
59       typedef __gnu_debug::_Safe_sequence<unordered_set> _Safe_base;
60
61     public:
62       typedef typename _Base::size_type       size_type;
63       typedef typename _Base::hasher          hasher;
64       typedef typename _Base::key_equal       key_equal;
65       typedef typename _Base::allocator_type  allocator_type;
66
67       typedef typename _Base::key_type        key_type;
68       typedef typename _Base::value_type      value_type;
69
70       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
71                                           unordered_set> iterator;
72       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
73                                           unordered_set> const_iterator;
74
75       explicit
76       unordered_set(size_type __n = 10,
77                     const hasher& __hf = hasher(),
78                     const key_equal& __eql = key_equal(),
79                     const allocator_type& __a = allocator_type())
80       : _Base(__n, __hf, __eql, __a) { }
81
82       template<typename _InputIterator>
83         unordered_set(_InputIterator __f, _InputIterator __l, 
84                       size_type __n = 10,
85                       const hasher& __hf = hasher(), 
86                       const key_equal& __eql = key_equal(), 
87                       const allocator_type& __a = allocator_type())
88         : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
89                 __hf, __eql, __a), _Safe_base() { }
90
91       unordered_set(const unordered_set& __x) 
92       : _Base(__x), _Safe_base() { }
93
94       unordered_set(const _Base& __x) 
95       : _Base(__x), _Safe_base() { }
96
97       unordered_set(unordered_set&& __x) 
98       : _Base(std::forward<unordered_set>(__x)), _Safe_base() { }
99
100       unordered_set(initializer_list<value_type> __l,
101                     size_type __n = 10,
102                     const hasher& __hf = hasher(),
103                     const key_equal& __eql = key_equal(),
104                     const allocator_type& __a = allocator_type())
105       : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
106
107       unordered_set&
108       operator=(const unordered_set& __x)
109       {
110         *static_cast<_Base*>(this) = __x;
111         this->_M_invalidate_all();
112         return *this;
113       }
114
115       unordered_set&
116       operator=(unordered_set&& __x)
117       {
118         // NB: DR 675.
119         clear();
120         swap(__x);
121         return *this;
122       }
123
124       unordered_set&
125       operator=(initializer_list<value_type> __l)
126       {
127         this->clear();
128         this->insert(__l);
129         return *this;
130       }
131
132       void
133       swap(unordered_set& __x)
134       {
135         _Base::swap(__x);
136         _Safe_base::_M_swap(__x);
137       }
138
139       void
140       clear()
141       {
142         _Base::clear();
143         this->_M_invalidate_all();
144       }
145
146       iterator 
147       begin()
148       { return iterator(_Base::begin(), this); }
149
150       const_iterator
151       begin() const
152       { return const_iterator(_Base::begin(), this); }
153
154       iterator
155       end()
156       { return iterator(_Base::end(), this); }
157
158       const_iterator
159       end() const
160       { return const_iterator(_Base::end(), this); }
161
162       const_iterator
163       cbegin() const
164       { return const_iterator(_Base::begin(), this); }
165
166       const_iterator
167       cend() const
168       { return const_iterator(_Base::end(), this); }
169
170       // local versions
171       using _Base::begin;
172       using _Base::end;
173       using _Base::cbegin;
174       using _Base::cend;
175
176       std::pair<iterator, bool>
177       insert(const value_type& __obj)
178       {
179         typedef std::pair<typename _Base::iterator, bool> __pair_type;
180         __pair_type __res = _Base::insert(__obj);
181         return std::make_pair(iterator(__res.first, this), __res.second);
182       }
183
184       iterator
185       insert(iterator, const value_type& __obj)
186       {
187         typedef std::pair<typename _Base::iterator, bool> __pair_type;
188         __pair_type __res = _Base::insert(__obj);
189         return iterator(__res.first, this);
190       }
191
192       const_iterator
193       insert(const_iterator, const value_type& __obj)
194       {
195         typedef std::pair<typename _Base::iterator, bool> __pair_type;
196         __pair_type __res = _Base::insert(__obj);
197         return const_iterator(__res.first, this);
198       }
199
200       void
201       insert(std::initializer_list<value_type> __l)
202       { _Base::insert(__l); }
203
204       template<typename _InputIterator>
205         void
206         insert(_InputIterator __first, _InputIterator __last)
207         {
208           __glibcxx_check_valid_range(__first, __last);
209           _Base::insert(__first, __last);
210         }
211
212       iterator
213       find(const key_type& __key)
214       { return iterator(_Base::find(__key), this); }
215
216       const_iterator
217       find(const key_type& __key) const
218       { return const_iterator(_Base::find(__key), this); }
219
220       std::pair<iterator, iterator>
221       equal_range(const key_type& __key)
222       {
223         typedef typename _Base::iterator _Base_iterator;
224         typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
225         __pair_type __res = _Base::equal_range(__key);
226         return std::make_pair(iterator(__res.first, this),
227                               iterator(__res.second, this));
228       }
229
230       std::pair<const_iterator, const_iterator>
231       equal_range(const key_type& __key) const
232       {
233         typedef typename _Base::const_iterator _Base_iterator;
234         typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
235         __pair_type __res = _Base::equal_range(__key);
236         return std::make_pair(const_iterator(__res.first, this),
237                               const_iterator(__res.second, this));
238       }
239
240       size_type
241       erase(const key_type& __key)
242       {
243         size_type __ret(0);
244         iterator __victim(_Base::find(__key), this);
245         if (__victim != end())
246           {
247             this->erase(__victim);
248             __ret = 1;
249           }
250         return __ret;
251       }
252
253       iterator
254       erase(iterator __it)
255       {
256         __glibcxx_check_erase(__it);
257         __it._M_invalidate();
258         return iterator(_Base::erase(__it.base()), this);
259       }
260
261       const_iterator
262       erase(const_iterator __it)
263       {
264         __glibcxx_check_erase(__it);
265         __it._M_invalidate();
266         return const_iterator(_Base::erase(__it.base()), this);
267       }
268
269       iterator
270       erase(iterator __first, iterator __last)
271       {
272         __glibcxx_check_erase_range(__first, __last);
273         for (iterator __tmp = __first; __tmp != __last;)
274         {
275           iterator __victim = __tmp++;
276           __victim._M_invalidate();
277         }
278         return iterator(_Base::erase(__first.base(),
279                                      __last.base()), this);
280       }
281
282       const_iterator
283       erase(const_iterator __first, const_iterator __last)
284       {
285         __glibcxx_check_erase_range(__first, __last);
286         for (const_iterator __tmp = __first; __tmp != __last;)
287         {
288           const_iterator __victim = __tmp++;
289           __victim._M_invalidate();
290         }
291         return const_iterator(_Base::erase(__first.base(),
292                                            __last.base()), this);
293       }
294
295       _Base&
296       _M_base() { return *this; }
297
298       const _Base&
299       _M_base() const { return *this; }
300
301     private:
302       void
303       _M_invalidate_all()
304       {
305         typedef typename _Base::const_iterator _Base_const_iterator;
306         typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
307         this->_M_invalidate_if(_Not_equal(_M_base().end()));
308       }
309     };
310
311   template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
312     inline void
313     swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
314          unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
315     { __x.swap(__y); }
316
317
318   /// Class std::unordered_multiset with safety/checking/debug instrumentation.
319   template<typename _Value,
320            typename _Hash = std::hash<_Value>,
321            typename _Pred = std::equal_to<_Value>,
322            typename _Alloc = std::allocator<_Value> >
323     class unordered_multiset
324     : public _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash, _Pred, _Alloc>,
325       public __gnu_debug::_Safe_sequence<unordered_multiset<_Value, _Hash,
326                                                             _Pred, _Alloc> >
327     {
328       typedef _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash,
329                                                  _Pred, _Alloc> _Base;
330       typedef __gnu_debug::_Safe_sequence<unordered_multiset> _Safe_base;
331
332     public:
333       typedef typename _Base::size_type       size_type;
334       typedef typename _Base::hasher          hasher;
335       typedef typename _Base::key_equal       key_equal;
336       typedef typename _Base::allocator_type  allocator_type;
337
338       typedef typename _Base::key_type        key_type;
339       typedef typename _Base::value_type      value_type;
340
341       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
342                                           unordered_multiset> iterator;
343       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
344                                           unordered_multiset> const_iterator;
345
346       explicit
347       unordered_multiset(size_type __n = 10,
348                          const hasher& __hf = hasher(),
349                          const key_equal& __eql = key_equal(),
350                          const allocator_type& __a = allocator_type())
351       : _Base(__n, __hf, __eql, __a) { }
352
353       template<typename _InputIterator>
354         unordered_multiset(_InputIterator __f, _InputIterator __l, 
355                            size_type __n = 10,
356                            const hasher& __hf = hasher(), 
357                            const key_equal& __eql = key_equal(), 
358                            const allocator_type& __a = allocator_type())
359         : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
360                 __hf, __eql, __a), _Safe_base() { }
361
362       unordered_multiset(const unordered_multiset& __x) 
363       : _Base(__x), _Safe_base() { }
364
365       unordered_multiset(const _Base& __x) 
366       : _Base(__x), _Safe_base() { }
367
368       unordered_multiset(unordered_multiset&& __x) 
369       : _Base(std::forward<unordered_multiset>(__x)), _Safe_base() { }
370
371       unordered_multiset(initializer_list<value_type> __l,
372                          size_type __n = 10,
373                          const hasher& __hf = hasher(),
374                          const key_equal& __eql = key_equal(),
375                          const allocator_type& __a = allocator_type())
376       : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
377
378       unordered_multiset&
379       operator=(const unordered_multiset& __x)
380       {
381         *static_cast<_Base*>(this) = __x;
382         this->_M_invalidate_all();
383         return *this;
384       }
385
386       unordered_multiset&
387       operator=(unordered_multiset&& __x)
388       {
389         // NB: DR 675.
390         clear();
391         swap(__x);
392         return *this;
393       }
394
395       unordered_multiset&
396       operator=(initializer_list<value_type> __l)
397       {
398         this->clear();
399         this->insert(__l);
400         return *this;
401       }
402
403       void
404       swap(unordered_multiset& __x)
405       {
406         _Base::swap(__x);
407         _Safe_base::_M_swap(__x);
408       }
409
410       void
411       clear()
412       {
413         _Base::clear();
414         this->_M_invalidate_all();
415       }
416
417       iterator
418       begin()
419       { return iterator(_Base::begin(), this); }
420
421       const_iterator
422       begin() const
423       { return const_iterator(_Base::begin(), this); }
424
425       iterator
426       end()
427       { return iterator(_Base::end(), this); }
428
429       const_iterator
430       end() const
431       { return const_iterator(_Base::end(), this); }
432
433       const_iterator
434       cbegin() const
435       { return const_iterator(_Base::begin(), this); }
436
437       const_iterator
438       cend() const
439       { return const_iterator(_Base::end(), this); }
440
441       // local versions
442       using _Base::begin;
443       using _Base::end;
444       using _Base::cbegin;
445       using _Base::cend;
446
447       iterator
448       insert(const value_type& __obj)
449       { return iterator(_Base::insert(__obj), this); }
450
451       iterator
452       insert(iterator, const value_type& __obj)
453       { return iterator(_Base::insert(__obj), this); }
454
455       const_iterator
456       insert(const_iterator, const value_type& __obj)
457       { return const_iterator(_Base::insert(__obj), this); }
458
459       void
460       insert(std::initializer_list<value_type> __l)
461       { _Base::insert(__l); }
462
463       template<typename _InputIterator>
464         void
465         insert(_InputIterator __first, _InputIterator __last)
466         {
467           __glibcxx_check_valid_range(__first, __last);
468           _Base::insert(__first, __last);
469         }
470
471       iterator
472       find(const key_type& __key)
473       { return iterator(_Base::find(__key), this); }
474
475       const_iterator
476       find(const key_type& __key) const
477       { return const_iterator(_Base::find(__key), this); }
478
479       std::pair<iterator, iterator>
480       equal_range(const key_type& __key)
481       {
482         typedef typename _Base::iterator _Base_iterator;
483         typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
484         __pair_type __res = _Base::equal_range(__key);
485         return std::make_pair(iterator(__res.first, this),
486                               iterator(__res.second, this));
487       }
488
489       std::pair<const_iterator, const_iterator>
490       equal_range(const key_type& __key) const
491       {
492         typedef typename _Base::const_iterator _Base_iterator;
493         typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
494         __pair_type __res = _Base::equal_range(__key);
495         return std::make_pair(const_iterator(__res.first, this),
496                               const_iterator(__res.second, this));
497       }
498
499       size_type
500       erase(const key_type& __key)
501       {
502         size_type __ret(0);
503         iterator __victim(_Base::find(__key), this);
504         if (__victim != end())
505           {
506             this->erase(__victim);
507             __ret = 1;
508           }
509         return __ret;
510       }
511
512       iterator
513       erase(iterator __it)
514       {
515         __glibcxx_check_erase(__it);
516         __it._M_invalidate();
517         return iterator(_Base::erase(__it.base()), this);
518       }
519
520       const_iterator
521       erase(const_iterator __it)
522       {
523         __glibcxx_check_erase(__it);
524         __it._M_invalidate();
525         return const_iterator(_Base::erase(__it.base()), this);
526       }
527
528       iterator
529       erase(iterator __first, iterator __last)
530       {
531         __glibcxx_check_erase_range(__first, __last);
532         for (iterator __tmp = __first; __tmp != __last;)
533         {
534           iterator __victim = __tmp++;
535           __victim._M_invalidate();
536         }
537         return iterator(_Base::erase(__first.base(),
538                                      __last.base()), this);
539       }
540
541       const_iterator
542       erase(const_iterator __first, const_iterator __last)
543       {
544         __glibcxx_check_erase_range(__first, __last);
545         for (const_iterator __tmp = __first; __tmp != __last;)
546         {
547           const_iterator __victim = __tmp++;
548           __victim._M_invalidate();
549         }
550         return const_iterator(_Base::erase(__first.base(),
551                                            __last.base()), this);
552       }
553
554       _Base&
555       _M_base() { return *this; }
556
557       const _Base&
558       _M_base() const { return *this; }
559
560     private:
561       void
562       _M_invalidate_all()
563       {
564         typedef typename _Base::const_iterator _Base_const_iterator;
565         typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
566         this->_M_invalidate_if(_Not_equal(_M_base().end()));
567       }
568     };
569
570   template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
571     inline void
572     swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
573          unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
574     { __x.swap(__y); }
575
576 } // namespace __debug
577 } // namespace std
578
579 #endif