3 // Copyright (C) 2005, 2006, 2007 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
55 #include <ext/throw_allocator.h>
56 #include <debug/debug.h>
62 // Need std::pair ostream extractor.
63 template<typename _CharT, typename _Traits, typename _Tp1, typename _Tp2>
64 inline std::basic_ostream<_CharT, _Traits>&
65 operator<<(std::basic_ostream<_CharT, _Traits>& __out,
66 const std::pair<_Tp1, _Tp2>& p)
67 { return (__out << '(' << p.first << ',' << p.second << ')'); }
69 #define PB_DS_CLASS_T_DEC \
70 template<typename Key, class Eq_Fn, typename Const_Key_Reference>
72 #define PB_DS_CLASS_C_DEC \
73 debug_map_base<Key, Eq_Fn, Const_Key_Reference>
75 template<typename Key, class Eq_Fn, typename Const_Key_Reference>
79 typedef typename std::allocator< Key> key_allocator;
81 typedef typename key_allocator::size_type size_type;
83 typedef Const_Key_Reference const_key_reference;
88 debug_map_base(const PB_DS_CLASS_C_DEC& other);
93 insert_new(const_key_reference r_key);
96 erase_existing(const_key_reference r_key);
102 check_key_exists(const_key_reference r_key) const;
105 check_key_does_not_exist(const_key_reference r_key) const;
108 check_size(size_type size) const;
111 swap(PB_DS_CLASS_C_DEC& other);
113 template<typename Cmp_Fn>
115 split(const_key_reference, Cmp_Fn, PB_DS_CLASS_C_DEC&);
118 join(PB_DS_CLASS_C_DEC& other);
121 typedef std::list< Key> key_set;
122 typedef typename key_set::iterator key_set_iterator;
123 typedef typename key_set::const_iterator const_key_set_iterator;
125 #ifdef _GLIBCXX_DEBUG
127 assert_valid() const;
130 const_key_set_iterator
131 find(const_key_reference r_key) const;
134 find(const_key_reference r_key);
143 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
147 debug_map_base(const PB_DS_CLASS_C_DEC& other) : m_key_set(other.m_key_set)
148 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
153 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
158 insert_new(const_key_reference r_key)
160 _GLIBCXX_DEBUG_ONLY(assert_valid();)
161 __gnu_cxx::throw_allocator<char> alloc;
162 const double orig_throw_prob = alloc.get_throw_prob();
163 alloc.set_throw_prob(0);
164 if (find(r_key) != m_key_set.end())
166 std::cerr << "insert_new" << r_key << std::endl;
172 m_key_set.push_back(r_key);
176 std::cerr << "insert_new" << r_key << std::endl;
179 alloc.set_throw_prob(orig_throw_prob);
180 _GLIBCXX_DEBUG_ONLY(assert_valid();)
186 erase_existing(const_key_reference r_key)
188 _GLIBCXX_DEBUG_ONLY(assert_valid();)
189 key_set_iterator it = find(r_key);
190 if (it == m_key_set.end())
192 std::cerr << "erase_existing" << r_key << std::endl;
196 _GLIBCXX_DEBUG_ONLY(assert_valid();)
204 _GLIBCXX_DEBUG_ONLY(assert_valid();)
206 _GLIBCXX_DEBUG_ONLY(assert_valid();)
212 check_key_exists(const_key_reference r_key) const
214 _GLIBCXX_DEBUG_ONLY(assert_valid();)
215 if (find(r_key) == m_key_set.end())
217 std::cerr << "check_key_exists" << r_key << std::endl;
220 _GLIBCXX_DEBUG_ONLY(assert_valid();)
226 check_key_does_not_exist(const_key_reference r_key) const
228 _GLIBCXX_DEBUG_ONLY(assert_valid();)
229 if (find(r_key) != m_key_set.end())
233 cerr << "check_key_does_not_exist" << r_key << endl;
241 check_size(size_type size) const
243 _GLIBCXX_DEBUG_ONLY(assert_valid();)
244 const size_type key_set_size = m_key_set.size();
245 if (size != key_set_size)
247 std::cerr << "check_size " << size
248 << " " << key_set_size << std::endl;
251 _GLIBCXX_DEBUG_ONLY(assert_valid();)
257 swap(PB_DS_CLASS_C_DEC& other)
259 _GLIBCXX_DEBUG_ONLY(assert_valid();)
260 m_key_set.swap(other.m_key_set);
261 _GLIBCXX_DEBUG_ONLY(assert_valid();)
265 typename PB_DS_CLASS_C_DEC::const_key_set_iterator
267 find(const_key_reference r_key) const
269 _GLIBCXX_DEBUG_ONLY(assert_valid();)
270 typedef const_key_set_iterator iterator_type;
271 for (iterator_type it = m_key_set.begin(); it != m_key_set.end(); ++it)
272 if (m_eq(*it, r_key))
274 return m_key_set.end();
278 typename PB_DS_CLASS_C_DEC::key_set_iterator
280 find(const_key_reference r_key)
282 _GLIBCXX_DEBUG_ONLY(assert_valid();)
283 key_set_iterator it = m_key_set.begin();
284 while (it != m_key_set.end())
286 if (m_eq(*it, r_key))
291 _GLIBCXX_DEBUG_ONLY(assert_valid();)
294 #ifdef _GLIBCXX_DEBUG
300 const_key_set_iterator prime_it = m_key_set.begin();
301 while (prime_it != m_key_set.end())
303 const_key_set_iterator sec_it = prime_it;
305 while (sec_it != m_key_set.end())
307 _GLIBCXX_DEBUG_ASSERT(!m_eq(*sec_it, *prime_it));
308 _GLIBCXX_DEBUG_ASSERT(!m_eq(*prime_it, *sec_it));
317 template<typename Cmp_Fn>
320 split(const_key_reference r_key, Cmp_Fn cmp_fn, PB_DS_CLASS_C_DEC& other)
322 __gnu_cxx::throw_allocator<char> alloc;
323 const double orig_throw_prob = alloc.get_throw_prob();
324 alloc.set_throw_prob(0);
326 key_set_iterator it = m_key_set.begin();
327 while (it != m_key_set.end())
328 if (cmp_fn(r_key, * it))
330 other.insert_new(*it);
331 it = m_key_set.erase(it);
335 alloc.set_throw_prob(orig_throw_prob);
341 join(PB_DS_CLASS_C_DEC& other)
343 __gnu_cxx::throw_allocator<char> alloc;
344 const double orig_throw_prob = alloc.get_throw_prob();
345 alloc.set_throw_prob(0);
346 key_set_iterator it = other.m_key_set.begin();
347 while (it != other.m_key_set.end())
350 it = other.m_key_set.erase(it);
352 _GLIBCXX_DEBUG_ASSERT(other.m_key_set.empty());
353 alloc.set_throw_prob(orig_throw_prob);
356 #undef PB_DS_CLASS_T_DEC
357 #undef PB_DS_CLASS_C_DEC
359 } // namespace detail
360 } // namespace __gnu_pbds