fix some allocation/free bugs when we put non objects into table...
[model-checker.git] / hashtable.h
index e59a995fd92347a361a785a2d5e36c646b1a7656..4eae7571d24af74369b0f5ba55ebce40cd297a48 100644 (file)
@@ -18,7 +18,7 @@ template<typename _Key, typename _Val>
 /** Hashtable class.  By default it is snapshotting, but you can pass
                in your own allocation functions. */
 
-template<typename _Key, typename _Val, typename _KeyInt, int _Shift, void * (* _malloc)(size_t)=malloc, void * (* _calloc)(size_t, size_t)=calloc, void (*_free)(void *)=free>
+template<typename _Key, typename _Val, typename _KeyInt, int _Shift=0, void * (* _malloc)(size_t)=malloc, void * (* _calloc)(size_t, size_t)=calloc, void (*_free)(void *)=free>
        class HashTable {
  public:
        HashTable(unsigned int initialcapacity=1024, double factor=0.5) {
@@ -36,7 +36,7 @@ template<typename _Key, typename _Val, typename _KeyInt, int _Shift, void * (* _
                        struct hashlistnode<_Key,_Val> * bin = table[i];
                        while(bin!=NULL) {
                                struct hashlistnode<_Key,_Val> * next=bin->next;
-                               _free(bin);
+                               delete bin;
                                bin=next;
                        }
                }
@@ -65,7 +65,7 @@ template<typename _Key, typename _Val, typename _KeyInt, int _Shift, void * (* _
                        struct hashlistnode<_Key,_Val> * bin = table[i];
                        while(bin!=NULL) {
                                struct hashlistnode<_Key,_Val> * next=bin->next;
-                               _free(bin);
+                               delete bin;
                                bin=next;
                        }
                }
@@ -93,13 +93,39 @@ template<typename _Key, typename _Val, typename _KeyInt, int _Shift, void * (* _
                        search=search->next;
                }
 
-               struct hashlistnode<_Key,_Val> *newptr=(struct hashlistnode<_Key,_Val> *)_malloc(sizeof(struct hashlistnode<_Key,_Val>));
+               struct hashlistnode<_Key,_Val> *newptr=(struct hashlistnode<_Key,_Val> *)new struct hashlistnode<_Key,_Val>;
                newptr->key=key;
                newptr->val=val;
                newptr->next=ptr;
                table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
        }
 
+       /** Put a key entry into the table. */
+  _Val * ensureptr(_Key key) {
+               if(size > threshold) {
+                       //Resize
+                 unsigned int newsize = capacity << 1;
+                 resize(newsize);
+         }
+
+         struct hashlistnode<_Key,_Val> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
+               size++;
+               struct hashlistnode<_Key,_Val> *search = ptr;
+
+               while(search!=NULL) {
+                       if (search->key==key) {
+                               return &search->val;
+                       }
+                       search=search->next;
+               }
+
+               struct hashlistnode<_Key,_Val> *newptr=(struct hashlistnode<_Key,_Val> *)new struct hashlistnode<_Key,_Val>;
+               newptr->key=key;
+               newptr->next=ptr;
+               table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
+               return &newptr->val;
+       }
+
        /** Lookup the corresponding value for the given key. */
        _Val get(_Key key) {
                struct hashlistnode<_Key,_Val> *search = table[(((_KeyInt)key) & mask)>>_Shift];
@@ -113,6 +139,19 @@ template<typename _Key, typename _Val, typename _KeyInt, int _Shift, void * (* _
                return (_Val)0;
        }
 
+       /** Lookup the corresponding value for the given key. */
+       _Val * getptr(_Key key) {
+               struct hashlistnode<_Key,_Val> *search = table[(((_KeyInt)key) & mask)>>_Shift];
+
+               while(search!=NULL) {
+                       if (search->key==key) {
+                               return & search->val;
+                       }
+                       search=search->next;
+               }
+               return (_Val *) NULL;
+       }
+
        /** Check whether the table contains a value for the given key. */
        bool contains(_Key key) {
                struct hashlistnode<_Key,_Val> *search = table[(((_KeyInt)key) & mask)>>_Shift];