3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
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 terms
8 // of the GNU General Public License as published by the Free Software
9 // Foundation; either version 3, or (at your option) any later
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
28 // Permission to use, copy, modify, sell, and distribute this software
29 // is hereby granted without fee, provided that the above copyright
30 // notice appears in all copies, and that both that copyright notice
31 // and this permission notice appear in supporting documentation. None
32 // of the above authors, nor IBM Haifa Research Laboratories, make any
33 // representation about the suitability of this software for any
34 // purpose. It is provided "as is" without express or implied
38 * @file detail/debug_map_base.hpp
39 * Contains a debug-mode base for all maps.
42 #ifndef PB_DS_DEBUG_MAP_BASE_HPP
43 #define PB_DS_DEBUG_MAP_BASE_HPP
51 #include <ext/throw_allocator.h>
52 #include <debug/debug.h>
58 // Need std::pair ostream extractor.
59 template<typename _CharT, typename _Traits, typename _Tp1, typename _Tp2>
60 inline std::basic_ostream<_CharT, _Traits>&
61 operator<<(std::basic_ostream<_CharT, _Traits>& __out,
62 const std::pair<_Tp1, _Tp2>& p)
63 { return (__out << '(' << p.first << ',' << p.second << ')'); }
65 #define PB_DS_CLASS_T_DEC \
66 template<typename Key, typename Eq_Fn, typename Const_Key_Reference>
68 #define PB_DS_CLASS_C_DEC \
69 debug_map_base<Key, Eq_Fn, Const_Key_Reference>
72 template<typename Key, typename Eq_Fn, typename Const_Key_Reference>
76 typedef Const_Key_Reference key_const_reference;
77 typedef std::_GLIBCXX_STD_C::list<Key> key_repository;
78 typedef typename key_repository::size_type size_type;
79 typedef typename key_repository::iterator iterator;
80 typedef typename key_repository::const_iterator const_iterator;
85 debug_map_base(const PB_DS_CLASS_C_DEC&);
90 insert_new(key_const_reference);
93 erase_existing(key_const_reference);
99 check_key_exists(key_const_reference, const char*, int) const;
102 check_key_does_not_exist(key_const_reference, const char*, int) const;
105 check_size(size_type, const char*, int) const;
108 swap(PB_DS_CLASS_C_DEC&);
110 template<typename Cmp_Fn>
112 split(key_const_reference, Cmp_Fn, PB_DS_CLASS_C_DEC&);
115 join(PB_DS_CLASS_C_DEC&, bool with_cleanup = true);
119 assert_valid(const char*, int) const;
122 find(key_const_reference) const;
125 find(key_const_reference);
127 key_repository m_keys;
134 { PB_DS_ASSERT_VALID((*this)) }
138 debug_map_base(const PB_DS_CLASS_C_DEC& other)
139 : m_keys(other.m_keys), m_eq(other.m_eq)
140 { PB_DS_ASSERT_VALID((*this)) }
145 { PB_DS_ASSERT_VALID((*this)) }
150 insert_new(key_const_reference r_key)
152 PB_DS_ASSERT_VALID((*this))
154 if (find(r_key) != m_keys.end())
156 std::cerr << "insert_new key already present " << r_key << std::endl;
162 m_keys.push_back(r_key);
166 std::cerr << "insert_new " << r_key << std::endl;
170 PB_DS_ASSERT_VALID((*this))
176 erase_existing(key_const_reference r_key)
178 PB_DS_ASSERT_VALID((*this))
179 iterator it = find(r_key);
180 if (it == m_keys.end())
182 std::cerr << "erase_existing" << r_key << std::endl;
186 PB_DS_ASSERT_VALID((*this))
194 PB_DS_ASSERT_VALID((*this))
196 PB_DS_ASSERT_VALID((*this))
202 check_key_exists(key_const_reference r_key,
203 const char* __file, int __line) const
205 assert_valid(__file, __line);
206 if (find(r_key) == m_keys.end())
208 std::cerr << __file << ':' << __line << ": check_key_exists "
209 << r_key << std::endl;
217 check_key_does_not_exist(key_const_reference r_key,
218 const char* __file, int __line) const
220 assert_valid(__file, __line);
221 if (find(r_key) != m_keys.end())
225 cerr << __file << ':' << __line << ": check_key_does_not_exist "
234 check_size(size_type size, const char* __file, int __line) const
236 assert_valid(__file, __line);
237 const size_type keys_size = m_keys.size();
238 if (size != keys_size)
240 std::cerr << __file << ':' << __line << ": check_size "
241 << size << " != " << keys_size << std::endl;
249 swap(PB_DS_CLASS_C_DEC& other)
251 PB_DS_ASSERT_VALID((*this))
252 m_keys.swap(other.m_keys);
253 std::swap(m_eq, other.m_eq);
254 PB_DS_ASSERT_VALID((*this))
258 typename PB_DS_CLASS_C_DEC::const_iterator
260 find(key_const_reference r_key) const
262 PB_DS_ASSERT_VALID((*this))
263 typedef const_iterator iterator_type;
264 for (iterator_type it = m_keys.begin(); it != m_keys.end(); ++it)
265 if (m_eq(*it, r_key))
271 typename PB_DS_CLASS_C_DEC::iterator
273 find(key_const_reference r_key)
275 PB_DS_ASSERT_VALID((*this))
276 iterator it = m_keys.begin();
277 while (it != m_keys.end())
279 if (m_eq(*it, r_key))
289 assert_valid(const char* __file, int __line) const
291 const_iterator prime_it = m_keys.begin();
292 while (prime_it != m_keys.end())
294 const_iterator sec_it = prime_it;
296 while (sec_it != m_keys.end())
298 PB_DS_DEBUG_VERIFY(!m_eq(*sec_it, *prime_it));
299 PB_DS_DEBUG_VERIFY(!m_eq(*prime_it, *sec_it));
307 template<typename Cmp_Fn>
310 split(key_const_reference r_key, Cmp_Fn cmp_fn, PB_DS_CLASS_C_DEC& other)
313 iterator it = m_keys.begin();
314 while (it != m_keys.end())
315 if (cmp_fn(r_key, *it))
317 other.insert_new(*it);
318 it = m_keys.erase(it);
327 join(PB_DS_CLASS_C_DEC& other, bool with_cleanup)
329 iterator it = other.m_keys.begin();
330 while (it != other.m_keys.end())
334 it = other.m_keys.erase(it);
338 _GLIBCXX_DEBUG_ASSERT(!with_cleanup || other.m_keys.empty());
341 #undef PB_DS_CLASS_T_DEC
342 #undef PB_DS_CLASS_C_DEC
344 } // namespace detail
345 } // namespace __gnu_pbds