3 // Copyright (C) 2005-2023 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 detail/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, typename Eq_Fn, typename Const_Key_Reference>
67 #define PB_DS_CLASS_C_DEC \
68 debug_map_base<Key, Eq_Fn, Const_Key_Reference>
71 template<typename Key, typename Eq_Fn, typename Const_Key_Reference>
75 typedef Const_Key_Reference key_const_reference;
76 typedef std::_GLIBCXX_STD_C::list<Key> key_repository;
77 typedef typename key_repository::size_type size_type;
78 typedef typename key_repository::iterator iterator;
79 typedef typename key_repository::const_iterator const_iterator;
84 debug_map_base(const PB_DS_CLASS_C_DEC&);
89 insert_new(key_const_reference);
92 erase_existing(key_const_reference);
98 check_key_exists(key_const_reference, const char*, int) const;
101 check_key_does_not_exist(key_const_reference, const char*, int) const;
104 check_size(size_type, const char*, int) const;
107 swap(PB_DS_CLASS_C_DEC&);
109 template<typename Cmp_Fn>
111 split(key_const_reference, Cmp_Fn, PB_DS_CLASS_C_DEC&);
114 join(PB_DS_CLASS_C_DEC&, bool with_cleanup = true);
118 assert_valid(const char*, int) const;
121 find(key_const_reference) const;
124 find(key_const_reference);
126 key_repository m_keys;
133 { PB_DS_ASSERT_VALID((*this)) }
137 debug_map_base(const PB_DS_CLASS_C_DEC& other)
138 : m_keys(other.m_keys), m_eq(other.m_eq)
139 { PB_DS_ASSERT_VALID((*this)) }
144 { PB_DS_ASSERT_VALID((*this)) }
149 insert_new(key_const_reference r_key)
151 PB_DS_ASSERT_VALID((*this))
153 if (find(r_key) != m_keys.end())
155 std::cerr << "insert_new key already present " << r_key << std::endl;
161 m_keys.push_back(r_key);
165 std::cerr << "insert_new " << r_key << std::endl;
169 PB_DS_ASSERT_VALID((*this))
175 erase_existing(key_const_reference r_key)
177 PB_DS_ASSERT_VALID((*this))
178 iterator it = find(r_key);
179 if (it == m_keys.end())
181 std::cerr << "erase_existing" << r_key << std::endl;
185 PB_DS_ASSERT_VALID((*this))
193 PB_DS_ASSERT_VALID((*this))
195 PB_DS_ASSERT_VALID((*this))
201 check_key_exists(key_const_reference r_key,
202 const char* __file, int __line) const
204 assert_valid(__file, __line);
205 if (find(r_key) == m_keys.end())
207 std::cerr << __file << ':' << __line << ": check_key_exists "
208 << r_key << std::endl;
216 check_key_does_not_exist(key_const_reference r_key,
217 const char* __file, int __line) const
219 assert_valid(__file, __line);
220 if (find(r_key) != m_keys.end())
224 cerr << __file << ':' << __line << ": check_key_does_not_exist "
233 check_size(size_type size, const char* __file, int __line) const
235 assert_valid(__file, __line);
236 const size_type keys_size = m_keys.size();
237 if (size != keys_size)
239 std::cerr << __file << ':' << __line << ": check_size "
240 << size << " != " << keys_size << std::endl;
248 swap(PB_DS_CLASS_C_DEC& other)
250 PB_DS_ASSERT_VALID((*this))
251 m_keys.swap(other.m_keys);
252 std::swap(m_eq, other.m_eq);
253 PB_DS_ASSERT_VALID((*this))
257 typename PB_DS_CLASS_C_DEC::const_iterator
259 find(key_const_reference r_key) const
261 PB_DS_ASSERT_VALID((*this))
262 typedef const_iterator iterator_type;
263 for (iterator_type it = m_keys.begin(); it != m_keys.end(); ++it)
264 if (m_eq(*it, r_key))
270 typename PB_DS_CLASS_C_DEC::iterator
272 find(key_const_reference r_key)
274 PB_DS_ASSERT_VALID((*this))
275 iterator it = m_keys.begin();
276 while (it != m_keys.end())
278 if (m_eq(*it, r_key))
288 assert_valid(const char* __file, int __line) const
290 const_iterator prime_it = m_keys.begin();
291 while (prime_it != m_keys.end())
293 const_iterator sec_it = prime_it;
295 while (sec_it != m_keys.end())
297 PB_DS_DEBUG_VERIFY(!m_eq(*sec_it, *prime_it));
298 PB_DS_DEBUG_VERIFY(!m_eq(*prime_it, *sec_it));
306 template<typename Cmp_Fn>
309 split(key_const_reference r_key, Cmp_Fn cmp_fn, PB_DS_CLASS_C_DEC& other)
312 iterator it = m_keys.begin();
313 while (it != m_keys.end())
314 if (cmp_fn(r_key, *it))
316 other.insert_new(*it);
317 it = m_keys.erase(it);
326 join(PB_DS_CLASS_C_DEC& other, bool with_cleanup)
328 iterator it = other.m_keys.begin();
329 while (it != other.m_keys.end())
333 it = other.m_keys.erase(it);
337 _GLIBCXX_DEBUG_ASSERT(!with_cleanup || other.m_keys.empty());
340 #undef PB_DS_CLASS_T_DEC
341 #undef PB_DS_CLASS_C_DEC
343 } // namespace detail
344 } // namespace __gnu_pbds