3 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
43 * @file debug_map_base.hpp
44 * Contains a debug-mode base for all maps.
47 #ifndef PB_DS_DEBUG_MAP_BASE_HPP
48 #define PB_DS_DEBUG_MAP_BASE_HPP
56 #include <ext/throw_allocator.h>
57 #include <debug/debug.h>
63 // Need std::pair ostream extractor.
64 template<typename _CharT
, typename _Traits
, typename _Tp1
, typename _Tp2
>
65 inline std::basic_ostream
<_CharT
, _Traits
>&
66 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __out
,
67 const std::pair
<_Tp1
, _Tp2
>& p
)
68 { return (__out
<< '(' << p
.first
<< ',' << p
.second
<< ')'); }
70 #define PB_DS_CLASS_T_DEC \
71 template<typename Key, class Eq_Fn, typename Const_Key_Reference>
73 #define PB_DS_CLASS_C_DEC \
74 debug_map_base<Key, Eq_Fn, Const_Key_Reference>
76 template<typename Key
, class Eq_Fn
, typename Const_Key_Reference
>
80 typedef typename
std::allocator
< Key
> key_allocator
;
82 typedef typename
key_allocator::size_type size_type
;
84 typedef Const_Key_Reference const_key_reference
;
89 debug_map_base(const PB_DS_CLASS_C_DEC
& other
);
94 insert_new(const_key_reference r_key
);
97 erase_existing(const_key_reference r_key
);
103 check_key_exists(const_key_reference r_key
) const;
106 check_key_does_not_exist(const_key_reference r_key
) const;
109 check_size(size_type size
) const;
112 swap(PB_DS_CLASS_C_DEC
& other
);
114 template<typename Cmp_Fn
>
116 split(const_key_reference
, Cmp_Fn
, PB_DS_CLASS_C_DEC
&);
119 join(PB_DS_CLASS_C_DEC
& other
);
122 typedef std::list
< Key
> key_set
;
123 typedef typename
key_set::iterator key_set_iterator
;
124 typedef typename
key_set::const_iterator const_key_set_iterator
;
126 #ifdef _GLIBCXX_DEBUG
128 assert_valid() const;
131 const_key_set_iterator
132 find(const_key_reference r_key
) const;
135 find(const_key_reference r_key
);
144 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
148 debug_map_base(const PB_DS_CLASS_C_DEC
& other
) : m_key_set(other
.m_key_set
)
149 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
154 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
159 insert_new(const_key_reference r_key
)
161 _GLIBCXX_DEBUG_ONLY(assert_valid();)
162 __gnu_cxx::throw_allocator
<char> alloc
;
163 const double orig_throw_prob
= alloc
.get_throw_prob();
164 alloc
.set_throw_prob(0);
165 if (find(r_key
) != m_key_set
.end())
167 std::cerr
<< "insert_new" << r_key
<< std::endl
;
173 m_key_set
.push_back(r_key
);
177 std::cerr
<< "insert_new" << r_key
<< std::endl
;
180 alloc
.set_throw_prob(orig_throw_prob
);
181 _GLIBCXX_DEBUG_ONLY(assert_valid();)
187 erase_existing(const_key_reference r_key
)
189 _GLIBCXX_DEBUG_ONLY(assert_valid();)
190 key_set_iterator it
= find(r_key
);
191 if (it
== m_key_set
.end())
193 std::cerr
<< "erase_existing" << r_key
<< std::endl
;
197 _GLIBCXX_DEBUG_ONLY(assert_valid();)
205 _GLIBCXX_DEBUG_ONLY(assert_valid();)
207 _GLIBCXX_DEBUG_ONLY(assert_valid();)
213 check_key_exists(const_key_reference r_key
) const
215 _GLIBCXX_DEBUG_ONLY(assert_valid();)
216 if (find(r_key
) == m_key_set
.end())
218 std::cerr
<< "check_key_exists" << r_key
<< std::endl
;
221 _GLIBCXX_DEBUG_ONLY(assert_valid();)
227 check_key_does_not_exist(const_key_reference r_key
) const
229 _GLIBCXX_DEBUG_ONLY(assert_valid();)
230 if (find(r_key
) != m_key_set
.end())
234 cerr
<< "check_key_does_not_exist" << r_key
<< endl
;
242 check_size(size_type size
) const
244 _GLIBCXX_DEBUG_ONLY(assert_valid();)
245 const size_type key_set_size
= m_key_set
.size();
246 if (size
!= key_set_size
)
248 std::cerr
<< "check_size " << size
249 << " " << key_set_size
<< std::endl
;
252 _GLIBCXX_DEBUG_ONLY(assert_valid();)
258 swap(PB_DS_CLASS_C_DEC
& other
)
260 _GLIBCXX_DEBUG_ONLY(assert_valid();)
261 m_key_set
.swap(other
.m_key_set
);
262 _GLIBCXX_DEBUG_ONLY(assert_valid();)
266 typename
PB_DS_CLASS_C_DEC::const_key_set_iterator
268 find(const_key_reference r_key
) const
270 _GLIBCXX_DEBUG_ONLY(assert_valid();)
271 typedef const_key_set_iterator iterator_type
;
272 for (iterator_type it
= m_key_set
.begin(); it
!= m_key_set
.end(); ++it
)
273 if (m_eq(*it
, r_key
))
275 return m_key_set
.end();
279 typename
PB_DS_CLASS_C_DEC::key_set_iterator
281 find(const_key_reference r_key
)
283 _GLIBCXX_DEBUG_ONLY(assert_valid();)
284 key_set_iterator it
= m_key_set
.begin();
285 while (it
!= m_key_set
.end())
287 if (m_eq(*it
, r_key
))
292 _GLIBCXX_DEBUG_ONLY(assert_valid();)
295 #ifdef _GLIBCXX_DEBUG
301 const_key_set_iterator prime_it
= m_key_set
.begin();
302 while (prime_it
!= m_key_set
.end())
304 const_key_set_iterator sec_it
= prime_it
;
306 while (sec_it
!= m_key_set
.end())
308 _GLIBCXX_DEBUG_ASSERT(!m_eq(*sec_it
, *prime_it
));
309 _GLIBCXX_DEBUG_ASSERT(!m_eq(*prime_it
, *sec_it
));
318 template<typename Cmp_Fn
>
321 split(const_key_reference r_key
, Cmp_Fn cmp_fn
, PB_DS_CLASS_C_DEC
& other
)
323 __gnu_cxx::throw_allocator
<char> alloc
;
324 const double orig_throw_prob
= alloc
.get_throw_prob();
325 alloc
.set_throw_prob(0);
327 key_set_iterator it
= m_key_set
.begin();
328 while (it
!= m_key_set
.end())
329 if (cmp_fn(r_key
, * it
))
331 other
.insert_new(*it
);
332 it
= m_key_set
.erase(it
);
336 alloc
.set_throw_prob(orig_throw_prob
);
342 join(PB_DS_CLASS_C_DEC
& other
)
344 __gnu_cxx::throw_allocator
<char> alloc
;
345 const double orig_throw_prob
= alloc
.get_throw_prob();
346 alloc
.set_throw_prob(0);
347 key_set_iterator it
= other
.m_key_set
.begin();
348 while (it
!= other
.m_key_set
.end())
351 it
= other
.m_key_set
.erase(it
);
353 _GLIBCXX_DEBUG_ASSERT(other
.m_key_set
.empty());
354 alloc
.set_throw_prob(orig_throw_prob
);
357 #undef PB_DS_CLASS_T_DEC
358 #undef PB_DS_CLASS_C_DEC
360 } // namespace detail
361 } // namespace __gnu_pbds