MeshKit  1.0
Heap.hpp
Go to the documentation of this file.
00001 #ifndef GFXTOOLS_HEAP_INCLUDED // -*- C++ -*-
00002 #define GFXTOOLS_HEAP_INCLUDED
00003 
00004 #include "Array.h"
00005 
00006 #define NOT_IN_HEAP -47
00007 
00008 //
00009 //
00010 // This file extracted from Terra
00011 //
00012 //
00013 
00014 class Heapable
00015 {
00016 private:
00017     int token;
00018 
00019 public:
00020     Heapable() { notInHeap(); }
00021 
00022     inline int isInHeap() { return token!=NOT_IN_HEAP; }
00023     inline void notInHeap() { token = NOT_IN_HEAP; }
00024     inline int getHeapPos() { return token; }
00025     inline void setHeapPos(int t) { token=t; }
00026 };
00027 
00028 
00029 class heap_node {
00030 public:
00031     double import;
00032     Heapable *obj;
00033 
00034     heap_node() { obj=NULL; import=0.0; }
00035     heap_node(Heapable *t, double i=0.0) { obj=t; import=i; }
00036     heap_node(const heap_node& h) { import=h.import; obj=h.obj; }
00037 };
00038 
00039 
00040 
00041 class Heap : public array<heap_node> {
00042 
00043     //
00044     // The actual size of the heap.  array::length()
00045     // simply returns the amount of allocated space
00046     int size;
00047 
00048     void swap(int i, int j);
00049 
00050     int parent(int i) { return (i-1)/2; }
00051     int left(int i) { return 2*i+1; }
00052     int right(int i) { return 2*i+2; }
00053 
00054     void upheap(int i);
00055     void downheap(int i);
00056 
00057 public:
00058 
00059     Heap() { size=0; }
00060     Heap(int s) : array<heap_node>(s) { size=0; }
00061 
00062 
00063     void insert(Heapable *, double);
00064     void update(Heapable *, double);
00065 
00066     heap_node *extract();
00067     heap_node *top() { return size<1 ? (heap_node *)NULL : &ref(0); }
00068     heap_node *kill(int i);
00069 };
00070 
00071 
00072 
00073 // GFXTOOLS_HEAP_INCLUDED
00074 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines