1 // Debugging map implementation -*- C++ -*-
2 
3 // Copyright (C) 2003, 2004
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21 
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30 
31 #ifndef _GLIBCXX_DEBUG_MAP_H
32 #define _GLIBCXX_DEBUG_MAP_H 1
33 
34 #include <debug/safe_sequence.h>
35 #include <debug/safe_iterator.h>
36 #include <utility>
37 
38 namespace __gnu_debug_def
39 {
40   template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
41 	   typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
42     class map
43     : public _GLIBCXX_STD::map<_Key, _Tp, _Compare, _Allocator>,
44       public __gnu_debug::_Safe_sequence<map<_Key, _Tp, _Compare, _Allocator> >
45     {
46       typedef _GLIBCXX_STD::map<_Key, _Tp, _Compare, _Allocator> _Base;
47       typedef __gnu_debug::_Safe_sequence<map> _Safe_base;
48 
49     public:
50       // types:
51       typedef _Key                                  key_type;
52       typedef _Tp                                   mapped_type;
53       typedef std::pair<const _Key, _Tp>            value_type;
54       typedef _Compare                              key_compare;
55       typedef _Allocator                            allocator_type;
56       typedef typename _Allocator::reference        reference;
57       typedef typename _Allocator::const_reference  const_reference;
58 
59       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, map>
60                                                     iterator;
61       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, map>
62                                                     const_iterator;
63 
64       typedef typename _Base::size_type             size_type;
65       typedef typename _Base::difference_type       difference_type;
66       typedef typename _Allocator::pointer          pointer;
67       typedef typename _Allocator::const_pointer    const_pointer;
68       typedef std::reverse_iterator<iterator>       reverse_iterator;
69       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
70 
71       using _Base::value_compare;
72 
73       // 23.3.1.1 construct/copy/destroy:
74       explicit map(const _Compare& __comp = _Compare(),
75 		   const _Allocator& __a = _Allocator())
76       : _Base(__comp, __a) { }
77 
78       template<typename _InputIterator>
79         map(_InputIterator __first, _InputIterator __last,
80 	    const _Compare& __comp = _Compare(),
81 	    const _Allocator& __a = _Allocator())
82 	: _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
83 		__comp, __a), _Safe_base() { }
84 
85       map(const map<_Key,_Tp,_Compare,_Allocator>& __x)
86       : _Base(__x), _Safe_base() { }
87 
88       map(const _Base& __x) : _Base(__x), _Safe_base() { }
89 
90       ~map() { }
91 
92       map<_Key,_Tp,_Compare,_Allocator>&
93       operator=(const map<_Key,_Tp,_Compare,_Allocator>& __x)
94       {
95 	*static_cast<_Base*>(this) = __x;
96 	this->_M_invalidate_all();
97 	return *this;
98       }
99 
100       // _GLIBCXX_RESOLVE_LIB_DEFECTS
101       // 133. map missing get_allocator()
102       using _Base::get_allocator;
103 
104       // iterators:
105       iterator
106       begin()
107       { return iterator(_Base::begin(), this); }
108 
109       const_iterator
110       begin() const
111       { return const_iterator(_Base::begin(), this); }
112 
113       iterator
114       end()
115       { return iterator(_Base::end(), this); }
116 
117       const_iterator
118       end() const
119       { return const_iterator(_Base::end(), this); }
120 
121       reverse_iterator
122       rbegin()
123       { return reverse_iterator(end()); }
124 
125       const_reverse_iterator
126       rbegin() const
127       { return const_reverse_iterator(end()); }
128 
129       reverse_iterator
130       rend()
131       { return reverse_iterator(begin()); }
132 
133       const_reverse_iterator
134       rend() const
135       { return const_reverse_iterator(begin()); }
136 
137       // capacity:
138       using _Base::empty;
139       using _Base::size;
140       using _Base::max_size;
141 
142       // 23.3.1.2 element access:
143       using _Base::operator[];
144 
145       // modifiers:
146       std::pair<iterator, bool>
147       insert(const value_type& __x)
148       {
149 	typedef typename _Base::iterator _Base_iterator;
150 	std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
151 	return std::pair<iterator, bool>(iterator(__res.first, this),
152 					 __res.second);
153       }
154 
155       iterator
156       insert(iterator __position, const value_type& __x)
157       {
158 	__glibcxx_check_insert(__position);
159 	return iterator(_Base::insert(__position.base(), __x), this);
160       }
161 
162       template<typename _InputIterator>
163         void
164         insert(_InputIterator __first, _InputIterator __last)
165         {
166 	  __glibcxx_valid_range(__first, __last);
167 	  _Base::insert(__first, __last);
168 	}
169 
170       void
171       erase(iterator __position)
172       {
173 	__glibcxx_check_erase(__position);
174 	__position._M_invalidate();
175 	_Base::erase(__position.base());
176       }
177 
178       size_type
179       erase(const key_type& __x)
180       {
181 	iterator __victim = find(__x);
182 	if (__victim == end())
183 	  return 0;
184 	else
185 	{
186 	  __victim._M_invalidate();
187 	  _Base::erase(__victim.base());
188 	  return 1;
189 	}
190       }
191 
192       void
193       erase(iterator __first, iterator __last)
194       {
195 	// _GLIBCXX_RESOLVE_LIB_DEFECTS
196 	// 151. can't currently clear() empty container
197 	__glibcxx_check_erase_range(__first, __last);
198 	while (__first != __last)
199 	  this->erase(__first++);
200       }
201 
202       void
203       swap(map<_Key,_Tp,_Compare,_Allocator>& __x)
204       {
205 	_Base::swap(__x);
206 	this->_M_swap(__x);
207       }
208 
209       void
210       clear()
211       { this->erase(begin(), end()); }
212 
213       // observers:
214       using _Base::key_comp;
215       using _Base::value_comp;
216 
217       // 23.3.1.3 map operations:
218       iterator
219       find(const key_type& __x)
220       { return iterator(_Base::find(__x), this); }
221 
222       const_iterator
223       find(const key_type& __x) const
224       { return const_iterator(_Base::find(__x), this); }
225 
226       using _Base::count;
227 
228       iterator
229       lower_bound(const key_type& __x)
230       { return iterator(_Base::lower_bound(__x), this); }
231 
232       const_iterator
233       lower_bound(const key_type& __x) const
234       { return const_iterator(_Base::lower_bound(__x), this); }
235 
236       iterator
237       upper_bound(const key_type& __x)
238       { return iterator(_Base::upper_bound(__x), this); }
239 
240       const_iterator
241       upper_bound(const key_type& __x) const
242       { return const_iterator(_Base::upper_bound(__x), this); }
243 
244       std::pair<iterator,iterator>
245       equal_range(const key_type& __x)
246       {
247 	typedef typename _Base::iterator _Base_iterator;
248 	std::pair<_Base_iterator, _Base_iterator> __res =
249 	_Base::equal_range(__x);
250 	return std::make_pair(iterator(__res.first, this),
251 			      iterator(__res.second, this));
252       }
253 
254       std::pair<const_iterator,const_iterator>
255       equal_range(const key_type& __x) const
256       {
257 	typedef typename _Base::const_iterator _Base_const_iterator;
258 	std::pair<_Base_const_iterator, _Base_const_iterator> __res =
259 	_Base::equal_range(__x);
260 	return std::make_pair(const_iterator(__res.first, this),
261 			      const_iterator(__res.second, this));
262       }
263 
264       _Base&
265       _M_base() { return *this; }
266 
267       const _Base&
268       _M_base() const { return *this; }
269 
270     private:
271       void
272       _M_invalidate_all()
273       {
274 	typedef typename _Base::const_iterator _Base_const_iterator;
275 	typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
276 	this->_M_invalidate_if(_Not_equal(_M_base().end()));
277       }
278     };
279 
280   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
281     inline bool
282     operator==(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
283 	       const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
284     { return __lhs._M_base() == __rhs._M_base(); }
285 
286   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
287     inline bool
288     operator!=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
289 	       const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
290     { return __lhs._M_base() != __rhs._M_base(); }
291 
292   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
293     inline bool
294     operator<(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
295 	      const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
296     { return __lhs._M_base() < __rhs._M_base(); }
297 
298   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
299     inline bool
300     operator<=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
301 	       const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
302     { return __lhs._M_base() <= __rhs._M_base(); }
303 
304   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
305     inline bool
306     operator>=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
307 	       const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
308     { return __lhs._M_base() >= __rhs._M_base(); }
309 
310   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
311     inline bool
312     operator>(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
313 	      const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
314     { return __lhs._M_base() > __rhs._M_base(); }
315 
316   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
317     inline void
318     swap(map<_Key,_Tp,_Compare,_Allocator>& __lhs,
319 	 map<_Key,_Tp,_Compare,_Allocator>& __rhs)
320     { __lhs.swap(__rhs); }
321 } // namespace __gnu_debug_def
322 
323 #endif
324