OSDN Git Service

4a7ed9e66882aff605d06ba30d8a28b71c67810e
[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         if (this != &__x)
117           {
118             // NB: DR 675.
119             clear();
120             swap(__x);
121           }
122         return *this;
123       }
124
125       multiset&
126       operator=(initializer_list<value_type> __l)
127       {
128         this->clear();
129         this->insert(__l);
130         return *this;
131       }
132 #endif
133
134       using _Base::get_allocator;
135
136       // iterators:
137       iterator
138       begin()
139       { return iterator(_Base::begin(), this); }
140
141       const_iterator
142       begin() const
143       { return const_iterator(_Base::begin(), this); }
144
145       iterator
146       end()
147       { return iterator(_Base::end(), this); }
148
149       const_iterator
150       end() const
151       { return const_iterator(_Base::end(), this); }
152
153       reverse_iterator
154       rbegin()
155       { return reverse_iterator(end()); }
156
157       const_reverse_iterator
158       rbegin() const
159       { return const_reverse_iterator(end()); }
160
161       reverse_iterator
162       rend()
163       { return reverse_iterator(begin()); }
164
165       const_reverse_iterator
166       rend() const
167       { return const_reverse_iterator(begin()); }
168
169 #ifdef __GXX_EXPERIMENTAL_CXX0X__
170       const_iterator
171       cbegin() const
172       { return const_iterator(_Base::begin(), this); }
173
174       const_iterator
175       cend() const
176       { return const_iterator(_Base::end(), this); }
177
178       const_reverse_iterator
179       crbegin() const
180       { return const_reverse_iterator(end()); }
181
182       const_reverse_iterator
183       crend() const
184       { return const_reverse_iterator(begin()); }
185 #endif
186
187       // capacity:
188       using _Base::empty;
189       using _Base::size;
190       using _Base::max_size;
191
192       // modifiers:
193       iterator
194       insert(const value_type& __x)
195       { return iterator(_Base::insert(__x), this); }
196
197       iterator
198       insert(iterator __position, const value_type& __x)
199       {
200         __glibcxx_check_insert(__position);
201         return iterator(_Base::insert(__position.base(), __x), this);
202       }
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 #ifdef __GXX_EXPERIMENTAL_CXX0X__
213       void
214       insert(initializer_list<value_type> __l)
215       { _Base::insert(__l); }
216 #endif
217
218 #ifdef __GXX_EXPERIMENTAL_CXX0X__
219       iterator
220       erase(iterator __position)
221       {
222         __glibcxx_check_erase(__position);
223         __position._M_invalidate();
224         return iterator(_Base::erase(__position.base()), this);
225       }
226 #else
227       void
228       erase(iterator __position)
229       {
230         __glibcxx_check_erase(__position);
231         __position._M_invalidate();
232         _Base::erase(__position.base());
233       }
234 #endif
235
236       size_type
237       erase(const key_type& __x)
238       {
239         std::pair<iterator, iterator> __victims = this->equal_range(__x);
240         size_type __count = 0;
241         while (__victims.first != __victims.second)
242         {
243           iterator __victim = __victims.first++;
244           __victim._M_invalidate();
245           _Base::erase(__victim.base());
246           ++__count;
247         }
248         return __count;
249       }
250
251 #ifdef __GXX_EXPERIMENTAL_CXX0X__
252       iterator
253       erase(iterator __first, iterator __last)
254       {
255         // _GLIBCXX_RESOLVE_LIB_DEFECTS
256         // 151. can't currently clear() empty container
257         __glibcxx_check_erase_range(__first, __last);
258         while (__first != __last)
259           this->erase(__first++);
260         return __last;
261       }
262 #else
263       void
264       erase(iterator __first, iterator __last)
265       {
266         // _GLIBCXX_RESOLVE_LIB_DEFECTS
267         // 151. can't currently clear() empty container
268         __glibcxx_check_erase_range(__first, __last);
269         while (__first != __last)
270           this->erase(__first++);
271       }
272 #endif
273
274       void
275       swap(multiset& __x)
276       {
277         _Base::swap(__x);
278         this->_M_swap(__x);
279       }
280
281       void
282       clear()
283       { this->erase(begin(), end()); }
284
285       // observers:
286       using _Base::key_comp;
287       using _Base::value_comp;
288
289       // multiset operations:
290       iterator
291       find(const key_type& __x)
292       { return iterator(_Base::find(__x), this); }
293
294       // _GLIBCXX_RESOLVE_LIB_DEFECTS
295       // 214. set::find() missing const overload
296       const_iterator
297       find(const key_type& __x) const
298       { return const_iterator(_Base::find(__x), this); }
299
300       using _Base::count;
301
302       iterator
303       lower_bound(const key_type& __x)
304       { return iterator(_Base::lower_bound(__x), this); }
305
306       // _GLIBCXX_RESOLVE_LIB_DEFECTS
307       // 214. set::find() missing const overload
308       const_iterator
309       lower_bound(const key_type& __x) const
310       { return const_iterator(_Base::lower_bound(__x), this); }
311
312       iterator
313       upper_bound(const key_type& __x)
314       { return iterator(_Base::upper_bound(__x), this); }
315
316       // _GLIBCXX_RESOLVE_LIB_DEFECTS
317       // 214. set::find() missing const overload
318       const_iterator
319       upper_bound(const key_type& __x) const
320       { return const_iterator(_Base::upper_bound(__x), this); }
321
322       std::pair<iterator,iterator>
323       equal_range(const key_type& __x)
324       {
325         typedef typename _Base::iterator _Base_iterator;
326         std::pair<_Base_iterator, _Base_iterator> __res =
327         _Base::equal_range(__x);
328         return std::make_pair(iterator(__res.first, this),
329                               iterator(__res.second, this));
330       }
331
332       // _GLIBCXX_RESOLVE_LIB_DEFECTS
333       // 214. set::find() missing const overload
334       std::pair<const_iterator,const_iterator>
335       equal_range(const key_type& __x) const
336       {
337         typedef typename _Base::const_iterator _Base_iterator;
338         std::pair<_Base_iterator, _Base_iterator> __res =
339         _Base::equal_range(__x);
340         return std::make_pair(const_iterator(__res.first, this),
341                               const_iterator(__res.second, this));
342       }
343
344       _Base&
345       _M_base() { return *this; }
346
347       const _Base&
348       _M_base() const { return *this; }
349
350     private:
351       void
352       _M_invalidate_all()
353       {
354         typedef typename _Base::const_iterator _Base_const_iterator;
355         typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
356         this->_M_invalidate_if(_Not_equal(_M_base().end()));
357       }
358     };
359
360   template<typename _Key, typename _Compare, typename _Allocator>
361     inline bool
362     operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
363                const multiset<_Key, _Compare, _Allocator>& __rhs)
364     { return __lhs._M_base() == __rhs._M_base(); }
365
366   template<typename _Key, typename _Compare, typename _Allocator>
367     inline bool
368     operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
369                const multiset<_Key, _Compare, _Allocator>& __rhs)
370     { return __lhs._M_base() != __rhs._M_base(); }
371
372   template<typename _Key, typename _Compare, typename _Allocator>
373     inline bool
374     operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
375               const multiset<_Key, _Compare, _Allocator>& __rhs)
376     { return __lhs._M_base() < __rhs._M_base(); }
377
378   template<typename _Key, typename _Compare, typename _Allocator>
379     inline bool
380     operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
381                const multiset<_Key, _Compare, _Allocator>& __rhs)
382     { return __lhs._M_base() <= __rhs._M_base(); }
383
384   template<typename _Key, typename _Compare, typename _Allocator>
385     inline bool
386     operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
387                const multiset<_Key, _Compare, _Allocator>& __rhs)
388     { return __lhs._M_base() >= __rhs._M_base(); }
389
390   template<typename _Key, typename _Compare, typename _Allocator>
391     inline bool
392     operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
393               const multiset<_Key, _Compare, _Allocator>& __rhs)
394     { return __lhs._M_base() > __rhs._M_base(); }
395
396   template<typename _Key, typename _Compare, typename _Allocator>
397     void
398     swap(multiset<_Key, _Compare, _Allocator>& __x,
399          multiset<_Key, _Compare, _Allocator>& __y)
400     { return __x.swap(__y); }
401
402 } // namespace __debug
403 } // namespace std
404
405 #endif