OSDN Git Service

2010-01-03 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / debug / multiset.h
1 // Debugging multiset implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010
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/multiset.h
27  *  This file is a GNU debug extension to the Standard C++ Library.
28  */
29
30 #ifndef _GLIBCXX_DEBUG_MULTISET_H
31 #define _GLIBCXX_DEBUG_MULTISET_H 1
32
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_iterator.h>
35 #include <utility>
36
37 namespace std
38 {
39 namespace __debug
40 {
41   /// Class std::multiset with safety/checking/debug instrumentation.
42   template<typename _Key, typename _Compare = std::less<_Key>,
43            typename _Allocator = std::allocator<_Key> >
44     class multiset
45     : public _GLIBCXX_STD_D::multiset<_Key, _Compare, _Allocator>,
46       public __gnu_debug::_Safe_sequence<multiset<_Key, _Compare, _Allocator> >
47     {
48       typedef _GLIBCXX_STD_D::multiset<_Key, _Compare, _Allocator> _Base;
49       typedef __gnu_debug::_Safe_sequence<multiset> _Safe_base;
50
51     public:
52       // types:
53       typedef _Key                                   key_type;
54       typedef _Key                                   value_type;
55       typedef _Compare                               key_compare;
56       typedef _Compare                               value_compare;
57       typedef _Allocator                             allocator_type;
58       typedef typename _Base::reference              reference;
59       typedef typename _Base::const_reference        const_reference;
60
61       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, multiset>
62       iterator;
63       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
64                                           multiset> const_iterator;
65
66       typedef typename _Base::size_type              size_type;
67       typedef typename _Base::difference_type        difference_type;
68       typedef typename _Base::pointer                pointer;
69       typedef typename _Base::const_pointer          const_pointer;
70       typedef std::reverse_iterator<iterator>        reverse_iterator;
71       typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
72
73       // 23.3.3.1 construct/copy/destroy:
74       explicit multiset(const _Compare& __comp = _Compare(),
75                         const _Allocator& __a = _Allocator())
76       : _Base(__comp, __a) { }
77
78       template<typename _InputIterator>
79         multiset(_InputIterator __first, _InputIterator __last,
80                  const _Compare& __comp = _Compare(),
81                  const _Allocator& __a = _Allocator())
82         : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
83                 __comp, __a) { }
84
85       multiset(const multiset& __x)
86       : _Base(__x), _Safe_base() { }
87
88       multiset(const _Base& __x)
89       : _Base(__x), _Safe_base() { }
90
91 #ifdef __GXX_EXPERIMENTAL_CXX0X__
92       multiset(multiset&& __x)
93       : _Base(std::forward<multiset>(__x)), _Safe_base()
94       { this->_M_swap(__x); }
95
96       multiset(initializer_list<value_type> __l,
97                const _Compare& __comp = _Compare(),
98                const allocator_type& __a = allocator_type())
99       : _Base(__l, __comp, __a), _Safe_base() { }
100 #endif
101
102       ~multiset() { }
103
104       multiset&
105       operator=(const multiset& __x)
106       {
107         *static_cast<_Base*>(this) = __x;
108         this->_M_invalidate_all();
109         return *this;
110       }
111
112 #ifdef __GXX_EXPERIMENTAL_CXX0X__
113       multiset&
114       operator=(multiset&& __x)
115       {
116         // NB: DR 675.
117         clear();
118         swap(__x);
119         return *this;
120       }
121
122       multiset&
123       operator=(initializer_list<value_type> __l)
124       {
125         this->clear();
126         this->insert(__l);
127         return *this;
128       }
129 #endif
130
131       using _Base::get_allocator;
132
133       // iterators:
134       iterator
135       begin()
136       { return iterator(_Base::begin(), this); }
137
138       const_iterator
139       begin() const
140       { return const_iterator(_Base::begin(), this); }
141
142       iterator
143       end()
144       { return iterator(_Base::end(), this); }
145
146       const_iterator
147       end() const
148       { return const_iterator(_Base::end(), this); }
149
150       reverse_iterator
151       rbegin()
152       { return reverse_iterator(end()); }
153
154       const_reverse_iterator
155       rbegin() const
156       { return const_reverse_iterator(end()); }
157
158       reverse_iterator
159       rend()
160       { return reverse_iterator(begin()); }
161
162       const_reverse_iterator
163       rend() const
164       { return const_reverse_iterator(begin()); }
165
166 #ifdef __GXX_EXPERIMENTAL_CXX0X__
167       const_iterator
168       cbegin() const
169       { return const_iterator(_Base::begin(), this); }
170
171       const_iterator
172       cend() const
173       { return const_iterator(_Base::end(), this); }
174
175       const_reverse_iterator
176       crbegin() const
177       { return const_reverse_iterator(end()); }
178
179       const_reverse_iterator
180       crend() const
181       { return const_reverse_iterator(begin()); }
182 #endif
183
184       // capacity:
185       using _Base::empty;
186       using _Base::size;
187       using _Base::max_size;
188
189       // modifiers:
190       iterator
191       insert(const value_type& __x)
192       { return iterator(_Base::insert(__x), this); }
193
194       iterator
195       insert(iterator __position, const value_type& __x)
196       {
197         __glibcxx_check_insert(__position);
198         return iterator(_Base::insert(__position.base(), __x), this);
199       }
200
201       template<typename _InputIterator>
202       void
203       insert(_InputIterator __first, _InputIterator __last)
204       {
205         __glibcxx_check_valid_range(__first, __last);
206         _Base::insert(__first, __last);
207       }
208
209 #ifdef __GXX_EXPERIMENTAL_CXX0X__
210       void
211       insert(initializer_list<value_type> __l)
212       { _Base::insert(__l); }
213 #endif
214
215 #ifdef __GXX_EXPERIMENTAL_CXX0X__
216       iterator
217       erase(iterator __position)
218       {
219         __glibcxx_check_erase(__position);
220         __position._M_invalidate();
221         return iterator(_Base::erase(__position.base()), this);
222       }
223 #else
224       void
225       erase(iterator __position)
226       {
227         __glibcxx_check_erase(__position);
228         __position._M_invalidate();
229         _Base::erase(__position.base());
230       }
231 #endif
232
233       size_type
234       erase(const key_type& __x)
235       {
236         std::pair<iterator, iterator> __victims = this->equal_range(__x);
237         size_type __count = 0;
238         while (__victims.first != __victims.second)
239         {
240           iterator __victim = __victims.first++;
241           __victim._M_invalidate();
242           _Base::erase(__victim.base());
243           ++__count;
244         }
245         return __count;
246       }
247
248 #ifdef __GXX_EXPERIMENTAL_CXX0X__
249       iterator
250       erase(iterator __first, iterator __last)
251       {
252         // _GLIBCXX_RESOLVE_LIB_DEFECTS
253         // 151. can't currently clear() empty container
254         __glibcxx_check_erase_range(__first, __last);
255         while (__first != __last)
256           this->erase(__first++);
257         return __last;
258       }
259 #else
260       void
261       erase(iterator __first, iterator __last)
262       {
263         // _GLIBCXX_RESOLVE_LIB_DEFECTS
264         // 151. can't currently clear() empty container
265         __glibcxx_check_erase_range(__first, __last);
266         while (__first != __last)
267           this->erase(__first++);
268       }
269 #endif
270
271       void
272       swap(multiset& __x)
273       {
274         _Base::swap(__x);
275         this->_M_swap(__x);
276       }
277
278       void
279       clear()
280       { this->erase(begin(), end()); }
281
282       // observers:
283       using _Base::key_comp;
284       using _Base::value_comp;
285
286       // multiset operations:
287       iterator
288       find(const key_type& __x)
289       { return iterator(_Base::find(__x), this); }
290
291       // _GLIBCXX_RESOLVE_LIB_DEFECTS
292       // 214. set::find() missing const overload
293       const_iterator
294       find(const key_type& __x) const
295       { return const_iterator(_Base::find(__x), this); }
296
297       using _Base::count;
298
299       iterator
300       lower_bound(const key_type& __x)
301       { return iterator(_Base::lower_bound(__x), this); }
302
303       // _GLIBCXX_RESOLVE_LIB_DEFECTS
304       // 214. set::find() missing const overload
305       const_iterator
306       lower_bound(const key_type& __x) const
307       { return const_iterator(_Base::lower_bound(__x), this); }
308
309       iterator
310       upper_bound(const key_type& __x)
311       { return iterator(_Base::upper_bound(__x), this); }
312
313       // _GLIBCXX_RESOLVE_LIB_DEFECTS
314       // 214. set::find() missing const overload
315       const_iterator
316       upper_bound(const key_type& __x) const
317       { return const_iterator(_Base::upper_bound(__x), this); }
318
319       std::pair<iterator,iterator>
320       equal_range(const key_type& __x)
321       {
322         typedef typename _Base::iterator _Base_iterator;
323         std::pair<_Base_iterator, _Base_iterator> __res =
324         _Base::equal_range(__x);
325         return std::make_pair(iterator(__res.first, this),
326                               iterator(__res.second, this));
327       }
328
329       // _GLIBCXX_RESOLVE_LIB_DEFECTS
330       // 214. set::find() missing const overload
331       std::pair<const_iterator,const_iterator>
332       equal_range(const key_type& __x) const
333       {
334         typedef typename _Base::const_iterator _Base_iterator;
335         std::pair<_Base_iterator, _Base_iterator> __res =
336         _Base::equal_range(__x);
337         return std::make_pair(const_iterator(__res.first, this),
338                               const_iterator(__res.second, this));
339       }
340
341       _Base&
342       _M_base() { return *this; }
343
344       const _Base&
345       _M_base() const { return *this; }
346
347     private:
348       void
349       _M_invalidate_all()
350       {
351         typedef typename _Base::const_iterator _Base_const_iterator;
352         typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
353         this->_M_invalidate_if(_Not_equal(_M_base().end()));
354       }
355     };
356
357   template<typename _Key, typename _Compare, typename _Allocator>
358     inline bool
359     operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
360                const multiset<_Key, _Compare, _Allocator>& __rhs)
361     { return __lhs._M_base() == __rhs._M_base(); }
362
363   template<typename _Key, typename _Compare, typename _Allocator>
364     inline bool
365     operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
366                const multiset<_Key, _Compare, _Allocator>& __rhs)
367     { return __lhs._M_base() != __rhs._M_base(); }
368
369   template<typename _Key, typename _Compare, typename _Allocator>
370     inline bool
371     operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
372               const multiset<_Key, _Compare, _Allocator>& __rhs)
373     { return __lhs._M_base() < __rhs._M_base(); }
374
375   template<typename _Key, typename _Compare, typename _Allocator>
376     inline bool
377     operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
378                const multiset<_Key, _Compare, _Allocator>& __rhs)
379     { return __lhs._M_base() <= __rhs._M_base(); }
380
381   template<typename _Key, typename _Compare, typename _Allocator>
382     inline bool
383     operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
384                const multiset<_Key, _Compare, _Allocator>& __rhs)
385     { return __lhs._M_base() >= __rhs._M_base(); }
386
387   template<typename _Key, typename _Compare, typename _Allocator>
388     inline bool
389     operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
390               const multiset<_Key, _Compare, _Allocator>& __rhs)
391     { return __lhs._M_base() > __rhs._M_base(); }
392
393   template<typename _Key, typename _Compare, typename _Allocator>
394     void
395     swap(multiset<_Key, _Compare, _Allocator>& __x,
396          multiset<_Key, _Compare, _Allocator>& __y)
397     { return __x.swap(__y); }
398
399 } // namespace __debug
400 } // namespace std
401
402 #endif