Grafalgo
Library of useful data structures and algorithms
|
00001 00009 #ifndef HASHMAP_H 00010 #define HASHMAP_H 00011 00012 #include "stdinc.h" 00013 #include "Adt.h" 00014 #include "HashTbl.h" 00015 #include "SetPair.h" 00016 00017 namespace grafalgo { 00018 00035 class HashMap : public Adt { 00036 public: 00037 HashMap(int); 00038 ~HashMap(); 00039 00040 // common methods 00041 void clear(); 00042 void resize(int); 00043 void expand(int); 00044 void copyFrom(const HashMap&); 00045 00046 // methods for accessing pairs by index 00047 index firstPair() const; 00048 index nextPair(index) const; 00049 uint64_t key(index) const; 00050 int val(index) const; 00051 00052 // modifiers 00053 int get(uint64_t) const; 00054 bool put(uint64_t, int); 00055 void remove(uint64_t); 00056 00057 string& toString(string&) const; 00058 private: 00059 int *values; 00060 HashTbl *ht; 00061 SetPair *kvx; 00062 00063 void makeSpace(int); 00064 void freeSpace(); 00065 }; 00066 00070 inline index HashMap::firstPair() const { return kvx->firstIn(); } 00071 00076 inline index HashMap::nextPair(index x) const { return kvx->nextIn(x); } 00077 00082 inline uint64_t HashMap::key(index x) const { return ht->getKey(x); } 00083 00088 inline int HashMap::val(index x) const { return values[x]; } 00089 00094 inline int HashMap::get(uint64_t key) const { 00095 index x = ht->lookup(key); return (x == 0 ? 0 : values[x]); 00096 } 00097 00103 inline bool HashMap::put(uint64_t key, int value) { 00104 index x = ht->lookup(key); 00105 if (x != 0) { values[x] = value; return true; } 00106 x = kvx->firstOut(); if (x == 0) return false; 00107 if (!ht->insert(key,x)) return false; 00108 kvx->swap(x); values[x] = value; 00109 return true; 00110 } 00111 00115 inline void HashMap::remove(uint64_t key) { 00116 index x = ht->remove(key); if (x != 0) kvx->swap(x); 00117 } 00118 00119 } // ends namespace 00120 00121 #endif