OSDN Git Service

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