32#ifndef VPSC_PAIRING_HEAP_H
33#define VPSC_PAIRING_HEAP_H
74template <
class T,
class TCompare>
77template <
class T,
class TCompare>
80template <
class T,
class TCompare = std::less<T> >
140template <
class T,
class TCompare>
146 if(
root ==
nullptr )
149 compareAndLink(
root, newNode );
158template <
class T,
class TCompare>
163 return root->element;
169template <
class T,
class TCompare>
177 if(
root->leftChild ==
nullptr )
180 root = combineSiblings(
root->leftChild );
189template <
class T,
class TCompare>
207template <
class T,
class TCompare>
225template <
class T,
class TCompare>
229 COLA_ASSERT(!lessThan(p->
element,newVal));
241 compareAndLink(
root, p );
248template <
class T,
class TCompare>
253 if (
root ==
nullptr) {
256 compareAndLink(
root, broot);
269template <
class T,
class TCompare>
274 if( second ==
nullptr )
281 first->
prev = second;
291 second->
prev = first;
307template <
class T,
class TCompare>
316 for( ; firstSibling !=
nullptr; numSiblings++ )
318 if( numSiblings == (
int)siblingsTreeArray.size( ) )
319 siblingsTreeArray.resize( numSiblings * 2 );
320 siblingsTreeArray[ numSiblings ] = firstSibling;
324 if( numSiblings == (
int)siblingsTreeArray.size( ) )
325 siblingsTreeArray.resize( numSiblings + 1 );
326 siblingsTreeArray[ numSiblings ] =
nullptr;
330 for( ; i + 1 < numSiblings; i += 2 )
331 compareAndLink( siblingsTreeArray[ i ], siblingsTreeArray[ i + 1 ] );
337 if( j == numSiblings - 3 )
338 compareAndLink( siblingsTreeArray[ j ], siblingsTreeArray[ j + 2 ] );
342 for( ; j >= 2; j -= 2 )
343 compareAndLink( siblingsTreeArray[ j - 2 ], siblingsTreeArray[ j ] );
344 return siblingsTreeArray[ 0 ];
351template <
class T,
class TCompare>
368template <
class T,
class TCompare>
372 if (b.
root !=
nullptr) {
374 std::list<PairNode<T>*> q;
382 while (
c !=
nullptr) {
void deleteMin()
Remove the smallest item from the priority queue.
void decreaseKey(PairNode< T > *p, const T &newVal)
Change the value of the item stored in the pairing heap.
const T & findMin() const
Find the smallest item in the priority queue.
PairNode< T > * removeRootForMerge(unsigned &size)
void merge(PairingHeap< T, TCompare > *rhs)
Merges rhs into this pairing heap by inserting its root.
std::vector< PairNode< T > * > siblingsTreeArray
PairingHeap(const PairingHeap &rhs)
void compareAndLink(PairNode< T > *&first, PairNode< T > *second) const
Internal method that is the basic operation to maintain order.
PairNode< T > * insert(const T &x)
Insert item x into the priority queue, maintaining heap order.
PairNode< T > * clone(PairNode< T > *t) const
Internal method to clone subtree.
void reclaimMemory(PairNode< T > *t) const
Internal method to make the tree empty.
const PairingHeap & operator=(const PairingHeap &rhs)
Deep copy.
PairNode< T > * combineSiblings(PairNode< T > *firstSibling)
Internal method that implements two-pass merging.
PairNode(const T &theElement)