start of new file
[IRC.git] / Robust / src / Runtime / GenericHashtable.c
index 8c24e05d71773080e1183264a9f52bf1a31ca894..7528b01e783027c5af17df95c0a84f131453f55c 100755 (executable)
@@ -1,12 +1,26 @@
+#ifdef RAW
+#include <raw.h>
+#else
 #include <stdio.h>
+#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <stdlib.h>
-#include <values.h>
+#include <limits.h>
+
 #include "GenericHashtable.h"
 #include "mem.h"
-//#include "dmalloc.h"
+#ifdef DMALLOC
+#include "dmalloc.h"
+#endif
+
+void * getfirstkey(struct genhashtable *ht) {
+       if(ht->list == NULL) {
+               return NULL;
+       }
+  return ht->list->src;
+}
 
 int genputtable(struct genhashtable *ht, void * key, void * object) {
   unsigned int bin=genhashfunction(ht,key);
@@ -27,9 +41,9 @@ int genputtable(struct genhashtable *ht, void * key, void * object) {
   }
   ht->bins[bin]=newptrlist;
   ht->counter++;
-  if(ht->counter>ht->currentsize&&ht->currentsize!=MAXINT) {
+  if(ht->counter>ht->currentsize&&ht->currentsize!=INT_MAX) {
     /* Expand hashtable */
-    long newcurrentsize=(ht->currentsize<(MAXINT/2))?ht->currentsize*2:MAXINT;
+    long newcurrentsize=(ht->currentsize<(INT_MAX/2))?ht->currentsize*2:INT_MAX;
     long oldcurrentsize=ht->currentsize;
     struct genpointerlist **newbins=(struct genpointerlist **) RUNMALLOC(sizeof (struct genpointerlist *)*newcurrentsize);
     struct genpointerlist **oldbins=ht->bins;
@@ -39,7 +53,52 @@ int genputtable(struct genhashtable *ht, void * key, void * object) {
     for(i=0;i<oldcurrentsize;i++) {
       struct genpointerlist * tmpptr=oldbins[i];
       while(tmpptr!=NULL) {
-        int hashcode=genhashfunction(ht, tmpptr->src);
+        unsigned int hashcode=genhashfunction(ht, tmpptr->src);
+        struct genpointerlist *nextptr=tmpptr->next;
+        tmpptr->next=newbins[hashcode];
+        newbins[hashcode]=tmpptr;
+        tmpptr=nextptr;
+      }
+    }
+    ht->bins=newbins;
+    RUNFREE(oldbins);
+  }
+  return 1;
+}
+
+#ifdef RAW
+int genputtable_I(struct genhashtable *ht, void * key, void * object) {
+  unsigned int bin=genhashfunction(ht,key);
+  struct genpointerlist * newptrlist=(struct genpointerlist *) RUNMALLOC_I(sizeof(struct genpointerlist));
+  newptrlist->src=key;
+  newptrlist->object=object;
+  newptrlist->next=ht->bins[bin];
+  newptrlist->inext=NULL;
+  /* maintain linked list of ht entries for iteration*/
+  if (ht->last==NULL) {
+    ht->last=newptrlist;
+    ht->list=newptrlist;
+    newptrlist->iprev=NULL;
+  } else {
+    ht->last->inext=newptrlist;
+    newptrlist->iprev=ht->last;
+    ht->last=newptrlist;
+  }
+  ht->bins[bin]=newptrlist;
+  ht->counter++;
+  if(ht->counter>ht->currentsize&&ht->currentsize!=INT_MAX) {
+    /* Expand hashtable */
+    long newcurrentsize=(ht->currentsize<(INT_MAX/2))?ht->currentsize*2:INT_MAX;
+    long oldcurrentsize=ht->currentsize;
+    struct genpointerlist **newbins=(struct genpointerlist **) RUNMALLOC_I(sizeof (struct genpointerlist *)*newcurrentsize);
+    struct genpointerlist **oldbins=ht->bins;
+    long j,i;
+    for(j=0;j<newcurrentsize;j++) newbins[j]=NULL;
+    ht->currentsize=newcurrentsize;
+    for(i=0;i<oldcurrentsize;i++) {
+      struct genpointerlist * tmpptr=oldbins[i];
+      while(tmpptr!=NULL) {
+        unsigned int hashcode=genhashfunction(ht, tmpptr->src);
         struct genpointerlist *nextptr=tmpptr->next;
         tmpptr->next=newbins[hashcode];
         newbins[hashcode]=tmpptr;
@@ -51,11 +110,31 @@ int genputtable(struct genhashtable *ht, void * key, void * object) {
   }
   return 1;
 }
+#endif
 
 int hashsize(struct genhashtable *ht) {
   return ht->counter;
 }
 
+void genrehash(struct genhashtable * ht) {
+  struct genpointerlist **newbins=(struct genpointerlist **) RUNMALLOC(sizeof (struct genpointerlist *)*ht->currentsize);
+  struct genpointerlist **oldbins=ht->bins;
+  long j,i;
+
+  for(i=0;i<ht->currentsize;i++) {
+    struct genpointerlist * tmpptr=oldbins[i];
+    while(tmpptr!=NULL) {
+      unsigned int hashcode=genhashfunction(ht, tmpptr->src);
+      struct genpointerlist *nextptr=tmpptr->next;
+      tmpptr->next=newbins[hashcode];
+      newbins[hashcode]=tmpptr;
+      tmpptr=nextptr;
+    }
+  }
+  ht->bins=newbins;
+  RUNFREE(oldbins);
+}
+
 void * gengettable(struct genhashtable *ht, void * key) {
   struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
   while(ptr!=NULL) {
@@ -63,7 +142,9 @@ void * gengettable(struct genhashtable *ht, void * key) {
       return ptr->object;
     ptr=ptr->next;
   }
+#ifndef RAW
   printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p\n",key);
+#endif
   return NULL;
 }
 
@@ -77,7 +158,9 @@ void * getnext(struct genhashtable *ht, void * key) {
        return NULL;
     ptr=ptr->next;
   }
+#ifndef RAW
   printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p...\n Likely concurrent removal--bad user!!!\n",key);
+#endif
   return NULL;
 }
 
@@ -95,7 +178,7 @@ int gencontains(struct genhashtable *ht, void * key) {
 
 void genfreekey(struct genhashtable *ht, void * key) {
   struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
-
+  
   if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key))) {
     ht->bins[genhashfunction(ht,key)]=ptr->next;
 
@@ -132,7 +215,9 @@ void genfreekey(struct genhashtable *ht, void * key) {
     }
     ptr=ptr->next;
   }
+#ifndef RAW
   printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p\n",key);
+#endif
 }
 
 unsigned int genhashfunction(struct genhashtable *ht, void * key) {
@@ -143,11 +228,28 @@ unsigned int genhashfunction(struct genhashtable *ht, void * key) {
 }
 
 struct genhashtable * genallocatehashtable(unsigned int (*hash_function)(void *),int (*comp_function)(void *, void *)) {
-  struct genhashtable *ght=(struct genhashtable *) RUNMALLOC(sizeof(struct genhashtable));
-  struct genpointerlist **gpl=(struct genpointerlist **) RUNMALLOC(sizeof(struct genpointerlist *)*geninitialnumbins);
+  struct genhashtable *ght;
+  struct genpointerlist **gpl;
   int i;
-  for(i=0;i<geninitialnumbins;i++)
+
+#ifdef RAWDEBUG
+  raw_test_pass(0xf000);
+#endif
+
+  gpl=(struct genpointerlist **) RUNMALLOC(sizeof(struct genpointerlist *)*geninitialnumbins);
+#ifdef RAWDEBUG
+  raw_test_pass(0xf001);
+#endif
+  for(i=0;i<geninitialnumbins;i++) {
     gpl[i]=NULL;
+  }
+#ifdef RAWDEBUG
+  raw_test_pass(0xf002);
+#endif
+  ght=(struct genhashtable *) RUNMALLOC(sizeof(struct genhashtable));
+#ifdef RAWDEBUG
+  raw_test_pass(0xf003);
+#endif
   ght->hash_function=hash_function;
   ght->comp_function=comp_function;
   ght->currentsize=geninitialnumbins;
@@ -155,6 +257,9 @@ struct genhashtable * genallocatehashtable(unsigned int (*hash_function)(void *)
   ght->counter=0;
   ght->list=NULL;
   ght->last=NULL;
+#ifdef RAWDEBUG
+  raw_test_pass(0xf004);
+#endif
   return ght;
 }