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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
27 // Permission to use, copy, modify, sell, and distribute this software
28 // is hereby granted without fee, provided that the above copyright
29 // notice appears in all copies, and that both that copyright notice
30 // and this permission notice appear in supporting documentation. None
31 // of the above authors, nor IBM Haifa Research Laboratories, make any
32 // representation about the suitability of this software for any
33 // purpose. It is provided "as is" without express or implied
37 * @file debug_map_base.hpp
38 * Contains a debug-mode base for all maps.
41 #ifndef PB_DS_DEBUG_MAP_BASE_HPP
42 #define PB_DS_DEBUG_MAP_BASE_HPP
50 #include <ext/throw_allocator.h>
51 #include <debug/debug.h>
57 // Need std::pair ostream extractor.
58 template<typename _CharT, typename _Traits, typename _Tp1, typename _Tp2>
59 inline std::basic_ostream<_CharT, _Traits>&
60 operator<<(std::basic_ostream<_CharT, _Traits>& __out,
61 const std::pair<_Tp1, _Tp2>& p)
62 { return (__out << '(' << p.first << ',' << p.second << ')'); }
64 #define PB_DS_CLASS_T_DEC \
65 template<typename Key, class Eq_Fn, typename Const_Key_Reference>
67 #define PB_DS_CLASS_C_DEC \
68 debug_map_base<Key, Eq_Fn, Const_Key_Reference>
70 template<typename Key, class Eq_Fn, typename Const_Key_Reference>
74 typedef typename std::allocator< Key> key_allocator;
76 typedef typename key_allocator::size_type size_type;
78 typedef Const_Key_Reference const_key_reference;
83 debug_map_base(const PB_DS_CLASS_C_DEC& other);
88 insert_new(const_key_reference r_key);
91 erase_existing(const_key_reference r_key);
97 check_key_exists(const_key_reference r_key) const;
100 check_key_does_not_exist(const_key_reference r_key) const;
103 check_size(size_type size) const;
106 swap(PB_DS_CLASS_C_DEC& other);
108 template<typename Cmp_Fn>
110 split(const_key_reference, Cmp_Fn, PB_DS_CLASS_C_DEC&);
113 join(PB_DS_CLASS_C_DEC& other);
116 typedef std::list< Key> key_set;
117 typedef typename key_set::iterator key_set_iterator;
118 typedef typename key_set::const_iterator const_key_set_iterator;
120 #ifdef _GLIBCXX_DEBUG
122 assert_valid() const;
125 const_key_set_iterator
126 find(const_key_reference r_key) const;
129 find(const_key_reference r_key);
138 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
142 debug_map_base(const PB_DS_CLASS_C_DEC& other) : m_key_set(other.m_key_set)
143 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
148 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
153 insert_new(const_key_reference r_key)
155 _GLIBCXX_DEBUG_ONLY(assert_valid();)
156 __gnu_cxx::throw_allocator<char> alloc;
157 const double orig_throw_prob = alloc.get_throw_prob();
158 alloc.set_throw_prob(0);
159 if (find(r_key) != m_key_set.end())
161 std::cerr << "insert_new" << r_key << std::endl;
167 m_key_set.push_back(r_key);
171 std::cerr << "insert_new" << r_key << std::endl;
174 alloc.set_throw_prob(orig_throw_prob);
175 _GLIBCXX_DEBUG_ONLY(assert_valid();)
181 erase_existing(const_key_reference r_key)
183 _GLIBCXX_DEBUG_ONLY(assert_valid();)
184 key_set_iterator it = find(r_key);
185 if (it == m_key_set.end())
187 std::cerr << "erase_existing" << r_key << std::endl;
191 _GLIBCXX_DEBUG_ONLY(assert_valid();)
199 _GLIBCXX_DEBUG_ONLY(assert_valid();)
201 _GLIBCXX_DEBUG_ONLY(assert_valid();)
207 check_key_exists(const_key_reference r_key) const
209 _GLIBCXX_DEBUG_ONLY(assert_valid();)
210 if (find(r_key) == m_key_set.end())
212 std::cerr << "check_key_exists" << r_key << std::endl;
215 _GLIBCXX_DEBUG_ONLY(assert_valid();)
221 check_key_does_not_exist(const_key_reference r_key) const
223 _GLIBCXX_DEBUG_ONLY(assert_valid();)
224 if (find(r_key) != m_key_set.end())
228 cerr << "check_key_does_not_exist" << r_key << endl;
236 check_size(size_type size) const
238 _GLIBCXX_DEBUG_ONLY(assert_valid();)
239 const size_type key_set_size = m_key_set.size();
240 if (size != key_set_size)
242 std::cerr << "check_size " << size
243 << " " << key_set_size << std::endl;
246 _GLIBCXX_DEBUG_ONLY(assert_valid();)
252 swap(PB_DS_CLASS_C_DEC& other)
254 _GLIBCXX_DEBUG_ONLY(assert_valid();)
255 m_key_set.swap(other.m_key_set);
256 _GLIBCXX_DEBUG_ONLY(assert_valid();)
260 typename PB_DS_CLASS_C_DEC::const_key_set_iterator
262 find(const_key_reference r_key) const
264 _GLIBCXX_DEBUG_ONLY(assert_valid();)
265 typedef const_key_set_iterator iterator_type;
266 for (iterator_type it = m_key_set.begin(); it != m_key_set.end(); ++it)
267 if (m_eq(*it, r_key))
269 return m_key_set.end();
273 typename PB_DS_CLASS_C_DEC::key_set_iterator
275 find(const_key_reference r_key)
277 _GLIBCXX_DEBUG_ONLY(assert_valid();)
278 key_set_iterator it = m_key_set.begin();
279 while (it != m_key_set.end())
281 if (m_eq(*it, r_key))
286 _GLIBCXX_DEBUG_ONLY(assert_valid();)
289 #ifdef _GLIBCXX_DEBUG
295 const_key_set_iterator prime_it = m_key_set.begin();
296 while (prime_it != m_key_set.end())
298 const_key_set_iterator sec_it = prime_it;
300 while (sec_it != m_key_set.end())
302 _GLIBCXX_DEBUG_ASSERT(!m_eq(*sec_it, *prime_it));
303 _GLIBCXX_DEBUG_ASSERT(!m_eq(*prime_it, *sec_it));
312 template<typename Cmp_Fn>
315 split(const_key_reference r_key, Cmp_Fn cmp_fn, PB_DS_CLASS_C_DEC& other)
317 __gnu_cxx::throw_allocator<char> alloc;
318 const double orig_throw_prob = alloc.get_throw_prob();
319 alloc.set_throw_prob(0);
321 key_set_iterator it = m_key_set.begin();
322 while (it != m_key_set.end())
323 if (cmp_fn(r_key, * it))
325 other.insert_new(*it);
326 it = m_key_set.erase(it);
330 alloc.set_throw_prob(orig_throw_prob);
336 join(PB_DS_CLASS_C_DEC& other)
338 __gnu_cxx::throw_allocator<char> alloc;
339 const double orig_throw_prob = alloc.get_throw_prob();
340 alloc.set_throw_prob(0);
341 key_set_iterator it = other.m_key_set.begin();
342 while (it != other.m_key_set.end())
345 it = other.m_key_set.erase(it);
347 _GLIBCXX_DEBUG_ASSERT(other.m_key_set.empty());
348 alloc.set_throw_prob(orig_throw_prob);
351 #undef PB_DS_CLASS_T_DEC
352 #undef PB_DS_CLASS_C_DEC
354 } // namespace detail
355 } // namespace __gnu_pbds