Import gcc-4.4.1
[dragonfly.git] / contrib / gcc-4.4 / libstdc++-v3 / include / ext / pb_ds / detail / debug_map_base.hpp
blob6f2defb648eb52a68f2191a0822572ab0109ec4f
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 //
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
9 // version.
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
34 // warranty.
36 /**
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
44 #ifdef _GLIBCXX_DEBUG
46 #include <list>
47 #include <utility>
48 #include <cstdlib>
49 #include <iostream>
50 #include <ext/throw_allocator.h>
51 #include <debug/debug.h>
53 namespace __gnu_pbds
55 namespace detail
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>
71 class debug_map_base
73 private:
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;
80 protected:
81 debug_map_base();
83 debug_map_base(const PB_DS_CLASS_C_DEC& other);
85 ~debug_map_base();
87 inline void
88 insert_new(const_key_reference r_key);
90 inline void
91 erase_existing(const_key_reference r_key);
93 void
94 clear();
96 inline void
97 check_key_exists(const_key_reference r_key) const;
99 inline void
100 check_key_does_not_exist(const_key_reference r_key) const;
102 inline void
103 check_size(size_type size) const;
105 void
106 swap(PB_DS_CLASS_C_DEC& other);
108 template<typename Cmp_Fn>
109 void
110 split(const_key_reference, Cmp_Fn, PB_DS_CLASS_C_DEC&);
112 void
113 join(PB_DS_CLASS_C_DEC& other);
115 private:
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
121 void
122 assert_valid() const;
123 #endif
125 const_key_set_iterator
126 find(const_key_reference r_key) const;
128 key_set_iterator
129 find(const_key_reference r_key);
131 key_set m_key_set;
132 Eq_Fn m_eq;
135 PB_DS_CLASS_T_DEC
136 PB_DS_CLASS_C_DEC::
137 debug_map_base()
138 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
140 PB_DS_CLASS_T_DEC
141 PB_DS_CLASS_C_DEC::
142 debug_map_base(const PB_DS_CLASS_C_DEC& other) : m_key_set(other.m_key_set)
143 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
145 PB_DS_CLASS_T_DEC
146 PB_DS_CLASS_C_DEC::
147 ~debug_map_base()
148 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
150 PB_DS_CLASS_T_DEC
151 inline void
152 PB_DS_CLASS_C_DEC::
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;
162 std::abort();
165 __try
167 m_key_set.push_back(r_key);
169 __catch(...)
171 std::cerr << "insert_new" << r_key << std::endl;
172 std::abort();
174 alloc.set_throw_prob(orig_throw_prob);
175 _GLIBCXX_DEBUG_ONLY(assert_valid();)
178 PB_DS_CLASS_T_DEC
179 inline void
180 PB_DS_CLASS_C_DEC::
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;
188 std::abort();
190 m_key_set.erase(it);
191 _GLIBCXX_DEBUG_ONLY(assert_valid();)
194 PB_DS_CLASS_T_DEC
195 void
196 PB_DS_CLASS_C_DEC::
197 clear()
199 _GLIBCXX_DEBUG_ONLY(assert_valid();)
200 m_key_set.clear();
201 _GLIBCXX_DEBUG_ONLY(assert_valid();)
204 PB_DS_CLASS_T_DEC
205 inline void
206 PB_DS_CLASS_C_DEC::
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;
213 std::abort();
215 _GLIBCXX_DEBUG_ONLY(assert_valid();)
218 PB_DS_CLASS_T_DEC
219 inline void
220 PB_DS_CLASS_C_DEC::
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())
226 using std::cerr;
227 using std::endl;
228 cerr << "check_key_does_not_exist" << r_key << endl;
229 std::abort();
233 PB_DS_CLASS_T_DEC
234 inline void
235 PB_DS_CLASS_C_DEC::
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;
244 std::abort();
246 _GLIBCXX_DEBUG_ONLY(assert_valid();)
249 PB_DS_CLASS_T_DEC
250 void
251 PB_DS_CLASS_C_DEC::
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();)
259 PB_DS_CLASS_T_DEC
260 typename PB_DS_CLASS_C_DEC::const_key_set_iterator
261 PB_DS_CLASS_C_DEC::
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))
268 return it;
269 return m_key_set.end();
272 PB_DS_CLASS_T_DEC
273 typename PB_DS_CLASS_C_DEC::key_set_iterator
274 PB_DS_CLASS_C_DEC::
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))
282 return it;
283 ++it;
285 return it;
286 _GLIBCXX_DEBUG_ONLY(assert_valid();)
289 #ifdef _GLIBCXX_DEBUG
290 PB_DS_CLASS_T_DEC
291 void
292 PB_DS_CLASS_C_DEC::
293 assert_valid() const
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;
299 ++sec_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));
304 ++sec_it;
306 ++prime_it;
309 #endif
311 PB_DS_CLASS_T_DEC
312 template<typename Cmp_Fn>
313 void
314 PB_DS_CLASS_C_DEC::
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);
320 other.clear();
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);
328 else
329 ++it;
330 alloc.set_throw_prob(orig_throw_prob);
333 PB_DS_CLASS_T_DEC
334 void
335 PB_DS_CLASS_C_DEC::
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())
344 insert_new(*it);
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
357 #endif
359 #endif