3 // Copyright (C) 2005, 2006, 2009 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 binary_heap_.hpp
38 * Contains an implementation class for a binary heap.
41 #ifndef PB_DS_BINARY_HEAP_HPP
42 #define PB_DS_BINARY_HEAP_HPP
50 #include <ext/pb_ds/detail/cond_dealtor.hpp>
51 #include <ext/pb_ds/detail/cond_dealtor.hpp>
52 #include <ext/pb_ds/detail/type_utils.hpp>
53 #include <ext/pb_ds/detail/binary_heap_/entry_cmp.hpp>
54 #include <ext/pb_ds/detail/binary_heap_/entry_pred.hpp>
55 #include <ext/pb_ds/detail/binary_heap_/resize_policy.hpp>
56 #include <ext/pb_ds/detail/binary_heap_/const_point_iterator.hpp>
57 #include <ext/pb_ds/detail/binary_heap_/const_iterator.hpp>
58 #ifdef PB_DS_BINARY_HEAP_TRACE_
61 #include <ext/pb_ds/detail/type_utils.hpp>
62 #include <debug/debug.h>
68 #define PB_DS_CLASS_T_DEC \
69 template<typename Value_Type, class Cmp_Fn, class Allocator>
71 #define PB_DS_CLASS_C_DEC \
72 binary_heap_<Value_Type, Cmp_Fn, Allocator>
74 #define PB_DS_ENTRY_CMP_DEC \
75 entry_cmp<Value_Type, Cmp_Fn, is_simple<Value_Type>::value, Allocator>::type
77 #define PB_DS_RESIZE_POLICY_DEC \
78 __gnu_pbds::detail::resize_policy<typename Allocator::size_type>
81 * class description = "Base class for some types of h3ap$">
83 template<typename Value_Type, class Cmp_Fn, class Allocator>
84 class binary_heap_ : public PB_DS_ENTRY_CMP_DEC,
85 public PB_DS_RESIZE_POLICY_DEC
91 simple_value = is_simple<Value_Type>::value
94 typedef integral_constant<int, simple_value> no_throw_copies_t;
97 typename Allocator::template rebind<
102 typename __conditional_type<
105 typename value_allocator::pointer>::__type
109 typename Allocator::template rebind<
113 typedef typename entry_allocator::pointer entry_pointer;
115 typedef typename PB_DS_ENTRY_CMP_DEC entry_cmp;
117 typedef PB_DS_RESIZE_POLICY_DEC resize_policy;
127 typedef typename Allocator::size_type size_type;
129 typedef typename Allocator::difference_type difference_type;
131 typedef Value_Type value_type;
134 typename Allocator::template rebind<
135 value_type>::other::pointer
139 typename Allocator::template rebind<
140 value_type>::other::const_pointer
144 typename Allocator::template rebind<
145 value_type>::other::reference
149 typename Allocator::template rebind<
150 value_type>::other::const_reference
154 binary_heap_const_point_iterator_<
159 const_point_iterator;
161 typedef const_point_iterator point_iterator;
164 binary_heap_const_iterator_<
171 typedef const_iterator iterator;
173 typedef Cmp_Fn cmp_fn;
175 typedef Allocator allocator_type;
181 binary_heap_(const Cmp_Fn& r_cmp_fn);
183 binary_heap_(const PB_DS_CLASS_C_DEC& other);
186 swap(PB_DS_CLASS_C_DEC& other);
205 inline point_iterator
206 push(const_reference r_val);
209 modify(point_iterator it, const_reference r_new_val);
211 inline const_reference
218 erase(point_iterator it);
220 template<typename Pred>
221 typename PB_DS_CLASS_C_DEC::size_type
225 erase_at(entry_pointer a_entries, size_type size, false_type);
228 erase_at(entry_pointer a_entries, size_type size, true_type);
233 inline const_iterator
239 inline const_iterator
245 template<typename Pred>
247 split(Pred pred, PB_DS_CLASS_C_DEC& other);
250 join(PB_DS_CLASS_C_DEC& other);
252 #ifdef PB_DS_BINARY_HEAP_TRACE_
259 template<typename It>
261 copy_from_range(It first_it, It last_it);
266 value_swap(PB_DS_CLASS_C_DEC& other);
269 insert_value(const_reference r_val, false_type);
272 insert_value(value_type val, true_type);
275 insert_entry(entry e);
278 resize_for_insert_if_needed();
281 swap_value_imp(entry_pointer p_e, value_type new_val, true_type);
284 swap_value_imp(entry_pointer p_e, const_reference r_new_val, false_type);
287 fix(entry_pointer p_e);
289 inline const_reference
290 top_imp(true_type) const;
292 inline const_reference
293 top_imp(false_type) const;
295 inline static size_type
296 left_child(size_type i);
298 inline static size_type
299 right_child(size_type i);
301 inline static size_type
305 resize_for_erase_if_needed();
307 template<typename Pred>
309 partition(Pred pred);
311 #ifdef _GLIBCXX_DEBUG
313 assert_valid() const;
316 #ifdef PB_DS_BINARY_HEAP_TRACE_
318 trace_entry(const entry& r_e, false_type) const;
321 trace_entry(const entry& r_e, true_type) const;
325 static entry_allocator s_entry_allocator;
327 static value_allocator s_value_allocator;
329 static no_throw_copies_t s_no_throw_copies_ind;
333 size_type m_actual_size;
335 entry_pointer m_a_entries;
338 #include <ext/pb_ds/detail/binary_heap_/insert_fn_imps.hpp>
339 #include <ext/pb_ds/detail/binary_heap_/constructors_destructor_fn_imps.hpp>
340 #include <ext/pb_ds/detail/binary_heap_/iterators_fn_imps.hpp>
341 #include <ext/pb_ds/detail/binary_heap_/debug_fn_imps.hpp>
342 #include <ext/pb_ds/detail/binary_heap_/trace_fn_imps.hpp>
343 #include <ext/pb_ds/detail/binary_heap_/erase_fn_imps.hpp>
344 #include <ext/pb_ds/detail/binary_heap_/info_fn_imps.hpp>
345 #include <ext/pb_ds/detail/binary_heap_/find_fn_imps.hpp>
346 #include <ext/pb_ds/detail/binary_heap_/split_join_fn_imps.hpp>
347 #include <ext/pb_ds/detail/binary_heap_/policy_access_fn_imps.hpp>
349 #undef PB_DS_CLASS_C_DEC
350 #undef PB_DS_CLASS_T_DEC
351 #undef PB_DS_ENTRY_CMP_DEC
352 #undef PB_DS_RESIZE_POLICY_DEC
354 } // namespace detail
355 } // namespace __gnu_pbds