map.h revision 146897
1132720Skan// Debugging map implementation -*- C++ -*- 2132720Skan 3132720Skan// Copyright (C) 2003, 2004 4132720Skan// Free Software Foundation, Inc. 5132720Skan// 6132720Skan// This file is part of the GNU ISO C++ Library. This library is free 7132720Skan// software; you can redistribute it and/or modify it under the 8132720Skan// terms of the GNU General Public License as published by the 9132720Skan// Free Software Foundation; either version 2, or (at your option) 10132720Skan// any later version. 11132720Skan 12132720Skan// This library is distributed in the hope that it will be useful, 13132720Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of 14132720Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15132720Skan// GNU General Public License for more details. 16132720Skan 17132720Skan// You should have received a copy of the GNU General Public License along 18132720Skan// with this library; see the file COPYING. If not, write to the Free 19132720Skan// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 20132720Skan// USA. 21132720Skan 22132720Skan// As a special exception, you may use this file as part of a free software 23132720Skan// library without restriction. Specifically, if other files instantiate 24132720Skan// templates or use macros or inline functions from this file, or you compile 25132720Skan// this file and link it with other files to produce an executable, this 26132720Skan// file does not by itself cause the resulting executable to be covered by 27132720Skan// the GNU General Public License. This exception does not however 28132720Skan// invalidate any other reasons why the executable file might be covered by 29132720Skan// the GNU General Public License. 30132720Skan 31132720Skan#ifndef _GLIBCXX_DEBUG_MAP_H 32132720Skan#define _GLIBCXX_DEBUG_MAP_H 1 33132720Skan 34132720Skan#include <debug/safe_sequence.h> 35132720Skan#include <debug/safe_iterator.h> 36132720Skan#include <utility> 37132720Skan 38132720Skannamespace __gnu_debug_def 39132720Skan{ 40132720Skan template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>, 41132720Skan typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > > 42132720Skan class map 43132720Skan : public _GLIBCXX_STD::map<_Key, _Tp, _Compare, _Allocator>, 44132720Skan public __gnu_debug::_Safe_sequence<map<_Key, _Tp, _Compare, _Allocator> > 45132720Skan { 46132720Skan typedef _GLIBCXX_STD::map<_Key, _Tp, _Compare, _Allocator> _Base; 47132720Skan typedef __gnu_debug::_Safe_sequence<map> _Safe_base; 48132720Skan 49132720Skan public: 50132720Skan // types: 51132720Skan typedef _Key key_type; 52132720Skan typedef _Tp mapped_type; 53132720Skan typedef std::pair<const _Key, _Tp> value_type; 54132720Skan typedef _Compare key_compare; 55132720Skan typedef _Allocator allocator_type; 56132720Skan typedef typename _Allocator::reference reference; 57132720Skan typedef typename _Allocator::const_reference const_reference; 58132720Skan 59132720Skan typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, map> 60132720Skan iterator; 61132720Skan typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, map> 62132720Skan const_iterator; 63132720Skan 64132720Skan typedef typename _Base::size_type size_type; 65132720Skan typedef typename _Base::difference_type difference_type; 66132720Skan typedef typename _Allocator::pointer pointer; 67132720Skan typedef typename _Allocator::const_pointer const_pointer; 68132720Skan typedef std::reverse_iterator<iterator> reverse_iterator; 69132720Skan typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 70132720Skan 71132720Skan using _Base::value_compare; 72132720Skan 73132720Skan // 23.3.1.1 construct/copy/destroy: 74132720Skan explicit map(const _Compare& __comp = _Compare(), 75132720Skan const _Allocator& __a = _Allocator()) 76132720Skan : _Base(__comp, __a) { } 77132720Skan 78132720Skan template<typename _InputIterator> 79132720Skan map(_InputIterator __first, _InputIterator __last, 80132720Skan const _Compare& __comp = _Compare(), 81132720Skan const _Allocator& __a = _Allocator()) 82132720Skan : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, 83132720Skan __comp, __a), _Safe_base() { } 84132720Skan 85132720Skan map(const map<_Key,_Tp,_Compare,_Allocator>& __x) 86132720Skan : _Base(__x), _Safe_base() { } 87132720Skan 88132720Skan map(const _Base& __x) : _Base(__x), _Safe_base() { } 89132720Skan 90132720Skan ~map() { } 91132720Skan 92132720Skan map<_Key,_Tp,_Compare,_Allocator>& 93132720Skan operator=(const map<_Key,_Tp,_Compare,_Allocator>& __x) 94132720Skan { 95132720Skan *static_cast<_Base*>(this) = __x; 96132720Skan this->_M_invalidate_all(); 97132720Skan return *this; 98132720Skan } 99132720Skan 100132720Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 101132720Skan // 133. map missing get_allocator() 102132720Skan using _Base::get_allocator; 103132720Skan 104132720Skan // iterators: 105132720Skan iterator 106132720Skan begin() 107132720Skan { return iterator(_Base::begin(), this); } 108132720Skan 109132720Skan const_iterator 110132720Skan begin() const 111132720Skan { return const_iterator(_Base::begin(), this); } 112132720Skan 113132720Skan iterator 114132720Skan end() 115132720Skan { return iterator(_Base::end(), this); } 116132720Skan 117132720Skan const_iterator 118132720Skan end() const 119132720Skan { return const_iterator(_Base::end(), this); } 120132720Skan 121132720Skan reverse_iterator 122132720Skan rbegin() 123132720Skan { return reverse_iterator(end()); } 124132720Skan 125132720Skan const_reverse_iterator 126132720Skan rbegin() const 127132720Skan { return const_reverse_iterator(end()); } 128132720Skan 129132720Skan reverse_iterator 130132720Skan rend() 131132720Skan { return reverse_iterator(begin()); } 132132720Skan 133132720Skan const_reverse_iterator 134132720Skan rend() const 135132720Skan { return const_reverse_iterator(begin()); } 136132720Skan 137132720Skan // capacity: 138132720Skan using _Base::empty; 139132720Skan using _Base::size; 140132720Skan using _Base::max_size; 141132720Skan 142132720Skan // 23.3.1.2 element access: 143132720Skan using _Base::operator[]; 144132720Skan 145132720Skan // modifiers: 146132720Skan std::pair<iterator, bool> 147132720Skan insert(const value_type& __x) 148132720Skan { 149132720Skan typedef typename _Base::iterator _Base_iterator; 150132720Skan std::pair<_Base_iterator, bool> __res = _Base::insert(__x); 151132720Skan return std::pair<iterator, bool>(iterator(__res.first, this), 152132720Skan __res.second); 153132720Skan } 154132720Skan 155132720Skan iterator 156132720Skan insert(iterator __position, const value_type& __x) 157132720Skan { 158132720Skan __glibcxx_check_insert(__position); 159132720Skan return iterator(_Base::insert(__position.base(), __x), this); 160132720Skan } 161132720Skan 162132720Skan template<typename _InputIterator> 163132720Skan void 164132720Skan insert(_InputIterator __first, _InputIterator __last) 165132720Skan { 166146897Skan __glibcxx_check_valid_range(__first, __last); 167132720Skan _Base::insert(__first, __last); 168132720Skan } 169132720Skan 170132720Skan void 171132720Skan erase(iterator __position) 172132720Skan { 173132720Skan __glibcxx_check_erase(__position); 174132720Skan __position._M_invalidate(); 175132720Skan _Base::erase(__position.base()); 176132720Skan } 177132720Skan 178132720Skan size_type 179132720Skan erase(const key_type& __x) 180132720Skan { 181132720Skan iterator __victim = find(__x); 182132720Skan if (__victim == end()) 183132720Skan return 0; 184132720Skan else 185132720Skan { 186132720Skan __victim._M_invalidate(); 187132720Skan _Base::erase(__victim.base()); 188132720Skan return 1; 189132720Skan } 190132720Skan } 191132720Skan 192132720Skan void 193132720Skan erase(iterator __first, iterator __last) 194132720Skan { 195132720Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 196132720Skan // 151. can't currently clear() empty container 197132720Skan __glibcxx_check_erase_range(__first, __last); 198132720Skan while (__first != __last) 199132720Skan this->erase(__first++); 200132720Skan } 201132720Skan 202132720Skan void 203132720Skan swap(map<_Key,_Tp,_Compare,_Allocator>& __x) 204132720Skan { 205132720Skan _Base::swap(__x); 206132720Skan this->_M_swap(__x); 207132720Skan } 208132720Skan 209132720Skan void 210132720Skan clear() 211132720Skan { this->erase(begin(), end()); } 212132720Skan 213132720Skan // observers: 214132720Skan using _Base::key_comp; 215132720Skan using _Base::value_comp; 216132720Skan 217132720Skan // 23.3.1.3 map operations: 218132720Skan iterator 219132720Skan find(const key_type& __x) 220132720Skan { return iterator(_Base::find(__x), this); } 221132720Skan 222132720Skan const_iterator 223132720Skan find(const key_type& __x) const 224132720Skan { return const_iterator(_Base::find(__x), this); } 225132720Skan 226132720Skan using _Base::count; 227132720Skan 228132720Skan iterator 229132720Skan lower_bound(const key_type& __x) 230132720Skan { return iterator(_Base::lower_bound(__x), this); } 231132720Skan 232132720Skan const_iterator 233132720Skan lower_bound(const key_type& __x) const 234132720Skan { return const_iterator(_Base::lower_bound(__x), this); } 235132720Skan 236132720Skan iterator 237132720Skan upper_bound(const key_type& __x) 238132720Skan { return iterator(_Base::upper_bound(__x), this); } 239132720Skan 240132720Skan const_iterator 241132720Skan upper_bound(const key_type& __x) const 242132720Skan { return const_iterator(_Base::upper_bound(__x), this); } 243132720Skan 244132720Skan std::pair<iterator,iterator> 245132720Skan equal_range(const key_type& __x) 246132720Skan { 247132720Skan typedef typename _Base::iterator _Base_iterator; 248132720Skan std::pair<_Base_iterator, _Base_iterator> __res = 249132720Skan _Base::equal_range(__x); 250132720Skan return std::make_pair(iterator(__res.first, this), 251132720Skan iterator(__res.second, this)); 252132720Skan } 253132720Skan 254132720Skan std::pair<const_iterator,const_iterator> 255132720Skan equal_range(const key_type& __x) const 256132720Skan { 257132720Skan typedef typename _Base::const_iterator _Base_const_iterator; 258132720Skan std::pair<_Base_const_iterator, _Base_const_iterator> __res = 259132720Skan _Base::equal_range(__x); 260132720Skan return std::make_pair(const_iterator(__res.first, this), 261132720Skan const_iterator(__res.second, this)); 262132720Skan } 263132720Skan 264132720Skan _Base& 265132720Skan _M_base() { return *this; } 266132720Skan 267132720Skan const _Base& 268132720Skan _M_base() const { return *this; } 269132720Skan 270132720Skan private: 271132720Skan void 272132720Skan _M_invalidate_all() 273132720Skan { 274132720Skan typedef typename _Base::const_iterator _Base_const_iterator; 275132720Skan typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal; 276132720Skan this->_M_invalidate_if(_Not_equal(_M_base().end())); 277132720Skan } 278132720Skan }; 279132720Skan 280132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 281132720Skan inline bool 282132720Skan operator==(const map<_Key,_Tp,_Compare,_Allocator>& __lhs, 283132720Skan const map<_Key,_Tp,_Compare,_Allocator>& __rhs) 284132720Skan { return __lhs._M_base() == __rhs._M_base(); } 285132720Skan 286132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 287132720Skan inline bool 288132720Skan operator!=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs, 289132720Skan const map<_Key,_Tp,_Compare,_Allocator>& __rhs) 290132720Skan { return __lhs._M_base() != __rhs._M_base(); } 291132720Skan 292132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 293132720Skan inline bool 294132720Skan operator<(const map<_Key,_Tp,_Compare,_Allocator>& __lhs, 295132720Skan const map<_Key,_Tp,_Compare,_Allocator>& __rhs) 296132720Skan { return __lhs._M_base() < __rhs._M_base(); } 297132720Skan 298132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 299132720Skan inline bool 300132720Skan operator<=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs, 301132720Skan const map<_Key,_Tp,_Compare,_Allocator>& __rhs) 302132720Skan { return __lhs._M_base() <= __rhs._M_base(); } 303132720Skan 304132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 305132720Skan inline bool 306132720Skan operator>=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs, 307132720Skan const map<_Key,_Tp,_Compare,_Allocator>& __rhs) 308132720Skan { return __lhs._M_base() >= __rhs._M_base(); } 309132720Skan 310132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 311132720Skan inline bool 312132720Skan operator>(const map<_Key,_Tp,_Compare,_Allocator>& __lhs, 313132720Skan const map<_Key,_Tp,_Compare,_Allocator>& __rhs) 314132720Skan { return __lhs._M_base() > __rhs._M_base(); } 315132720Skan 316132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 317132720Skan inline void 318132720Skan swap(map<_Key,_Tp,_Compare,_Allocator>& __lhs, 319132720Skan map<_Key,_Tp,_Compare,_Allocator>& __rhs) 320132720Skan { __lhs.swap(__rhs); } 321132720Skan} // namespace __gnu_debug_def 322132720Skan 323132720Skan#endif 324