SNAP Library 2.0, Developer Reference
2013-05-13 16:33:57
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
Simple heap data structure. More...
#include <gbase.h>
Public Member Functions | |
THeap () | |
THeap (const int &MxVals) | |
THeap (const TCmp &_Cmp) | |
THeap (const TVec< TVal > &Vec) | |
THeap (const TVec< TVal > &Vec, const TCmp &_Cmp) | |
THeap (const THeap &Heap) | |
THeap & | operator= (const THeap &H) |
const TVal & | TopHeap () const |
Returns a reference to the element at the top of the heap (the largest element of the heap). | |
void | PushHeap (const TVal &Val) |
Pushes an element Val to the heap. | |
TVal | PopHeap () |
Removes the top element from the heap. | |
int | Len () const |
Returns the number of elements in the heap. | |
bool | Empty () const |
Tests whether the heap is empty. | |
const TVec< TVal > & | operator() () const |
Returns a reference to the vector containing the elements of the heap. | |
TVec< TVal > & | operator() () |
Returns a reference to the vector containing the elements of the heap. | |
void | Add (const TVal &Val) |
Adds an element to the data structure. Heap property is not maintained by Add() and thus after all the elements are added MakeHeap() needs to be called. | |
void | MakeHeap () |
Builds a heap from a set of elements. | |
Private Member Functions | |
void | PushHeap (const int &First, int HoleIdx, const int &Top, TVal Val) |
void | AdjustHeap (const int &First, int HoleIdx, const int &Len, TVal Val) |
void | MakeHeap (const int &First, const int &Len) |
Private Attributes | |
TCmp | Cmp |
TVec< TVal > | HeapV |
Simple heap data structure.
Data structure provides insertion of elements, and inspection and removal of the top element. It is guaranteed that the top element is the largest element in the heap, where the function object TCmp
is used for comparisons.
void THeap< TVal, TCmp >::Add | ( | const TVal & | Val | ) | [inline] |
Adds an element to the data structure. Heap property is not maintained by Add()
and thus after all the elements are added MakeHeap()
needs to be called.
Definition at line 260 of file gbase.h.
Referenced by TSnap::TSnapDetail::TCNMQMatrix::Init().
void THeap< TVal, TCmp >::AdjustHeap | ( | const int & | First, |
int | HoleIdx, | ||
const int & | Len, | ||
TVal | Val | ||
) | [private] |
Definition at line 294 of file gbase.h.
References Cmp().
{ const int Top = HoleIdx; int Right = 2*HoleIdx+2; while (Right < Len) { if (Cmp(HeapV[First+Right], HeapV[First+Right-1])) { Right--; } HeapV[First+HoleIdx] = HeapV[First+Right]; HoleIdx = Right; Right = 2*(Right+1); } if (Right == Len) { HeapV[First+HoleIdx] = HeapV[First+Right-1]; HoleIdx = Right-1; } PushHeap(First, HoleIdx, Top, Val); }
Tests whether the heap is empty.
Definition at line 254 of file gbase.h.
Referenced by TSnap::TSnapDetail::TCNMQMatrix::FindMxQEdge().
Returns the number of elements in the heap.
Definition at line 252 of file gbase.h.
Referenced by THeap< TFltIntIntTr >::MakeHeap().
void THeap< TVal, TCmp >::MakeHeap | ( | const int & | First, |
const int & | Len | ||
) | [private] |
Definition at line 308 of file gbase.h.
Referenced by TSnap::TSnapDetail::TCNMQMatrix::Init().
{ if (Len < 2) { return; } int Parent = (Len-2)/2; while (true) { AdjustHeap(First, Parent, Len, HeapV[First+Parent]); if (Parent == 0) { return; } Parent--; } }
Builds a heap from a set of elements.
Definition at line 262 of file gbase.h.
Referenced by THeap< TFltIntIntTr >::MakeHeap(), and THeap< TFltIntIntTr >::THeap().
Removes the top element from the heap.
Definition at line 272 of file gbase.h.
References IAssert.
Referenced by TSnap::TSnapDetail::TCNMQMatrix::FindMxQEdge().
{ IAssert(! HeapV.Empty()); const TVal Top = HeapV[0]; HeapV[0] = HeapV.Last(); HeapV.DelLast(); if (! HeapV.Empty()) { AdjustHeap(0, 0, HeapV.Len(), HeapV[0]); } return Top; }
void THeap< TVal, TCmp >::PushHeap | ( | const int & | First, |
int | HoleIdx, | ||
const int & | Top, | ||
TVal | Val | ||
) | [private] |
Definition at line 284 of file gbase.h.
References Cmp().
Referenced by TSnap::TSnapDetail::TCNMQMatrix::MergeBestQ().
{ int Parent = (HoleIdx-1)/2; while (HoleIdx > Top && Cmp(HeapV[First+Parent], Val)) { HeapV[First+HoleIdx] = HeapV[First+Parent]; HoleIdx = Parent; Parent = (HoleIdx-1)/2; } HeapV[First+HoleIdx] = Val; }
Definition at line 230 of file gbase.h.
Referenced by THeap< TFltIntIntTr >::operator=().
Definition at line 231 of file gbase.h.
Referenced by THeap< TFltIntIntTr >::Add(), THeap< TFltIntIntTr >::Empty(), THeap< TFltIntIntTr >::Len(), THeap< TFltIntIntTr >::operator()(), THeap< TFltIntIntTr >::operator=(), and THeap< TFltIntIntTr >::TopHeap().