3 // Copyright (C) 2005, 2006 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 map_debug_base.hpp
44 * Contains a debug-mode base for all maps.
47 #ifndef PB_DS_MAP_DEBUG_BASE_HPP
48 #define PB_DS_MAP_DEBUG_BASE_HPP
54 #include <ext/throw_allocator.h>
55 #include <debug/debug.h>
62 #define PB_DS_CLASS_T_DEC \
63 template<typename Key, class Eq_Fn, typename Const_Key_Reference>
65 #define PB_DS_CLASS_C_DEC \
66 map_debug_base<Key, Eq_Fn, Const_Key_Reference>
68 template<typename Key, class Eq_Fn, typename Const_Key_Reference>
72 typedef typename std::allocator< Key> key_allocator;
74 typedef typename key_allocator::size_type size_type;
76 typedef Const_Key_Reference const_key_reference;
81 map_debug_base(const PB_DS_CLASS_C_DEC& other);
86 insert_new(const_key_reference r_key);
89 erase_existing(const_key_reference r_key);
95 check_key_exists(const_key_reference r_key) const;
98 check_key_does_not_exist(const_key_reference r_key) const;
101 check_size(size_type size) const;
104 swap(PB_DS_CLASS_C_DEC& other);
106 template<typename Cmp_Fn>
108 split(const_key_reference, Cmp_Fn, PB_DS_CLASS_C_DEC&);
111 join(PB_DS_CLASS_C_DEC& other);
114 typedef std::list< Key> key_set;
115 typedef typename key_set::iterator key_set_iterator;
116 typedef typename key_set::const_iterator const_key_set_iterator;
118 #ifdef _GLIBCXX_DEBUG
120 assert_valid() const;
123 const_key_set_iterator
124 find(const_key_reference r_key) const;
127 find(const_key_reference r_key);
136 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
140 map_debug_base(const PB_DS_CLASS_C_DEC& other) : m_key_set(other.m_key_set)
141 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
146 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
151 insert_new(const_key_reference r_key)
153 _GLIBCXX_DEBUG_ONLY(assert_valid();)
154 __gnu_cxx::throw_allocator<char> alloc;
155 const double orig_throw_prob = alloc.get_throw_prob();
156 alloc.set_throw_prob(0);
157 if (find(r_key) != m_key_set.end())
159 std::cerr << "insert_new " << r_key << std::endl;
165 m_key_set.push_back(r_key);
169 std::cerr << "insert_new 1" << r_key << std::endl;
172 alloc.set_throw_prob(orig_throw_prob);
173 _GLIBCXX_DEBUG_ONLY(assert_valid();)
179 erase_existing(const_key_reference r_key)
181 _GLIBCXX_DEBUG_ONLY(assert_valid();)
182 key_set_iterator it = find(r_key);
183 if (it == m_key_set.end())
185 std::cerr << "erase_existing " << r_key << std::endl;
189 _GLIBCXX_DEBUG_ONLY(assert_valid();)
197 _GLIBCXX_DEBUG_ONLY(assert_valid();)
199 _GLIBCXX_DEBUG_ONLY(assert_valid();)
205 check_key_exists(const_key_reference r_key) const
207 _GLIBCXX_DEBUG_ONLY(assert_valid();)
208 if (find(r_key) == m_key_set.end())
210 std::cerr << "check_key_exists " << r_key << std::endl;
213 _GLIBCXX_DEBUG_ONLY(assert_valid();)
219 check_key_does_not_exist(const_key_reference r_key) const
221 _GLIBCXX_DEBUG_ONLY(assert_valid();)
222 if (find(r_key) != m_key_set.end())
224 std::cerr << "check_key_does_not_exist " << r_key << std::endl;
232 check_size(size_type size) const
234 _GLIBCXX_DEBUG_ONLY(assert_valid();)
235 const size_type key_set_size = m_key_set.size();
236 if (size != key_set_size)
238 std::cerr << "check_size " << size
239 << " " << key_set_size << std::endl;
242 _GLIBCXX_DEBUG_ONLY(assert_valid();)
248 swap(PB_DS_CLASS_C_DEC& other)
250 _GLIBCXX_DEBUG_ONLY(assert_valid();)
251 m_key_set.swap(other.m_key_set);
252 _GLIBCXX_DEBUG_ONLY(assert_valid();)
256 typename PB_DS_CLASS_C_DEC::const_key_set_iterator
258 find(const_key_reference r_key) const
260 _GLIBCXX_DEBUG_ONLY(assert_valid();)
261 typedef const_key_set_iterator iterator_type;
262 for (iterator_type it = m_key_set.begin(); it != m_key_set.end(); ++it)
263 if (m_eq(*it, r_key))
265 return m_key_set.end();
269 typename PB_DS_CLASS_C_DEC::key_set_iterator
271 find(const_key_reference r_key)
273 _GLIBCXX_DEBUG_ONLY(assert_valid();)
274 key_set_iterator it = m_key_set.begin();
275 while (it != m_key_set.end())
277 if (m_eq(*it, r_key))
282 _GLIBCXX_DEBUG_ONLY(assert_valid();)
285 #ifdef _GLIBCXX_DEBUG
291 const_key_set_iterator prime_it = m_key_set.begin();
292 while (prime_it != m_key_set.end())
294 const_key_set_iterator sec_it = prime_it;
296 while (sec_it != m_key_set.end())
298 _GLIBCXX_DEBUG_ASSERT(!m_eq(*sec_it, *prime_it));
299 _GLIBCXX_DEBUG_ASSERT(!m_eq(*prime_it, *sec_it));
308 template<typename Cmp_Fn>
311 split(const_key_reference r_key, Cmp_Fn cmp_fn, PB_DS_CLASS_C_DEC& other)
313 __gnu_cxx::throw_allocator<char> alloc;
314 const double orig_throw_prob = alloc.get_throw_prob();
315 alloc.set_throw_prob(0);
317 key_set_iterator it = m_key_set.begin();
318 while (it != m_key_set.end())
319 if (cmp_fn(r_key, * it))
321 other.insert_new(*it);
322 it = m_key_set.erase(it);
326 alloc.set_throw_prob(orig_throw_prob);
332 join(PB_DS_CLASS_C_DEC& other)
334 __gnu_cxx::throw_allocator<char> alloc;
335 const double orig_throw_prob = alloc.get_throw_prob();
336 alloc.set_throw_prob(0);
337 key_set_iterator it = other.m_key_set.begin();
338 while (it != other.m_key_set.end())
341 it = other.m_key_set.erase(it);
343 _GLIBCXX_DEBUG_ASSERT(other.m_key_set.empty());
344 alloc.set_throw_prob(orig_throw_prob);
347 #undef PB_DS_CLASS_T_DEC
348 #undef PB_DS_CLASS_C_DEC
350 } // namespace detail