1132720Skan// Debugging map implementation -*- C++ -*- 2132720Skan 3169691Skan// Copyright (C) 2003, 2004, 2005 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 19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 31169691Skan/** @file debug/map.h 32169691Skan * This file is a GNU debug extension to the Standard C++ Library. 33169691Skan */ 34169691Skan 35132720Skan#ifndef _GLIBCXX_DEBUG_MAP_H 36132720Skan#define _GLIBCXX_DEBUG_MAP_H 1 37132720Skan 38132720Skan#include <debug/safe_sequence.h> 39132720Skan#include <debug/safe_iterator.h> 40132720Skan#include <utility> 41132720Skan 42169691Skannamespace std 43132720Skan{ 44169691Skannamespace __debug 45169691Skan{ 46132720Skan template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>, 47132720Skan typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > > 48132720Skan class map 49132720Skan : public _GLIBCXX_STD::map<_Key, _Tp, _Compare, _Allocator>, 50132720Skan public __gnu_debug::_Safe_sequence<map<_Key, _Tp, _Compare, _Allocator> > 51132720Skan { 52132720Skan typedef _GLIBCXX_STD::map<_Key, _Tp, _Compare, _Allocator> _Base; 53132720Skan typedef __gnu_debug::_Safe_sequence<map> _Safe_base; 54132720Skan 55132720Skan public: 56132720Skan // types: 57132720Skan typedef _Key key_type; 58132720Skan typedef _Tp mapped_type; 59132720Skan typedef std::pair<const _Key, _Tp> value_type; 60132720Skan typedef _Compare key_compare; 61132720Skan typedef _Allocator allocator_type; 62169691Skan typedef typename _Base::reference reference; 63169691Skan typedef typename _Base::const_reference const_reference; 64132720Skan 65132720Skan typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, map> 66132720Skan iterator; 67132720Skan typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, map> 68132720Skan const_iterator; 69132720Skan 70132720Skan typedef typename _Base::size_type size_type; 71132720Skan typedef typename _Base::difference_type difference_type; 72169691Skan typedef typename _Base::pointer pointer; 73169691Skan typedef typename _Base::const_pointer const_pointer; 74132720Skan typedef std::reverse_iterator<iterator> reverse_iterator; 75132720Skan typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 76132720Skan 77132720Skan // 23.3.1.1 construct/copy/destroy: 78132720Skan explicit map(const _Compare& __comp = _Compare(), 79132720Skan const _Allocator& __a = _Allocator()) 80132720Skan : _Base(__comp, __a) { } 81132720Skan 82132720Skan template<typename _InputIterator> 83132720Skan map(_InputIterator __first, _InputIterator __last, 84132720Skan const _Compare& __comp = _Compare(), 85132720Skan const _Allocator& __a = _Allocator()) 86132720Skan : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, 87132720Skan __comp, __a), _Safe_base() { } 88132720Skan 89132720Skan map(const map<_Key,_Tp,_Compare,_Allocator>& __x) 90132720Skan : _Base(__x), _Safe_base() { } 91132720Skan 92132720Skan map(const _Base& __x) : _Base(__x), _Safe_base() { } 93132720Skan 94132720Skan ~map() { } 95132720Skan 96132720Skan map<_Key,_Tp,_Compare,_Allocator>& 97132720Skan operator=(const map<_Key,_Tp,_Compare,_Allocator>& __x) 98132720Skan { 99132720Skan *static_cast<_Base*>(this) = __x; 100132720Skan this->_M_invalidate_all(); 101132720Skan return *this; 102132720Skan } 103132720Skan 104132720Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 105132720Skan // 133. map missing get_allocator() 106132720Skan using _Base::get_allocator; 107132720Skan 108132720Skan // iterators: 109132720Skan iterator 110132720Skan begin() 111132720Skan { return iterator(_Base::begin(), this); } 112132720Skan 113132720Skan const_iterator 114132720Skan begin() const 115132720Skan { return const_iterator(_Base::begin(), this); } 116132720Skan 117132720Skan iterator 118132720Skan end() 119132720Skan { return iterator(_Base::end(), this); } 120132720Skan 121132720Skan const_iterator 122132720Skan end() const 123132720Skan { return const_iterator(_Base::end(), this); } 124132720Skan 125132720Skan reverse_iterator 126132720Skan rbegin() 127132720Skan { return reverse_iterator(end()); } 128132720Skan 129132720Skan const_reverse_iterator 130132720Skan rbegin() const 131132720Skan { return const_reverse_iterator(end()); } 132132720Skan 133132720Skan reverse_iterator 134132720Skan rend() 135132720Skan { return reverse_iterator(begin()); } 136132720Skan 137132720Skan const_reverse_iterator 138132720Skan rend() const 139132720Skan { return const_reverse_iterator(begin()); } 140132720Skan 141132720Skan // capacity: 142132720Skan using _Base::empty; 143132720Skan using _Base::size; 144132720Skan using _Base::max_size; 145132720Skan 146132720Skan // 23.3.1.2 element access: 147132720Skan using _Base::operator[]; 148132720Skan 149169691Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 150169691Skan // DR 464. Suggestion for new member functions in standard containers. 151169691Skan using _Base::at; 152169691Skan 153132720Skan // modifiers: 154132720Skan std::pair<iterator, bool> 155132720Skan insert(const value_type& __x) 156132720Skan { 157132720Skan typedef typename _Base::iterator _Base_iterator; 158132720Skan std::pair<_Base_iterator, bool> __res = _Base::insert(__x); 159132720Skan return std::pair<iterator, bool>(iterator(__res.first, this), 160132720Skan __res.second); 161132720Skan } 162132720Skan 163132720Skan iterator 164132720Skan insert(iterator __position, const value_type& __x) 165132720Skan { 166132720Skan __glibcxx_check_insert(__position); 167132720Skan return iterator(_Base::insert(__position.base(), __x), this); 168132720Skan } 169132720Skan 170132720Skan template<typename _InputIterator> 171132720Skan void 172132720Skan insert(_InputIterator __first, _InputIterator __last) 173132720Skan { 174146897Skan __glibcxx_check_valid_range(__first, __last); 175132720Skan _Base::insert(__first, __last); 176132720Skan } 177132720Skan 178132720Skan void 179132720Skan erase(iterator __position) 180132720Skan { 181132720Skan __glibcxx_check_erase(__position); 182132720Skan __position._M_invalidate(); 183132720Skan _Base::erase(__position.base()); 184132720Skan } 185132720Skan 186132720Skan size_type 187132720Skan erase(const key_type& __x) 188132720Skan { 189132720Skan iterator __victim = find(__x); 190132720Skan if (__victim == end()) 191132720Skan return 0; 192132720Skan else 193132720Skan { 194132720Skan __victim._M_invalidate(); 195132720Skan _Base::erase(__victim.base()); 196132720Skan return 1; 197132720Skan } 198132720Skan } 199132720Skan 200132720Skan void 201132720Skan erase(iterator __first, iterator __last) 202132720Skan { 203132720Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 204132720Skan // 151. can't currently clear() empty container 205132720Skan __glibcxx_check_erase_range(__first, __last); 206132720Skan while (__first != __last) 207132720Skan this->erase(__first++); 208132720Skan } 209132720Skan 210132720Skan void 211132720Skan swap(map<_Key,_Tp,_Compare,_Allocator>& __x) 212132720Skan { 213132720Skan _Base::swap(__x); 214132720Skan this->_M_swap(__x); 215132720Skan } 216132720Skan 217132720Skan void 218132720Skan clear() 219132720Skan { this->erase(begin(), end()); } 220132720Skan 221132720Skan // observers: 222132720Skan using _Base::key_comp; 223132720Skan using _Base::value_comp; 224132720Skan 225132720Skan // 23.3.1.3 map operations: 226132720Skan iterator 227132720Skan find(const key_type& __x) 228132720Skan { return iterator(_Base::find(__x), this); } 229132720Skan 230132720Skan const_iterator 231132720Skan find(const key_type& __x) const 232132720Skan { return const_iterator(_Base::find(__x), this); } 233132720Skan 234132720Skan using _Base::count; 235132720Skan 236132720Skan iterator 237132720Skan lower_bound(const key_type& __x) 238132720Skan { return iterator(_Base::lower_bound(__x), this); } 239132720Skan 240132720Skan const_iterator 241132720Skan lower_bound(const key_type& __x) const 242132720Skan { return const_iterator(_Base::lower_bound(__x), this); } 243132720Skan 244132720Skan iterator 245132720Skan upper_bound(const key_type& __x) 246132720Skan { return iterator(_Base::upper_bound(__x), this); } 247132720Skan 248132720Skan const_iterator 249132720Skan upper_bound(const key_type& __x) const 250132720Skan { return const_iterator(_Base::upper_bound(__x), this); } 251132720Skan 252132720Skan std::pair<iterator,iterator> 253132720Skan equal_range(const key_type& __x) 254132720Skan { 255132720Skan typedef typename _Base::iterator _Base_iterator; 256132720Skan std::pair<_Base_iterator, _Base_iterator> __res = 257132720Skan _Base::equal_range(__x); 258132720Skan return std::make_pair(iterator(__res.first, this), 259132720Skan iterator(__res.second, this)); 260132720Skan } 261132720Skan 262132720Skan std::pair<const_iterator,const_iterator> 263132720Skan equal_range(const key_type& __x) const 264132720Skan { 265132720Skan typedef typename _Base::const_iterator _Base_const_iterator; 266132720Skan std::pair<_Base_const_iterator, _Base_const_iterator> __res = 267132720Skan _Base::equal_range(__x); 268132720Skan return std::make_pair(const_iterator(__res.first, this), 269132720Skan const_iterator(__res.second, this)); 270132720Skan } 271132720Skan 272132720Skan _Base& 273132720Skan _M_base() { return *this; } 274132720Skan 275132720Skan const _Base& 276132720Skan _M_base() const { return *this; } 277132720Skan 278132720Skan private: 279132720Skan void 280132720Skan _M_invalidate_all() 281132720Skan { 282132720Skan typedef typename _Base::const_iterator _Base_const_iterator; 283132720Skan typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal; 284132720Skan this->_M_invalidate_if(_Not_equal(_M_base().end())); 285132720Skan } 286132720Skan }; 287132720Skan 288132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 289132720Skan inline bool 290132720Skan operator==(const map<_Key,_Tp,_Compare,_Allocator>& __lhs, 291132720Skan const map<_Key,_Tp,_Compare,_Allocator>& __rhs) 292132720Skan { return __lhs._M_base() == __rhs._M_base(); } 293132720Skan 294132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 295132720Skan inline bool 296132720Skan operator!=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs, 297132720Skan const map<_Key,_Tp,_Compare,_Allocator>& __rhs) 298132720Skan { return __lhs._M_base() != __rhs._M_base(); } 299132720Skan 300132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 301132720Skan inline bool 302132720Skan operator<(const map<_Key,_Tp,_Compare,_Allocator>& __lhs, 303132720Skan const map<_Key,_Tp,_Compare,_Allocator>& __rhs) 304132720Skan { return __lhs._M_base() < __rhs._M_base(); } 305132720Skan 306132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 307132720Skan inline bool 308132720Skan operator<=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs, 309132720Skan const map<_Key,_Tp,_Compare,_Allocator>& __rhs) 310132720Skan { return __lhs._M_base() <= __rhs._M_base(); } 311132720Skan 312132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 313132720Skan inline bool 314132720Skan operator>=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs, 315132720Skan const map<_Key,_Tp,_Compare,_Allocator>& __rhs) 316132720Skan { return __lhs._M_base() >= __rhs._M_base(); } 317132720Skan 318132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 319132720Skan inline bool 320132720Skan operator>(const map<_Key,_Tp,_Compare,_Allocator>& __lhs, 321132720Skan const map<_Key,_Tp,_Compare,_Allocator>& __rhs) 322132720Skan { return __lhs._M_base() > __rhs._M_base(); } 323132720Skan 324132720Skan template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 325132720Skan inline void 326132720Skan swap(map<_Key,_Tp,_Compare,_Allocator>& __lhs, 327132720Skan map<_Key,_Tp,_Compare,_Allocator>& __rhs) 328132720Skan { __lhs.swap(__rhs); } 329169691Skan} // namespace __debug 330169691Skan} // namespace std 331132720Skan 332132720Skan#endif 333