In libobjc/: 2011-06-02 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / libstdc++-v3 / include / debug / map.h
blob9f80251e3ae94d50651e6666c2be0121e48cf1ad
1 // Debugging map implementation -*- C++ -*-
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.
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.
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.
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/>.
26 /** @file debug/map.h
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_MAP_H
31 #define _GLIBCXX_DEBUG_MAP_H 1
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_iterator.h>
35 #include <utility>
37 namespace std _GLIBCXX_VISIBILITY(default)
39 namespace __debug
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_C::map<_Key, _Tp, _Compare, _Allocator>,
46 public __gnu_debug::_Safe_sequence<map<_Key, _Tp, _Compare, _Allocator> >
48 typedef _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator> _Base;
49 typedef __gnu_debug::_Safe_sequence<map> _Safe_base;
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;
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;
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;
76 // 23.3.1.1 construct/copy/destroy:
77 explicit map(const _Compare& __comp = _Compare(),
78 const _Allocator& __a = _Allocator())
79 : _Base(__comp, __a) { }
81 template<typename _InputIterator>
82 map(_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), _Safe_base() { }
90 map(const map& __x)
91 : _Base(__x), _Safe_base() { }
93 map(const _Base& __x)
94 : _Base(__x), _Safe_base() { }
96 #ifdef __GXX_EXPERIMENTAL_CXX0X__
97 map(map&& __x)
98 noexcept(is_nothrow_copy_constructible<_Compare>::value)
99 : _Base(std::move(__x)), _Safe_base()
100 { this->_M_swap(__x); }
102 map(initializer_list<value_type> __l,
103 const _Compare& __c = _Compare(),
104 const allocator_type& __a = allocator_type())
105 : _Base(__l, __c, __a), _Safe_base() { }
106 #endif
108 ~map() _GLIBCXX_NOEXCEPT { }
110 map&
111 operator=(const map& __x)
113 *static_cast<_Base*>(this) = __x;
114 this->_M_invalidate_all();
115 return *this;
118 #ifdef __GXX_EXPERIMENTAL_CXX0X__
119 map&
120 operator=(map&& __x)
122 // NB: DR 1204.
123 // NB: DR 675.
124 clear();
125 swap(__x);
126 return *this;
129 map&
130 operator=(initializer_list<value_type> __l)
132 this->clear();
133 this->insert(__l);
134 return *this;
136 #endif
138 // _GLIBCXX_RESOLVE_LIB_DEFECTS
139 // 133. map missing get_allocator()
140 using _Base::get_allocator;
142 // iterators:
143 iterator
144 begin() _GLIBCXX_NOEXCEPT
145 { return iterator(_Base::begin(), this); }
147 const_iterator
148 begin() const _GLIBCXX_NOEXCEPT
149 { return const_iterator(_Base::begin(), this); }
151 iterator
152 end() _GLIBCXX_NOEXCEPT
153 { return iterator(_Base::end(), this); }
155 const_iterator
156 end() const _GLIBCXX_NOEXCEPT
157 { return const_iterator(_Base::end(), this); }
159 reverse_iterator
160 rbegin() _GLIBCXX_NOEXCEPT
161 { return reverse_iterator(end()); }
163 const_reverse_iterator
164 rbegin() const _GLIBCXX_NOEXCEPT
165 { return const_reverse_iterator(end()); }
167 reverse_iterator
168 rend() _GLIBCXX_NOEXCEPT
169 { return reverse_iterator(begin()); }
171 const_reverse_iterator
172 rend() const _GLIBCXX_NOEXCEPT
173 { return const_reverse_iterator(begin()); }
175 #ifdef __GXX_EXPERIMENTAL_CXX0X__
176 const_iterator
177 cbegin() const noexcept
178 { return const_iterator(_Base::begin(), this); }
180 const_iterator
181 cend() const noexcept
182 { return const_iterator(_Base::end(), this); }
184 const_reverse_iterator
185 crbegin() const noexcept
186 { return const_reverse_iterator(end()); }
188 const_reverse_iterator
189 crend() const noexcept
190 { return const_reverse_iterator(begin()); }
191 #endif
193 // capacity:
194 using _Base::empty;
195 using _Base::size;
196 using _Base::max_size;
198 // 23.3.1.2 element access:
199 using _Base::operator[];
201 // _GLIBCXX_RESOLVE_LIB_DEFECTS
202 // DR 464. Suggestion for new member functions in standard containers.
203 using _Base::at;
205 // modifiers:
206 std::pair<iterator, bool>
207 insert(const value_type& __x)
209 typedef typename _Base::iterator _Base_iterator;
210 std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
211 return std::pair<iterator, bool>(iterator(__res.first, this),
212 __res.second);
215 #ifdef __GXX_EXPERIMENTAL_CXX0X__
216 template<typename _Pair, typename = typename
217 std::enable_if<std::is_convertible<_Pair,
218 value_type>::value>::type>
219 std::pair<iterator, bool>
220 insert(_Pair&& __x)
222 typedef typename _Base::iterator _Base_iterator;
223 std::pair<_Base_iterator, bool> __res
224 = _Base::insert(std::forward<_Pair>(__x));
225 return std::pair<iterator, bool>(iterator(__res.first, this),
226 __res.second);
228 #endif
230 #ifdef __GXX_EXPERIMENTAL_CXX0X__
231 void
232 insert(std::initializer_list<value_type> __list)
233 { _Base::insert(__list); }
234 #endif
236 iterator
237 #ifdef __GXX_EXPERIMENTAL_CXX0X__
238 insert(const_iterator __position, const value_type& __x)
239 #else
240 insert(iterator __position, const value_type& __x)
241 #endif
243 __glibcxx_check_insert(__position);
244 return iterator(_Base::insert(__position.base(), __x), this);
247 #ifdef __GXX_EXPERIMENTAL_CXX0X__
248 template<typename _Pair, typename = typename
249 std::enable_if<std::is_convertible<_Pair,
250 value_type>::value>::type>
251 iterator
252 insert(const_iterator __position, _Pair&& __x)
254 __glibcxx_check_insert(__position);
255 return iterator(_Base::insert(__position.base(),
256 std::forward<_Pair>(__x)), this);
258 #endif
260 template<typename _InputIterator>
261 void
262 insert(_InputIterator __first, _InputIterator __last)
264 __glibcxx_check_valid_range(__first, __last);
265 _Base::insert(__gnu_debug::__base(__first),
266 __gnu_debug::__base(__last));
269 #ifdef __GXX_EXPERIMENTAL_CXX0X__
270 iterator
271 erase(const_iterator __position)
273 __glibcxx_check_erase(__position);
274 this->_M_invalidate_if(_Equal(__position.base()));
275 return iterator(_Base::erase(__position.base()), this);
277 #else
278 void
279 erase(iterator __position)
281 __glibcxx_check_erase(__position);
282 this->_M_invalidate_if(_Equal(__position.base()));
283 _Base::erase(__position.base());
285 #endif
287 size_type
288 erase(const key_type& __x)
290 _Base_iterator __victim = _Base::find(__x);
291 if (__victim == _Base::end())
292 return 0;
293 else
295 this->_M_invalidate_if(_Equal(__victim));
296 _Base::erase(__victim);
297 return 1;
301 #ifdef __GXX_EXPERIMENTAL_CXX0X__
302 iterator
303 erase(const_iterator __first, const_iterator __last)
305 // _GLIBCXX_RESOLVE_LIB_DEFECTS
306 // 151. can't currently clear() empty container
307 __glibcxx_check_erase_range(__first, __last);
308 for (_Base_const_iterator __victim = __first.base();
309 __victim != __last.base(); ++__victim)
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));
317 return iterator(_Base::erase(__first.base(), __last.base()), this);
319 #else
320 void
321 erase(iterator __first, iterator __last)
323 // _GLIBCXX_RESOLVE_LIB_DEFECTS
324 // 151. can't currently clear() empty container
325 __glibcxx_check_erase_range(__first, __last);
326 for (_Base_iterator __victim = __first.base();
327 __victim != __last.base(); ++__victim)
329 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
330 _M_message(__gnu_debug::__msg_valid_range)
331 ._M_iterator(__first, "first")
332 ._M_iterator(__last, "last"));
333 this->_M_invalidate_if(_Equal(__victim));
335 _Base::erase(__first.base(), __last.base());
337 #endif
339 void
340 swap(map& __x)
342 _Base::swap(__x);
343 this->_M_swap(__x);
346 void
347 clear() _GLIBCXX_NOEXCEPT
349 this->_M_invalidate_all();
350 _Base::clear();
353 // observers:
354 using _Base::key_comp;
355 using _Base::value_comp;
357 // 23.3.1.3 map operations:
358 iterator
359 find(const key_type& __x)
360 { return iterator(_Base::find(__x), this); }
362 const_iterator
363 find(const key_type& __x) const
364 { return const_iterator(_Base::find(__x), this); }
366 using _Base::count;
368 iterator
369 lower_bound(const key_type& __x)
370 { return iterator(_Base::lower_bound(__x), this); }
372 const_iterator
373 lower_bound(const key_type& __x) const
374 { return const_iterator(_Base::lower_bound(__x), this); }
376 iterator
377 upper_bound(const key_type& __x)
378 { return iterator(_Base::upper_bound(__x), this); }
380 const_iterator
381 upper_bound(const key_type& __x) const
382 { return const_iterator(_Base::upper_bound(__x), this); }
384 std::pair<iterator,iterator>
385 equal_range(const key_type& __x)
387 typedef typename _Base::iterator _Base_iterator;
388 std::pair<_Base_iterator, _Base_iterator> __res =
389 _Base::equal_range(__x);
390 return std::make_pair(iterator(__res.first, this),
391 iterator(__res.second, this));
394 std::pair<const_iterator,const_iterator>
395 equal_range(const key_type& __x) const
397 typedef typename _Base::const_iterator _Base_const_iterator;
398 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
399 _Base::equal_range(__x);
400 return std::make_pair(const_iterator(__res.first, this),
401 const_iterator(__res.second, this));
404 _Base&
405 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
407 const _Base&
408 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
410 private:
411 void
412 _M_invalidate_all()
414 typedef typename _Base::const_iterator _Base_const_iterator;
415 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
416 this->_M_invalidate_if(_Not_equal(_M_base().end()));
420 template<typename _Key, typename _Tp,
421 typename _Compare, typename _Allocator>
422 inline bool
423 operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
424 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
425 { return __lhs._M_base() == __rhs._M_base(); }
427 template<typename _Key, typename _Tp,
428 typename _Compare, typename _Allocator>
429 inline bool
430 operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
431 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
432 { return __lhs._M_base() != __rhs._M_base(); }
434 template<typename _Key, typename _Tp,
435 typename _Compare, typename _Allocator>
436 inline bool
437 operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
438 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
439 { return __lhs._M_base() < __rhs._M_base(); }
441 template<typename _Key, typename _Tp,
442 typename _Compare, typename _Allocator>
443 inline bool
444 operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
445 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
446 { return __lhs._M_base() <= __rhs._M_base(); }
448 template<typename _Key, typename _Tp,
449 typename _Compare, typename _Allocator>
450 inline bool
451 operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
452 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
453 { return __lhs._M_base() >= __rhs._M_base(); }
455 template<typename _Key, typename _Tp,
456 typename _Compare, typename _Allocator>
457 inline bool
458 operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
459 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
460 { return __lhs._M_base() > __rhs._M_base(); }
462 template<typename _Key, typename _Tp,
463 typename _Compare, typename _Allocator>
464 inline void
465 swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
466 map<_Key, _Tp, _Compare, _Allocator>& __rhs)
467 { __lhs.swap(__rhs); }
469 } // namespace __debug
470 } // namespace std
472 #endif