space hacks
[iotcloud.git] / version2 / src / C / hashset.h
index 5c81425b5b26ca9ad69a5587c806cf8b01e5c128..73495060ac6a3eb0c1f7af7d04330f46c7684797 100644 (file)
@@ -17,8 +17,9 @@ class Hashset;
 template<typename _Key, typename _Val, typename _KeyInt = uintptr_t, int _Shift = 0, unsigned int (*hash_function)(_Key) = defaultHashFunction<_Key, _Shift, _KeyInt>, bool (*equals)(_Key, _Key) = defaultEquals<_Key> >
 class SetIterator {
 public:
-       SetIterator(Hashlistnode<_Key, _Val> *_curr, Hashtable <_Key, _Val, _KeyInt, _Shift, hash_function, equals> *_table) :
-               curr(_curr),
+       SetIterator(Hashtable <_Key, _Val, _KeyInt, _Shift, hash_function, equals> *_table) :
+    currptr(1),
+    nextptr(0),
                table(_table)
        {
        }
@@ -44,32 +45,52 @@ public:
        }
 
        bool hasNext() {
-               return curr != NULL;
+               if (currptr > nextptr) {
+                       currptr = 0;
+               } else {
+                       nextptr++;
+               }
+               if (nextptr == 0) {
+                       if (table->zero && table->zero->val)
+                               return true;
+                       else
+                               nextptr++;
+               }
+               for(;nextptr < (table->capacity + 1);nextptr++) {
+                       struct Hashlistnode<_Key, _Val> *ptr = & table->table[nextptr-1];
+                       if (ptr->key && ptr->val) {
+                               return true;
+                       }
+               }
+               return false;
        }
 
   _Val currVal() {
-         return last->val;
+    if (currptr == 0)
+                       return table->zero->val;
+               else
+                       return table->table[currptr-1].val;
   }
 
-  _Key next() {
-               _Key k = curr->key;
-               last = curr;
-               curr = curr->next;
-               return k;
+  _Key currKey() {
+               if (currptr == 0)
+                       return table->zero->key;
+               else
+                       return table->table[currptr-1].key;
        }
 
-       _Key currKey() {
-               return last->key;
+  _Key next() {
+               currptr = nextptr;
+               return currKey();
        }
 
        void remove() {
-               _Key k = last->key;
-               table->remove(k);
+               table->remove(currKey());
        }
 
 private:
-       Hashlistnode<_Key,_Val> *curr;
-       Hashlistnode<_Key, _Val> *last;
+  unsigned int currptr;
+  unsigned int nextptr;
        Hashtable <_Key, _Val, _KeyInt, _Shift, hash_function, equals> *table;
 };
 
@@ -151,7 +172,7 @@ public:
        }
 
        SetIterator<_Key, _Key, _KeyInt, _Shift, hash_function, equals> *iterator() {
-               return new SetIterator<_Key, _Key, _KeyInt, _Shift, hash_function, equals>(table->list, table);
+               return new SetIterator<_Key, _Key, _KeyInt, _Shift, hash_function, equals>(table);
        }
 
        /** Override: new operator */
@@ -179,6 +200,6 @@ private:
 
 template<typename _Key, typename _Val, typename _KeyInt, int _Shift, unsigned int (*hash_function)(_Key), bool (*equals)(_Key, _Key)>
 SetIterator<_Key, _Val,_KeyInt, _Shift, hash_function, equals> *getKeyIterator(Hashtable<_Key,_Val,_KeyInt,_Shift,hash_function,equals> *table) {
-       return new SetIterator<_Key, _Val, _KeyInt, _Shift, hash_function, equals>(table->list, table);
+       return new SetIterator<_Key, _Val, _KeyInt, _Shift, hash_function, equals>(table);
 }
 #endif