OSDN Git Service

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