Had a bug in our implementation. If the list empties when the tail is at the last...
[repair.git] / Repair / RepairCompiler / MCC / Runtime / SimpleHash.cc
index 77beed787a1e7cafc4aaaa02f4dd9eceabef518e..6281d150906ea41f4dacad41add0e523d4222f51 100755 (executable)
@@ -119,10 +119,12 @@ void WorkList::pop() {
   int newoffset=tailoffset+4;
   struct ListNode *ptr=tail;
   if (newoffset>=WLISTSIZE) {
-    newoffset-=WLISTSIZE;
-    struct ListNode *oldptr=ptr;
-    ptr=ptr->next;
-    free(oldptr);
+    if (newoffset!=WLISTSIZE||head!=tail) {
+      newoffset-=WLISTSIZE;
+      struct ListNode *oldptr=ptr;
+      ptr=ptr->next;
+      free(oldptr);
+    }
   }
   tail=ptr;
   tailoffset=newoffset;
@@ -132,10 +134,14 @@ void WorkList::add(int id,int type, int lvalue, int rvalue) {
   if (headoffset==WLISTSIZE) {
     if (head->next==0) {
       head->next=(struct ListNode *)malloc(sizeof(struct ListNode));
-      head->next=0;
+      head->next->next=0;
     }
     headoffset=0;
     head=head->next;
+    if (tailoffset==WLISTSIZE) { /* roll the tail over also */
+      tailoffset=0;
+      tail=tail->next;
+    }
   }
   head->data[headoffset++]=id;
   head->data[headoffset++]=type;
@@ -229,7 +235,15 @@ int SimpleHash::remove(int key, int data) {
     return 0;
 }
 
-
+void SimpleHash::addAll(SimpleHash * set) {
+  SimpleIterator it;
+  set->iterator(it);
+  while(it.hasNext()) {
+    int key=it.key();
+    int data=it.next();
+    add(key,data);
+  }
+}
 
 int SimpleHash::add(int key, int data) {
   /* Rehash code */
@@ -325,6 +339,20 @@ int SimpleHash::count(int key) {
     return count;
 }
 
+SimpleHash * SimpleHash::imageSet(int key) {
+  SimpleHash * newset=new SimpleHash(2*count(key)+4);
+  unsigned int hashkey = (unsigned int)key % size;
+  
+  struct SimpleNode *ptr = bucket[hashkey];
+  while (ptr) {
+    if (ptr->key == key) {
+      newset->add(ptr->data,ptr->data);
+    }
+    ptr = ptr->next;
+  }
+  return newset;
+}
+
 int SimpleHash::get(int key, int&data) {
     unsigned int hashkey = (unsigned int)key % size;