OSDN Git Service

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