Grafalgo
Library of useful data structures and algorithms
/Users/jst/src/grafalgo/cpp/include/DiffHeap.cpp
00001 
00009 #ifndef DIFFHEAP_H
00010 #define DIFFHEAP_H
00011 
00012 #include "Adt.h"
00013 
00014 namespace grafalgo {
00015 
00016 typedef int keytyp;
00017 
00024 class DiffHeap : public Adt {
00025 public:         DiffHeap(int,int);
00026                 ~DiffHeap();
00027 
00028         // common methods
00029         void    clear();
00030         void    resize(int);
00031         void    expand(int);
00032         void    copyFrom(const DiffHeap&);
00033 
00034         // access methods
00035         index   findmin() const;
00036         keytyp  key(index) const;
00037 
00038         // predicates
00039         bool    member(index) const;
00040         bool    empty() const;  
00041 
00042         // modifiers
00043         void    insert(index,keytyp);
00044         void    remove(index);
00045         index   deletemin();
00046         void    changekey(index,keytyp);        
00047         void    addtokeys(keytyp);
00048 
00049         // stats methods
00050         void    clearStats();
00051         string& stats2string(string&) const;
00052 
00053         string& toString(string&) const;
00054 private:
00055         int     hn;                     
00056 
00057         index   *h;                     
00058         int     *pos;                   
00059         keytyp  *dkey;                  
00060 
00061         index   minchild(index);                
00062         void    siftup(index,int);
00063         void    siftdown(index,int);
00064 
00065         void    makeSpace(int);
00066         void    freeSpace();
00067 };
00068 
00072 inline int DiffHeap::findmin() const { return hn == 0 ? 0 : h[1]; }
00073 
00078 inline int DiffHeap::deletemin() {
00079         if (hn == 0) return 0;
00080         index i = h[1]; remove(h[1]);
00081         return i;
00082 }
00083 
00088 inline bool DiffHeap::member(index i) const { return pos[i] != 0; }
00089 
00093 inline bool DiffHeap::empty() const { return hn == 0; };
00094 
00095 } // ends namespace
00096 
00097 #endif
 All Classes Files Functions Variables Typedefs Friends