1132720Skan// Debugging hash_map implementation -*- C++ -*-
2132720Skan
3169691Skan// Copyright (C) 2003, 2005, 2006
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/hash_map.h
32169691Skan *  This file is a GNU debug extension to the Standard C++ Library.
33169691Skan */
34169691Skan
35132720Skan#ifndef _GLIBCXX_DEBUG_HASH_MAP_H
36132720Skan#define _GLIBCXX_DEBUG_HASH_MAP_H 1
37132720Skan
38132720Skan#include <debug/safe_sequence.h>
39132720Skan#include <debug/safe_iterator.h>
40132720Skan
41169691Skannamespace __gnu_cxx
42132720Skan{
43169691Skannamespace __debug
44169691Skan{
45132720Skan  template<typename _Value, typename _Tp,
46132720Skan	   typename _HashFcn  = __gnu_cxx::hash<_Value>,
47132720Skan	   typename _EqualKey = std::equal_to<_Value>,
48132720Skan	   typename _Alloc = std::allocator<_Value> >
49132720Skan    class hash_map
50169691Skan    : public _GLIBCXX_EXT::hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>,
51132720Skan      public __gnu_debug::_Safe_sequence<hash_map<_Value, _Tp, _HashFcn,
52132720Skan						 _EqualKey, _Alloc> >
53132720Skan    {
54169691Skan      typedef _GLIBCXX_EXT::hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>
55132720Skan      							_Base;
56132720Skan      typedef __gnu_debug::_Safe_sequence<hash_map> 	_Safe_base;
57132720Skan
58132720Skan    public:
59132720Skan      typedef typename _Base::key_type        key_type;
60132720Skan      typedef typename _Base::data_type       data_type;
61132720Skan      typedef typename _Base::mapped_type     mapped_type;
62132720Skan      typedef typename _Base::value_type      value_type;
63132720Skan      typedef typename _Base::hasher          hasher;
64132720Skan      typedef typename _Base::key_equal       key_equal;
65132720Skan      typedef typename _Base::size_type       size_type;
66132720Skan      typedef typename _Base::difference_type difference_type;
67132720Skan      typedef typename _Base::pointer         pointer;
68132720Skan      typedef typename _Base::const_pointer   const_pointer;
69132720Skan      typedef typename _Base::reference       reference;
70132720Skan      typedef typename _Base::const_reference const_reference;
71132720Skan
72132720Skan      typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, hash_map>
73132720Skan					      iterator;
74132720Skan      typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
75132720Skan					  hash_map>
76132720Skan					      const_iterator;
77132720Skan
78132720Skan      typedef typename _Base::allocator_type  allocator_type;
79132720Skan
80132720Skan      using _Base::hash_funct;
81132720Skan      using _Base::key_eq;
82132720Skan      using _Base::get_allocator;
83132720Skan
84132720Skan      hash_map() { }
85132720Skan
86132720Skan      explicit hash_map(size_type __n) : _Base(__n) { }
87132720Skan
88132720Skan      hash_map(size_type __n, const hasher& __hf) : _Base(__n, __hf) { }
89132720Skan
90132720Skan      hash_map(size_type __n, const hasher& __hf, const key_equal& __eql,
91132720Skan	       const allocator_type& __a = allocator_type())
92132720Skan      : _Base(__n, __hf, __eql, __a) { }
93132720Skan
94132720Skan      template<typename _InputIterator>
95132720Skan        hash_map(_InputIterator __f, _InputIterator __l)
96132720Skan        : _Base(__gnu_debug::__check_valid_range(__f, __l), __l) { }
97132720Skan
98132720Skan      template<typename _InputIterator>
99132720Skan        hash_map(_InputIterator __f, _InputIterator __l, size_type __n)
100132720Skan	: _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n) { }
101132720Skan
102132720Skan      template<typename _InputIterator>
103132720Skan        hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
104132720Skan		 const hasher& __hf)
105132720Skan        : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf) { }
106132720Skan
107132720Skan      template<typename _InputIterator>
108132720Skan        hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
109132720Skan		 const hasher& __hf, const key_equal& __eql,
110132720Skan		 const allocator_type& __a = allocator_type())
111132720Skan	: _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf,
112132720Skan		__eql, __a) { }
113132720Skan
114132720Skan      hash_map(const _Base& __x) : _Base(__x), _Safe_base() { }
115132720Skan
116132720Skan      using _Base::size;
117132720Skan      using _Base::max_size;
118132720Skan      using _Base::empty;
119132720Skan
120132720Skan      void
121132720Skan      swap(hash_map& __x)
122132720Skan      {
123132720Skan	_Base::swap(__x);
124132720Skan	this->_M_swap(__x);
125132720Skan      }
126132720Skan
127132720Skan      iterator
128132720Skan      begin() { return iterator(_Base::begin(), this); }
129132720Skan
130132720Skan      iterator
131132720Skan      end() { return iterator(_Base::end(),   this); }
132132720Skan
133132720Skan      const_iterator
134132720Skan      begin() const
135132720Skan      { return const_iterator(_Base::begin(), this); }
136132720Skan
137132720Skan      const_iterator
138132720Skan      end() const
139132720Skan      { return const_iterator(_Base::end(),   this); }
140132720Skan
141132720Skan      std::pair<iterator, bool>
142132720Skan      insert(const value_type& __obj)
143132720Skan      {
144132720Skan	std::pair<typename _Base::iterator, bool> __res = _Base::insert(__obj);
145132720Skan	return std::make_pair(iterator(__res.first, this), __res.second);
146132720Skan      }
147132720Skan
148169691Skan      void
149169691Skan      insert(const value_type* __first, const value_type* __last)
150169691Skan      {
151169691Skan	__glibcxx_check_valid_range(__first, __last);
152169691Skan	_Base::insert(__first, __last);
153169691Skan      }
154169691Skan
155169691Skan     template<typename _InputIterator>
156132720Skan        void
157132720Skan        insert(_InputIterator __first, _InputIterator __last)
158132720Skan        {
159132720Skan	  __glibcxx_check_valid_range(__first, __last);
160132720Skan	  _Base::insert(__first.base(), __last.base());
161132720Skan	}
162132720Skan
163132720Skan
164132720Skan      std::pair<iterator, bool>
165132720Skan      insert_noresize(const value_type& __obj)
166132720Skan      {
167132720Skan	std::pair<typename _Base::iterator, bool> __res =
168132720Skan	                                        _Base::insert_noresize(__obj);
169132720Skan	return std::make_pair(iterator(__res.first, this), __res.second);
170132720Skan      }
171132720Skan
172132720Skan      iterator
173132720Skan      find(const key_type& __key)
174132720Skan      { return iterator(_Base::find(__key), this); }
175132720Skan
176132720Skan      const_iterator
177132720Skan      find(const key_type& __key) const
178132720Skan      { return const_iterator(_Base::find(__key), this); }
179132720Skan
180132720Skan      using _Base::operator[];
181132720Skan      using _Base::count;
182132720Skan
183132720Skan      std::pair<iterator, iterator>
184132720Skan      equal_range(const key_type& __key)
185132720Skan      {
186132720Skan	typedef typename _Base::iterator _Base_iterator;
187132720Skan	std::pair<_Base_iterator, _Base_iterator> __res =
188132720Skan	                  _Base::equal_range(__key);
189132720Skan	return std::make_pair(iterator(__res.first, this),
190132720Skan			      iterator(__res.second, this));
191132720Skan      }
192132720Skan
193132720Skan      std::pair<const_iterator, const_iterator>
194132720Skan      equal_range(const key_type& __key) const
195132720Skan      {
196132720Skan	typedef typename _Base::const_iterator _Base_iterator;
197132720Skan	std::pair<_Base_iterator, _Base_iterator> __res =
198132720Skan	_Base::equal_range(__key);
199132720Skan	return std::make_pair(const_iterator(__res.first, this),
200132720Skan			      const_iterator(__res.second, this));
201132720Skan      }
202132720Skan
203132720Skan      size_type
204132720Skan      erase(const key_type& __key)
205132720Skan      {
206132720Skan	iterator __victim(_Base::find(__key), this);
207132720Skan	if (__victim != end())
208132720Skan	  return this->erase(__victim), 1;
209132720Skan	else
210132720Skan	  return 0;
211132720Skan      }
212132720Skan
213132720Skan      void
214132720Skan      erase(iterator __it)
215132720Skan      {
216132720Skan	__glibcxx_check_erase(__it);
217132720Skan	__it._M_invalidate();
218132720Skan	_Base::erase(__it.base());
219132720Skan      }
220132720Skan
221132720Skan      void
222132720Skan      erase(iterator __first, iterator __last)
223132720Skan      {
224132720Skan	__glibcxx_check_erase_range(__first, __last);
225132720Skan	for (iterator __tmp = __first; __tmp != __last;)
226132720Skan	{
227132720Skan	  iterator __victim = __tmp++;
228132720Skan	  __victim._M_invalidate();
229132720Skan	}
230132720Skan	_Base::erase(__first.base(), __last.base());
231132720Skan      }
232132720Skan
233132720Skan      void
234132720Skan      clear()
235132720Skan      {
236132720Skan	_Base::clear();
237132720Skan	this->_M_invalidate_all();
238132720Skan      }
239132720Skan
240132720Skan      using _Base::resize;
241132720Skan      using _Base::bucket_count;
242132720Skan      using _Base::max_bucket_count;
243132720Skan      using _Base::elems_in_bucket;
244132720Skan
245132720Skan      _Base&
246132720Skan      _M_base()       { return *this; }
247132720Skan
248132720Skan      const _Base&
249132720Skan      _M_base() const { return *this; }
250132720Skan
251132720Skan    private:
252132720Skan      void
253132720Skan      _M_invalidate_all()
254132720Skan      {
255132720Skan	typedef typename _Base::const_iterator _Base_const_iterator;
256132720Skan	typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
257132720Skan	this->_M_invalidate_if(_Not_equal(_M_base().end()));
258132720Skan      }
259132720Skan    };
260132720Skan
261132720Skan  template<typename _Value, typename _Tp, typename _HashFcn,
262132720Skan	   typename _EqualKey, typename _Alloc>
263132720Skan    inline bool
264132720Skan    operator==(const hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __x,
265132720Skan	       const hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __y)
266132720Skan    { return __x._M_base() == __y._M_base(); }
267132720Skan
268132720Skan  template<typename _Value, typename _Tp, typename _HashFcn,
269132720Skan	   typename _EqualKey, typename _Alloc>
270132720Skan    inline bool
271132720Skan    operator!=(const hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __x,
272132720Skan	       const hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __y)
273132720Skan    { return __x._M_base() != __y._M_base(); }
274132720Skan
275132720Skan  template<typename _Value, typename _Tp, typename _HashFcn,
276132720Skan	   typename _EqualKey, typename _Alloc>
277132720Skan    inline void
278132720Skan    swap(hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __x,
279132720Skan	 hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __y)
280132720Skan    { __x.swap(__y); }
281169691Skan} // namespace __debug
282169691Skan} // namespace __gnu_cxx
283132720Skan
284132720Skan#endif
285