Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

nci/suif/suif2b/common/suif_hash_map.h

Go to the documentation of this file.
00001 
00002 #ifndef _HASH_MAP_H_
00003 #define _HASH_MAP_H_
00004 
00005 #include <stddef.h>
00006 #include <assert.h>
00007 
00019 class suif_hash_map_table;
00020 // avoid nested class decs -some compilers cannot handle
00021 
00022 class suif_hash_map_inner
00023     {
00024         void dup_table(const suif_hash_map_inner &x);
00025     public:
00026         class pair_inner {
00027           public:
00028             pair_inner *next;
00029             };
00030 
00031         class helper_inner {
00032           public:
00033             virtual pair_inner* clone(const pair_inner *) const = 0;
00034             virtual void set_range(const pair_inner *val,pair_inner *x) = 0;
00035             };
00036 
00037 
00038         class key_inner
00039             {
00040             public:
00041                 virtual bool operator == (pair_inner *p) const =0;
00042                 virtual int hash() const =0;
00043             };
00044 
00045         class iterator_inner {
00046             const suif_hash_map_table *hash;
00047             pair_inner *current;
00048             int index;
00049             void advance();
00050             void retreat();
00051           public:
00052             bool operator ==(const iterator_inner &x) const {return current == x.current;}
00053             bool operator !=(const iterator_inner &x) const {return current != x.current;}
00054             pair_inner *get() const {return current;}
00055             iterator_inner & operator ++();
00056             iterator_inner operator ++(int dummy);
00057             iterator_inner & operator --();
00058             iterator_inner operator --(int dummy);
00059 
00060             iterator_inner(const suif_hash_map_table *x,pair_inner *t,int inx) : hash(x),current(t),index(inx) {}
00061             int get_index() {return index;}
00062             const suif_hash_map_table* get_table() {return hash;}
00063           public:
00064             iterator_inner(const iterator_inner &other) :
00065               hash(other.hash),current(other.current),index(other.index) {}
00066           iterator_inner() : hash(0), current(0), index(0) {}
00067             iterator_inner &operator=(const iterator_inner &other) {
00068               hash = other.hash;
00069               current = other.current;
00070               index = other.index;
00071               return(*this);
00072             }
00073           };
00074 
00075         suif_hash_map_inner(helper_inner &x,int table_size = 32);
00076         iterator_inner find(const key_inner &x) const;
00077         void erase(iterator_inner &x);
00078         pair_inner* enter_value(const key_inner &x,const pair_inner &y);
00079         pair_inner* enter_value_no_change(const key_inner &x,const pair_inner &y);
00080 
00081         iterator_inner begin() const;
00082         iterator_inner end() const {return iterator_inner(0,(pair_inner *)0,0);}
00083 
00084         virtual ~suif_hash_map_inner();
00085 
00086         void clear();
00087 
00088         suif_hash_map_inner &operator =(const suif_hash_map_inner &x);
00089         suif_hash_map_inner(const suif_hash_map_inner &x);
00090         unsigned size() const;
00091 
00092         suif_hash_map_table *top_table;
00093         helper_inner *help;
00094     };
00095 
00096 template <class domain,class range>
00097 #ifndef MSVC
00098 class suif_hash_map :  private suif_hash_map_inner {
00099 #else
00100 class suif_hash_map :  public suif_hash_map_inner {
00101 #endif
00102         class key : public suif_hash_map_inner::key_inner {
00103                 const domain &value;
00104             public:
00105                 key(const domain &v) : value(v) {}
00106                 bool operator == (suif_hash_map_inner::pair_inner *p) const {
00107                   return ((pair *)p)->first == value;}
00108                 int hash() const {return ::hash(value);}
00109             };
00110 
00111     class helper : public suif_hash_map_inner::helper_inner {
00112           public:
00113             virtual pair_inner* clone(const pair_inner *x) const {
00114                 pair *y = (pair *)x;
00115                 return new pair(y->first,y->second);
00116                 }
00117             virtual void set_range(const pair_inner *val,pair_inner *x) {
00118                 pair *yval = (pair *)val;
00119                 pair *ref = (pair *)x;
00120                 ref->second = yval->second;
00121                 }
00122             };
00123 
00124         helper the_helper;
00125 
00126 
00127     public:
00128 
00129         class pair : public suif_hash_map_inner::pair_inner {
00130             public:
00131                 domain first;
00132                 range  second;
00133                 pair & operator =(const range &x) {second = x;return *this;}
00134                 pair(domain x,range y) : pair_inner() , first(x),second(y) {}
00135                 pair(domain x) : pair_inner() , first(x) {}
00136             private:
00137               pair(const pair &other) :
00138                 first(other.first), second(other.second) {}
00139               pair &operator=(const pair &other) {
00140                 first = other.first; second = other.second;
00141                 return(*this);
00142               }
00143 
00144             };
00145 
00146         class literator : public suif_hash_map_inner::iterator_inner {
00147             public:
00148                 literator(iterator_inner x) : iterator_inner(x) {}
00149                 literator() {}
00150                 pair & operator *() const{return *(pair *)get();}
00151             public:
00152               literator(const literator &other) :
00153                 iterator_inner((const literator &)other)
00154               {}
00155             };
00156         typedef literator iterator;
00157         typedef const literator const_iterator;
00158 
00160         pair& enter_value(domain x,range y) {
00161                 pair *pPair;
00162                 pair P(x,y);
00163                 pPair = &P;
00164                 pPair = (pair*)(suif_hash_map_inner :: enter_value(key(x),P));
00165                 return *pPair;
00166                 }
00167 
00169         iterator find(const domain &x) const {
00170             return suif_hash_map_inner :: find(key(x));
00171             }
00172 
00176         range   lookup(const domain &x) {
00177             iterator iter = find(x);
00178             assert(iter != end());
00179             return (*iter).second;
00180         }
00181 
00187         iterator begin() {return iterator(suif_hash_map_inner :: begin());}
00188         const_iterator begin() const {return iterator(suif_hash_map_inner :: begin());}
00189         iterator end() {return iterator(suif_hash_map_inner :: end());}
00190         const_iterator end() const {return iterator(suif_hash_map_inner :: end());}
00191 
00193         void erase(iterator &iter) {suif_hash_map_inner::erase(iter);}
00194 
00199         suif_hash_map(int size = 32) :
00200           suif_hash_map_inner(the_helper,size), the_helper() {}
00201 
00202         ~suif_hash_map() {}
00203 
00204         typedef pair value_type;
00205         typedef domain key_type;
00206         typedef range data_type;
00207 
00209         iterator insert(iterator &x,const pair &p) {
00210          enter_value(p.first, p.second);
00211          return(x);
00212          }
00214         unsigned size() const { return suif_hash_map_inner::size(); }
00215 
00217         void clear() { suif_hash_map_inner::clear(); }
00218 
00219     };
00220 
00221 
00222 size_t hash( const void * a );
00223 size_t hash( const unsigned int i );
00224 
00225 #endif

Generated at Mon Jul 31 13:41:40 2000 for NCI SUIF by doxygen 1.1.2 written by Dimitri van Heesch, © 1997-2000