Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

/users/u3/mtikir/PMaCInstrumentor_v1601/include/SimpleHash.h

Go to the documentation of this file.
00001 #ifndef _SimpleHash_h_
00002 #define _SimpleHash_h_
00003 
00004 template <class T=uint64_t>
00005 class SimpleHash {
00006 private:
00007 #define BUCKET_COUNT 991
00008     typedef struct entry {
00009         struct entry* next;
00010         uint64_t key;
00011         T value;
00012     } entry_t;
00013 
00014     uint32_t entryCount;
00015     entry_t* buckets[BUCKET_COUNT];
00016 
00017 public:
00018     SimpleHash() {
00019         entryCount = 0;
00020         for(uint32_t i=0;i<BUCKET_COUNT;i++){
00021             buckets[i] = NULL;
00022         }
00023     }
00024     ~SimpleHash() {
00025         for(uint32_t i=0;i<BUCKET_COUNT;i++){
00026             entry_t* current = buckets[i];
00027             while(current){
00028                 entry_t* tmp = current->next;
00029                 delete current;
00030                 current = tmp;
00031             }
00032         }
00033     }
00034     bool exists(uint64_t key,T value){
00035         uint64_t bucketIdx = key % BUCKET_COUNT;
00036         entry_t* current = buckets[bucketIdx];
00037 
00038         for(;current;current=current->next){
00039             if(current->value == value){
00040                 break;
00041             }
00042         }
00043         return (current ? true : false);
00044     }
00045 
00046     T* values(){
00047         if(!entryCount){
00048             return NULL;
00049         }
00050         T* ret = new T[entryCount];
00051         uint32_t idx = 0;
00052         for(uint32_t i=0;i<BUCKET_COUNT;i++){
00053             for(entry_t* current = buckets[i];
00054                 current;current=current->next){
00055                 ret[idx++] = current->value;
00056             }
00057         }
00058         ASSERT((entryCount == idx) && "Fatal: There is a problem with Hash size");
00059         return ret;
00060     }
00061 
00062     void insert(uint64_t key,T value){
00063 
00064         uint64_t bucketIdx = key % BUCKET_COUNT;
00065         entry_t* current = buckets[bucketIdx];
00066 
00067         for(;current;current=current->next){
00068             if(current->value == value){
00069                 break;
00070             }
00071         }
00072 
00073         if(!current){
00074             entry_t* newEntry = new entry_t;            
00075             newEntry->key = key;
00076             newEntry->value = value;
00077             newEntry->next = buckets[bucketIdx];
00078             buckets[bucketIdx] = newEntry;
00079             entryCount++;
00080         }
00081     }
00082     uint32_t size() { return entryCount; }
00083 
00084     void print(){
00085         printf("Element Count : %d\n",entryCount);
00086         for(uint32_t i=0;i<BUCKET_COUNT;i++){
00087             printf("\tBucket %5d :{ ",i);
00088             for(entry_t* current = buckets[i];
00089                 current;current=current->next){
00090                 printf("%lld ",current->key);
00091             }
00092             printf("}\n");
00093         }
00094     }
00095 };
00096 #endif

Generated on Mon Jan 28 11:08:31 2008 for PMaCInstrumentor by doxygen 1.3.5