Go to the first, previous, next, last section, table of contents.


CNHashIterator -- Sequential Iterator for Hash Tables

SYNOPSIS

#include <CNCL/HashIterator.h>

TYPE

CN_HASHITERATOR

BASE CLASSES

CNObject

DERIVED CLASSES

None

RELATED CLASSES

CNHashTable, CNHashDynamic, CNHashStatic, CNKey, CNKeyString, CNKeyInt, CNManager

DESCRIPTION

CNHashIterator is an iterator to sequentially traverse a CNHashTable hash table.

Constructors:

CNHashIterator();
CNHashIterator(CNParam *param);
Initializes CNHashIterator.
CNHashIterator(const CNHashTable *new_hash_table);
CNHashIterator(const CNHashTable &new_hash_table);
Initializes CNHashIterator with hash table new_hash_table. The iterator is reset to the first element of the hash table.

In addition to the member functions required by CNCL, CNHashIterator provides:

void reset(const CNHashTable *new_hash_table);
void reset(const CNHashTable &new_hash_table);
void reset();
Resets the iterator to a new hash table new_hash_table and/or sets the iterator to the first element of the hash table.
CNKey *key()
CNKey *get_key()
Gets the referenced key from the current iterator position. It returns the key or NIL, if none is available.
CNKey *first_key();
CNKey *first();
Sets the iterator to the first element of the hash table. It returns the referenced key or NIL, if none is available.
CNKey *last_key();
CNKey *last();
Sets the iterator to the last element of the hash table. It returns the referenced key or NIL, if none is available.
CNKey *next_key();
CNKey *next();
CNKey *operator ++();
CNKey *operator ++(int);
Moves the iterator to the next element of the hash table. It returns the current referenced key (the one BEFORE moving the iterator) or NIL, if none is available.
CNKey *prev_key();
CNKey *prev();
CNKey *operator --();
CNKey *operator --(int);
Moves the iterator to the previous element of the hash table. It returns the current referenced key (the one BEFORE moving the iterator) or NIL, if none is available.
CNObject *object()
CNObject *get_object()
Gets the object of the referenced key from the current iterator position. It returns the object or NIL, if none is available.
CNObject *first_object();
Sets the iterator to the first element of the hash table. It returns the object of the referenced key or NIL, if none is available.
CNObject *last_object();
Sets the iterator to the last element of the hash table. It returns the object of the referenced key or NIL, if none is available.
CNObject *next_object();
Moves the iterator to the next element of the hash table. It returns the object of the current referenced key (the one BEFORE moving the iterator) or NIL, if none is available.
CNObject *prev_object();
Moves the iterator to the previous element of the hash table. It returns the current referenced key (the one BEFORE moving the iterator) or NIL, if none is available.

The following examples show how to use a CNHashIterator object to traverse an hash table:

Forward:

CNHashDynamic hash_table;

...

CNHashIterator trav(hash_table);
CNKey *key;
CNObject *obj;

while(key = trav++)
{
    // Do something with key ...
    obj = key->get_object();        
}

Alternate forward:

for(trav.reset(hash_table); key = trav.key(); trav.next())
{
    // ...
}

Forward with objects:

for(trav.first(); obj = trav.object(); trav.next_object())
{
    // ...
}

Backward:

for(trav.last(); key = trav.key(); trav--)
{
    // ...
}

The only way to delete all entries of an hash table, that is guaranteed to work with CNHashIterator:

for(trav.reset(hash_table); key = trav.key(); trav.reset())
{
    // delete object associated with key
    delete key->get_object();
    // delete hash table entry and key
    hash_table->delete_key_absolutely(key);        
}


Go to the first, previous, next, last section, table of contents.