Install gcc-4.4.0-tdm-1-core-2.tar.gz
[msysgit.git] / mingw / lib / gcc / mingw32 / 4.3.3 / include / c++ / ext / pb_ds / detail / debug_map_base.hpp
blob086a50fc5fd412139e10f2c779a1a6b2894fa5b7
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007 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 2, 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 // 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
29 // Public License.
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
40 // warranty.
42 /**
43 * @file debug_map_base.hpp
44 * Contains a debug-mode base for all maps.
47 #ifndef PB_DS_DEBUG_MAP_BASE_HPP
48 #define PB_DS_DEBUG_MAP_BASE_HPP
50 #ifdef _GLIBCXX_DEBUG
52 #include <list>
53 #include <utility>
54 #include <cstdlib>
55 #include <ext/throw_allocator.h>
56 #include <debug/debug.h>
58 namespace __gnu_pbds
60 namespace detail
62 // Need std::pair ostream extractor.
63 template<typename _CharT, typename _Traits, typename _Tp1, typename _Tp2>
64 inline std::basic_ostream<_CharT, _Traits>&
65 operator<<(std::basic_ostream<_CharT, _Traits>& __out,
66 const std::pair<_Tp1, _Tp2>& p)
67 { return (__out << '(' << p.first << ',' << p.second << ')'); }
69 #define PB_DS_CLASS_T_DEC \
70 template<typename Key, class Eq_Fn, typename Const_Key_Reference>
72 #define PB_DS_CLASS_C_DEC \
73 debug_map_base<Key, Eq_Fn, Const_Key_Reference>
75 template<typename Key, class Eq_Fn, typename Const_Key_Reference>
76 class debug_map_base
78 private:
79 typedef typename std::allocator< Key> key_allocator;
81 typedef typename key_allocator::size_type size_type;
83 typedef Const_Key_Reference const_key_reference;
85 protected:
86 debug_map_base();
88 debug_map_base(const PB_DS_CLASS_C_DEC& other);
90 ~debug_map_base();
92 inline void
93 insert_new(const_key_reference r_key);
95 inline void
96 erase_existing(const_key_reference r_key);
98 void
99 clear();
101 inline void
102 check_key_exists(const_key_reference r_key) const;
104 inline void
105 check_key_does_not_exist(const_key_reference r_key) const;
107 inline void
108 check_size(size_type size) const;
110 void
111 swap(PB_DS_CLASS_C_DEC& other);
113 template<typename Cmp_Fn>
114 void
115 split(const_key_reference, Cmp_Fn, PB_DS_CLASS_C_DEC&);
117 void
118 join(PB_DS_CLASS_C_DEC& other);
120 private:
121 typedef std::list< Key> key_set;
122 typedef typename key_set::iterator key_set_iterator;
123 typedef typename key_set::const_iterator const_key_set_iterator;
125 #ifdef _GLIBCXX_DEBUG
126 void
127 assert_valid() const;
128 #endif
130 const_key_set_iterator
131 find(const_key_reference r_key) const;
133 key_set_iterator
134 find(const_key_reference r_key);
136 key_set m_key_set;
137 Eq_Fn m_eq;
140 PB_DS_CLASS_T_DEC
141 PB_DS_CLASS_C_DEC::
142 debug_map_base()
143 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
145 PB_DS_CLASS_T_DEC
146 PB_DS_CLASS_C_DEC::
147 debug_map_base(const PB_DS_CLASS_C_DEC& other) : m_key_set(other.m_key_set)
148 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
150 PB_DS_CLASS_T_DEC
151 PB_DS_CLASS_C_DEC::
152 ~debug_map_base()
153 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
155 PB_DS_CLASS_T_DEC
156 inline void
157 PB_DS_CLASS_C_DEC::
158 insert_new(const_key_reference r_key)
160 _GLIBCXX_DEBUG_ONLY(assert_valid();)
161 __gnu_cxx::throw_allocator<char> alloc;
162 const double orig_throw_prob = alloc.get_throw_prob();
163 alloc.set_throw_prob(0);
164 if (find(r_key) != m_key_set.end())
166 std::cerr << "insert_new" << r_key << std::endl;
167 std::abort();
172 m_key_set.push_back(r_key);
174 catch(...)
176 std::cerr << "insert_new" << r_key << std::endl;
177 std::abort();
179 alloc.set_throw_prob(orig_throw_prob);
180 _GLIBCXX_DEBUG_ONLY(assert_valid();)
183 PB_DS_CLASS_T_DEC
184 inline void
185 PB_DS_CLASS_C_DEC::
186 erase_existing(const_key_reference r_key)
188 _GLIBCXX_DEBUG_ONLY(assert_valid();)
189 key_set_iterator it = find(r_key);
190 if (it == m_key_set.end())
192 std::cerr << "erase_existing" << r_key << std::endl;
193 std::abort();
195 m_key_set.erase(it);
196 _GLIBCXX_DEBUG_ONLY(assert_valid();)
199 PB_DS_CLASS_T_DEC
200 void
201 PB_DS_CLASS_C_DEC::
202 clear()
204 _GLIBCXX_DEBUG_ONLY(assert_valid();)
205 m_key_set.clear();
206 _GLIBCXX_DEBUG_ONLY(assert_valid();)
209 PB_DS_CLASS_T_DEC
210 inline void
211 PB_DS_CLASS_C_DEC::
212 check_key_exists(const_key_reference r_key) const
214 _GLIBCXX_DEBUG_ONLY(assert_valid();)
215 if (find(r_key) == m_key_set.end())
217 std::cerr << "check_key_exists" << r_key << std::endl;
218 std::abort();
220 _GLIBCXX_DEBUG_ONLY(assert_valid();)
223 PB_DS_CLASS_T_DEC
224 inline void
225 PB_DS_CLASS_C_DEC::
226 check_key_does_not_exist(const_key_reference r_key) const
228 _GLIBCXX_DEBUG_ONLY(assert_valid();)
229 if (find(r_key) != m_key_set.end())
231 using std::cerr;
232 using std::endl;
233 cerr << "check_key_does_not_exist" << r_key << endl;
234 std::abort();
238 PB_DS_CLASS_T_DEC
239 inline void
240 PB_DS_CLASS_C_DEC::
241 check_size(size_type size) const
243 _GLIBCXX_DEBUG_ONLY(assert_valid();)
244 const size_type key_set_size = m_key_set.size();
245 if (size != key_set_size)
247 std::cerr << "check_size " << size
248 << " " << key_set_size << std::endl;
249 std::abort();
251 _GLIBCXX_DEBUG_ONLY(assert_valid();)
254 PB_DS_CLASS_T_DEC
255 void
256 PB_DS_CLASS_C_DEC::
257 swap(PB_DS_CLASS_C_DEC& other)
259 _GLIBCXX_DEBUG_ONLY(assert_valid();)
260 m_key_set.swap(other.m_key_set);
261 _GLIBCXX_DEBUG_ONLY(assert_valid();)
264 PB_DS_CLASS_T_DEC
265 typename PB_DS_CLASS_C_DEC::const_key_set_iterator
266 PB_DS_CLASS_C_DEC::
267 find(const_key_reference r_key) const
269 _GLIBCXX_DEBUG_ONLY(assert_valid();)
270 typedef const_key_set_iterator iterator_type;
271 for (iterator_type it = m_key_set.begin(); it != m_key_set.end(); ++it)
272 if (m_eq(*it, r_key))
273 return it;
274 return m_key_set.end();
277 PB_DS_CLASS_T_DEC
278 typename PB_DS_CLASS_C_DEC::key_set_iterator
279 PB_DS_CLASS_C_DEC::
280 find(const_key_reference r_key)
282 _GLIBCXX_DEBUG_ONLY(assert_valid();)
283 key_set_iterator it = m_key_set.begin();
284 while (it != m_key_set.end())
286 if (m_eq(*it, r_key))
287 return it;
288 ++it;
290 return it;
291 _GLIBCXX_DEBUG_ONLY(assert_valid();)
294 #ifdef _GLIBCXX_DEBUG
295 PB_DS_CLASS_T_DEC
296 void
297 PB_DS_CLASS_C_DEC::
298 assert_valid() const
300 const_key_set_iterator prime_it = m_key_set.begin();
301 while (prime_it != m_key_set.end())
303 const_key_set_iterator sec_it = prime_it;
304 ++sec_it;
305 while (sec_it != m_key_set.end())
307 _GLIBCXX_DEBUG_ASSERT(!m_eq(*sec_it, *prime_it));
308 _GLIBCXX_DEBUG_ASSERT(!m_eq(*prime_it, *sec_it));
309 ++sec_it;
311 ++prime_it;
314 #endif
316 PB_DS_CLASS_T_DEC
317 template<typename Cmp_Fn>
318 void
319 PB_DS_CLASS_C_DEC::
320 split(const_key_reference r_key, Cmp_Fn cmp_fn, PB_DS_CLASS_C_DEC& other)
322 __gnu_cxx::throw_allocator<char> alloc;
323 const double orig_throw_prob = alloc.get_throw_prob();
324 alloc.set_throw_prob(0);
325 other.clear();
326 key_set_iterator it = m_key_set.begin();
327 while (it != m_key_set.end())
328 if (cmp_fn(r_key, * it))
330 other.insert_new(*it);
331 it = m_key_set.erase(it);
333 else
334 ++it;
335 alloc.set_throw_prob(orig_throw_prob);
338 PB_DS_CLASS_T_DEC
339 void
340 PB_DS_CLASS_C_DEC::
341 join(PB_DS_CLASS_C_DEC& other)
343 __gnu_cxx::throw_allocator<char> alloc;
344 const double orig_throw_prob = alloc.get_throw_prob();
345 alloc.set_throw_prob(0);
346 key_set_iterator it = other.m_key_set.begin();
347 while (it != other.m_key_set.end())
349 insert_new(*it);
350 it = other.m_key_set.erase(it);
352 _GLIBCXX_DEBUG_ASSERT(other.m_key_set.empty());
353 alloc.set_throw_prob(orig_throw_prob);
356 #undef PB_DS_CLASS_T_DEC
357 #undef PB_DS_CLASS_C_DEC
359 } // namespace detail
360 } // namespace __gnu_pbds
362 #endif
364 #endif