3 // Copyright (C) 2005, 2006 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 2, 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 // 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
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
43 * @file binary_heap_.hpp
44 * Contains an implementation class for a binary heap.
47 #ifndef PB_DS_BINARY_HEAP_HPP
48 #define PB_DS_BINARY_HEAP_HPP
56 #include <ext/pb_ds/detail/cond_dealtor.hpp>
57 #include <ext/pb_ds/detail/cond_dealtor.hpp>
58 #include <ext/pb_ds/detail/type_utils.hpp>
59 #include <ext/pb_ds/detail/binary_heap_/entry_cmp.hpp>
60 #include <ext/pb_ds/detail/binary_heap_/entry_pred.hpp>
61 #include <ext/pb_ds/detail/binary_heap_/resize_policy.hpp>
62 #include <ext/pb_ds/detail/binary_heap_/const_point_iterator.hpp>
63 #include <ext/pb_ds/detail/binary_heap_/const_iterator.hpp>
64 #ifdef PB_DS_BINARY_HEAP_TRACE_
67 #include <ext/pb_ds/detail/type_utils.hpp>
68 #include <debug/debug.h>
74 #define PB_DS_CLASS_T_DEC \
75 template<typename Value_Type, class Cmp_Fn, class Allocator>
77 #define PB_DS_CLASS_C_DEC \
78 binary_heap_<Value_Type, Cmp_Fn, Allocator>
80 #define PB_DS_ENTRY_CMP_DEC \
81 entry_cmp<Value_Type, Cmp_Fn, is_simple<Value_Type>::value, Allocator>::type
83 #define PB_DS_RESIZE_POLICY_DEC \
84 resize_policy<typename Allocator::size_type>
87 * class description = "Base class for some types of h3ap$">
89 template<typename Value_Type, class Cmp_Fn, class Allocator>
90 class binary_heap_ : public PB_DS_ENTRY_CMP_DEC,
91 public PB_DS_RESIZE_POLICY_DEC
97 simple_value = is_simple<Value_Type>::value
100 typedef integral_constant<int, simple_value> no_throw_copies_t;
103 typename Allocator::template rebind<
108 typename __conditional_type<
111 typename value_allocator::pointer>::__type
115 typename Allocator::template rebind<
119 typedef typename entry_allocator::pointer entry_pointer;
121 typedef typename PB_DS_ENTRY_CMP_DEC entry_cmp;
123 typedef PB_DS_RESIZE_POLICY_DEC resize_policy;
133 typedef typename Allocator::size_type size_type;
135 typedef typename Allocator::difference_type difference_type;
137 typedef Value_Type value_type;
140 typename Allocator::template rebind<
141 value_type>::other::pointer
145 typename Allocator::template rebind<
146 value_type>::other::const_pointer
150 typename Allocator::template rebind<
151 value_type>::other::reference
155 typename Allocator::template rebind<
156 value_type>::other::const_reference
160 binary_heap_const_point_iterator_<
165 const_point_iterator;
167 typedef const_point_iterator point_iterator;
170 binary_heap_const_iterator_<
177 typedef const_iterator iterator;
179 typedef Cmp_Fn cmp_fn;
181 typedef Allocator allocator;
187 binary_heap_(const Cmp_Fn& r_cmp_fn);
189 binary_heap_(const PB_DS_CLASS_C_DEC& other);
192 swap(PB_DS_CLASS_C_DEC& other);
211 inline point_iterator
212 push(const_reference r_val);
215 modify(point_iterator it, const_reference r_new_val);
217 inline const_reference
224 erase(point_iterator it);
226 template<typename Pred>
227 typename PB_DS_CLASS_C_DEC::size_type
231 erase_at(entry_pointer a_entries, size_type size, false_type);
234 erase_at(entry_pointer a_entries, size_type size, true_type);
239 inline const_iterator
245 inline const_iterator
251 template<typename Pred>
253 split(Pred pred, PB_DS_CLASS_C_DEC& other);
256 join(PB_DS_CLASS_C_DEC& other);
258 #ifdef PB_DS_BINARY_HEAP_TRACE_
265 template<typename It>
267 copy_from_range(It first_it, It last_it);
272 value_swap(PB_DS_CLASS_C_DEC& other);
275 insert_value(const_reference r_val, false_type);
278 insert_value(value_type val, true_type);
281 insert_entry(entry e);
284 resize_for_insert_if_needed();
287 swap_value_imp(entry_pointer p_e, value_type new_val, true_type);
290 swap_value_imp(entry_pointer p_e, const_reference r_new_val, false_type);
293 fix(entry_pointer p_e);
295 inline const_reference
296 top_imp(true_type) const;
298 inline const_reference
299 top_imp(false_type) const;
301 inline static size_type
302 left_child(size_type i);
304 inline static size_type
305 right_child(size_type i);
307 inline static size_type
311 resize_for_erase_if_needed();
313 template<typename Pred>
315 partition(Pred pred);
317 #ifdef _GLIBCXX_DEBUG
319 assert_valid() const;
322 #ifdef PB_DS_BINARY_HEAP_TRACE_
324 trace_entry(const entry& r_e, false_type) const;
327 trace_entry(const entry& r_e, true_type) const;
331 static entry_allocator s_entry_allocator;
333 static value_allocator s_value_allocator;
335 static no_throw_copies_t s_no_throw_copies_ind;
339 size_type m_actual_size;
341 entry_pointer m_a_entries;
344 #include <ext/pb_ds/detail/binary_heap_/insert_fn_imps.hpp>
345 #include <ext/pb_ds/detail/binary_heap_/constructors_destructor_fn_imps.hpp>
346 #include <ext/pb_ds/detail/binary_heap_/iterators_fn_imps.hpp>
347 #include <ext/pb_ds/detail/binary_heap_/debug_fn_imps.hpp>
348 #include <ext/pb_ds/detail/binary_heap_/trace_fn_imps.hpp>
349 #include <ext/pb_ds/detail/binary_heap_/erase_fn_imps.hpp>
350 #include <ext/pb_ds/detail/binary_heap_/info_fn_imps.hpp>
351 #include <ext/pb_ds/detail/binary_heap_/find_fn_imps.hpp>
352 #include <ext/pb_ds/detail/binary_heap_/split_join_fn_imps.hpp>
353 #include <ext/pb_ds/detail/binary_heap_/policy_access_fn_imps.hpp>
355 #undef PB_DS_CLASS_C_DEC
356 #undef PB_DS_CLASS_T_DEC
357 #undef PB_DS_ENTRY_CMP_DEC
358 #undef PB_DS_RESIZE_POLICY_DEC
360 } // namespace detail