1.1 --- a/lemon/binom_heap.h Thu Jul 09 02:39:47 2009 +0200
1.2 +++ b/lemon/binom_heap.h Thu Jul 09 04:07:08 2009 +0200
1.3 @@ -1,8 +1,8 @@
1.4 -/* -*- C++ -*-
1.5 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.6 *
1.7 - * This file is a part of LEMON, a generic C++ optimization library
1.8 + * This file is a part of LEMON, a generic C++ optimization library.
1.9 *
1.10 - * Copyright (C) 2003-2008
1.11 + * Copyright (C) 2003-2009
1.12 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.13 * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.14 *
1.15 @@ -20,193 +20,199 @@
1.16 #define LEMON_BINOM_HEAP_H
1.17
1.18 ///\file
1.19 -///\ingroup auxdat
1.20 +///\ingroup heaps
1.21 ///\brief Binomial Heap implementation.
1.22
1.23 #include <vector>
1.24 +#include <utility>
1.25 #include <functional>
1.26 #include <lemon/math.h>
1.27 #include <lemon/counter.h>
1.28
1.29 namespace lemon {
1.30
1.31 - /// \ingroup auxdat
1.32 + /// \ingroup heaps
1.33 ///
1.34 - ///\brief Binomial Heap.
1.35 + ///\brief Binomial heap data structure.
1.36 ///
1.37 - ///This class implements the \e Binomial \e heap data structure. A \e heap
1.38 - ///is a data structure for storing items with specified values called \e
1.39 - ///priorities in such a way that finding the item with minimum priority is
1.40 - ///efficient. \c Compare specifies the ordering of the priorities. In a heap
1.41 - ///one can change the priority of an item, add or erase an item, etc.
1.42 + /// This class implements the \e binomial \e heap data structure.
1.43 + /// It fully conforms to the \ref concepts::Heap "heap concept".
1.44 ///
1.45 - ///The methods \ref increase and \ref erase are not efficient in a Binomial
1.46 - ///heap. In case of many calls to these operations, it is better to use a
1.47 - ///\ref BinHeap "binary heap".
1.48 + /// The methods \ref increase() and \ref erase() are not efficient
1.49 + /// in a binomial heap. In case of many calls of these operations,
1.50 + /// it is better to use other heap structure, e.g. \ref BinHeap
1.51 + /// "binary heap".
1.52 ///
1.53 - ///\param _Prio Type of the priority of the items.
1.54 - ///\param _ItemIntMap A read and writable Item int map, used internally
1.55 - ///to handle the cross references.
1.56 - ///\param _Compare A class for the ordering of the priorities. The
1.57 - ///default is \c std::less<_Prio>.
1.58 - ///
1.59 - ///\sa BinHeap
1.60 - ///\sa Dijkstra
1.61 - ///\author Dorian Batha
1.62 -
1.63 + /// \tparam PR Type of the priorities of the items.
1.64 + /// \tparam IM A read-writable item map with \c int values, used
1.65 + /// internally to handle the cross references.
1.66 + /// \tparam CMP A functor class for comparing the priorities.
1.67 + /// The default is \c std::less<PR>.
1.68 #ifdef DOXYGEN
1.69 - template <typename _Prio,
1.70 - typename _ItemIntMap,
1.71 - typename _Compare>
1.72 + template <typename PR, typename IM, typename CMP>
1.73 #else
1.74 - template <typename _Prio,
1.75 - typename _ItemIntMap,
1.76 - typename _Compare = std::less<_Prio> >
1.77 + template <typename PR, typename IM, typename CMP = std::less<PR> >
1.78 #endif
1.79 class BinomHeap {
1.80 public:
1.81 - typedef _ItemIntMap ItemIntMap;
1.82 - typedef _Prio Prio;
1.83 + /// Type of the item-int map.
1.84 + typedef IM ItemIntMap;
1.85 + /// Type of the priorities.
1.86 + typedef PR Prio;
1.87 + /// Type of the items stored in the heap.
1.88 typedef typename ItemIntMap::Key Item;
1.89 - typedef std::pair<Item,Prio> Pair;
1.90 - typedef _Compare Compare;
1.91 + /// Functor type for comparing the priorities.
1.92 + typedef CMP Compare;
1.93 +
1.94 + /// \brief Type to represent the states of the items.
1.95 + ///
1.96 + /// Each item has a state associated to it. It can be "in heap",
1.97 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
1.98 + /// heap's point of view, but may be useful to the user.
1.99 + ///
1.100 + /// The item-int map must be initialized in such way that it assigns
1.101 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
1.102 + enum State {
1.103 + IN_HEAP = 0, ///< = 0.
1.104 + PRE_HEAP = -1, ///< = -1.
1.105 + POST_HEAP = -2 ///< = -2.
1.106 + };
1.107
1.108 private:
1.109 class store;
1.110
1.111 - std::vector<store> container;
1.112 - int minimum, head;
1.113 - ItemIntMap &iimap;
1.114 - Compare comp;
1.115 - int num_items;
1.116 + std::vector<store> _data;
1.117 + int _min, _head;
1.118 + ItemIntMap &_iim;
1.119 + Compare _comp;
1.120 + int _num_items;
1.121
1.122 public:
1.123 - ///Status of the nodes
1.124 - enum State {
1.125 - ///The node is in the heap
1.126 - IN_HEAP = 0,
1.127 - ///The node has never been in the heap
1.128 - PRE_HEAP = -1,
1.129 - ///The node was in the heap but it got out of it
1.130 - POST_HEAP = -2
1.131 - };
1.132 + /// \brief Constructor.
1.133 + ///
1.134 + /// Constructor.
1.135 + /// \param map A map that assigns \c int values to the items.
1.136 + /// It is used internally to handle the cross references.
1.137 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.138 + explicit BinomHeap(ItemIntMap &map)
1.139 + : _min(0), _head(-1), _iim(map), _num_items(0) {}
1.140
1.141 - /// \brief The constructor
1.142 + /// \brief Constructor.
1.143 ///
1.144 - /// \c _iimap should be given to the constructor, since it is
1.145 - /// used internally to handle the cross references.
1.146 - explicit BinomHeap(ItemIntMap &_iimap)
1.147 - : minimum(0), head(-1), iimap(_iimap), num_items() {}
1.148 -
1.149 - /// \brief The constructor
1.150 - ///
1.151 - /// \c _iimap should be given to the constructor, since it is used
1.152 - /// internally to handle the cross references. \c _comp is an
1.153 - /// object for ordering of the priorities.
1.154 - BinomHeap(ItemIntMap &_iimap, const Compare &_comp)
1.155 - : minimum(0), head(-1), iimap(_iimap), comp(_comp), num_items() {}
1.156 + /// Constructor.
1.157 + /// \param map A map that assigns \c int values to the items.
1.158 + /// It is used internally to handle the cross references.
1.159 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.160 + /// \param comp The function object used for comparing the priorities.
1.161 + BinomHeap(ItemIntMap &map, const Compare &comp)
1.162 + : _min(0), _head(-1), _iim(map), _comp(comp), _num_items(0) {}
1.163
1.164 /// \brief The number of items stored in the heap.
1.165 ///
1.166 - /// Returns the number of items stored in the heap.
1.167 - int size() const { return num_items; }
1.168 + /// This function returns the number of items stored in the heap.
1.169 + int size() const { return _num_items; }
1.170
1.171 - /// \brief Checks if the heap stores no items.
1.172 + /// \brief Check if the heap is empty.
1.173 ///
1.174 - /// Returns \c true if and only if the heap stores no items.
1.175 - bool empty() const { return num_items==0; }
1.176 + /// This function returns \c true if the heap is empty.
1.177 + bool empty() const { return _num_items==0; }
1.178
1.179 - /// \brief Make empty this heap.
1.180 + /// \brief Make the heap empty.
1.181 ///
1.182 - /// Make empty this heap. It does not change the cross reference
1.183 - /// map. If you want to reuse a heap what is not surely empty you
1.184 - /// should first clear the heap and after that you should set the
1.185 - /// cross reference map for each item to \c PRE_HEAP.
1.186 + /// This functon makes the heap empty.
1.187 + /// It does not change the cross reference map. If you want to reuse
1.188 + /// a heap that is not surely empty, you should first clear it and
1.189 + /// then you should set the cross reference map to \c PRE_HEAP
1.190 + /// for each item.
1.191 void clear() {
1.192 - container.clear(); minimum=0; num_items=0; head=-1;
1.193 + _data.clear(); _min=0; _num_items=0; _head=-1;
1.194 }
1.195
1.196 - /// \brief \c item gets to the heap with priority \c value independently
1.197 - /// if \c item was already there.
1.198 + /// \brief Set the priority of an item or insert it, if it is
1.199 + /// not stored in the heap.
1.200 ///
1.201 - /// This method calls \ref push(\c item, \c value) if \c item is not
1.202 - /// stored in the heap and it calls \ref decrease(\c item, \c value) or
1.203 - /// \ref increase(\c item, \c value) otherwise.
1.204 + /// This method sets the priority of the given item if it is
1.205 + /// already stored in the heap. Otherwise it inserts the given
1.206 + /// item into the heap with the given priority.
1.207 + /// \param item The item.
1.208 + /// \param value The priority.
1.209 void set (const Item& item, const Prio& value) {
1.210 - int i=iimap[item];
1.211 - if ( i >= 0 && container[i].in ) {
1.212 - if ( comp(value, container[i].prio) ) decrease(item, value);
1.213 - if ( comp(container[i].prio, value) ) increase(item, value);
1.214 + int i=_iim[item];
1.215 + if ( i >= 0 && _data[i].in ) {
1.216 + if ( _comp(value, _data[i].prio) ) decrease(item, value);
1.217 + if ( _comp(_data[i].prio, value) ) increase(item, value);
1.218 } else push(item, value);
1.219 }
1.220
1.221 - /// \brief Adds \c item to the heap with priority \c value.
1.222 + /// \brief Insert an item into the heap with the given priority.
1.223 ///
1.224 - /// Adds \c item to the heap with priority \c value.
1.225 - /// \pre \c item must not be stored in the heap.
1.226 + /// This function inserts the given item into the heap with the
1.227 + /// given priority.
1.228 + /// \param item The item to insert.
1.229 + /// \param value The priority of the item.
1.230 + /// \pre \e item must not be stored in the heap.
1.231 void push (const Item& item, const Prio& value) {
1.232 - int i=iimap[item];
1.233 + int i=_iim[item];
1.234 if ( i<0 ) {
1.235 - int s=container.size();
1.236 - iimap.set( item,s );
1.237 + int s=_data.size();
1.238 + _iim.set( item,s );
1.239 store st;
1.240 st.name=item;
1.241 - container.push_back(st);
1.242 + _data.push_back(st);
1.243 i=s;
1.244 }
1.245 else {
1.246 - container[i].parent=container[i].right_neighbor=container[i].child=-1;
1.247 - container[i].degree=0;
1.248 - container[i].in=true;
1.249 + _data[i].parent=_data[i].right_neighbor=_data[i].child=-1;
1.250 + _data[i].degree=0;
1.251 + _data[i].in=true;
1.252 }
1.253 - container[i].prio=value;
1.254 + _data[i].prio=value;
1.255
1.256 - if( 0==num_items ) { head=i; minimum=i; }
1.257 + if( 0==_num_items ) { _head=i; _min=i; }
1.258 else { merge(i); }
1.259
1.260 - minimum = find_min();
1.261 + _min = findMin();
1.262
1.263 - ++num_items;
1.264 + ++_num_items;
1.265 }
1.266
1.267 - /// \brief Returns the item with minimum priority relative to \c Compare.
1.268 + /// \brief Return the item having minimum priority.
1.269 ///
1.270 - /// This method returns the item with minimum priority relative to \c
1.271 - /// Compare.
1.272 - /// \pre The heap must be nonempty.
1.273 - Item top() const { return container[minimum].name; }
1.274 + /// This function returns the item having minimum priority.
1.275 + /// \pre The heap must be non-empty.
1.276 + Item top() const { return _data[_min].name; }
1.277
1.278 - /// \brief Returns the minimum priority relative to \c Compare.
1.279 + /// \brief The minimum priority.
1.280 ///
1.281 - /// It returns the minimum priority relative to \c Compare.
1.282 - /// \pre The heap must be nonempty.
1.283 - const Prio& prio() const { return container[minimum].prio; }
1.284 + /// This function returns the minimum priority.
1.285 + /// \pre The heap must be non-empty.
1.286 + Prio prio() const { return _data[_min].prio; }
1.287
1.288 - /// \brief Returns the priority of \c item.
1.289 + /// \brief The priority of the given item.
1.290 ///
1.291 - /// It returns the priority of \c item.
1.292 - /// \pre \c item must be in the heap.
1.293 + /// This function returns the priority of the given item.
1.294 + /// \param item The item.
1.295 + /// \pre \e item must be in the heap.
1.296 const Prio& operator[](const Item& item) const {
1.297 - return container[iimap[item]].prio;
1.298 + return _data[_iim[item]].prio;
1.299 }
1.300
1.301 - /// \brief Deletes the item with minimum priority relative to \c Compare.
1.302 + /// \brief Remove the item having minimum priority.
1.303 ///
1.304 - /// This method deletes the item with minimum priority relative to \c
1.305 - /// Compare from the heap.
1.306 + /// This function removes the item having minimum priority.
1.307 /// \pre The heap must be non-empty.
1.308 void pop() {
1.309 - container[minimum].in=false;
1.310 + _data[_min].in=false;
1.311
1.312 int head_child=-1;
1.313 - if ( container[minimum].child!=-1 ) {
1.314 - int child=container[minimum].child;
1.315 + if ( _data[_min].child!=-1 ) {
1.316 + int child=_data[_min].child;
1.317 int neighb;
1.318 int prev=-1;
1.319 while( child!=-1 ) {
1.320 - neighb=container[child].right_neighbor;
1.321 - container[child].parent=-1;
1.322 - container[child].right_neighbor=prev;
1.323 + neighb=_data[child].right_neighbor;
1.324 + _data[child].parent=-1;
1.325 + _data[child].right_neighbor=prev;
1.326 head_child=child;
1.327 prev=child;
1.328 child=neighb;
1.329 @@ -214,142 +220,144 @@
1.330 }
1.331
1.332 // The first case is that there are only one root.
1.333 - if ( -1==container[head].right_neighbor ) {
1.334 - head=head_child;
1.335 + if ( -1==_data[_head].right_neighbor ) {
1.336 + _head=head_child;
1.337 }
1.338 // The case where there are more roots.
1.339 else {
1.340 - if( head!=minimum ) { unlace(minimum); }
1.341 - else { head=container[head].right_neighbor; }
1.342 + if( _head!=_min ) { unlace(_min); }
1.343 + else { _head=_data[_head].right_neighbor; }
1.344
1.345 merge(head_child);
1.346 }
1.347 - minimum=find_min();
1.348 - --num_items;
1.349 + _min=findMin();
1.350 + --_num_items;
1.351 }
1.352
1.353 - /// \brief Deletes \c item from the heap.
1.354 + /// \brief Remove the given item from the heap.
1.355 ///
1.356 - /// This method deletes \c item from the heap, if \c item was already
1.357 - /// stored in the heap. It is quite inefficient in Binomial heaps.
1.358 + /// This function removes the given item from the heap if it is
1.359 + /// already stored.
1.360 + /// \param item The item to delete.
1.361 + /// \pre \e item must be in the heap.
1.362 void erase (const Item& item) {
1.363 - int i=iimap[item];
1.364 - if ( i >= 0 && container[i].in ) {
1.365 - decrease( item, container[minimum].prio-1 );
1.366 + int i=_iim[item];
1.367 + if ( i >= 0 && _data[i].in ) {
1.368 + decrease( item, _data[_min].prio-1 );
1.369 pop();
1.370 }
1.371 }
1.372
1.373 - /// \brief Decreases the priority of \c item to \c value.
1.374 + /// \brief Decrease the priority of an item to the given value.
1.375 ///
1.376 - /// This method decreases the priority of \c item to \c value.
1.377 - /// \pre \c item must be stored in the heap with priority at least \c
1.378 - /// value relative to \c Compare.
1.379 + /// This function decreases the priority of an item to the given value.
1.380 + /// \param item The item.
1.381 + /// \param value The priority.
1.382 + /// \pre \e item must be stored in the heap with priority at least \e value.
1.383 void decrease (Item item, const Prio& value) {
1.384 - int i=iimap[item];
1.385 + int i=_iim[item];
1.386
1.387 - if( comp( value,container[i].prio ) ) {
1.388 - container[i].prio=value;
1.389 + if( _comp( value,_data[i].prio ) ) {
1.390 + _data[i].prio=value;
1.391
1.392 - int p_loc=container[i].parent, loc=i;
1.393 + int p_loc=_data[i].parent, loc=i;
1.394 int parent, child, neighb;
1.395
1.396 - while( -1!=p_loc && comp(container[loc].prio,container[p_loc].prio) ) {
1.397 + while( -1!=p_loc && _comp(_data[loc].prio,_data[p_loc].prio) ) {
1.398
1.399 // parent set for other loc_child
1.400 - child=container[loc].child;
1.401 + child=_data[loc].child;
1.402 while( -1!=child ) {
1.403 - container[child].parent=p_loc;
1.404 - child=container[child].right_neighbor;
1.405 + _data[child].parent=p_loc;
1.406 + child=_data[child].right_neighbor;
1.407 }
1.408
1.409 // parent set for other p_loc_child
1.410 - child=container[p_loc].child;
1.411 + child=_data[p_loc].child;
1.412 while( -1!=child ) {
1.413 - container[child].parent=loc;
1.414 - child=container[child].right_neighbor;
1.415 + _data[child].parent=loc;
1.416 + child=_data[child].right_neighbor;
1.417 }
1.418
1.419 - child=container[p_loc].child;
1.420 - container[p_loc].child=container[loc].child;
1.421 + child=_data[p_loc].child;
1.422 + _data[p_loc].child=_data[loc].child;
1.423 if( child==loc )
1.424 child=p_loc;
1.425 - container[loc].child=child;
1.426 + _data[loc].child=child;
1.427
1.428 // left_neighb set for p_loc
1.429 - if( container[loc].child!=p_loc ) {
1.430 - while( container[child].right_neighbor!=loc )
1.431 - child=container[child].right_neighbor;
1.432 - container[child].right_neighbor=p_loc;
1.433 + if( _data[loc].child!=p_loc ) {
1.434 + while( _data[child].right_neighbor!=loc )
1.435 + child=_data[child].right_neighbor;
1.436 + _data[child].right_neighbor=p_loc;
1.437 }
1.438
1.439 // left_neighb set for loc
1.440 - parent=container[p_loc].parent;
1.441 - if( -1!=parent ) child=container[parent].child;
1.442 - else child=head;
1.443 + parent=_data[p_loc].parent;
1.444 + if( -1!=parent ) child=_data[parent].child;
1.445 + else child=_head;
1.446
1.447 if( child!=p_loc ) {
1.448 - while( container[child].right_neighbor!=p_loc )
1.449 - child=container[child].right_neighbor;
1.450 - container[child].right_neighbor=loc;
1.451 + while( _data[child].right_neighbor!=p_loc )
1.452 + child=_data[child].right_neighbor;
1.453 + _data[child].right_neighbor=loc;
1.454 }
1.455
1.456 - neighb=container[p_loc].right_neighbor;
1.457 - container[p_loc].right_neighbor=container[loc].right_neighbor;
1.458 - container[loc].right_neighbor=neighb;
1.459 + neighb=_data[p_loc].right_neighbor;
1.460 + _data[p_loc].right_neighbor=_data[loc].right_neighbor;
1.461 + _data[loc].right_neighbor=neighb;
1.462
1.463 - container[p_loc].parent=loc;
1.464 - container[loc].parent=parent;
1.465 + _data[p_loc].parent=loc;
1.466 + _data[loc].parent=parent;
1.467
1.468 - if( -1!=parent && container[parent].child==p_loc )
1.469 - container[parent].child=loc;
1.470 + if( -1!=parent && _data[parent].child==p_loc )
1.471 + _data[parent].child=loc;
1.472
1.473 /*if new parent will be the first root*/
1.474 - if( head==p_loc )
1.475 - head=loc;
1.476 + if( _head==p_loc )
1.477 + _head=loc;
1.478
1.479 - p_loc=container[loc].parent;
1.480 + p_loc=_data[loc].parent;
1.481 }
1.482 }
1.483 - if( comp(value,container[minimum].prio) ) {
1.484 - minimum=i;
1.485 + if( _comp(value,_data[_min].prio) ) {
1.486 + _min=i;
1.487 }
1.488 }
1.489
1.490 - /// \brief Increases the priority of \c item to \c value.
1.491 + /// \brief Increase the priority of an item to the given value.
1.492 ///
1.493 - /// This method sets the priority of \c item to \c value. Though
1.494 - /// there is no precondition on the priority of \c item, this
1.495 - /// method should be used only if it is indeed necessary to increase
1.496 - /// (relative to \c Compare) the priority of \c item, because this
1.497 - /// method is inefficient.
1.498 + /// This function increases the priority of an item to the given value.
1.499 + /// \param item The item.
1.500 + /// \param value The priority.
1.501 + /// \pre \e item must be stored in the heap with priority at most \e value.
1.502 void increase (Item item, const Prio& value) {
1.503 erase(item);
1.504 push(item, value);
1.505 }
1.506
1.507 -
1.508 - /// \brief Returns if \c item is in, has already been in, or has never
1.509 - /// been in the heap.
1.510 + /// \brief Return the state of an item.
1.511 ///
1.512 - /// This method returns PRE_HEAP if \c item has never been in the
1.513 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.514 - /// otherwise. In the latter case it is possible that \c item will
1.515 - /// get back to the heap again.
1.516 + /// This method returns \c PRE_HEAP if the given item has never
1.517 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.518 + /// and \c POST_HEAP otherwise.
1.519 + /// In the latter case it is possible that the item will get back
1.520 + /// to the heap again.
1.521 + /// \param item The item.
1.522 State state(const Item &item) const {
1.523 - int i=iimap[item];
1.524 + int i=_iim[item];
1.525 if( i>=0 ) {
1.526 - if ( container[i].in ) i=0;
1.527 + if ( _data[i].in ) i=0;
1.528 else i=-2;
1.529 }
1.530 return State(i);
1.531 }
1.532
1.533 - /// \brief Sets the state of the \c item in the heap.
1.534 + /// \brief Set the state of an item in the heap.
1.535 ///
1.536 - /// Sets the state of the \c item in the heap. It can be used to
1.537 - /// manually clear the heap when it is important to achive the
1.538 - /// better time complexity.
1.539 + /// This function sets the state of the given item in the heap.
1.540 + /// It can be used to manually clear the heap when it is important
1.541 + /// to achive better time complexity.
1.542 /// \param i The item.
1.543 /// \param st The state. It should not be \c IN_HEAP.
1.544 void state(const Item& i, State st) {
1.545 @@ -359,7 +367,7 @@
1.546 if (state(i) == IN_HEAP) {
1.547 erase(i);
1.548 }
1.549 - iimap[i] = st;
1.550 + _iim[i] = st;
1.551 break;
1.552 case IN_HEAP:
1.553 break;
1.554 @@ -367,20 +375,20 @@
1.555 }
1.556
1.557 private:
1.558 - int find_min() {
1.559 + int findMin() {
1.560 int min_loc=-1, min_val;
1.561 - int x=head;
1.562 + int x=_head;
1.563 if( x!=-1 ) {
1.564 - min_val=container[x].prio;
1.565 + min_val=_data[x].prio;
1.566 min_loc=x;
1.567 - x=container[x].right_neighbor;
1.568 + x=_data[x].right_neighbor;
1.569
1.570 while( x!=-1 ) {
1.571 - if( comp( container[x].prio,min_val ) ) {
1.572 - min_val=container[x].prio;
1.573 + if( _comp( _data[x].prio,min_val ) ) {
1.574 + min_val=_data[x].prio;
1.575 min_loc=x;
1.576 }
1.577 - x=container[x].right_neighbor;
1.578 + x=_data[x].right_neighbor;
1.579 }
1.580 }
1.581 return min_loc;
1.582 @@ -389,29 +397,29 @@
1.583 void merge(int a) {
1.584 interleave(a);
1.585
1.586 - int x=head;
1.587 + int x=_head;
1.588 if( -1!=x ) {
1.589 - int x_prev=-1, x_next=container[x].right_neighbor;
1.590 + int x_prev=-1, x_next=_data[x].right_neighbor;
1.591 while( -1!=x_next ) {
1.592 - if( container[x].degree!=container[x_next].degree || ( -1!=container[x_next].right_neighbor && container[container[x_next].right_neighbor].degree==container[x].degree ) ) {
1.593 + if( _data[x].degree!=_data[x_next].degree || ( -1!=_data[x_next].right_neighbor && _data[_data[x_next].right_neighbor].degree==_data[x].degree ) ) {
1.594 x_prev=x;
1.595 x=x_next;
1.596 }
1.597 else {
1.598 - if( comp(container[x].prio,container[x_next].prio) ) {
1.599 - container[x].right_neighbor=container[x_next].right_neighbor;
1.600 + if( _comp(_data[x].prio,_data[x_next].prio) ) {
1.601 + _data[x].right_neighbor=_data[x_next].right_neighbor;
1.602 fuse(x_next,x);
1.603 }
1.604 else {
1.605 - if( -1==x_prev ) { head=x_next; }
1.606 + if( -1==x_prev ) { _head=x_next; }
1.607 else {
1.608 - container[x_prev].right_neighbor=x_next;
1.609 + _data[x_prev].right_neighbor=x_next;
1.610 }
1.611 fuse(x,x_next);
1.612 x=x_next;
1.613 }
1.614 }
1.615 - x_next=container[x].right_neighbor;
1.616 + x_next=_data[x].right_neighbor;
1.617 }
1.618 }
1.619 }
1.620 @@ -419,68 +427,68 @@
1.621 void interleave(int a) {
1.622 int other=-1, head_other=-1;
1.623
1.624 - while( -1!=a || -1!=head ) {
1.625 + while( -1!=a || -1!=_head ) {
1.626 if( -1==a ) {
1.627 if( -1==head_other ) {
1.628 - head_other=head;
1.629 + head_other=_head;
1.630 }
1.631 else {
1.632 - container[other].right_neighbor=head;
1.633 + _data[other].right_neighbor=_head;
1.634 }
1.635 - head=-1;
1.636 + _head=-1;
1.637 }
1.638 - else if( -1==head ) {
1.639 + else if( -1==_head ) {
1.640 if( -1==head_other ) {
1.641 head_other=a;
1.642 }
1.643 else {
1.644 - container[other].right_neighbor=a;
1.645 + _data[other].right_neighbor=a;
1.646 }
1.647 a=-1;
1.648 }
1.649 else {
1.650 - if( container[a].degree<container[head].degree ) {
1.651 + if( _data[a].degree<_data[_head].degree ) {
1.652 if( -1==head_other ) {
1.653 head_other=a;
1.654 }
1.655 else {
1.656 - container[other].right_neighbor=a;
1.657 + _data[other].right_neighbor=a;
1.658 }
1.659 other=a;
1.660 - a=container[a].right_neighbor;
1.661 + a=_data[a].right_neighbor;
1.662 }
1.663 else {
1.664 if( -1==head_other ) {
1.665 - head_other=head;
1.666 + head_other=_head;
1.667 }
1.668 else {
1.669 - container[other].right_neighbor=head;
1.670 + _data[other].right_neighbor=_head;
1.671 }
1.672 - other=head;
1.673 - head=container[head].right_neighbor;
1.674 + other=_head;
1.675 + _head=_data[_head].right_neighbor;
1.676 }
1.677 }
1.678 }
1.679 - head=head_other;
1.680 + _head=head_other;
1.681 }
1.682
1.683 // Lacing a under b
1.684 void fuse(int a, int b) {
1.685 - container[a].parent=b;
1.686 - container[a].right_neighbor=container[b].child;
1.687 - container[b].child=a;
1.688 + _data[a].parent=b;
1.689 + _data[a].right_neighbor=_data[b].child;
1.690 + _data[b].child=a;
1.691
1.692 - ++container[b].degree;
1.693 + ++_data[b].degree;
1.694 }
1.695
1.696 // It is invoked only if a has siblings.
1.697 void unlace(int a) {
1.698 - int neighb=container[a].right_neighbor;
1.699 - int other=head;
1.700 + int neighb=_data[a].right_neighbor;
1.701 + int other=_head;
1.702
1.703 - while( container[other].right_neighbor!=a )
1.704 - other=container[other].right_neighbor;
1.705 - container[other].right_neighbor=neighb;
1.706 + while( _data[other].right_neighbor!=a )
1.707 + other=_data[other].right_neighbor;
1.708 + _data[other].right_neighbor=neighb;
1.709 }
1.710
1.711 private: