3 // Copyright (C) 2005, 2006, 2009, 2011 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 thin_heap_/thin_heap_.hpp
38 * Contains an implementation class for a thin heap.
41 #ifndef PB_DS_THIN_HEAP_HPP
42 #define PB_DS_THIN_HEAP_HPP
45 #include <ext/pb_ds/detail/cond_dealtor.hpp>
46 #include <ext/pb_ds/detail/type_utils.hpp>
47 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/left_child_next_sibling_heap_.hpp>
48 #include <debug/debug.h>
54 #define PB_DS_CLASS_T_DEC \
55 template<typename Value_Type, typename Cmp_Fn, typename _Alloc>
57 #define PB_DS_CLASS_C_DEC \
58 thin_heap<Value_Type, Cmp_Fn, _Alloc>
61 #define PB_DS_BASE_T_P \
62 <Value_Type, Cmp_Fn, typename _Alloc::size_type, _Alloc, true>
64 #define PB_DS_BASE_T_P \
65 <Value_Type, Cmp_Fn, typename _Alloc::size_type, _Alloc>
70 * Base class for @ref priority_queue.
72 * See Tarjan and Kaplan.
74 template<typename Value_Type, typename Cmp_Fn, typename _Alloc>
76 : public left_child_next_sibling_heap PB_DS_BASE_T_P
79 typedef typename _Alloc::template rebind<Value_Type>::other __rebind_a;
80 typedef left_child_next_sibling_heap PB_DS_BASE_T_P base_type;
83 typedef typename base_type::node node;
84 typedef typename base_type::node_pointer node_pointer;
85 typedef typename base_type::node_const_pointer node_const_pointer;
88 typedef Value_Type value_type;
89 typedef Cmp_Fn cmp_fn;
90 typedef _Alloc allocator_type;
91 typedef typename _Alloc::size_type size_type;
92 typedef typename _Alloc::difference_type difference_type;
94 typedef typename __rebind_a::pointer pointer;
95 typedef typename __rebind_a::const_pointer const_pointer;
96 typedef typename __rebind_a::reference reference;
97 typedef typename __rebind_a::const_reference const_reference;
99 typedef typename base_type::point_iterator point_iterator;
100 typedef typename base_type::point_const_iterator point_const_iterator;
101 typedef typename base_type::iterator iterator;
102 typedef typename base_type::const_iterator const_iterator;
105 inline point_iterator
106 push(const_reference);
109 modify(point_iterator, const_reference);
111 inline const_reference
118 erase(point_iterator);
123 template<typename Pred>
127 template<typename Pred>
129 split(Pred, PB_DS_CLASS_C_DEC&);
132 join(PB_DS_CLASS_C_DEC&);
137 thin_heap(const Cmp_Fn&);
139 thin_heap(const PB_DS_CLASS_C_DEC&);
142 swap(PB_DS_CLASS_C_DEC&);
146 template<typename It>
148 copy_from_range(It, It);
150 #ifdef _GLIBCXX_DEBUG
152 assert_valid(const char*, int) const;
155 assert_max(const char*, int) const;
158 #ifdef PB_DS_THIN_HEAP_TRACE_
166 max_rank = (sizeof(size_type) << 4) + 2
173 update_max(node_pointer);
179 fix_root(node_pointer);
182 fix_sibling_rank_1_unmarked(node_pointer);
185 fix_sibling_rank_1_marked(node_pointer);
188 fix_sibling_general_unmarked(node_pointer);
191 fix_sibling_general_marked(node_pointer);
194 fix_child(node_pointer);
197 make_root(node_pointer);
200 make_root_and_link(node_pointer);
209 add_to_aux(node_pointer);
218 make_child_of(node_pointer, node_pointer);
221 remove_node(node_pointer);
224 join(node_pointer, node_pointer) const;
226 #ifdef _GLIBCXX_DEBUG
228 assert_node_consistent(node_const_pointer, bool, const char*, int) const;
231 assert_aux_null(const char*, int) const;
234 node_pointer m_p_max;
235 node_pointer m_a_aux[max_rank];
240 num_distinct_rank_bounds = 48
243 // Taken from the SGI implementation; acknowledged in the docs.
244 static const std::size_t g_a_rank_bounds[num_distinct_rank_bounds] =
246 /* Dealing cards... */
287 /* 40 */ 141422324ul,
288 /* 41 */ 228826127ul,
289 /* 42 */ 370248451ul,
290 /* 43 */ 599074578ul,
291 /* 44 */ 969323029ul,
292 /* 45 */ 1568397607ul,
293 /* 46 */ 2537720636ul,
294 /* 47 */ 4106118243ul
295 /* Pot's good, let's play */
298 #define PB_DS_ASSERT_NODE_CONSISTENT(_Node, _Bool) \
299 _GLIBCXX_DEBUG_ONLY(assert_node_consistent(_Node, _Bool, \
300 __FILE__, __LINE__);)
302 #define PB_DS_ASSERT_AUX_NULL(X) \
303 _GLIBCXX_DEBUG_ONLY(X.assert_aux_null(__FILE__, __LINE__);)
305 #include <ext/pb_ds/detail/thin_heap_/constructors_destructor_fn_imps.hpp>
306 #include <ext/pb_ds/detail/thin_heap_/debug_fn_imps.hpp>
307 #include <ext/pb_ds/detail/thin_heap_/trace_fn_imps.hpp>
308 #include <ext/pb_ds/detail/thin_heap_/find_fn_imps.hpp>
309 #include <ext/pb_ds/detail/thin_heap_/insert_fn_imps.hpp>
310 #include <ext/pb_ds/detail/thin_heap_/erase_fn_imps.hpp>
311 #include <ext/pb_ds/detail/thin_heap_/split_join_fn_imps.hpp>
313 #undef PB_DS_ASSERT_AUX_NULL
314 #undef PB_DS_ASSERT_NODE_CONSISTENT
315 #undef PB_DS_CLASS_C_DEC
316 #undef PB_DS_CLASS_T_DEC
317 #undef PB_DS_BASE_T_P
319 } // namespace detail
320 } // namespace __gnu_pbds