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)
{
}
}
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;
};
}
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 */
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