CrystalSpace

Public API Reference

CS::Utility::StringHash< Tag > Class Template Reference

A string-to-ID hash table. More...

#include <csutil/strhash.h>

Inheritance diagram for CS::Utility::StringHash< Tag >:

List of all members.

Public Member Functions

void Clear ()
 Delete all stored strings.
bool Contains (StringID< Tag > id) const
 Check if the hash contains a string with a particular ID.
bool Contains (char const *s) const
 Check if the hash contains a particular string.
bool Delete (StringID< Tag > id)
 Remove a string with the specified ID.
bool Delete (char const *s)
 Remove specified string.
void Empty ()
 Remove all stored strings.
GlobalIterator GetIterator () const
 Return an iterator for the string hash which iterates over all elements.
size_t GetSize () const
 Get the number of elements in the hash.
bool IsEmpty () const
 Return true if the hash is empty.
StringHashoperator= (StringHash const &h)
 Assignment operator.
const char * Register (const char *s, StringID< Tag > id=0)
 Register a string with an ID.
const char * Request (StringID< Tag > id) const
 Request the string for a given ID.
StringID< Tag > Request (const char *s) const
 Request the ID for the given string.
 StringHash (StringHash const &h)
 Copy constructor.
 StringHash (size_t size=23)
 Constructor.
 ~StringHash ()
 Destructor.

Detailed Description

template<typename Tag>
class CS::Utility::StringHash< Tag >

A string-to-ID hash table.

Useful when you need to work with strings but want the performance characteristics of simple numeric comparisons. Register a string with a unique numeric ID and then compare ID's rather than comparing strings. You can fetch a string's ID via Request().

See also:
csStringSet

Definition at line 43 of file strhash.h.


Constructor & Destructor Documentation

template<typename Tag>
CS::Utility::StringHash< Tag >::StringHash ( size_t  size = 23  )  [inline]

Constructor.

Definition at line 70 of file strhash.h.

template<typename Tag>
CS::Utility::StringHash< Tag >::StringHash ( StringHash< Tag > const &  h  )  [inline]

Copy constructor.

Definition at line 72 of file strhash.h.

template<typename Tag>
CS::Utility::StringHash< Tag >::~StringHash (  )  [inline]

Destructor.

Definition at line 74 of file strhash.h.


Member Function Documentation

template<typename Tag>
void CS::Utility::StringHash< Tag >::Clear (  )  [inline]

Delete all stored strings.

Deprecated:
Use Empty() instead.

Definition at line 191 of file strhash.h.

template<typename Tag>
bool CS::Utility::StringHash< Tag >::Contains ( StringID< Tag >  id  )  const [inline]

Check if the hash contains a string with a particular ID.

Remarks:
This is rigidly equivalent to return Request(id) != NULL, but more idiomatic.
Warning:
This operation is slow. If you need to check containment of ID's frequently, then instead consider using csStringSet, in which such checks are optimized.

Definition at line 153 of file strhash.h.

template<typename Tag>
bool CS::Utility::StringHash< Tag >::Contains ( char const *  s  )  const [inline]

Check if the hash contains a particular string.

Remarks:
This is rigidly equivalent to return Request(s) != csInvalidStringID.

Definition at line 142 of file strhash.h.

template<typename Tag>
bool CS::Utility::StringHash< Tag >::Delete ( StringID< Tag >  id  )  [inline]

Remove a string with the specified ID.

Returns:
True if a matching string was in thet set; else false.
Remarks:
If more than one string is associated with the ID, then one is removed (but specifically which one is unspecified).

Definition at line 171 of file strhash.h.

template<typename Tag>
bool CS::Utility::StringHash< Tag >::Delete ( char const *  s  )  [inline]

Remove specified string.

Returns:
True if a matching string was in thet set; else false.

Reimplemented in csStringHashReversible.

Definition at line 160 of file strhash.h.

template<typename Tag>
void CS::Utility::StringHash< Tag >::Empty (  )  [inline]

Remove all stored strings.

Reimplemented in csStringHashReversible.

Definition at line 180 of file strhash.h.

template<typename Tag>
GlobalIterator CS::Utility::StringHash< Tag >::GetIterator (  )  const [inline]

Return an iterator for the string hash which iterates over all elements.

Warning:
Modifying the hash while you have open iterators will result undefined behaviour.

Definition at line 211 of file strhash.h.

template<typename Tag>
size_t CS::Utility::StringHash< Tag >::GetSize (  )  const [inline]

Get the number of elements in the hash.

Definition at line 195 of file strhash.h.

template<typename Tag>
bool CS::Utility::StringHash< Tag >::IsEmpty (  )  const [inline]

Return true if the hash is empty.

Remarks:
Rigidly equivalent to return GetSize() == 0, but more idiomatic.

Definition at line 203 of file strhash.h.

template<typename Tag>
StringHash& CS::Utility::StringHash< Tag >::operator= ( StringHash< Tag > const &  h  )  [inline]

Assignment operator.

Reimplemented in csStringHashReversible.

Definition at line 76 of file strhash.h.

template<typename Tag>
const char* CS::Utility::StringHash< Tag >::Register ( const char *  s,
StringID< Tag >  id = 0 
) [inline]

Register a string with an ID.

Parameters:
s The string with which to associate the ID.
id A numeric value with which to identify this string.
Returns:
A pointer to the copy of the string in this hash.
Remarks:
If the string is already registered with a different ID, the old ID will be replaced with the one specified here. If you would like the convenience of having the ID assigned automatically, then consider using csStringSet, instead.
If you do not care about the ID, but instead simply want to use the hash as a string set which merely records if a string is present, then you can omit id. To find out if a string is contained in the set, invoke Contains(). The same functionality can be accomplished via csStringSet, however csStringSet is more heavyweight because it also maintains a reverse-mapping from ID to string. Omitting the id makes for a good alternative to csStringSet when you do not require its extra bulk.

Definition at line 97 of file strhash.h.

template<typename Tag>
const char* CS::Utility::StringHash< Tag >::Request ( StringID< Tag >  id  )  const [inline]

Request the string for a given ID.

Returns:
The string associated with the given ID, or the null pointer if the string has not yet been registered. If more than one string is associated with the ID, then one is returned (but specifically which one is unspecified).
Warning:
This operation is slow. If you need to perform reverse lookups frequently, then instead consider using csStringSet or csStringHashReversible, in which reverse lookups are optimized.

Definition at line 124 of file strhash.h.

template<typename Tag>
StringID<Tag> CS::Utility::StringHash< Tag >::Request ( const char *  s  )  const [inline]

Request the ID for the given string.

Returns:
The string's ID or csInvalidStringID if the string has not yet been registered.

Reimplemented in csStringHashReversible.

Definition at line 109 of file strhash.h.


The documentation for this class was generated from the following file:

Generated for Crystal Space 2.0 by doxygen 1.6.1