SNAP Library 2.4, User Reference  2015-05-11 19:40:56
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
THeap< TVal, TCmp > Class Template Reference

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)
 
THeapoperator= (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). More...
 
void PushHeap (const TVal &Val)
 Pushes an element Val to the heap. More...
 
TVal PopHeap ()
 Removes the top element from the heap. More...
 
int Len () const
 Returns the number of elements in the heap. More...
 
bool Empty () const
 Tests whether the heap is empty. More...
 
const TVec< TVal > & operator() () const
 Returns a reference to the vector containing the elements of the heap. More...
 
TVec< TVal > & operator() ()
 Returns a reference to the vector containing the elements of the heap. More...
 
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. More...
 
void MakeHeap ()
 Builds a heap from a set of elements. More...
 

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
 

Detailed Description

template<class TVal, class TCmp = TLss<TVal>>
class THeap< TVal, TCmp >

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.

Definition at line 253 of file gbase.h.

Constructor & Destructor Documentation

template<class TVal, class TCmp = TLss<TVal>>
THeap< TVal, TCmp >::THeap ( )
inline

Definition at line 262 of file gbase.h.

262 : HeapV() { }
TVec< TVal > HeapV
Definition: gbase.h:256
template<class TVal, class TCmp = TLss<TVal>>
THeap< TVal, TCmp >::THeap ( const int &  MxVals)
inline

Definition at line 263 of file gbase.h.

263 : Cmp(), HeapV(MxVals, 0) { }
TVec< TVal > HeapV
Definition: gbase.h:256
TCmp Cmp
Definition: gbase.h:255
template<class TVal, class TCmp = TLss<TVal>>
THeap< TVal, TCmp >::THeap ( const TCmp _Cmp)
inline

Definition at line 264 of file gbase.h.

264 : Cmp(_Cmp), HeapV() { }
TVec< TVal > HeapV
Definition: gbase.h:256
TCmp Cmp
Definition: gbase.h:255
template<class TVal, class TCmp = TLss<TVal>>
THeap< TVal, TCmp >::THeap ( const TVec< TVal > &  Vec)
inline

Definition at line 265 of file gbase.h.

265 : Cmp(), HeapV(Vec) { MakeHeap(); }
TVec< TVal > HeapV
Definition: gbase.h:256
void MakeHeap()
Builds a heap from a set of elements.
Definition: gbase.h:287
TCmp Cmp
Definition: gbase.h:255
template<class TVal, class TCmp = TLss<TVal>>
THeap< TVal, TCmp >::THeap ( const TVec< TVal > &  Vec,
const TCmp _Cmp 
)
inline

Definition at line 266 of file gbase.h.

266 : Cmp(_Cmp), HeapV(Vec) { MakeHeap(); }
TVec< TVal > HeapV
Definition: gbase.h:256
void MakeHeap()
Builds a heap from a set of elements.
Definition: gbase.h:287
TCmp Cmp
Definition: gbase.h:255
template<class TVal, class TCmp = TLss<TVal>>
THeap< TVal, TCmp >::THeap ( const THeap< TVal, TCmp > &  Heap)
inline

Definition at line 267 of file gbase.h.

267 : Cmp(Heap.Cmp), HeapV(Heap.HeapV) { }
TVec< TVal > HeapV
Definition: gbase.h:256
TCmp Cmp
Definition: gbase.h:255

Member Function Documentation

template<class TVal, class TCmp = TLss<TVal>>
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 285 of file gbase.h.

285 { HeapV.Add(Val); }
TVec< TVal > HeapV
Definition: gbase.h:256
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
template<class TVal, class TCmp >
void THeap< TVal, TCmp >::AdjustHeap ( const int &  First,
int  HoleIdx,
const int &  Len,
TVal  Val 
)
private

Definition at line 319 of file gbase.h.

319  {
320  const int Top = HoleIdx;
321  int Right = 2*HoleIdx+2;
322  while (Right < Len) {
323  if (Cmp(HeapV[First+Right], HeapV[First+Right-1])) { Right--; }
324  HeapV[First+HoleIdx] = HeapV[First+Right];
325  HoleIdx = Right; Right = 2*(Right+1); }
326  if (Right == Len) {
327  HeapV[First+HoleIdx] = HeapV[First+Right-1];
328  HoleIdx = Right-1; }
329  PushHeap(First, HoleIdx, Top, Val);
330 }
TVec< TVal > HeapV
Definition: gbase.h:256
TCmp Cmp
Definition: gbase.h:255
void PushHeap(const int &First, int HoleIdx, const int &Top, TVal Val)
Definition: gbase.h:309
int Len() const
Returns the number of elements in the heap.
Definition: gbase.h:277
template<class TVal, class TCmp = TLss<TVal>>
bool THeap< TVal, TCmp >::Empty ( ) const
inline

Tests whether the heap is empty.

Definition at line 279 of file gbase.h.

279 { return HeapV.Empty(); }
TVec< TVal > HeapV
Definition: gbase.h:256
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:530
template<class TVal, class TCmp = TLss<TVal>>
int THeap< TVal, TCmp >::Len ( ) const
inline

Returns the number of elements in the heap.

Definition at line 277 of file gbase.h.

277 { return HeapV.Len(); }
TVec< TVal > HeapV
Definition: gbase.h:256
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
template<class TVal , class TCmp >
void THeap< TVal, TCmp >::MakeHeap ( const int &  First,
const int &  Len 
)
private

Definition at line 333 of file gbase.h.

333  {
334  if (Len < 2) { return; }
335  int Parent = (Len-2)/2;
336  while (true) {
337  AdjustHeap(First, Parent, Len, HeapV[First+Parent]);
338  if (Parent == 0) { return; }
339  Parent--;
340  }
341 }
TVec< TVal > HeapV
Definition: gbase.h:256
void AdjustHeap(const int &First, int HoleIdx, const int &Len, TVal Val)
Definition: gbase.h:319
int Len() const
Returns the number of elements in the heap.
Definition: gbase.h:277
template<class TVal, class TCmp = TLss<TVal>>
void THeap< TVal, TCmp >::MakeHeap ( )
inline

Builds a heap from a set of elements.

Definition at line 287 of file gbase.h.

287 { MakeHeap(0, Len()); }
void MakeHeap()
Builds a heap from a set of elements.
Definition: gbase.h:287
int Len() const
Returns the number of elements in the heap.
Definition: gbase.h:277
template<class TVal, class TCmp = TLss<TVal>>
const TVec<TVal>& THeap< TVal, TCmp >::operator() ( ) const
inline

Returns a reference to the vector containing the elements of the heap.

Definition at line 281 of file gbase.h.

281 { return HeapV; }
TVec< TVal > HeapV
Definition: gbase.h:256
template<class TVal, class TCmp = TLss<TVal>>
TVec<TVal>& THeap< TVal, TCmp >::operator() ( )
inline

Returns a reference to the vector containing the elements of the heap.

Definition at line 283 of file gbase.h.

283 { return HeapV; }
TVec< TVal > HeapV
Definition: gbase.h:256
template<class TVal, class TCmp = TLss<TVal>>
THeap& THeap< TVal, TCmp >::operator= ( const THeap< TVal, TCmp > &  H)
inline

Definition at line 268 of file gbase.h.

268 { Cmp=H.Cmp; HeapV=H.HeapV; return *this; }
TVec< TVal > HeapV
Definition: gbase.h:256
TCmp Cmp
Definition: gbase.h:255
template<class TVal , class TCmp >
TVal THeap< TVal, TCmp >::PopHeap ( )

Removes the top element from the heap.

Definition at line 297 of file gbase.h.

297  {
298  IAssert(! HeapV.Empty());
299  const TVal Top = HeapV[0];
300  HeapV[0] = HeapV.Last();
301  HeapV.DelLast();
302  if (! HeapV.Empty()) {
303  AdjustHeap(0, 0, HeapV.Len(), HeapV[0]);
304  }
305  return Top;
306 }
#define IAssert(Cond)
Definition: bd.h:262
TVec< TVal > HeapV
Definition: gbase.h:256
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:530
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:539
void AdjustHeap(const int &First, int HoleIdx, const int &Len, TVal Val)
Definition: gbase.h:319
void DelLast()
Removes the last element of the vector.
Definition: ds.h:609
template<class TVal, class TCmp >
void THeap< TVal, TCmp >::PushHeap ( const int &  First,
int  HoleIdx,
const int &  Top,
TVal  Val 
)
private

Definition at line 309 of file gbase.h.

309  {
310  int Parent = (HoleIdx-1)/2;
311  while (HoleIdx > Top && Cmp(HeapV[First+Parent], Val)) {
312  HeapV[First+HoleIdx] = HeapV[First+Parent];
313  HoleIdx = Parent; Parent = (HoleIdx-1)/2;
314  }
315  HeapV[First+HoleIdx] = Val;
316 }
TVec< TVal > HeapV
Definition: gbase.h:256
TCmp Cmp
Definition: gbase.h:255
template<class TVal, class TCmp >
void THeap< TVal, TCmp >::PushHeap ( const TVal &  Val)

Pushes an element Val to the heap.

Definition at line 291 of file gbase.h.

291  {
292  HeapV.Add(Val);
293  PushHeap(0, HeapV.Len()-1, 0, Val);
294 }
TVec< TVal > HeapV
Definition: gbase.h:256
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
void PushHeap(const int &First, int HoleIdx, const int &Top, TVal Val)
Definition: gbase.h:309
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
template<class TVal, class TCmp = TLss<TVal>>
const TVal& THeap< TVal, TCmp >::TopHeap ( ) const
inline

Returns a reference to the element at the top of the heap (the largest element of the heap).

Definition at line 271 of file gbase.h.

271 { return HeapV[0]; }
TVec< TVal > HeapV
Definition: gbase.h:256

Member Data Documentation

template<class TVal, class TCmp = TLss<TVal>>
TCmp THeap< TVal, TCmp >::Cmp
private

Definition at line 255 of file gbase.h.

template<class TVal, class TCmp = TLss<TVal>>
TVec<TVal> THeap< TVal, TCmp >::HeapV
private

Definition at line 256 of file gbase.h.


The documentation for this class was generated from the following file: