TOP --> libjdl

class CJdlHashTable

This class provides a simple hash dictionary.

It can be used to store data that is keyed by a string value. The following example shows how this class is used:

     #include "jdlhashtable.h"
     int main(int argc,char* argv[]) {
       CJdlHashTable tbl;
       // Populate the list
       tbl.Insert("FOO",(void*)10);
       tbl.Insert("BAR",(void*)30);
       tbl.Insert("SPAM",(void*)5);
       // Contains?
       if(tbl.Contains("FOO")) {
         int i = int(tbl.Get("FOO"));
       }
       return 0;
     }
 

Author:
Joe Linoff

Version:
$Id: jdlhashtable.h,v 1.4 1999/06/12 18:26:00 jdl Exp $

Source:
../../libjdl/src/jdlhashtable.h:51

See Also:
CJdlString, CJdlRedBlackTree

Constructors Index

CJdlHashTable
[public] Default constructor.
CJdlHashTable
[public] Constructor.
~CJdlHashTable
[public] Destructor.


Variables Index

m_ListOfRbTrees
[protected] The hash table is a list of Red-black trees. This guarantees that worst case performance is k + O(n (log (n) ) ).
m_NumItems
[protected] Number of items in the hash table.


Methods Index

Clear
[public] Clear the hash table.
Contains
[public] Is this record in the hash table?
Copy
[public] Copy one hash table to another.
Get
[public] Retrieve a value from the hash table.
GetKey
[public] Retrieve the key address from the hash table.
GetNumItems
[public]
Hash
[public] Hash function based on hashpjw on page 436 of the Aho, Sethi and Ullman Compiler book.
Insert
[public] Insert a key/value pair into the hash table.
operator =
[public] Copy operator.
Put
[public] Insert a record into the hash table. This method is identical to Insert().
Remove
[public] Remove an entry from the hash table. If the specified entry does not exist, nothing is changed.
Resize
[public] Resize the hash table. This is a very costly operation because each entry has to be rehashed. Don't do it if you can possibly avoid it.
Size
[public]


Constructors

CJdlHashTable

public CJdlHashTable ( ) ;

Default constructor.

This sets the hash table list size to 10357. If you expect to have more than about 5000 items, you should increase the table size to the nearest prime that is larger than twice the number of expected items.

CJdlHashTable

public CJdlHashTable ( uint size ) ;

Constructor.

Parameters:
size The size of the hash table. This number should be a large prime.

CJdlHashTable

public ~ CJdlHashTable ( ) ;

Destructor.


Variables

m_ListOfRbTrees

protected CJdlVector < CJdlRedBlackTree > m_ListOfRbTrees

The hash table is a list of Red-black trees. This guarantees that worst case performance is k + O(n (log (n) ) ).

m_NumItems

protected uint m_NumItems

Number of items in the hash table.


Methods

Resize

public void Resize ( uint size ) ;

Resize the hash table. This is a very costly operation because each entry has to be rehashed. Don't do it if you can possibly avoid it.

Parameters:
size Size of the new hashtable.

Insert

public void Insert ( const char * key ,
                     void * value ) ;

Insert a key/value pair into the hash table.

Duplicate keys are allowed but the retrieval order is not defined.

Parameters:
key The retrieval key.
value The user data.

Put

public void Put ( const char * ,
                  void * ) ;

Insert a record into the hash table. This method is identical to Insert().

Get

public void * Get ( const char * key ) ;

Retrieve a value from the hash table.

If the key does not exist, 0 is returned. This method should be used in conjunction with Contains() as follows:

     if(hash.Contains("MYKEY")) {
       int val = (int) hash.Get("MYKEY");
     }
 

Parameters:
key The retrieval key.

Return:
The value associated with the key.

GetKey

public const char * GetKey ( const char * key ) ;

Retrieve the key address from the hash table.

If the key does not exist, 0 is returned. This method should be used in conjunction with Contains() as follows:

     if(hash.Contains("MYKEY")) {
       const char* key = hash.GetKey("MYKEY");
     }
 

Parameters:
key The retrieval key.

Return:
The pointer to the key.

Remove

public void Remove ( const char * ) ;

Remove an entry from the hash table. If the specified entry does not exist, nothing is changed.

Parameters:
key The retrieval key.

Contains

public bool Contains ( const char * key ) ;

Is this record in the hash table?

Parameters:
key The retrieval key.

Return:
Returns non-zero if this record exists in the hash table or 0 if it does not.

Clear

public void Clear ( ) ;

Clear the hash table.

Hash

public virtual uint Hash ( const char * ) const ;

Hash function based on hashpjw on page 436 of the Aho, Sethi and Ullman Compiler book.

Parameters:
key The retrieval key.

Return:
The hash index.

Copy

public void Copy ( const CJdlHashTable & tbl ) ;

Copy one hash table to another.

Parameters:
tbl Hash table to copy.

Size

public uint Size ( ) const ;

Return:
the number of records currently stored in the hash table.

GetNumItems

public uint GetNumItems ( ) const ;

Return:
the number of items currently stored in the hash table (same as Size()).

operator =

public CJdlHashTable & operator = ( const CJdlHashTable & tbl ) ;

Copy operator.

Parameters:
tbl Hash table to copy.

Return:
A reference to this hash table.

This documentation was generated automatically by the ccdoc tool (version 0.7a).
Click here to submit a bug report or feature request.

Click here to return to the top of the page.