Chris@16
|
1 // boost heap: pairing heap
|
Chris@16
|
2 //
|
Chris@16
|
3 // Copyright (C) 2010 Tim Blechmann
|
Chris@16
|
4 //
|
Chris@16
|
5 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
6 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8
|
Chris@16
|
9 #ifndef BOOST_HEAP_PAIRING_HEAP_HPP
|
Chris@16
|
10 #define BOOST_HEAP_PAIRING_HEAP_HPP
|
Chris@16
|
11
|
Chris@16
|
12 #include <algorithm>
|
Chris@16
|
13 #include <utility>
|
Chris@16
|
14 #include <vector>
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/assert.hpp>
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/heap/detail/heap_comparison.hpp>
|
Chris@16
|
19 #include <boost/heap/detail/heap_node.hpp>
|
Chris@16
|
20 #include <boost/heap/policies.hpp>
|
Chris@16
|
21 #include <boost/heap/detail/stable_heap.hpp>
|
Chris@16
|
22 #include <boost/heap/detail/tree_iterator.hpp>
|
Chris@16
|
23
|
Chris@101
|
24 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@101
|
25 #pragma once
|
Chris@101
|
26 #endif
|
Chris@101
|
27
|
Chris@101
|
28
|
Chris@16
|
29 #ifndef BOOST_DOXYGEN_INVOKED
|
Chris@16
|
30 #ifdef BOOST_HEAP_SANITYCHECKS
|
Chris@16
|
31 #define BOOST_HEAP_ASSERT BOOST_ASSERT
|
Chris@16
|
32 #else
|
Chris@16
|
33 #define BOOST_HEAP_ASSERT(expression)
|
Chris@16
|
34 #endif
|
Chris@16
|
35 #endif
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost {
|
Chris@16
|
38 namespace heap {
|
Chris@16
|
39 namespace detail {
|
Chris@16
|
40
|
Chris@16
|
41 typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
|
Chris@16
|
42 boost::parameter::optional<tag::compare>,
|
Chris@16
|
43 boost::parameter::optional<tag::stable>,
|
Chris@16
|
44 boost::parameter::optional<tag::constant_time_size>,
|
Chris@16
|
45 boost::parameter::optional<tag::stability_counter_type>
|
Chris@16
|
46 > pairing_heap_signature;
|
Chris@16
|
47
|
Chris@16
|
48 template <typename T, typename Parspec>
|
Chris@16
|
49 struct make_pairing_heap_base
|
Chris@16
|
50 {
|
Chris@16
|
51 static const bool constant_time_size = parameter::binding<Parspec,
|
Chris@16
|
52 tag::constant_time_size,
|
Chris@16
|
53 boost::mpl::true_
|
Chris@16
|
54 >::type::value;
|
Chris@16
|
55 typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::type base_type;
|
Chris@16
|
56 typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::allocator_argument allocator_argument;
|
Chris@16
|
57 typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::compare_argument compare_argument;
|
Chris@16
|
58
|
Chris@16
|
59 typedef heap_node<typename base_type::internal_type, false> node_type;
|
Chris@16
|
60
|
Chris@16
|
61 typedef typename allocator_argument::template rebind<node_type>::other allocator_type;
|
Chris@16
|
62
|
Chris@16
|
63 struct type:
|
Chris@16
|
64 base_type,
|
Chris@16
|
65 allocator_type
|
Chris@16
|
66 {
|
Chris@16
|
67 type(compare_argument const & arg):
|
Chris@16
|
68 base_type(arg)
|
Chris@16
|
69 {}
|
Chris@16
|
70
|
Chris@16
|
71 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
Chris@16
|
72 type(type const & rhs):
|
Chris@16
|
73 base_type(rhs), allocator_type(rhs)
|
Chris@16
|
74 {}
|
Chris@16
|
75
|
Chris@16
|
76 type(type && rhs):
|
Chris@16
|
77 base_type(std::move(static_cast<base_type&>(rhs))),
|
Chris@16
|
78 allocator_type(std::move(static_cast<allocator_type&>(rhs)))
|
Chris@16
|
79 {}
|
Chris@16
|
80
|
Chris@16
|
81 type & operator=(type && rhs)
|
Chris@16
|
82 {
|
Chris@16
|
83 base_type::operator=(std::move(static_cast<base_type&>(rhs)));
|
Chris@16
|
84 allocator_type::operator=(std::move(static_cast<allocator_type&>(rhs)));
|
Chris@16
|
85 return *this;
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 type & operator=(type const & rhs)
|
Chris@16
|
89 {
|
Chris@16
|
90 base_type::operator=(static_cast<base_type const &>(rhs));
|
Chris@16
|
91 allocator_type::operator=(static_cast<const allocator_type&>(rhs));
|
Chris@16
|
92 return *this;
|
Chris@16
|
93 }
|
Chris@16
|
94 #endif
|
Chris@16
|
95 };
|
Chris@16
|
96 };
|
Chris@16
|
97
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 /**
|
Chris@16
|
101 * \class pairing_heap
|
Chris@16
|
102 * \brief pairing heap
|
Chris@16
|
103 *
|
Chris@16
|
104 * Pairing heaps are self-adjusting binary heaps. Although design and implementation are rather simple,
|
Chris@16
|
105 * the complexity analysis is yet unsolved. For details, consult:
|
Chris@16
|
106 *
|
Chris@16
|
107 * Pettie, Seth (2005), "Towards a final analysis of pairing heaps",
|
Chris@16
|
108 * Proc. 46th Annual IEEE Symposium on Foundations of Computer Science, pp. 174-183
|
Chris@16
|
109 *
|
Chris@16
|
110 * The template parameter T is the type to be managed by the container.
|
Chris@16
|
111 * The user can specify additional options and if no options are provided default options are used.
|
Chris@16
|
112 *
|
Chris@16
|
113 * The container supports the following options:
|
Chris@16
|
114 * - \c boost::heap::compare<>, defaults to \c compare<std::less<T> >
|
Chris@16
|
115 * - \c boost::heap::stable<>, defaults to \c stable<false>
|
Chris@16
|
116 * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type<boost::uintmax_t>
|
Chris@16
|
117 * - \c boost::heap::allocator<>, defaults to \c allocator<std::allocator<T> >
|
Chris@16
|
118 * - \c boost::heap::constant_time_size<>, defaults to \c constant_time_size<true>
|
Chris@16
|
119 *
|
Chris@16
|
120 *
|
Chris@16
|
121 */
|
Chris@16
|
122 #ifdef BOOST_DOXYGEN_INVOKED
|
Chris@16
|
123 template<class T, class ...Options>
|
Chris@16
|
124 #else
|
Chris@16
|
125 template <typename T,
|
Chris@16
|
126 class A0 = boost::parameter::void_,
|
Chris@16
|
127 class A1 = boost::parameter::void_,
|
Chris@16
|
128 class A2 = boost::parameter::void_,
|
Chris@16
|
129 class A3 = boost::parameter::void_,
|
Chris@16
|
130 class A4 = boost::parameter::void_
|
Chris@16
|
131 >
|
Chris@16
|
132 #endif
|
Chris@16
|
133 class pairing_heap:
|
Chris@16
|
134 private detail::make_pairing_heap_base<T,
|
Chris@16
|
135 typename detail::pairing_heap_signature::bind<A0, A1, A2, A3, A4>::type
|
Chris@16
|
136 >::type
|
Chris@16
|
137 {
|
Chris@16
|
138 typedef typename detail::pairing_heap_signature::bind<A0, A1, A2, A3, A4>::type bound_args;
|
Chris@16
|
139 typedef detail::make_pairing_heap_base<T, bound_args> base_maker;
|
Chris@16
|
140 typedef typename base_maker::type super_t;
|
Chris@16
|
141
|
Chris@16
|
142 typedef typename super_t::internal_type internal_type;
|
Chris@16
|
143 typedef typename super_t::size_holder_type size_holder;
|
Chris@16
|
144 typedef typename base_maker::allocator_argument allocator_argument;
|
Chris@16
|
145
|
Chris@16
|
146 private:
|
Chris@16
|
147 template <typename Heap1, typename Heap2>
|
Chris@16
|
148 friend struct heap_merge_emulate;
|
Chris@16
|
149
|
Chris@16
|
150 #ifndef BOOST_DOXYGEN_INVOKED
|
Chris@16
|
151 struct implementation_defined:
|
Chris@16
|
152 detail::extract_allocator_types<typename base_maker::allocator_argument>
|
Chris@16
|
153 {
|
Chris@16
|
154 typedef T value_type;
|
Chris@16
|
155 typedef typename detail::extract_allocator_types<typename base_maker::allocator_argument>::size_type size_type;
|
Chris@16
|
156 typedef typename detail::extract_allocator_types<typename base_maker::allocator_argument>::reference reference;
|
Chris@16
|
157
|
Chris@16
|
158 typedef typename base_maker::compare_argument value_compare;
|
Chris@16
|
159 typedef typename base_maker::allocator_type allocator_type;
|
Chris@16
|
160
|
Chris@16
|
161 typedef typename allocator_type::pointer node_pointer;
|
Chris@16
|
162 typedef typename allocator_type::const_pointer const_node_pointer;
|
Chris@16
|
163
|
Chris@16
|
164 typedef detail::heap_node_list node_list_type;
|
Chris@16
|
165 typedef typename node_list_type::iterator node_list_iterator;
|
Chris@16
|
166 typedef typename node_list_type::const_iterator node_list_const_iterator;
|
Chris@16
|
167
|
Chris@16
|
168 typedef typename base_maker::node_type node;
|
Chris@16
|
169
|
Chris@16
|
170 typedef detail::value_extractor<value_type, internal_type, super_t> value_extractor;
|
Chris@16
|
171 typedef typename super_t::internal_compare internal_compare;
|
Chris@16
|
172 typedef detail::node_handle<node_pointer, super_t, reference> handle_type;
|
Chris@16
|
173
|
Chris@16
|
174 typedef detail::tree_iterator<node,
|
Chris@16
|
175 const value_type,
|
Chris@16
|
176 allocator_type,
|
Chris@16
|
177 value_extractor,
|
Chris@16
|
178 detail::pointer_to_reference<node>,
|
Chris@16
|
179 false,
|
Chris@16
|
180 false,
|
Chris@16
|
181 value_compare
|
Chris@16
|
182 > iterator;
|
Chris@16
|
183
|
Chris@16
|
184 typedef iterator const_iterator;
|
Chris@16
|
185
|
Chris@16
|
186 typedef detail::tree_iterator<node,
|
Chris@16
|
187 const value_type,
|
Chris@16
|
188 allocator_type,
|
Chris@16
|
189 value_extractor,
|
Chris@16
|
190 detail::pointer_to_reference<node>,
|
Chris@16
|
191 false,
|
Chris@16
|
192 true,
|
Chris@16
|
193 value_compare
|
Chris@16
|
194 > ordered_iterator;
|
Chris@16
|
195 };
|
Chris@16
|
196
|
Chris@16
|
197 typedef typename implementation_defined::node node;
|
Chris@16
|
198 typedef typename implementation_defined::node_pointer node_pointer;
|
Chris@16
|
199 typedef typename implementation_defined::node_list_type node_list_type;
|
Chris@16
|
200 typedef typename implementation_defined::node_list_iterator node_list_iterator;
|
Chris@16
|
201 typedef typename implementation_defined::node_list_const_iterator node_list_const_iterator;
|
Chris@16
|
202 typedef typename implementation_defined::internal_compare internal_compare;
|
Chris@16
|
203
|
Chris@16
|
204 typedef boost::intrusive::list<detail::heap_node_base<true>,
|
Chris@16
|
205 boost::intrusive::constant_time_size<false>
|
Chris@16
|
206 > node_child_list;
|
Chris@16
|
207 #endif
|
Chris@16
|
208
|
Chris@16
|
209 public:
|
Chris@16
|
210 typedef T value_type;
|
Chris@16
|
211
|
Chris@16
|
212 typedef typename implementation_defined::size_type size_type;
|
Chris@16
|
213 typedef typename implementation_defined::difference_type difference_type;
|
Chris@16
|
214 typedef typename implementation_defined::value_compare value_compare;
|
Chris@16
|
215 typedef typename implementation_defined::allocator_type allocator_type;
|
Chris@16
|
216 typedef typename implementation_defined::reference reference;
|
Chris@16
|
217 typedef typename implementation_defined::const_reference const_reference;
|
Chris@16
|
218 typedef typename implementation_defined::pointer pointer;
|
Chris@16
|
219 typedef typename implementation_defined::const_pointer const_pointer;
|
Chris@16
|
220 /// \copydoc boost::heap::priority_queue::iterator
|
Chris@16
|
221 typedef typename implementation_defined::iterator iterator;
|
Chris@16
|
222 typedef typename implementation_defined::const_iterator const_iterator;
|
Chris@16
|
223 typedef typename implementation_defined::ordered_iterator ordered_iterator;
|
Chris@16
|
224
|
Chris@16
|
225 typedef typename implementation_defined::handle_type handle_type;
|
Chris@16
|
226
|
Chris@16
|
227 static const bool constant_time_size = super_t::constant_time_size;
|
Chris@16
|
228 static const bool has_ordered_iterators = true;
|
Chris@16
|
229 static const bool is_mergable = true;
|
Chris@16
|
230 static const bool is_stable = detail::extract_stable<bound_args>::value;
|
Chris@16
|
231 static const bool has_reserve = false;
|
Chris@16
|
232
|
Chris@16
|
233 /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &)
|
Chris@16
|
234 explicit pairing_heap(value_compare const & cmp = value_compare()):
|
Chris@16
|
235 super_t(cmp), root(NULL)
|
Chris@16
|
236 {}
|
Chris@16
|
237
|
Chris@16
|
238 /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &)
|
Chris@16
|
239 pairing_heap(pairing_heap const & rhs):
|
Chris@16
|
240 super_t(rhs), root(NULL)
|
Chris@16
|
241 {
|
Chris@16
|
242 if (rhs.empty())
|
Chris@16
|
243 return;
|
Chris@16
|
244
|
Chris@16
|
245 clone_tree(rhs);
|
Chris@16
|
246 size_holder::set_size(rhs.get_size());
|
Chris@16
|
247 }
|
Chris@16
|
248
|
Chris@16
|
249 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
Chris@16
|
250 /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&)
|
Chris@16
|
251 pairing_heap(pairing_heap && rhs):
|
Chris@16
|
252 super_t(std::move(rhs)), root(rhs.root)
|
Chris@16
|
253 {
|
Chris@16
|
254 rhs.root = NULL;
|
Chris@16
|
255 }
|
Chris@16
|
256
|
Chris@16
|
257 /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&)
|
Chris@16
|
258 pairing_heap & operator=(pairing_heap && rhs)
|
Chris@16
|
259 {
|
Chris@16
|
260 super_t::operator=(std::move(rhs));
|
Chris@16
|
261 root = rhs.root;
|
Chris@16
|
262 rhs.root = NULL;
|
Chris@16
|
263 return *this;
|
Chris@16
|
264 }
|
Chris@16
|
265 #endif
|
Chris@16
|
266
|
Chris@16
|
267 /// \copydoc boost::heap::priority_queue::operator=(priority_queue const & rhs)
|
Chris@16
|
268 pairing_heap & operator=(pairing_heap const & rhs)
|
Chris@16
|
269 {
|
Chris@16
|
270 clear();
|
Chris@16
|
271 size_holder::set_size(rhs.get_size());
|
Chris@16
|
272 static_cast<super_t&>(*this) = rhs;
|
Chris@16
|
273
|
Chris@16
|
274 clone_tree(rhs);
|
Chris@16
|
275 return *this;
|
Chris@16
|
276 }
|
Chris@16
|
277
|
Chris@16
|
278 ~pairing_heap(void)
|
Chris@16
|
279 {
|
Chris@16
|
280 while (!empty())
|
Chris@16
|
281 pop();
|
Chris@16
|
282 }
|
Chris@16
|
283
|
Chris@16
|
284 /// \copydoc boost::heap::priority_queue::empty
|
Chris@16
|
285 bool empty(void) const
|
Chris@16
|
286 {
|
Chris@16
|
287 return root == NULL;
|
Chris@16
|
288 }
|
Chris@16
|
289
|
Chris@16
|
290 /// \copydoc boost::heap::binomial_heap::size
|
Chris@16
|
291 size_type size(void) const
|
Chris@16
|
292 {
|
Chris@16
|
293 if (constant_time_size)
|
Chris@16
|
294 return size_holder::get_size();
|
Chris@16
|
295
|
Chris@16
|
296 if (root == NULL)
|
Chris@16
|
297 return 0;
|
Chris@16
|
298 else
|
Chris@16
|
299 return detail::count_nodes(root);
|
Chris@16
|
300 }
|
Chris@16
|
301
|
Chris@16
|
302 /// \copydoc boost::heap::priority_queue::max_size
|
Chris@16
|
303 size_type max_size(void) const
|
Chris@16
|
304 {
|
Chris@16
|
305 return allocator_type::max_size();
|
Chris@16
|
306 }
|
Chris@16
|
307
|
Chris@16
|
308 /// \copydoc boost::heap::priority_queue::clear
|
Chris@16
|
309 void clear(void)
|
Chris@16
|
310 {
|
Chris@16
|
311 if (empty())
|
Chris@16
|
312 return;
|
Chris@16
|
313
|
Chris@16
|
314 root->template clear_subtree<allocator_type>(*this);
|
Chris@16
|
315 root->~node();
|
Chris@16
|
316 allocator_type::deallocate(root, 1);
|
Chris@16
|
317 root = NULL;
|
Chris@16
|
318 size_holder::set_size(0);
|
Chris@16
|
319 }
|
Chris@16
|
320
|
Chris@16
|
321 /// \copydoc boost::heap::priority_queue::get_allocator
|
Chris@16
|
322 allocator_type get_allocator(void) const
|
Chris@16
|
323 {
|
Chris@16
|
324 return *this;
|
Chris@16
|
325 }
|
Chris@16
|
326
|
Chris@16
|
327 /// \copydoc boost::heap::priority_queue::swap
|
Chris@16
|
328 void swap(pairing_heap & rhs)
|
Chris@16
|
329 {
|
Chris@16
|
330 super_t::swap(rhs);
|
Chris@16
|
331 std::swap(root, rhs.root);
|
Chris@16
|
332 }
|
Chris@16
|
333
|
Chris@16
|
334
|
Chris@16
|
335 /// \copydoc boost::heap::priority_queue::top
|
Chris@16
|
336 const_reference top(void) const
|
Chris@16
|
337 {
|
Chris@16
|
338 BOOST_ASSERT(!empty());
|
Chris@16
|
339
|
Chris@16
|
340 return super_t::get_value(root->value);
|
Chris@16
|
341 }
|
Chris@16
|
342
|
Chris@16
|
343 /**
|
Chris@16
|
344 * \b Effects: Adds a new element to the priority queue. Returns handle to element
|
Chris@16
|
345 *
|
Chris@16
|
346 * \cond
|
Chris@16
|
347 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
|
Chris@16
|
348 * \endcond
|
Chris@16
|
349 *
|
Chris@16
|
350 * \b Complexity: 2**2*log(log(N)) (amortized).
|
Chris@16
|
351 *
|
Chris@16
|
352 * */
|
Chris@16
|
353 handle_type push(value_type const & v)
|
Chris@16
|
354 {
|
Chris@16
|
355 size_holder::increment();
|
Chris@16
|
356
|
Chris@16
|
357 node_pointer n = allocator_type::allocate(1);
|
Chris@16
|
358
|
Chris@16
|
359 new(n) node(super_t::make_node(v));
|
Chris@16
|
360
|
Chris@16
|
361 merge_node(n);
|
Chris@16
|
362 return handle_type(n);
|
Chris@16
|
363 }
|
Chris@16
|
364
|
Chris@16
|
365 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
Chris@16
|
366 /**
|
Chris@16
|
367 * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. Returns handle to element.
|
Chris@16
|
368 *
|
Chris@16
|
369 * \cond
|
Chris@16
|
370 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
|
Chris@16
|
371 * \endcond
|
Chris@16
|
372 *
|
Chris@16
|
373 * \b Complexity: 2**2*log(log(N)) (amortized).
|
Chris@16
|
374 *
|
Chris@16
|
375 * */
|
Chris@16
|
376 template <class... Args>
|
Chris@16
|
377 handle_type emplace(Args&&... args)
|
Chris@16
|
378 {
|
Chris@16
|
379 size_holder::increment();
|
Chris@16
|
380
|
Chris@16
|
381 node_pointer n = allocator_type::allocate(1);
|
Chris@16
|
382
|
Chris@16
|
383 new(n) node(super_t::make_node(std::forward<Args>(args)...));
|
Chris@16
|
384
|
Chris@16
|
385 merge_node(n);
|
Chris@16
|
386 return handle_type(n);
|
Chris@16
|
387 }
|
Chris@16
|
388 #endif
|
Chris@16
|
389
|
Chris@16
|
390 /**
|
Chris@16
|
391 * \b Effects: Removes the top element from the priority queue.
|
Chris@16
|
392 *
|
Chris@16
|
393 * \b Complexity: Logarithmic (amortized).
|
Chris@16
|
394 *
|
Chris@16
|
395 * */
|
Chris@16
|
396 void pop(void)
|
Chris@16
|
397 {
|
Chris@16
|
398 BOOST_ASSERT(!empty());
|
Chris@16
|
399
|
Chris@16
|
400 erase(handle_type(root));
|
Chris@16
|
401 }
|
Chris@16
|
402
|
Chris@16
|
403 /**
|
Chris@16
|
404 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
|
Chris@16
|
405 *
|
Chris@16
|
406 * \cond
|
Chris@16
|
407 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
|
Chris@16
|
408 * \endcond
|
Chris@16
|
409 *
|
Chris@16
|
410 * \b Complexity: 2**2*log(log(N)) (amortized).
|
Chris@16
|
411 *
|
Chris@16
|
412 * */
|
Chris@16
|
413 void update (handle_type handle, const_reference v)
|
Chris@16
|
414 {
|
Chris@16
|
415 handle.node_->value = super_t::make_node(v);
|
Chris@16
|
416 update(handle);
|
Chris@16
|
417 }
|
Chris@16
|
418
|
Chris@16
|
419 /**
|
Chris@16
|
420 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
|
Chris@16
|
421 *
|
Chris@16
|
422 * \cond
|
Chris@16
|
423 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
|
Chris@16
|
424 * \endcond
|
Chris@16
|
425 *
|
Chris@16
|
426 * \b Complexity: 2**2*log(log(N)) (amortized).
|
Chris@16
|
427 *
|
Chris@16
|
428 * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
|
Chris@16
|
429 * */
|
Chris@16
|
430 void update (handle_type handle)
|
Chris@16
|
431 {
|
Chris@16
|
432 node_pointer n = handle.node_;
|
Chris@16
|
433
|
Chris@16
|
434 n->unlink();
|
Chris@16
|
435 if (!n->children.empty())
|
Chris@16
|
436 n = merge_nodes(n, merge_node_list(n->children));
|
Chris@16
|
437
|
Chris@16
|
438 if (n != root)
|
Chris@16
|
439 merge_node(n);
|
Chris@16
|
440 }
|
Chris@16
|
441
|
Chris@16
|
442 /**
|
Chris@16
|
443 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
|
Chris@16
|
444 *
|
Chris@16
|
445 * \cond
|
Chris@16
|
446 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
|
Chris@16
|
447 * \endcond
|
Chris@16
|
448 *
|
Chris@16
|
449 * \b Complexity: 2**2*log(log(N)) (amortized).
|
Chris@16
|
450 *
|
Chris@16
|
451 * \b Note: The new value is expected to be greater than the current one
|
Chris@16
|
452 * */
|
Chris@16
|
453 void increase (handle_type handle, const_reference v)
|
Chris@16
|
454 {
|
Chris@16
|
455 update(handle, v);
|
Chris@16
|
456 }
|
Chris@16
|
457
|
Chris@16
|
458 /**
|
Chris@16
|
459 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
|
Chris@16
|
460 *
|
Chris@16
|
461 * \cond
|
Chris@16
|
462 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
|
Chris@16
|
463 * \endcond
|
Chris@16
|
464 *
|
Chris@16
|
465 * \b Complexity: 2**2*log(log(N)) (amortized).
|
Chris@16
|
466 *
|
Chris@16
|
467 * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
|
Chris@16
|
468 * */
|
Chris@16
|
469 void increase (handle_type handle)
|
Chris@16
|
470 {
|
Chris@16
|
471 update(handle);
|
Chris@16
|
472 }
|
Chris@16
|
473
|
Chris@16
|
474 /**
|
Chris@16
|
475 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
|
Chris@16
|
476 *
|
Chris@16
|
477 * \cond
|
Chris@16
|
478 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
|
Chris@16
|
479 * \endcond
|
Chris@16
|
480 *
|
Chris@16
|
481 * \b Complexity: 2**2*log(log(N)) (amortized).
|
Chris@16
|
482 *
|
Chris@16
|
483 * \b Note: The new value is expected to be less than the current one
|
Chris@16
|
484 * */
|
Chris@16
|
485 void decrease (handle_type handle, const_reference v)
|
Chris@16
|
486 {
|
Chris@16
|
487 update(handle, v);
|
Chris@16
|
488 }
|
Chris@16
|
489
|
Chris@16
|
490 /**
|
Chris@16
|
491 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
|
Chris@16
|
492 *
|
Chris@16
|
493 * \cond
|
Chris@16
|
494 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
|
Chris@16
|
495 * \endcond
|
Chris@16
|
496 *
|
Chris@16
|
497 * \b Complexity: 2**2*log(log(N)) (amortized).
|
Chris@16
|
498 *
|
Chris@16
|
499 * \b Note: The new value is expected to be less than the current one. If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
|
Chris@16
|
500 * */
|
Chris@16
|
501 void decrease (handle_type handle)
|
Chris@16
|
502 {
|
Chris@16
|
503 update(handle);
|
Chris@16
|
504 }
|
Chris@16
|
505
|
Chris@16
|
506 /**
|
Chris@16
|
507 * \b Effects: Removes the element handled by \c handle from the priority_queue.
|
Chris@16
|
508 *
|
Chris@16
|
509 * \cond
|
Chris@16
|
510 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
|
Chris@16
|
511 * \endcond
|
Chris@16
|
512 *
|
Chris@16
|
513 * \b Complexity: 2**2*log(log(N)) (amortized).
|
Chris@16
|
514 * */
|
Chris@16
|
515 void erase(handle_type handle)
|
Chris@16
|
516 {
|
Chris@16
|
517 node_pointer n = handle.node_;
|
Chris@16
|
518 if (n != root) {
|
Chris@16
|
519 n->unlink();
|
Chris@16
|
520 if (!n->children.empty())
|
Chris@16
|
521 merge_node(merge_node_list(n->children));
|
Chris@16
|
522 } else {
|
Chris@16
|
523 if (!n->children.empty())
|
Chris@16
|
524 root = merge_node_list(n->children);
|
Chris@16
|
525 else
|
Chris@16
|
526 root = NULL;
|
Chris@16
|
527 }
|
Chris@16
|
528
|
Chris@16
|
529 size_holder::decrement();
|
Chris@16
|
530 n->~node();
|
Chris@16
|
531 allocator_type::deallocate(n, 1);
|
Chris@16
|
532 }
|
Chris@16
|
533
|
Chris@16
|
534 /// \copydoc boost::heap::priority_queue::begin
|
Chris@16
|
535 iterator begin(void) const
|
Chris@16
|
536 {
|
Chris@16
|
537 return iterator(root, super_t::value_comp());
|
Chris@16
|
538 }
|
Chris@16
|
539
|
Chris@16
|
540 /// \copydoc boost::heap::priority_queue::end
|
Chris@16
|
541 iterator end(void) const
|
Chris@16
|
542 {
|
Chris@16
|
543 return iterator();
|
Chris@16
|
544 }
|
Chris@16
|
545
|
Chris@16
|
546 /// \copydoc boost::heap::fibonacci_heap::ordered_begin
|
Chris@16
|
547 ordered_iterator ordered_begin(void) const
|
Chris@16
|
548 {
|
Chris@16
|
549 return ordered_iterator(root, super_t::value_comp());
|
Chris@16
|
550 }
|
Chris@16
|
551
|
Chris@16
|
552 /// \copydoc boost::heap::fibonacci_heap::ordered_begin
|
Chris@16
|
553 ordered_iterator ordered_end(void) const
|
Chris@16
|
554 {
|
Chris@16
|
555 return ordered_iterator(NULL, super_t::value_comp());
|
Chris@16
|
556 }
|
Chris@16
|
557
|
Chris@16
|
558
|
Chris@16
|
559 /// \copydoc boost::heap::d_ary_heap_mutable::s_handle_from_iterator
|
Chris@16
|
560 static handle_type s_handle_from_iterator(iterator const & it)
|
Chris@16
|
561 {
|
Chris@16
|
562 node * ptr = const_cast<node *>(it.get_node());
|
Chris@16
|
563 return handle_type(ptr);
|
Chris@16
|
564 }
|
Chris@16
|
565
|
Chris@16
|
566 /**
|
Chris@16
|
567 * \b Effects: Merge all elements from rhs into this
|
Chris@16
|
568 *
|
Chris@16
|
569 * \cond
|
Chris@16
|
570 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
|
Chris@16
|
571 * \endcond
|
Chris@16
|
572 *
|
Chris@16
|
573 * \b Complexity: 2**2*log(log(N)) (amortized).
|
Chris@16
|
574 *
|
Chris@16
|
575 * */
|
Chris@16
|
576 void merge(pairing_heap & rhs)
|
Chris@16
|
577 {
|
Chris@16
|
578 if (rhs.empty())
|
Chris@16
|
579 return;
|
Chris@16
|
580
|
Chris@16
|
581 merge_node(rhs.root);
|
Chris@16
|
582
|
Chris@16
|
583 size_holder::add(rhs.get_size());
|
Chris@16
|
584 rhs.set_size(0);
|
Chris@16
|
585 rhs.root = NULL;
|
Chris@16
|
586
|
Chris@16
|
587 super_t::set_stability_count((std::max)(super_t::get_stability_count(),
|
Chris@16
|
588 rhs.get_stability_count()));
|
Chris@16
|
589 rhs.set_stability_count(0);
|
Chris@16
|
590 }
|
Chris@16
|
591
|
Chris@16
|
592 /// \copydoc boost::heap::priority_queue::value_comp
|
Chris@16
|
593 value_compare const & value_comp(void) const
|
Chris@16
|
594 {
|
Chris@16
|
595 return super_t::value_comp();
|
Chris@16
|
596 }
|
Chris@16
|
597
|
Chris@16
|
598 /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const
|
Chris@16
|
599 template <typename HeapType>
|
Chris@16
|
600 bool operator<(HeapType const & rhs) const
|
Chris@16
|
601 {
|
Chris@16
|
602 return detail::heap_compare(*this, rhs);
|
Chris@16
|
603 }
|
Chris@16
|
604
|
Chris@16
|
605 /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const
|
Chris@16
|
606 template <typename HeapType>
|
Chris@16
|
607 bool operator>(HeapType const & rhs) const
|
Chris@16
|
608 {
|
Chris@16
|
609 return detail::heap_compare(rhs, *this);
|
Chris@16
|
610 }
|
Chris@16
|
611
|
Chris@16
|
612 /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const
|
Chris@16
|
613 template <typename HeapType>
|
Chris@16
|
614 bool operator>=(HeapType const & rhs) const
|
Chris@16
|
615 {
|
Chris@16
|
616 return !operator<(rhs);
|
Chris@16
|
617 }
|
Chris@16
|
618
|
Chris@16
|
619 /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const
|
Chris@16
|
620 template <typename HeapType>
|
Chris@16
|
621 bool operator<=(HeapType const & rhs) const
|
Chris@16
|
622 {
|
Chris@16
|
623 return !operator>(rhs);
|
Chris@16
|
624 }
|
Chris@16
|
625
|
Chris@16
|
626 /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const
|
Chris@16
|
627 template <typename HeapType>
|
Chris@16
|
628 bool operator==(HeapType const & rhs) const
|
Chris@16
|
629 {
|
Chris@16
|
630 return detail::heap_equality(*this, rhs);
|
Chris@16
|
631 }
|
Chris@16
|
632
|
Chris@16
|
633 /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const
|
Chris@16
|
634 template <typename HeapType>
|
Chris@16
|
635 bool operator!=(HeapType const & rhs) const
|
Chris@16
|
636 {
|
Chris@16
|
637 return !(*this == rhs);
|
Chris@16
|
638 }
|
Chris@16
|
639
|
Chris@16
|
640 private:
|
Chris@16
|
641 #if !defined(BOOST_DOXYGEN_INVOKED)
|
Chris@16
|
642 void clone_tree(pairing_heap const & rhs)
|
Chris@16
|
643 {
|
Chris@16
|
644 BOOST_HEAP_ASSERT(root == NULL);
|
Chris@16
|
645 if (rhs.empty())
|
Chris@16
|
646 return;
|
Chris@16
|
647
|
Chris@16
|
648 root = allocator_type::allocate(1);
|
Chris@16
|
649
|
Chris@16
|
650 new(root) node(static_cast<node const &>(*rhs.root), static_cast<allocator_type&>(*this));
|
Chris@16
|
651 }
|
Chris@16
|
652
|
Chris@16
|
653 void merge_node(node_pointer other)
|
Chris@16
|
654 {
|
Chris@16
|
655 BOOST_HEAP_ASSERT(other);
|
Chris@16
|
656 if (root != NULL)
|
Chris@16
|
657 root = merge_nodes(root, other);
|
Chris@16
|
658 else
|
Chris@16
|
659 root = other;
|
Chris@16
|
660 }
|
Chris@16
|
661
|
Chris@16
|
662 node_pointer merge_node_list(node_child_list & children)
|
Chris@16
|
663 {
|
Chris@16
|
664 BOOST_HEAP_ASSERT(!children.empty());
|
Chris@16
|
665 node_pointer merged = merge_first_pair(children);
|
Chris@16
|
666 if (children.empty())
|
Chris@16
|
667 return merged;
|
Chris@16
|
668
|
Chris@16
|
669 node_child_list node_list;
|
Chris@16
|
670 node_list.push_back(*merged);
|
Chris@16
|
671
|
Chris@16
|
672 do {
|
Chris@16
|
673 node_pointer next_merged = merge_first_pair(children);
|
Chris@16
|
674 node_list.push_back(*next_merged);
|
Chris@16
|
675 } while (!children.empty());
|
Chris@16
|
676
|
Chris@16
|
677 return merge_node_list(node_list);
|
Chris@16
|
678 }
|
Chris@16
|
679
|
Chris@16
|
680 node_pointer merge_first_pair(node_child_list & children)
|
Chris@16
|
681 {
|
Chris@16
|
682 BOOST_HEAP_ASSERT(!children.empty());
|
Chris@16
|
683 node_pointer first_child = static_cast<node_pointer>(&children.front());
|
Chris@16
|
684 children.pop_front();
|
Chris@16
|
685 if (children.empty())
|
Chris@16
|
686 return first_child;
|
Chris@16
|
687
|
Chris@16
|
688 node_pointer second_child = static_cast<node_pointer>(&children.front());
|
Chris@16
|
689 children.pop_front();
|
Chris@16
|
690
|
Chris@16
|
691 return merge_nodes(first_child, second_child);
|
Chris@16
|
692 }
|
Chris@16
|
693
|
Chris@16
|
694 node_pointer merge_nodes(node_pointer node1, node_pointer node2)
|
Chris@16
|
695 {
|
Chris@16
|
696 if (super_t::operator()(node1->value, node2->value))
|
Chris@16
|
697 std::swap(node1, node2);
|
Chris@16
|
698
|
Chris@16
|
699 node2->unlink();
|
Chris@16
|
700 node1->children.push_front(*node2);
|
Chris@16
|
701 return node1;
|
Chris@16
|
702 }
|
Chris@16
|
703
|
Chris@16
|
704 node_pointer root;
|
Chris@16
|
705 #endif
|
Chris@16
|
706 };
|
Chris@16
|
707
|
Chris@16
|
708
|
Chris@16
|
709 } /* namespace heap */
|
Chris@16
|
710 } /* namespace boost */
|
Chris@16
|
711
|
Chris@16
|
712 #undef BOOST_HEAP_ASSERT
|
Chris@16
|
713 #endif /* BOOST_HEAP_PAIRING_HEAP_HPP */
|