]> git.ipfire.org Git - thirdparty/gcc.git/blob
411c4f28b146a0814cc753485b67dee5e8dd1cbd
[thirdparty/gcc.git] /
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006 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.
10
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.
15
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.
20
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.
30
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
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.
41
42 /**
43 * @file left_child_next_sibling_heap_.hpp
44 * Contains an implementation class for a basic heap.
45 */
46
47 #ifndef PB_DS_LEFT_CHILD_NEXT_SIBLING_HEAP_HPP
48 #define PB_DS_LEFT_CHILD_NEXT_SIBLING_HEAP_HPP
49
50 /*
51 * Based on CLRS.
52 */
53
54 #include <iterator>
55 #include <ext/pb_ds/detail/cond_dealtor.hpp>
56 #include <ext/pb_ds/detail/type_utils.hpp>
57 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/node.hpp>
58 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/const_point_iterator.hpp>
59 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/const_iterator.hpp>
60 #ifdef PB_DS_LC_NS_HEAP_TRACE_
61 #include <iostream>
62 #endif // #ifdef PB_DS_LC_NS_HEAP_TRACE_
63
64 namespace pb_ds
65 {
66 namespace detail
67 {
68
69 #ifdef PB_DS_LC_NS_HEAP_DEBUG_
70 #define PB_DS_DBG_ASSERT(X) assert(X)
71 #define PB_DS_DBG_VERIFY(X) assert(X)
72 #define PB_DS_DBG_ONLY(X) X
73 #else // #ifdef PB_DS_LC_NS_HEAP_DEBUG_
74 #define PB_DS_DBG_ASSERT(X)
75 #define PB_DS_DBG_VERIFY(X) {if((X)==0);}
76 #define PB_DS_DBG_ONLY(X) ;
77 #endif // #ifdef PB_DS_LC_NS_HEAP_DEBUG_
78
79 #ifdef PB_DS_LC_NS_HEAP_DEBUG_
80 #define PB_DS_CLASS_T_DEC \
81 template< \
82 typename Value_Type, \
83 class Cmp_Fn, \
84 typename Node_Metadata, \
85 class Allocator, \
86 bool Single_Link_Roots>
87 #else // #ifdef PB_DS_LC_NS_HEAP_DEBUG_
88 #define PB_DS_CLASS_T_DEC \
89 template< \
90 typename Value_Type, \
91 class Cmp_Fn, \
92 typename Node_Metadata, \
93 class Allocator>
94 #endif // #ifdef PB_DS_LC_NS_HEAP_DEBUG_
95
96 #ifdef PB_DS_LC_NS_HEAP_DEBUG_
97 #define PB_DS_CLASS_C_DEC \
98 left_child_next_sibling_heap_< \
99 Value_Type, \
100 Cmp_Fn, \
101 Node_Metadata, \
102 Allocator, \
103 Single_Link_Roots>
104 #else // #ifdef PB_DS_LC_NS_HEAP_DEBUG_
105 #define PB_DS_CLASS_C_DEC \
106 left_child_next_sibling_heap_< \
107 Value_Type, \
108 Cmp_Fn, \
109 Node_Metadata, \
110 Allocator>
111 #endif // #ifdef PB_DS_LC_NS_HEAP_DEBUG_
112
113 /**
114 * class description = "Base class for some types of h3ap$">
115 **/
116 #ifdef PB_DS_LC_NS_HEAP_DEBUG_
117 template<typename Value_Type,
118 class Cmp_Fn,
119 typename Node_Metadata,
120 class Allocator,
121 bool Single_Link_Roots>
122 #else // #ifdef PB_DS_LC_NS_HEAP_DEBUG_
123 template<typename Value_Type,
124 class Cmp_Fn,
125 typename Node_Metadata,
126 class Allocator>
127 #endif // #ifdef PB_DS_LC_NS_HEAP_DEBUG_
128 class left_child_next_sibling_heap_ : public Cmp_Fn
129 {
130
131 protected:
132 typedef
133 typename Allocator::template rebind<
134 left_child_next_sibling_heap_node_<
135 Value_Type,
136 Node_Metadata,
137 Allocator> >::other
138 node_allocator;
139
140 typedef typename node_allocator::value_type node;
141
142 typedef typename node_allocator::pointer node_pointer;
143
144 typedef typename node_allocator::const_pointer const_node_pointer;
145
146 typedef Node_Metadata node_metadata;
147
148 typedef std::pair< node_pointer, node_pointer> node_pointer_pair;
149
150 private:
151 typedef cond_dealtor< node, Allocator> cond_dealtor_t;
152
153 enum
154 {
155 simple_value = is_simple<
156 Value_Type>::value
157 };
158
159 typedef integral_constant<int, simple_value> no_throw_copies_t;
160
161 public:
162
163 typedef typename Allocator::size_type size_type;
164
165 typedef typename Allocator::difference_type difference_type;
166
167 typedef Value_Type value_type;
168
169 typedef
170 typename Allocator::template rebind<
171 value_type>::other::pointer
172 pointer;
173
174 typedef
175 typename Allocator::template rebind<
176 value_type>::other::const_pointer
177 const_pointer;
178
179 typedef
180 typename Allocator::template rebind<
181 value_type>::other::reference
182 reference;
183
184 typedef
185 typename Allocator::template rebind<
186 value_type>::other::const_reference
187 const_reference;
188
189 typedef
190 left_child_next_sibling_heap_node_const_point_iterator_<
191 node,
192 Allocator>
193 const_point_iterator;
194
195 typedef const_point_iterator point_iterator;
196
197 typedef
198 left_child_next_sibling_heap_const_iterator_<
199 node,
200 Allocator>
201 const_iterator;
202
203 typedef const_iterator iterator;
204
205 typedef Cmp_Fn cmp_fn;
206
207 typedef Allocator allocator;
208
209 public:
210
211 left_child_next_sibling_heap_();
212
213 left_child_next_sibling_heap_(const Cmp_Fn& r_cmp_fn);
214
215 left_child_next_sibling_heap_(const PB_DS_CLASS_C_DEC& other);
216
217 void
218 swap(PB_DS_CLASS_C_DEC& other);
219
220 ~left_child_next_sibling_heap_();
221
222 inline bool
223 empty() const;
224
225 inline size_type
226 size() const;
227
228 inline size_type
229 max_size() const;
230
231 Cmp_Fn&
232 get_cmp_fn();
233
234 const Cmp_Fn&
235 get_cmp_fn() const;
236
237 inline iterator
238 begin();
239
240 inline const_iterator
241 begin() const;
242
243 inline iterator
244 end();
245
246 inline const_iterator
247 end() const;
248
249 void
250 clear();
251
252 #ifdef PB_DS_LC_NS_HEAP_TRACE_
253
254 void
255 trace() const;
256
257 #endif // #ifdef PB_DS_LC_NS_HEAP_TRACE_
258
259 protected:
260
261 inline node_pointer
262 get_new_node_for_insert(const_reference r_val);
263
264 inline static void
265 make_child_of(node_pointer p_nd, node_pointer p_new_parent);
266
267 void
268 value_swap(PB_DS_CLASS_C_DEC& other);
269
270 inline static node_pointer
271 parent(node_pointer p_nd);
272
273 inline void
274 swap_with_parent(node_pointer p_nd, node_pointer p_parent);
275
276 void
277 bubble_to_top(node_pointer p_nd);
278
279 inline void
280 actual_erase_node(node_pointer p_nd);
281
282 void
283 clear_imp(node_pointer p_nd);
284
285 void
286 to_linked_list();
287
288 template<typename Pred>
289 node_pointer
290 prune(Pred pred);
291
292 #ifdef PB_DS_LC_NS_HEAP_DEBUG_
293
294 void
295 assert_valid() const;
296
297 void
298 assert_node_consistent(const_node_pointer p_nd, bool single_link) const;
299
300 static size_type
301 size_under_node(const_node_pointer p_nd);
302
303 static size_type
304 degree(const_node_pointer p_nd);
305
306 #endif // #ifdef PB_DS_LC_NS_HEAP_DEBUG_
307
308 #ifdef PB_DS_LC_NS_HEAP_TRACE_
309
310 static void
311 trace_node(const_node_pointer, size_type level);
312
313 #endif // #ifdef PB_DS_LC_NS_HEAP_TRACE_
314
315 protected:
316 node_pointer m_p_root;
317
318 size_type m_size;
319
320 private:
321 #ifdef PB_DS_LC_NS_HEAP_DEBUG_
322
323 void
324 assert_iterators() const;
325
326 void
327 assert_size() const;
328
329 static size_type
330 size_from_node(const_node_pointer p_nd);
331
332 #endif // #ifdef PB_DS_LC_NS_HEAP_DEBUG_
333
334 node_pointer
335 recursive_copy_node(const_node_pointer p_nd);
336
337 inline node_pointer
338 get_new_node_for_insert(const_reference r_val, false_type);
339
340 inline node_pointer
341 get_new_node_for_insert(const_reference r_val, true_type);
342
343 #ifdef PB_DS_LC_NS_HEAP_TRACE_
344
345 template<typename Metadata_>
346 static void
347 trace_node_metadata(const_node_pointer p_nd, type_to_type<Metadata_>);
348
349 static void
350 trace_node_metadata(const_node_pointer, type_to_type<null_left_child_next_sibling_heap_node_metadata>);
351
352 #endif // #ifdef PB_DS_LC_NS_HEAP_TRACE_
353
354 private:
355 static node_allocator s_node_allocator;
356
357 static no_throw_copies_t s_no_throw_copies_ind;
358 };
359
360 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/constructors_destructor_fn_imps.hpp>
361 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/iterators_fn_imps.hpp>
362 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/debug_fn_imps.hpp>
363 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/trace_fn_imps.hpp>
364 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/insert_fn_imps.hpp>
365 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/erase_fn_imps.hpp>
366 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/info_fn_imps.hpp>
367 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/policy_access_fn_imps.hpp>
368
369 #undef PB_DS_CLASS_C_DEC
370
371 #undef PB_DS_CLASS_T_DEC
372
373 #undef PB_DS_DBG_ASSERT
374 #undef PB_DS_DBG_VERIFY
375 #undef PB_DS_DBG_ONLY
376
377 } // namespace detail
378 } // namespace pb_ds
379
380 #endif // #ifndef PB_DS_LEFT_CHILD_NEXT_SIBLING_HEAP_HPP