00001
00009 #ifndef FIBONACCI_HEAP_H
00010 #define FIBONACCI_HEAP_H
00011
00012 #include "fibheap.h"
00013 #include "hash_map.h"
00014
00016 template<class Data>
00017 class HeapNode : public FibHeapNode {
00018 public:
00019 Data *data;
00020 double key;
00021
00022 HeapNode() : FibHeapNode() { key = 0.0; data=NULL;}
00023 HeapNode(Data* _data, double _key) : FibHeapNode() { key=_key; data=_data;}
00024
00025 virtual void operator =(FibHeapNode& RHS);
00026 virtual int operator ==(FibHeapNode& RHS);
00027 virtual int operator <(FibHeapNode& RHS);
00028
00029 virtual void Print();
00030
00031 inline void set(Data *_data, double _key) { data=_data; key=_key;}
00032 };
00033
00035 template<class Data>
00036 class FHeap {
00037 private:
00038 typedef hashers::hash_map<Data*, unsigned> Hash_T;
00039 typedef typename Hash_T::iterator Hash_iterator_T;
00040 Hash_T node_idx;
00041 HeapNode<Data> *nodes;
00042 FibHeap heap;
00043 unsigned next_idx;
00044 unsigned size;
00045
00046 public:
00047 FHeap(unsigned _size);
00048 ~FHeap();
00049
00050
00051 bool decrease(Data* data, double val);
00052 bool rem_min(Data* &data, double &val);
00053 inline bool empty();
00054 };
00055
00056
00057 template<class Data>
00058 void HeapNode<Data>::Print() {
00059 FibHeapNode::Print();
00060 std::cout <<"Key:"<< key << " Data:"<< data;
00061 }
00062
00063 template<class Data>
00064 void HeapNode<Data>::operator =(FibHeapNode& RHS) {
00065 FHN_Assign(RHS);
00066 key = ((HeapNode&) RHS).key;
00067 data = ((HeapNode&) RHS).data;
00068 }
00069
00070 template<class Data>
00071 int HeapNode<Data>::operator ==(FibHeapNode& RHS) {
00072 if (FHN_Cmp(RHS)) return 0;
00073 return key == ((HeapNode&) RHS).key ? 1 : 0;
00074 }
00075
00076 template<class Data>
00077 int HeapNode<Data>::operator <(FibHeapNode& RHS) {
00078 int x;
00079 if ((x = FHN_Cmp(RHS)) != 0) return x < 0 ? 1 : 0;
00080 return key < ((HeapNode&) RHS).key ? 1 : 0;
00081 };
00082
00083
00084 template <class Data>
00085 FHeap<Data>::FHeap(unsigned _size){
00086 size = _size;
00087 next_idx = 0;
00088 nodes = new HeapNode<Data>[size];
00089 node_idx.resize(3*size);
00090
00091 }
00092
00093 template <class Data>
00094 FHeap<Data>::~FHeap(){
00095 delete[] nodes;
00096 }
00097
00098 template <class Data>
00099 bool FHeap<Data>::decrease(Data* data, double val){
00100 Hash_iterator_T i;
00101 int idx;
00102
00103 i = node_idx.find(data);
00104 if( i == node_idx.end() ){
00105 if(next_idx == size) return false;
00106 idx = next_idx++;
00107 node_idx[data] = idx;
00108 nodes[idx].set(data, val);
00109 heap.Insert( &nodes[idx] );
00110 return true;
00111 }
00112
00113 HeapNode<Data> temp(data, val);
00114 idx = (*i).second;
00115 return heap.DecreaseKey(&nodes[idx], temp) == OK;
00116 }
00117
00118 template <class Data>
00119 bool FHeap<Data>::rem_min(Data* &data, double &val){
00120 HeapNode<Data> *temp;
00121 temp = (HeapNode<Data>*)heap.ExtractMin();
00122 if(!temp) return false;
00123 data = temp->data;
00124 val = temp->key;
00125 return true;
00126 }
00127
00128 template <class Data>
00129 bool FHeap<Data>::empty(){
00130 return heap.Minimum() == NULL;
00131 }
00132
00133 #endif