Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

StringKeyHashMap Class Template Reference

#include <HashMap.h>

Inheritance diagram for StringKeyHashMap:

HashMap ICStringKeyHashMap List of all members.

Detailed Description

template<class VALUETYPE>
class toolbox::StringKeyHashMap< VALUETYPE >

A StringKeyHashMap stores objects in the same way as the HashMap does, but uses char arrays (strings) as keys.

To keys equal each other, if they contain the same characters.

Author:
Thomas Jacob

Definition at line 510 of file HashMap.h.

Public Member Functions

bool ContainsKey (void *key) const
 Returns, if the hash table contains a given key.

bool ContainsKey (const char *key) const
 Returns, if the hash table contains a given key.

void Delete (void *key)
 Removes a key/value pair from the hash table.

void Delete (const char *key)
 Removes a key/value pair from the hash table.

void DeleteAll ()
 Removes all key/value pairs from the hash table.

VALUETYPE * Get (void *key) const
 Returns the value of a key/value pair by its key.

VALUETYPE * Get (const char *key) const
 Returns the value of a key/value pair by its key.

long GetCount () const
 Returns the number of set keys.

int GetDesiredFillRatio () const
 Returns the fill ratio after a hash table resize.

int GetFillRatio () const
 Returns the current fill ratio.

PointeredListGetKeys () const
 Returns a new PointeredList of all keys currently in the hash table.

long GetSize () const
 Returns the number of buckets currently used.

PointeredListGetValues () const
 Returns a new PointeredList of all values currently in the hash table.

bool IsEmpty () const
 Returns whether there are no elements in the hash table.

void Set (void *key, VALUETYPE *Value)
 Sets a key/value pair into the hash table.

void Set (const char *key, VALUETYPE *Value)
 Sets a key/value pair into the hash table.

 StringKeyHashMap (long initSize, int desiredFillRatio)
 Creates a new string key hash table with customized settings.

 StringKeyHashMap (long initSize)
 Creates a new string key hash table with an initial size.

 StringKeyHashMap ()
 Creates a new string key hash table.

VALUETYPE * Unset (void *key)
 Removes a key/value pair from the hash table.

VALUETYPE * Unset (const char *key)
 Removes a key/value pair from the hash table.

void UnsetAll ()
 Removes all key/value pairs from the hash table.

virtual ~StringKeyHashMap ()
 Destroys the string key hash table.


Protected Member Functions

virtual void * CreateKey (void *key) const
 Creates the key object that shall be stored in the key buckets.

virtual void DestroyKey (void *key) const
 Disposes a key created by the CreateKey method.

virtual bool Equals (void *key1, void *key2) const
 Checks, if to keys equal each other.

virtual unsigned long HashCode (void *key) const
 Returns the hash code of the key.


Protected Attributes

PointeredList ** KeyBuckets
 The array of key buckets.

long Size
 The number of buckets currently used.

PointeredList ** ValueBuckets
 The array of value buckets.


Constructor & Destructor Documentation

StringKeyHashMap  ) 
 

Creates a new string key hash table.

StringKeyHashMap long  initSize  ) 
 

Creates a new string key hash table with an initial size.

Parameters:
initSize The minimum and initial size of the hash table. This is the minimum and initial number of buckets.

StringKeyHashMap long  initSize,
int  desiredFillRatio
 

Creates a new string key hash table with customized settings.

Parameters:
initSize The minimum and initial size of the hash table. This is the minimum and initial number of buckets.
desiredFillRatio The fill ratio after a hash table resize. This happens, if the fill ratio gets higher than one and a half times the fill ratio or lower than half the fill ratio. The rest of the buckets is reserved for further setting of keys.

virtual ~StringKeyHashMap  )  [virtual]
 

Destroys the string key hash table.


Member Function Documentation

bool ContainsKey void *  key  )  const [inherited]
 

Returns, if the hash table contains a given key.

Parameters:
key The key to be looked up. Note that NULL is a valid value among all possible values for keys.
Returns:
if the hash table contains the key.

bool ContainsKey const char *  key  )  const
 

Returns, if the hash table contains a given key.

Parameters:
key The key to be looked up.
Returns:
if the hash table contains the key.

virtual void* CreateKey void *  key  )  const [protected, virtual]
 

Creates the key object that shall be stored in the key buckets.

The method receives a key and e.g. creates a clone, or uses the key directly, if this is possible. The Equals and HashCode method must return the same values when using the original key and the one returned by this method. The DestroyKey method disposes this key again. In the StringKeyHashMap class, this method clones the key, since it is a string (const char *).

Parameters:
key The original key provided by the user of this class. Note that NULL is a valid value among all possible values for keys.
Returns:
The key to be used by the key buckets.

Reimplemented from HashMap.

void Delete void *  key  )  [inherited]
 

Removes a key/value pair from the hash table.

The memory of the key is not freed, since it was not acquired, but the memory of the value is freed.

Parameters:
key The key to be looked up for deletion. Note that NULL is a valid value among all possible values for keys.

void Delete const char *  key  )  [inline]
 

Removes a key/value pair from the hash table.

The original memory of the key provided when setting the key/value is not freed, since the StringKeyHashMap uses a private copy of it.

Parameters:
key The key to be looked up for deletion.

void DeleteAll  )  [inherited]
 

Removes all key/value pairs from the hash table.

The memory of the keys is not freed, since it was not acquired, but the memory of the values is freed.

virtual void DestroyKey void *  key  )  const [protected, virtual]
 

Disposes a key created by the CreateKey method.

This can be done by freeing the memory, if the key was cloned, or by simply doing nothing, if the user's key was directly used. This method should do the opposite of the CreateKey method. In the StringKeyHashMap class, this method frees the memory, since the CreateKey method created it.

Parameters:
key The key created by the CreateKey method. Note that NULL is a valid value among all possible values for keys.

Reimplemented from HashMap.

virtual bool Equals void *  key1,
void *  key2
const [protected, virtual]
 

Checks, if to keys equal each other.

In the StringKeyHashMap class, this is true, if they contain the same characters.

Parameters:
key1 The first key.
key2 The second key.
Returns:
If both keys equal each other.

Reimplemented from HashMap.

Reimplemented in ICStringKeyHashMap, and ICStringKeyHashMap< Void >.

VALUETYPE* Get void *  key  )  const [inherited]
 

Returns the value of a key/value pair by its key.

Parameters:
key The key to be looked up. Note that NULL is a valid value among all possible values for keys.
Returns:
The value to key, or NULL, if the key cannot be found or its value was NULL.

VALUETYPE* Get const char *  key  )  const [inline]
 

Returns the value of a key/value pair by its key.

Parameters:
key The key to be looked up.
Returns:
The value to key, or NULL, if the key cannot be found or its value was NULL.

long GetCount  )  const [inline, inherited]
 

Returns the number of set keys.

Returns:
The number of set keys.

Reimplemented in HashSet.

int GetDesiredFillRatio  )  const [inline, inherited]
 

Returns the fill ratio after a hash table resize.

Returns:
The fill ratio after a hash table resize.

int GetFillRatio  )  const [inline, inherited]
 

Returns the current fill ratio.

This is the ratio between the used hash table cells (Count), and the reserved cells (Size).

Returns:
The current fill ratio.

PointeredList* GetKeys  )  const [inherited]
 

Returns a new PointeredList of all keys currently in the hash table.

Returns:
The keys as a PointeredList.
Warning:
This method creates a PointeredList and expects that it is deleted by the caller.

long GetSize  )  const [inline, inherited]
 

Returns the number of buckets currently used.

Returns:
The number of buckets currently used.
See also:
GetCount()

PointeredList* GetValues  )  const [inherited]
 

Returns a new PointeredList of all values currently in the hash table.

Returns:
The values as a PointeredList.
Warning:
This method creates a PointeredList and expects that it is deleted by the caller.

virtual unsigned long HashCode void *  key  )  const [protected, virtual]
 

Returns the hash code of the key.

This is a value that has to be constant for the same key all the time, needs not to be injective, and should distribute the keys quite good.

Parameters:
key The key to determine the hash code of. Note that NULL is a valid value among all possible values for keys.
Returns:
The key's hash code.

Reimplemented from HashMap.

Reimplemented in ICStringKeyHashMap, and ICStringKeyHashMap< Void >.

bool IsEmpty  )  const [inline, inherited]
 

Returns whether there are no elements in the hash table.

Returns:
Whether there are no elements in the hash table.

Reimplemented in HashSet.

void Set void *  key,
VALUETYPE *  Value
[inherited]
 

Sets a key/value pair into the hash table.

Afterwards, the method checks if the hash table has to be resized. If the key existed, the old value is replaced, and its memory is freed.

Parameters:
key The key to be set. Since the memory of the key is not freed after unsetting the key or deleting the hash table, the memory management is still yours after the call. Note that NULL is a valid value among all possible values for keys.
Value The value to be set. The memory of the value is freed when deleting the key or hash table, and is not freed when unsetting the key. A NULL value is valid. Its key is set anyway. Use Unset to remove the key, too.

void Set const char *  key,
VALUETYPE *  Value
[inline]
 

Sets a key/value pair into the hash table.

Afterwards, the method checks if the hash table has to be resized. If the key existed, the old value is replaced, and its memory is freed.

Parameters:
key The key to be set. Since the StringKeyHashMap uses a private copy of the key's memory, it is not freed after unsetting or deleting the key or deleting the hash table.
Value The value to be set. The memory of the value is freed when deleting the key or hash table, and is not freed when unsetting the key. A NULL value is valid. Its key is set anyway. Use Unset to remove the key, too.

VALUETYPE* Unset void *  key  )  [inherited]
 

Removes a key/value pair from the hash table.

Afterwards, the method checks if the hash table has to be resized. Both the memory of the key and the memory of the value are not freed, but the value is returned.

Parameters:
key The key to be looked up for removal. Note that NULL is a valid value among all possible values for keys.
Returns:
The value that was stored at the key before.

VALUETYPE* Unset const char *  key  )  [inline]
 

Removes a key/value pair from the hash table.

Afterwards, the method checks if the hash table has to be resized. Both the memory of the key and the memory of the value are not freed, but the value is returned.

Parameters:
key The key to be looked up for removal.
Returns:
The value that was stored at the key before.

void UnsetAll  )  [inherited]
 

Removes all key/value pairs from the hash table.

Afterwards, the method checks if the hash table has to be resized. Both the memory of the keys and the memory of the values are not freed.

Warning:
No value is freed, so you have to have a pointer to the objects, or the memory is lost.


Member Data Documentation

PointeredList** KeyBuckets [protected, inherited]
 

The array of key buckets.

Definition at line 118 of file HashMap.h.

long Size [protected, inherited]
 

The number of buckets currently used.

See also:
Count

Definition at line 124 of file HashMap.h.

PointeredList** ValueBuckets [protected, inherited]
 

The array of value buckets.

Definition at line 129 of file HashMap.h.


The documentation for this class was generated from the following file:
Generated on Tue Oct 3 00:23:41 2006 for ToolBox by doxygen 1.3.6