A hash is a data structure that saves objects in such a way as to make it efficient to locate and retrieve an element. A hash consists of:
This chapter describes the following hash classes:
wchash.h
The WCPtrHashDict<Key,Value> class is a templated class used to store objects in a dictionary. Dictionaries store values with an associated key, which may be of any type. One example of a dictionary used in everyday life is the phone book. The phone numbers are the data values, and the customer name is the key. An example of a specialized dictionary is a vector, where the key value is the integer index.
As an element is looked up or inserted in the dictionary, the associated key is hashed. Hashing converts the key into a numeric index value that's used to locate the value. The storage area referenced by the hash value is usually called a bucket. If more than one key results in the same hash, the values associated with the keys are placed in a list stored in the bucket. The equality operator of the key's type is used to locate the key-value pairs.
In the description of each member function, the text Key is used to indicate the template parameter defining the type of the indices pointed to by the pointers stored in the dictionary. The text Value is used to indicate the template parameter defining the type of the data pointed to by the pointers stored in the dictionary.
The constructor for the WCPtrHashDict<Key,Value> class requires a hashing function, which given a reference to Key, returns an unsigned value. The returned value modulo the number of buckets determines the bucket in which the key-value pair is located. The return values of the hash function can be spread over the entire range of unsigned numbers. The hash function return value must be the same for values that are equivalent according to the equivalence operator for Key.
Pointers to the key values are stored in the dictionary. Destructors aren't called on the keys pointed to. The key values pointed to in the dictionary shouldn't be changed such that the equivalence to the old value is modified. |
The WCExcept class is a base class of the WCPtrHashDict<Key,Value> class, and provides the exceptions() member function. This member function controls the exceptions that can be thrown by the WCPtrHashDict<Key,Value> object. No exceptions are enabled unless they're set by the exceptions() member function.
The WCPtrHashDict<Key,Value> class requires Key to have:
int operator ==( const Key & ) const
The following member functions are declared in the public interface:
WCPtrHashDict( unsigned (*hash_fn)( const Key & ), unsigned = WC_DEFAULT_HASH_SIZE ); WCPtrHashDict( unsigned (*hash_fn)( const Key & ), unsigned = WC_DEFAULT_HASH_SIZE, void * (*user_alloc)( size_t size ), void (*user_dealloc)( void *old, size_t size ) ); WCPtrHashDict( const WCPtrHashDict & ); virtual ~WCPtrHashDict(); static unsigned bitHash( void *, size_t ); unsigned buckets() const; void clear(); void clearAndDestroy(); int contains( const Key * ) const; unsigned entries() const; Value * find( const Key * ) const; Value * findKeyAndValue( const Key *, Key * & ) const; void forAll( void (*user_fn)( Key *, Value *, void * ) , void * ); int insert( Key *, Value * ); int isEmpty() const; Value * remove( const Key * ); void resize( unsigned );
The following member operators are declared in the public interface:
Value * & operator [ ]( const Key & ); const Value * & operator [ ]( const Key & ) const; WCPtrHashDict & operator =( const WCPtrHashDict & ); int operator ==( const WCPtrHashDict & ) const;
create a WCPtrHashDict<Key,Value> object
#include <wchash.h> public: WCPtrHashDict( unsigned (*hash_fn)( const Key & ), unsigned = WC_DEFAULT_HASH_SIZE ); WCPtrHashDict( unsigned (*hash_fn)( const Key & ), unsigned = WC_DEFAULT_HASH_SIZE, void * (*user_alloc)( size_t ), void (*user_dealloc)( void *, size_t ) ); WCPtrHashDict( const WCPtrHashDict & );
The public WCPtrHashDict<Key,Value> constructor creates a WCPtrHashDict<Key,Value> object:
If the hash dictionary object can be created, but an allocation failure occurs when creating the buckets, the table is created with zero buckets. If the out_of_memory exception is enabled, then attempting to insert into a hash table with zero buckets throws an out_of_memory error.
The hash function hash_fn is used to determine to which bucket each key-value pair is assigned. If no hash function exists, the static member function bitHash() is available to help create one.
The allocation function must return a zero if it can't perform the allocation. The deallocation function is passed the size as well as the pointer to the data. Your allocation system may take advantage of the characteristic that the allocation function is always called with the same size value for any particular instantiation of a hash dictionary. To determine the size of the objects that the memory management functions are required to allocate and free, the following macro may be used:
WCPtrHashDictItemSize( Key, Value )
If the hash dictionary object can be created, but an allocation failure occurs when creating the buckets, the table is created with zero buckets. If there isn't enough memory to copy all of the values in the dictionary, then only some are copied, and the number of entries correctly reflects the number copied. If all of the elements can't be copied, then the out_of_memory exception is thrown, if it's enabled.
The public WCPtrHashDict<Key,Value> constructor creates the specified WCPtrHashDict<Key,Value> object.
WCExcept::out_of_memory, WCPtrHashDict<Key,Value>::~WCPtrHashDict(), WCPtrHashDict<Key,Value>::bitHash()
destroy a WCPtrHashDict<Key,Value> object
#include <wchash.h> public: virtual ~WCPtrHashDict();
The public WCPtrHashDict<Key,Value> destructor is the destructor for the WCPtrHashDict<Key,Value> class. If the number of dictionary elements isn't zero and the not_empty exception is enabled, the exception is thrown. Otherwise, the dictionary elements are cleared using the clear() member function.
The objects to which the dictionary elements point aren't deleted unless the clearAndDestroy() member function is explicitly called before the destructor is called.
The call to this destructor is inserted implicitly by the compiler at the point where the WCPtrHashDict<Key,Value> object goes out of scope.
This destructor destroys an WCPtrHashDict<Key,Value> object.
WCExcept::not_empty, WCPtrHashDict<Key,Value>::clear(), WCPtrHashDict<Key,Value>::clearAndDestroy()
implement a hashing function
#include <wchash.h> public: static unsigned bitHash( void *, size_t );
The bitHash() public member function can be used to implement a hashing function for any type. A hashing value is generated from the value stored for the number of specified bytes pointed to by the first parameter.
The bitHash() public member function returns an unsigned value that can be used as the basis of a user-defined hash function.
WCPtrHashDict<Key,Value>::WCPtrHashDict()
determine the number of buckets in the object
#include <wchash.h> public: unsigned buckets const;
The buckets() public member function is used to find the number of buckets contained in the WCPtrHashDict<Key,Value> object.
This function returns the number of buckets in the dictionary.
WCPtrHashDict<Key,Value>::resize()
clear the dictionary
#include <wchash.h> public: void clear();
The clear() public member function is used to clear the dictionary so that it has no entries. The number of buckets remains unaffected. Objects pointed to by the dictionary elements aren't deleted. The dictionary object isn't destroyed and re-created by this function, so the object destructor isn't invoked.
This function clears the dictionary to have no elements.
WCPtrHashDict<Key,Value>::~WCPtrHashDict(), WCPtrHashDict<Key,Value>::clearAndDestroy(), WCPtrHashDict<Key,Value>::operator =()
clear the dictionary, destroying its elements
#include <wchash.h> public: void clearAndDestroy();
The clearAndDestroy() public member function is used to clear the dictionary and delete the objects pointed to by the dictionary elements. The dictionary object isn't destroyed and re-created by this function, so the dictionary object destructor isn't invoked.
This function clears the dictionary by deleting the objects pointed to by the dictionary elements.
WCPtrHashDict<Key,Value>::clear()
determine whether or not the dictionary contains a given element
#include <wchash.h> public: int contains( const Key * ) const;
The contains() public member function returns non-zero if an element with the specified key is stored in the dictionary, or zero if there is no equivalent element. Note that equivalence is based on the equivalence operator of the Key type.
This function returns a non-zero value if the Key is found in the dictionary.
WCPtrHashDict<Key,Value>::find(), WCPtrHashDict<Key,Value>::findKeyAndValue()
count the entries in the dictionary
#include <wchash.h> public: unsigned entries() const;
The entries() public member function is used to return the current number of elements stored in the dictionary.
This function returns the number of elements in the dictionary.
WCPtrHashDict<Key,Value>::buckets(), WCPtrHashDict<Key,Value>::isEmpty()
find an element in the dictionary
#include <wchash.h> public: Value * find( const Key * ) const;
The find() public member function is used to find an element with an equivalent key in the dictionary. If an equivalent element is found, a pointer to the element Value is returned. Zero is returned if the element isn't found. Note that equivalence is based on the equivalence operator of the Key type.
The element equivalent to the passed key is located in the dictionary.
WCPtrHashDict<Key,Value>::findKeyAndValue()
find an element in the dictionary
#include <wchash.h> public: Value * findKeyAndValue( const Key *, Key &, Value & ) const;
The findKeyAndValue() public member function is used to find an element in the dictionary with an key equivalent to the first parameter. If an equivalent element is found, a pointer to the element Value is returned. The reference to a Key passed as the second parameter is assigned the found element's key. Zero is returned if the element isn't found. Note that equivalence is based on the equivalence operator of the Key type.
The element equivalent to the passed key is located in the dictionary.
WCPtrHashDict<Key,Value>::find()
invoke a function for each element in the dictionary
#include <wchash.h> public: void forAll( void (*user_fn)( Key *, Value *, void * ), void * );
The forAll() public member function causes the user-supplied function to be invoked for every key-value pair in the dictionary. The user function has the prototype:
void user_func( Key * key, Value * value, void * data );
As the elements are visited, the user function is invoked with the Key and Value components of the element passed as the first two parameters. The second parameter of the forAll() function is passed as the third parameter to the user function. This value can be used to pass any appropriate data from the main code to the user function.
The elements in the dictionary are all visited, and the user function is invoked for each one.
WCPtrHashDict<Key,Value>::find(), WCPtrHashDict<Key,Value>::findKeyAndValue()
insert an element into the dictionary
#include <wchash.h> public: int insert( Key *, Value * );
The insert() public member function inserts a key and value into the dictionary, using the hash function on the key to determine in which bucket it should be stored. If allocation of the node to store the key-value pair fails, then the out_of_memory exception is thrown if it's enabled. If the exception isn't enabled, the insertion isn't completed.
At some point, the number of buckets initially selected may be too small for the number of elements inserted. The resizing of the dictionary can be controlled by the insertion mechanism by using WCPtrHashDict as a base class, and providing an insert member function to do a resize when appropriate. This insert could then call WCPtrHashDict::insert to insert the element.
Copy constructors and assignment operators aren't inherited in your class,
but you can provide the following inline definitions (assuming that the class
inherited from WCPtrHashDict is named
MyHashDict):
inline MyHashDict( const MyHashDict &orig ) : WCPtrHashDict( orig ) {}; inline MyHashDict &operator=( const MyHashDict &orig ) { return( WCPtrHashDict::operator=( orig ) ); } |
The insert() public member function inserts a key and value into the dictionary. If the insertion is successful, a non-zero value is returned. Zero is returned if the insertion fails.
WCExcept::out_of_memory, WCPtrHashDict<Key,Value>::operator =()
detemine whether or not the dictionary is empty
#include <wchash.h> public: int isEmpty() const;
The isEmpty() public member function is used to determine if the dictionary is empty.
This function returns zero if the dictionary contains at least one entry, non-zero if it's empty.
WCPtrHashDict<Key,Value>::buckets(), WCPtrHashDict<Key,Value>::entries()
index into the dictionary
#include <wchash.h> public: Value * & operator[ ]( const Key & ); Value * const & operator[ ]( const Key * ) const;
The operator [ ]() public member function is the dictionary index operator. It's available in two forms.
If an allocation error occurs while inserting a new key-value pair, then the out_of_memory exception is thrown, if it's enabled. If the exception isn't enabled, then a reference to address zero is returned. This results in a run-time error on systems that trap references to address zero.
WCExcept::index_range, WCExcept::out_of_memory
assign one dictionary to another
#include <wchash.h> public: WCPtrHashDict & operator =( const WCPtrHashDict & );
The operator =() public member function is the assignment operator for the WCPtrHashDict<Key,Value> class. The left hand side dictionary is first cleared, using the clear() member function, and then the right hand side dictionary is copied. The hash function, exception trap states, and all of the dictionary elements are copied.
If an allocation failure occurs when creating the buckets, the table is created with zero buckets, and the out_of_memory exception is thrown, if it's enabled. If there isn't enough memory to copy all of the values or pointers in the dictionary, then only some are copied, and the out_of_memory exception is thrown, if it's enabled. The number of entries correctly reflects the number copied.
The operator =() public member function assigns the left hand side dictionary to be a copy of the right hand side.
WCExcept::out_of_memory, WCPtrHashDict<Key,Value>::clear()
determine whether or not two dictionaries are equivalent
#include <wchash.h> public: int operator ==( const WCPtrHashDict & ) const;
The operator ==() public member function is the equivalence operator for the WCPtrHashDict<Key,Value> class. Two dictionary objects are equivalent if they're the same object and share the same address.
A TRUE (non-zero) value is returned if the left hand side and right hand side dictionaries are the same object. A FALSE (zero) value is returned otherwise.
remove an element from the dictionary
#include <wchash.h> public: Value * remove( const Key * );
The remove() public member function is used to remove the specified element from the dictionary. If an equivalent element is found, the pointer value is returned. Zero is returned if the element isn't found. Note that equivalence is based on the equivalence operator of the Key type.
The element is removed from the dictionary if it's found.
change the number of buckets in the dictionary
#include <wchash.h> public: void resize( unsigned );
The resize() public member function is used to change the number of buckets contained in the dictionary.
If the new number is larger than the previous dictionary size, then the hash function is used on all of the stored elements to determine in which bucket they should be stored. Entries aren't destroyed or created in the process of being moved. If there isn't enough memory to resize the dictionary, the out_of_memory exception is thrown, if it's enabled, and the dictionary contains the number of buckets it contained before the resize.
If the new number is zero, then the zero_buckets exception is thrown, if it's enabled, and no resize is performed. The dictionary is guaranteed to contain the same number of entries after the resize.
The dictionary is resized to contain the new number of buckets.
WCExcept::out_of_memory, WCExcept::zero_buckets
wchash.h
The WCPtrHashSet<Type> and WCPtrHashTable<Type> classes are templated classes used to store objects in a hash. A hash saves objects in such a way as to make it efficient to locate and retrieve an element. As an element is looked up or inserted into the hash, the value of the element is hashed. Hashing results in a numeric index that is used to locate the value. The storage area referenced by the hash value is usually called a bucket. If more than one element results in the same hash, the value associated with the hash is placed in a list stored in the bucket. A hash table allows more than one copy of an element that is equivalent, while the hash set allows only one copy. The equality operator of the element's type is used to locate the value.
In the description of each member function, the text Type is used to indicate the template parameter defining the type of the data pointed to by the pointers stored in the hash.
The constructors for the WCPtrHashTable<Type> and WCPtrHashSet<Type> classes require a hashing function that, given a reference to Type, returns an unsigned value. The returned value modulo the number of buckets determines the bucket in which the element is located. The return values of the hash function can be spread over the entire range of unsigned numbers. The hash function return value must be the same for values that are equivalent according to the equivalence operator for Type.
Pointers to the elements are stored in the hash. Destructors aren't called on the elements pointed to. The data values pointed to in the hash shouldn't be changed such that the equivalence to the old value is modified. |
The WCExcept class is a base class of the WCPtrHashTable<Type> and WCPtrHashSet<Type> classes, and provides the exceptions() member function. This member function controls the exceptions that can be thrown by these objects. No exceptions are enabled unless they're set by the exceptions() member function.
The WCPtrHashTable<Type> and WCPtrHashSet<Type> classes require Type to have:
int operator ==( const Type & ) const
The following member functions are declared in the public interface:
WCPtrHashSet( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE ); WCPtrHashSet( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE, void * (*user_alloc)( size_t size ), void (*user_dealloc)( void *old, size_t size ) ); WCPtrHashSet( const WCPtrHashSet & ); virtual ~WCPtrHashSet(); WCPtrHashTable( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE ); WCPtrHashTable( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE, void * (*user_alloc)( size_t size ), void (*user_dealloc)( void *old, size_t size ) ); WCPtrHashTable( const WCPtrHashTable & ); virtual ~WCPtrHashTable(); static unsigned bitHash( void *, size_t ); unsigned buckets() const; void clear(); void clearAndDestroy(); int contains( const Type * ) const; unsigned entries() const; Type * find( const Type * ) const; void forAll( void (*user_fn)( Type *, void * ), void * ); int insert( Type * ); int isEmpty() const; Type * remove( const Type * ); void resize( unsigned );
The following public member functions are available for the WCPtrHashTable<Type> class only:
unsigned occurrencesOf( const Type * ) const; unsigned removeAll( const Type * );
The following member operators are declared in the public interface:
WCPtrHashSet & operator =( const WCPtrHashSet & ); int operator ==( const WCPtrHashSet & ) const; WCPtrHashTable & operator =( const WCPtrHashTable & ); int operator ==( const WCPtrHashTable & ) const;
create a WCPtrHashSet object
#include <wchash.h> public: WCPtrHashSet( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE ); WCPtrHashSet( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE, void * (*user_alloc)( size_t ), void (*user_dealloc)( void *, size_t ) ); WCPtrHashSet( const WCPtrHashSet & );
The WCPtrHashSet<Type> constructor creates a WCPtrHashSet object:
If the hash object can be created, but an allocation failure occurs when creating the buckets, the table is created with zero buckets. If the out_of_memory exception is enabled, then attempting to insert into a hash table with zero buckets throws an out_of_memory error.
The hash function hash_fn is used to determine to which bucket each value is assigned. If no hash function exists, the static member function bitHash() is available to help create one.
The allocation function must return a zero if it can't perform the allocation. The deallocation function is passed the size as well as the pointer to the data. Your allocation system may take advantage of the characteristic that the allocation function is always called with the same size value for any particular instantiation of a hash. To determine the size of the objects that the memory management functions are required to allocate and free, use the WCPtrHashSetItemSize( Type ) macro.
If the hash object can be created, but an allocation failure occurs when creating the buckets, the hash is created with zero buckets. If there isn't enough memory to copy all of the values, then only some are copied, and the number of entries correctly reflects the number copied. If all of the elements can't be copied, then the out_of_memory exception is thrown, if it's enabled.
This constructor creates a WCPtrHashSet object.
WCExcept::out_of_memory, WCPtrHashSet<Type>::~WCPtrHashset(), WCPtrHashSet<Type>::bitHash() WCPtrHashDict<Key,Value>::operator =()
destroy a WCPtrHashSet object
#include <wchash.h> public: virtual ~WCPtrHashSet();
This function is the destructor for the WCPtrHashSet class. If the number of elements isn't zero and the not_empty exception is enabled, the exception is thrown. Otherwise, the hash elements are cleared using the clear() member function. The objects to which the hash elements point aren't deleted unless the clearAndDestroy() member function is explicitly called before the destructor is called.
The call to the WCPtrHashSet<Type> destructor is inserted implicitly by the compiler at the point where the WCPtrHashSet object goes out of scope.
The WCPtrHashSet object is destroyed.
WCExcept::not_empty, WCPtrHashSet<Key,Value>::clear(), WCPtrHashSet<Key,Value>::clearAndDestroy()
create a WCPtrHashTable object
#include <wchash.h> public: WCPtrHashTable( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE ); WCPtrHashTable( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE, void * (*user_alloc)( size_t ), void (*user_dealloc)( void *, size_t ) ); WCPtrHashTable( const WCPtrHashTable & );
The WCPtrHashTable<Type> constructor creates a WCPtrHashTable object:
If the hash object can be created, but an allocation failure occurs when creating the buckets, the table is created with zero buckets. If the out_of_memory exception is enabled, then attempting to insert into a hash table with zero buckets throws an out_of_memory error.
The hash function hash_fn is used to determine to which bucket each value is assigned. If no hash function exists, the static member function bitHash() is available to help create one.
The allocation function must return a zero if it can't perform the allocation. The deallocation function is passed the size as well as the pointer to the data. Your allocation system may take advantage of the characteristic that the allocation function is always called with the same size value for any particular instantiation of a hash. To determine the size of the objects that the memory management functions are required to allocate and free, you can use the WCPtrHashTableItemSize( Type ) macro.
If the hash object can be created, but an allocation failure occurs when creating the buckets, the hash is created with zero buckets. If there isn't enough memory to copy all of the values, then only some are copied, and the number of entries correctly reflects the number copied. If all of the elements can't be copied, then the out_of_memory exception is thrown, if it's enabled.
The WCPtrHashTable<Type> constructor creates a WCPtrHashTable object.
WCExcept::out_of_memory, WCPtrHashTable<Type>::~WCPtrHashTable(), WCPtrHashTable<Type>::bitHash(), WCPtrHashTable<Type>::operator =()
destroy a WCPtrHashTable object
#include <wchash.h> public: virtual ~WCPtrHashTable();
This function is the destructor for the WCPtrHashTable class. If the number of elements isn't zero and the not_empty exception is enabled, the exception is thrown. Otherwise, the hash elements are cleared using the clear() member function. The objects to which the hash elements point aren't deleted unless the clearAndDestroy() member function is explicitly called before the destructor is called.
The call to this destructor is inserted implicitly by the compiler at the point where the WCPtrHashTable object goes out of scope.
This destructor destroys a WCPtrHashTable object.
WCExcept::not_empty, WCPtrHashTable<Type>::clear(), WCPtrHashTable<Type>::clearAndDestroy()
implement a hashing function
#include <wchash.h> public: static unsigned bitHash( void *, size_t );
The bitHash() public member function can be used to implement a hashing function for any type. A hashing value is generated from the value stored for the number of specified bytes pointed to by the first parameter.
The bitHash() public member function returns an unsigned value that can be used as the basis of a user-defined hash function.
WCPtrHashSet<Type>::WCPtrHashSet(), WCPtrHashTable<Type>::WCPtrHashTable()
count the buckets in the hash table
#include <wchash.h> public: unsigned buckets const;
The buckets() public member function is used to find the number of buckets contained in the hash object.
This function returns the number of buckets in the hash.
WCPtrHashTable<Type>::resize()
clear the hash object
#include <wchash.h> public: void clear();
The clear() public member function is used to clear the hash so that it has no entries. The number of buckets remains unaffected. Objects pointed to by the hash elements aren't deleted. The hash object isn't destroyed and re-created by this function, so the object destructor isn't invoked.
This function clears the hash to have no elements.
WCPtrHashSet<Type>::~WCPtrHashSet(), WCPtrHashTable<Type>::~WCPtrHashTable(), WCPtrHashSet<Type>::clearAndDestroy(), WCPtrHashTable<Type>::clearAndDestroy(), WCPtrHashSet<Type>::operator =(), WCPtrHashTable<Type>::operator =()
clear the hash, by deleting the objects in it
#include <wchash.h> public: void clearAndDestroy();
The clearAndDestroy() public member function is used to clear the hash and delete the objects pointed to by the hash elements. The hash object isn't destroyed and re-created by this function, so the hash object destructor isn't invoked.
The clearAndDestroy() public member function clears the hash by deleting the objects pointed to by the hash elements.
WCPtrHashSet<Type>::clear() WCPtrHashTable<Type>::clear()
determine whether or not the hash contains a given element
#include <wchash.h> public: int contains( const Type * ) const;
The contains() public member function returns non-zero if the element is stored in the hash, or zero if there's no equivalent element. Note that equivalence is based on the equivalence operator of the element type.
The contains() public member function returns a non-zero value if the element is found in the hash.
WCPtrHashSet<Type>::find(), WCPtrHashTable<Type>::find()
count the elements in the hash
#include <wchash.h> public: unsigned entries() const;
The entries() public member function is used to return the current number of elements stored in the hash.
The entries() public member function returns the number of elements in the hash.
WCPtrHashSet<Type>::buckets(), WCPtrHashTable<Type>::buckets(), WCPtrHashSet<Type>::isEmpty(), WCPtrHashTable<Type>::isEmpty()
find an element in the hash
#include <wchash.h> public: Type * find( const Type * ) const;
The find() public member function is used to find an element with an equivalent key in the hash. If an equivalent element is found, a pointer to the element is returned. Zero is returned if the element isn't found. Note that equivalence is based on the equivalence operator of the element type.
The element equivalent to the passed key is located in the hash.
invoke a function for each element in the hash table
#include <wchash.h> public: void forAll( void (*user_fn)( Type *, void * ), void * );
The forAll() public member function causes the user-supplied function to be invoked for every value in the hash. The user function has the prototype
void user_func( Type * value, void * data );
As the elements are visited, the user function is invoked with the element passed as the first parameter. The second parameter of the forAll function is passed as the second parameter to the user function. This value can be used to pass any appropriate data from the main program to the user function.
The elements in the hash are all visited, and the the user function is invoked for each one.
WCPtrHashSet<Type>::find(), WCPtrHashTable<Type>::find()
insert a value into the hash
#include <wchash.h> public: int insert( Type * );
The insert() public member function inserts a value into the hash, using the hash function to determine in which bucket it should be stored.
If allocation of the node to store the value fails, then the out_of_memory exception is thrown, if it's enabled. If the exception isn't enabled, the insertion isn't completed.
With a WCPtrHashSet, there must be only one equivalent element in the set. If an element equivalent to the inserted element is already in the hash set, the hash set remains unchanged, and the not_unique exception is thrown, if it's enabled. If the exception isn't enabled, the insertion isn't completed.
At some point, the number of buckets initially selected may be too small for the number of elements inserted. The resize of the hash can be controlled by the insertion mechanism by using WCPtrHashSet (or WCPtrHashTable) as a base class, and providing an insert-member function to do a resize when appropriate. This function could then call WCPtrHashSet::insert() (or WCPtrHashTable::insert()) to insert the element.
Copy constructors and assignment operators aren't
inherited in your class, but you can provide the following inline
definitions (assuming that the class inherited
from WCPtrHashTable is named MyHashTable):
inline MyHashTable( const MyHashTable &orig ) : WCPtrHashTable( orig ) {}; inline MyHashTable &operator=( const MyHashTable &orig ) { return( WCPtrHashTable::operator=( orig ) ); } |
The insert() public member function inserts a value into the hash. If the insertion is successful, a non-zero is returned. A zero is returned if the insertion fails.
WCExcept::out_of_memory, WCPtrHashSet<Type>::operator =(), WCPtrHashTable<Type>::operator =()
determine if the hash is empty
#include <wchash.h> public: int isEmpty() const;
The isEmpty() public member function is used to determine if the hash is empty.
The isEmpty() public member function returns zero if the hash contains at least one entry, non-zero if it's empty.
WCPtrHashSet<Type>::buckets(), WCPtrHashTable<Type>::buckets(), WCPtrHashSet<Type>::entries(), WCPtrHashTable<Type>::entries()
count the occurrences of an element in the hash
#include <wchash.h> public: unsigned occurrencesOf( const Type * ) const;
The occurrencesOf() public member function is used to count the current number of elements stored in the hash that are equivalent to the passed value. Note that equivalence is based on the equivalence operator of the element type.
The occurrencesOf() public member function returns the number of equivalent elements in the hash.
WCPtrHashTable<Type>::buckets(), WCPtrHashTable<Type>::entries(), WCPtrHashTable<Type>::find(), WCPtrHashTable<Type>::isEmpty()
assign a value to the hash
#include <wchash.h> public: WCPtrHashSet & operator =( const WCPtrHashSet &); WCPtrHashTable & operator =( const WCPtrHashTable &);
The operator =() public member function is the assignment operator for the WCPtrHashTable<Type> and WCPtrHashSet<Type> classes. The left hand side hash is first cleared using the clear() member function, and then the right hand side hash is copied. The hash function, exception trap states, and all of the hash elements are copied.
If an allocation failure occurs when creating the buckets, the table is created with zero buckets, and the out_of_memory exception is thrown, if it's enabled. If there isn't enough memory to copy all of the values or pointers in the hash, then only some are copied, and the out_of_memory exception is thrown, if it's enabled. The number of entries correctly reflects the number copied.
The operator =() public member function assigns a copy of the right hand side hash to the left hand side.
WCExcept::out_of_memory, WCPtrHashSet<Type>::clear(), WCPtrHashTable<Type>::clear()
determine whether or not two elements are equivalent
#include <wchash.h> public: int operator ==( const WCPtrHashSet & ) const; int operator ==( const WCPtrHashTable & ) const;
The operator ==() public member function is the equivalence operator for the WCPtrHashTable<Type> and WCPtrHashSet<Type> classes. Two hash objects are equivalent if they're the same object and share the same address.
A TRUE (non-zero) value is returned if the left-hand-side and right-hand-side hashes are the same object. A FALSE (zero) value is returned otherwise.
remove an element from the hash
#include <wchash.h> public: Type * remove( const Type * );
The remove() public member function is used to remove the specified element from the hash. If an equivalent element is found, the pointer value is returned. Zero is returned if the element isn't found.
If the hash is a table and there's more than one element equivalent to the specified element, then the first equivalent element added to the table is removed.
Note that equivalence is based on the equivalence operator of the element type.
The element is removed from the hash if it's found.
remove all occurrences of a given element from the hash
#include <wchash.h> public: unsigned removeAll( const Type * );
The removeAll() public member function is used to remove all elements equivalent to the specified element from the hash. Zero is returned if no equivalent elements are found. Note that equivalence is based on the equivalence operator of the element type.
All equivalent elements are removed from the hash.
change the number of buckets in the hash
#include <wchash.h> public: void resize( unsigned );
The resize() public member function is used to change the number of buckets contained in the hash.
If the new number is larger than the previous hash size, then the hash function is used on all of the stored elements to determine in which bucket they should be stored. Entries aren't destroyed or created in the process of being moved. If there isn't enough memory to resize the hash, the out_of_memory exception is thrown, if it's enabled, and the hash contains the number of buckets it contained before the resize.
If the new number is zero, then the zero_buckets exception is thrown, if it's enabled, and no resize is performed. The hash is guaranteed to contain the same number of entries after the resize.
The hash is resized to the new number of buckets.
WCExcept::out_of_memory, WCExcept::zero_buckets
wchash.h
The WCValHashDict<Key,Value> class is a templated class used to store objects in a dictionary. Dictionaries store values with an associated key, which may be of any type. One example of a dictionary used in everyday life is the phone book. The phone numbers are the data values, and the customer name is the key. An example of a specialized dictionary is a vector, where the key value is the integer index.
As an element is looked up or inserted into the dictionary, the associated key is hashed. Hashing converts the key into a numeric index value that is used to locate the value. The storage area referenced by the hash value is usually called a bucket. If more than one key results in the same hash, the values associated with the keys are placed in a list stored in the bucket. The equality operator of the key's type is used to locate the key-value pairs.
In the description of each member function, the text Key is used to indicate the template parameter defining the type of the indices used to store data in the dictionary. The text Value is used to indicate the template parameter defining the type of the data stored in the dictionary.
The constructor for the WCValHashDict<Key,Value> class requires a hashing function, which given a reference to Key, returns an unsigned value. The returned value modulo the number of buckets determines the bucket in which the key-value pair is located. The return values of the hash function can be spread over the entire range of unsigned numbers. The hash function return value must be the same for values that are equivalent according to the equivalence operator for Key.
Values are copied into the dictionary, which could be undesirable if the stored objects are complicated and copying is expensive. Value dictionaries shouldn't be used to store objects of a base class if any derived types of different sizes would be stored in the dictionary, or if the destructor for a derived class must be called.
The WCExcept class is a base class of the WCValHashDict class, and provides the exceptions() member function. This member function controls the exceptions that can be thrown by the WCValHashDict<Key,Value> object. No exceptions are enabled unless they're set by the exceptions() member function.
The WCValHashDict<Key,Value> class requires Key to have:
Key::Key()
Key::Key( const Key & )
Key & operator =( const Key & )
int operator ==( const Key & ) const
The WCValHashDict<Key,Value> class requires Value to have:
Value::Value()
Value::Value( const Value & )
Value & operator =( const Value & )
The following member functions are declared in the public interface:
WCValHashDict( unsigned (*hash_fn)( const Key & ), unsigned = WC_DEFAULT_HASH_SIZE ); WCValHashDict( unsigned (*hash_fn)( const Key & ), unsigned = WC_DEFAULT_HASH_SIZE, void * (*user_alloc)( size_t size ), void (*user_dealloc)( void *old, size_t size ) ); WCValHashDict( const WCValHashDict & ); virtual ~WCValHashDict(); static unsigned bitHash( void *, size_t ); unsigned buckets() const; void clear(); int contains( const Key & ) const; unsigned entries() const; int find( const Key &, Value & ) const; int findKeyAndValue( const Key &, Key &, Value & ) const; void forAll( void (*user_fn)( Key, Value, void * ), void * ); int insert( const Key &, const Value & ); int isEmpty() const; int remove( const Key & ); void resize( unsigned );
The following member operators are declared in the public interface:
Value & operator [ ]( const Key & ); const Value & operator [ ]( const Key & ) const; WCValHashDict & operator =( const WCValHashDict & ); int operator ==( const WCValHashDict & ) const;
create a WCValHashDict object
#include <wchash.h> public: WCValHashDict( unsigned (*hash_fn)( const Key & ), unsigned = WC_DEFAULT_HASH_SIZE ); WCValHashDict( unsigned (*hash_fn)( const Key & ), unsigned = WC_DEFAULT_HASH_SIZE, void * (*user_alloc)( size_t ), void (*user_dealloc)( void *, size_t ) ); WCValHashDict( const WCValHashDict & );
The public WCValHashDict<Key,Value> constructor creates a WCValHashDict<Key,Value> object:
If the hash dictionary object can be created, but an allocation failure occurs when creating the buckets, the table is created with zero buckets. If the out_of_memory exception is enabled, then attempting to insert into a hash table with zero buckets throws an out_of_memory error.
The hash function hash_fn is used to determine to which bucket each key-value pair is assigned. If no hash function exists, the static member function bitHash() is available to help create one.
The allocation function must return a zero if it can't perform the allocation. The deallocation function is passed the size as well as the pointer to the data. Your allocation system may take advantage of the characteristic that the allocation function is always called with the same size value for any particular instantiation of a hash dictionary. To determine the size of the objects that the memory management functions are required to allocate and free, you can use the WCValHashDictItemSize( Key, Value ) macro.
If the hash dictionary object can be created, but an allocation failure occurs when creating the buckets, the table is created with zero buckets. If there isn't enough memory to copy all of the values in the dictionary, then only some are copied, and the number of entries correctly reflects the number copied. If all of the elements can't be copied, then the out_of_memory exception is thrown, if it's enabled.
The public WCValHashDict<Key,Value> constructor creates a WCValHashDict<Key,Value> object.
WCExcept::out_of_memory, WCValHashDict<Key,Value>::~WCValHashDict(), WCValHashDict<Key,Value>::bitHash(), WCValHashDict<Key,Value>::operator =()
destroy a WCValHashDict object
#include <wchash.h> public: virtual ~WCValHashDict();
This function is the destructor for the WCValHashDict<Key,Value> class. If the number of dictionary elements isn't zero and the not_empty exception is enabled, the exception is thrown. Otherwise, the dictionary elements are cleared using the clear() member function. The call to this destructor is inserted implicitly by the compiler at the point where the WCValHashDict<Key,Value> object goes out of scope.
The public WCValHashDict<Key,Value> destructor destroys an WCValHashDict<Key,Value> object.
WCExcept::not_empty, WCValHashDict<Key,Value>::clear()
implement a hashing function for any type
#include <wchash.h> public: static unsigned bitHash( void *, size_t );
The bitHash() public member function can be used to implement a hashing function for any type. A hashing value is generated from the value stored for the number of specified bytes pointed to by the first parameter. For example:
unsigned my_hash_fn( const int & key ) { return( WCValHashDict<int,String>::bitHash( &key, sizeof( int ) ); } WCValHashDict<int,String> data_object( &my_hash_fn );
The bitHash() public member function returns an unsigned value that can be used as the basis of a user-defined hash function.
WCValHashDict<Key,Value>::WCValHashDict()
count the buckets in a WCValHashDict object
#include <wchash.h> public: unsigned buckets const;
The buckets() public member function is used to find the number of buckets contained in the WCValHashDict<Key,Value> object.
This function returns the number of buckets in the dictionary.
WCValHashDict<Key,Value>::resize()
clear the dictionary of all entries
#include <wchash.h> public: void clear();
The clear() public member function is used to clear the dictionary so that it has no entries. The number of buckets remains unaffected. Elements stored in the dictionary are destroyed using the destructors of Key and of Value. The dictionary object isn't destroyed and re-created by this function, so the object destructor isn't invoked.
The clear() public member function clears the dictionary to have no elements.
WCValHashDict<Key,Value>::~WCValHashDict(), WCValHashDict<Key,Value>::operator =()
determine whther or not a given key is stored in the dictionary
#include <wchash.h> public: int contains( const Key & ) const;
The contains() public member function returns non-zero if an element with the specified key is stored in the dictionary, or zero if there is no equivalent element. Note that equivalence is based on the equivalence operator of the Key type.
This function returns a non-zero value if the Key is found in the dictionary.
WCValHashDict<Key,Value>::find(), WCValHashDict<Key,Value>::findKeyAndValue()
count the entries in the dictionary
#include <wchash.h> public: unsigned entries() const;
The entries() public member function is used to return the current number of elements stored in the dictionary.
This function returns the number of elements in the dictionary.
WCValHashDict<Key,Value>::buckets(), WCValHashDict<Key,Value>::isEmpty()
find an element in the dictionary
#include <wchash.h> public: int find( const Key &, Value & ) const;
The find() public member function is used to find an element with an equivalent key in the dictionary. If an equivalent element is found, a non-zero value is returned. The reference to a Value passed as the second argument is assigned the found element's Value. Zero is returned if the element isn't found. Note that equivalence is based on the equivalence operator of the Key type.
The element equivalent to the passed key is located in the dictionary.
WCValHashDict<Key,Value>::findKeyAndValue()
find an element in the dictionary
#include <wchash.h> public: int findKeyAndValue( const Key &, Key &, Value & ) const;
The findKeyAndValue() public member function is used to find an element in the dictionary with an key equivalent to the first parameter. If an equivalent element is found, a non-zero value is returned. The reference to a Key passed as the second parameter is assigned the found element's key. The reference to a Value passed as the third argument is assigned the found element's Value. Zero is returned if the element isn't found. Note that equivalence is based on the equivalence operator of the Key type.
The element equivalent to the passed key is located in the dictionary.
WCValHashDict<Key,Value>::find()
invoke a user-supplied function for each entry in the dictionary
#include <wchash.h> public: void forAll( void (*user_fn)( Key, Value, void * ), void * );
The forAll() public member function causes the user-supplied function to be invoked for every key-value pair in the dictionary. The user function has the prototype
void user_func( Key key, Value value, void * data );
As the elements are visited, the user function is invoked with the Key and Value components of the element passed as the first two parameters. The second parameter of the forAll function is passed as the third parameter to the user function. This value can be used to pass any appropriate data from the main program to the user function.
The elements in the dictionary are all visited, and the user function is invoked for each one.
WCValHashDict<Key,Value>::find(), WCValHashDict<Key,Value>::findKeyAndValue()
insert a key and a value into the dictionary
#include <wchash.h> public: int insert( const Key &, const Value & );
The insert() public member function inserts a key and value into the dictionary, using the hash function on the key to determine in which bucket it should be stored.
If allocation of the node to store the key-value pair fails, then the out_of_memory exception is thrown if it's enabled. If the exception isn't enabled, the insertion isn't completed.
At some point, the number of buckets initially selected may be too small for the number of elements inserted. The resize of the dictionary can be controlled by the insertion mechanism by using WCValHashDict as a base class, and providing an insert-member function to do a resize when appropriate. This function could then call WCValHashDict::insert to insert the element.
Copy constructors and assignment operators aren't
inherited in your class, but you can provide the following inline
definitions (assuming that the class inherited
from WCValHashDict is named MyHashDict):
inline MyHashDict( const MyHashDict &orig ) : WCValHashDict( orig ) {}; inline MyHashDict &operator=( const MyHashDict &orig ) { return( WCValHashDict::operator=( orig ) ); } |
The insert() public member function inserts a key and value into the dictionary. If the insertion is successful, a non-zero value is returned. A zero is returned if the insertion fails.
WCExcept::out_of_memory, WCValHashDict<Key,Value>::operator =()
determine whether or not the dictionary is empty
#include <wchash.h> public: int isEmpty() const;
The isEmpty() public member function is used to determine if the dictionary is empty.
This function returns zero if the dictionary contains at least one entry, non-zero if it's empty.
WCValHashDict<Key,Value>::buckets(), WCValHashDict<Key,Value>::entries()
index into the dictionary
#include <wchash.h> public: Value & operator[ ]( const Key & ); const Value & operator[ ]( const Key & ) const;
operator [ ]() is the dictionary index operator. It's available in two forms:
For example,
WCValHashDict<int,String> data_object( &my_hash_fn ); data_object[ 5 ] = "Hello";
If an allocation error occurs while inserting a new key-value pair, then the out_of_memory exception is thrown if it's enabled. If the exception isn't enabled, then a reference to address zero is returned. This results in a run-time error on systems that trap references to address zero.
WCExcept::index_range, WCExcept::out_of_memory
assign the value of one dictionary to another
#include <wchash.h> public: WCValHashDict & operator =( const WCValHashDict & );
The operator =() public member function is the assignment operator for the WCValHashDict<Key,Value> class. The left hand side dictionary is first cleared using the clear() member function, and then the right hand side dictionary is copied. The hash function, exception trap states, and all of the dictionary elements are copied.
If an allocation failure occurs when creating the buckets, the table is created with zero buckets, and the out_of_memory exception is thrown, if it's enabled. If there isn't enough memory to copy all of the values or pointers in the dictionary, then only some are copied, and the out_of_memory exception is thrown, if it's enabled. The number of entries correctly reflects the number copied.
The operator =() public member function assigns the left hand side dictionary to be a copy of the right hand side.
WCExcept::out_of_memory, WCValHashDict<Key,Value>::clear()
determine whether or not two dictionary objects are equivalent
#include <wchash.h> public: int operator ==( const WCValHashDict & ) const;
The operator ==() public member function is the equivalence operator for the WCValHashDict<Key,Value> class. Two dictionary objects are equivalent if they're the same object and share the same address.
A TRUE (non-zero) value is returned if the left hand side and right hand side dictionaries are the same object. A FALSE (zero) value is returned otherwise.
remove an element from a dictionary
#include <wchash.h> public: int remove( const Key & );
The remove() public member function is used to remove the specified element from the dictionary. If an equivalent element is found, a non-zero value is returned. Zero is returned if the element isn't found. Note that equivalence is based on the equivalence operator of the Key type.
The element is removed from the dictionary if it's found.
change the number of buckets for the dictionary
#include <wchash.h> public: void resize( unsigned );
The resize() public member function is used to change the number of buckets contained in the dictionary.
If the new number is larger than the previous dictionary size, then the hash function is used on all of the stored elements to determine in which bucket they should be stored. Entries aren't destroyed or created in the process of being moved. If there isn't enough memory to resize the dictionary, the out_of_memory exception is thrown if it's enabled, and the dictionary contains the number of buckets it contained before the resize.
If the new number is zero, then the zero_buckets exception is thrown if it's enabled, and no resize is performed. The dictionary is guaranteed to contain the same number of entries after the resize.
The dictionary is resized to the new number of buckets.
WCExcept::out_of_memory, WCExcept::zero_buckets
wchash.h
WCValHashTable<Type> and WCValHashSet<Type> classes are templated classes used to store objects in a hash. A hash saves objects in such a way as to make it efficient to locate and retrieve an element. As an element is looked up or inserted into the hash, the value of the element is hashed. Hashing results in a numeric index which is used to locate the value. The storage area referenced by the hash value is usually called a bucket. If more than one element results in the same hash, the value associated with the hash is placed in a list stored in the bucket. A hash table allows more than one copy of an element that is equivalent, while the hash set allows only one copy. The equality operator of the element's type is used to locate the value.
In the description of each member function, the text Type is used to indicate the template parameter defining the type of the data to be stored in the hash.
The constructors for the WCValHashTable<Type> and WCValHashSet<Type> classes require a hashing function that, given a reference to Type, returns an unsigned value. The returned value modulo the number of buckets determines the bucket in which the element is located. The return values of the hash function can be spread over the entire range of unsigned numbers. The hash function return value must be the same for values that are equivalent according to the equivalence operator for Type.
Values are copied into the hash, which could be undesirable if the stored objects are complicated and copying is expensive. Value hashes shouldn't be used to store objects of a base class if any derived types of different sizes would be stored in the hash, or if the destructor for a derived class must be called.
The WCExcept class is a base class of the WCValHashTable<Type> and WCValHashSet<Type> classes, and provides the exceptions() member function. This member function controls the exceptions that can be thrown by the WCValHashTable<Type> and WCValHashSet<Type> objects. No exceptions are enabled unless they're set by the exceptions() member function.
The WCValHashTable<Type> and WCValHashSet<Type> classes require Type to have:
Type::Type()
Type::Type( const Type & )
Type & operator =( const Type & )
int operator ==( const Type & ) const
The following member functions are declared in the public interface:
WCValHashSet( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE ); WCValHashSet( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE, void * (*user_alloc)( size_t size ), void (*user_dealloc)( void *old, size_t size ) ); WCValHashSet( const WCValHashSet & ); virtual ~WCValHashSet(); WCValHashTable( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE ); WCValHashTable( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE, void * (*user_alloc)( size_t size ), void (*user_dealloc)( void *old, size_t size ) ); WCValHashTable( const WCValHashTable & ); virtual ~WCValHashTable(); static unsigned bitHash( void *, size_t ); unsigned buckets() const; void clear(); int contains( const Type & ) const; unsigned entries() const; int find( const Type &, Type & ) const; void forAll( void (*user_fn)( Type, void * ), void * ); int insert( const Type & ); int isEmpty() const; int remove( const Type & ); void resize( unsigned );
The following public member functions are available for the WCValHashTable class only:
unsigned occurrencesOf( const Type & ) const; unsigned removeAll( const Type & );
The following member operators are declared in the public interface:
WCValHashSet & operator =( const WCValHashSet & ); int operator ==( const WCValHashSet & ) const; WCValHashTable & operator =( const WCValHashTable & ); int operator ==( const WCValHashTable & ) const;
create a WCValHashSet object
#include <wchash.h> public: WCValHashSet( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE ); WCValHashSet( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE, void * (*user_alloc)( size_t ), void (*user_dealloc)( void *, size_t ) ); WCValHashSet( const WCValHashSet & );
The WCValHashSet<Type> constructor creates a WCValHashSet object:
If the hash object can be created, but an allocation failure occurs when creating the buckets, the table is created with zero buckets. If the out_of_memory exception is enabled, then attempting to insert into a hash table with zero buckets throws an out_of_memory error.
The hash function hash_fn is used to determine to which bucket each value is assigned. If no hash function exists, the static member function bitHash is available to help create one.
The allocation function must return a zero if it can't perform the allocation. The deallocation function is passed the size as well as the pointer to the data. Your allocation system may take advantage of the characteristic that the allocation function is always called with the same size value for any particular instantiation of a hash. To determine the size of the objects that the memory management functions are required to allocate and free, you can use the WCValHashSetItemSize( Type ) macro.
If the hash object can be created, but an allocation failure occurs when creating the buckets, the hash is created with zero buckets. If there isn't enough memory to copy all of the values, then only some are copied, and the number of entries correctly reflects the number copied. If all of the elements can't be copied, then the out_of_memory exception is thrown, if it's enabled.
The WCValHashSet<Type> constructor creates a WCValHashSet object.
WCExcept::out_of_memory, WCValHashSet<Type>::~WCValHashSet(), WCValHashSet<Type>::bitHash(), WCValHashSet<Type>::operator =()
destroy a WCValHashSet object
#include <wchash.h> public: virtual ~WCValHashSet();
This function is the destructor for the WCValHashSet class. If the number of elements isn't zero and the not_empty exception is enabled, the exception is thrown. Otherwise, the hash elements are cleared using the clear() member function. The call to the WCValHashSet<Type> destructor is inserted implicitly by the compiler at the point where the WCValHashSet object goes out of scope.
The WCValHashSet object is destroyed.
WCExcept::not_empty, WCValHashSet<Type>::clear()
create a WCValHashTable object
#include <wchash.h> public: WCValHashTable( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE ); WCValHashTable( unsigned (*hash_fn)( const Type & ), unsigned = WC_DEFAULT_HASH_SIZE, void * (*user_alloc)( size_t ), void (*user_dealloc)( void *, size_t ) ); WCValHashTable( const WCValHashTable & );
The WCValHashTable<Type> constructor creates a WCValHashTable object:
If the hash object can be created, but an allocation failure occurs when creating the buckets, the table is created with zero buckets. If the out_of_memory exception is enabled, then attempting to insert into a hash table with zero buckets throws an out_of_memory error.
The hash function hash_fn is used to determine to which bucket each value is assigned. If no hash function exists, the static member function bitHash is available to help create one.
The allocation function must return a zero if it can't perform the allocation. The deallocation function is passed the size as well as the pointer to the data. Your allocation system may take advantage of the characteristic that the allocation function is always called with the same size value for any particular instantiation of a hash. To determine the size of the objects that the memory management functions are required to allocate and free, you can use the WCValHashTableItemSize( Type ) macro.
If the hash object can be created, but an allocation failure occurs when creating the buckets, the hash is created with zero buckets. If there isn't enough memory to copy all of the values, then only some are copied, and the number of entries correctly reflects the number copied. If all of the elements can't be copied, then the out_of_memory exception is thrown if it's enabled.
The WCValHashTable<Type> constructor creates a WCValHashTable object.
WCExcept::out_of_memory, WCValHashTable<Type>::~WCValHashTable(), WCValHashTable<Type>::bitHash(), WCValHashTable<Type>::operator =()
destroy a WCValHashTable object
#include <wchash.h> public: virtual ~WCValHashTable();
This function is the destructor for the WCValHashTable class. If the number of elements isn't zero and the not_empty exception is enabled, the exception is thrown. Otherwise, the hash elements are cleared using the clear() member function.
The call to this destructor is inserted implicitly by the compiler at the point where the WCValHashTable object goes out of scope.
The WCValHashTable object is destroyed.
WCExcept::not_empty, WCValHashTable<Type>::clear()
implement a hashing function
#include <wchash.h> public: static unsigned bitHash( void *, size_t );
The bitHash() public member function can be used to implement a hashing function for any type. A hashing value is generated from the value stored for the number of specified bytes pointed to by the first parameter. For example:
unsigned my_hash_fn( const int & elem ) { return( WCValHashSet<int,String>::bitHash(&elem, sizeof(int)); } WCValHashSet<int> data_object( &my_hash_fn );
The bitHash() public member function returns an unsigned value that can be used as the basis of a user-defined hash function.
WCValHashSet<Type>::WCValHashSet(), WCValHashTable<Type>::WCValHashTable()
count the buckets in the hash object
#include <wchash.h> public: unsigned buckets const;
The buckets() public member function is used to find the number of buckets contained in the hash object.
This function returns the number of buckets in the hash.
WCValHashSet<Type>::resize() WCValHashTable<Type>::resize()
clear the hash object so that it has no entries
#include <wchash.h> public: void clear();
The clear() public member function is used to clear the hash so that it has no entries. The number of buckets remains unaffected. Elements stored in the hash are destroyed using the destructors of Type. The hash object isn't destroyed and re-created by this function, so the object destructor isn't invoked.
The clear() public member function clears the hash to have no elements.
WCValHashSet<Type>::~WCValHashSet(), WCValHashTable<Type>::~WCValHashTable(), WCValHashSet<Type>::operator =(), WCValHashTable<Type>::operator =()
find an element in the hash object
#include <wchash.h> public: int contains( const Type & ) const;
The contains() public member function returns non-zero if the element is stored in the hash, or zero if there's no equivalent element. Note that equivalence is based on the equivalence operator of the element type.
The contains() public member function returns a non-zero value if the element is found in the hash.
WCValHashSet<Type>::find(), WCValHashTable<Type>::find()
count the entries in the hash object
#include <wchash.h> public: unsigned entries() const;
The entries() public member function is used to return the current number of elements stored in the hash.
This function returns the number of elements in the hash.
WCValHashSet<Type>::buckets(), WCValHashTable<Type>::buckets(), WCValHashSet<Type>::isEmpty(), WCValHashTable<Type>::isEmpty()
find an element in the hash object
#include <wchash.h> public: int find( const Type &, Type & ) const;
The find() public member function is used to find an element with an equivalent key in the hash. If an equivalent element is found, a non-zero value is returned. The reference to the element passed as the second argument is assigned the found element's value. Zero is returned if the element isn't found. Note that equivalence is based on the equivalence operator of the element type.
The element equivalent to the passed key is located in the hash.
invoke a user-supplied function for each element in the hash object
#include <wchash.h> public: void forAll( void (*user_fn)( Type, void * ), void * );
The forAll() public member function causes the user-supplied function to be invoked for every value in the hash. The user function has the prototype:
void user_func( Type & value, void * data );
As the elements are visited, the user function is invoked with the element passed as the first parameter. The second parameter of the forAll() function is passed as the second parameter to the user function. This value can be used to pass any appropriate data from the main program to the user function.
The elements in the hash are all visited, and the user function is invoked for each one.
WCValHashSet<Type>::find(), WCValHashTable<Type>::find()
insert a value into the hash object
#include <wchash.h> public: int insert( const Type & );
The insert() public member function inserts a value into the hash, using the hash function to determine in which bucket it should be stored. If allocation of the node to store the value fails, then the out_of_memory exception is thrown, if it's enabled. If the exception isn't enabled, the insertion isn't completed.
With a WCValHashSet, there must be only one equivalent element in the set. If an element equivalent to the inserted element is already in the hash set, the hash set remains unchanged, and the not_unique exception is thrown, if it's enabled. If the exception isn't enabled, the insertion isn't completed.
At some point, the number of buckets initially selected may be too small for the number of elements inserted. The resize of the hash can be controlled by the insertion mechanism by using WCValHashSet (or WCValHashTable ) as a base class, and providing an insert-member function to do a resize when appropriate. This insertion function could then call WCValHashSet::insert (or WCValHashTable::insert ) to insert the element.
Note that copy constructors and assignment operators aren't inherited in your class, but you can provide the following inline definitions (assuming that the class inherited from WCValHashTable is named MyHashTable):
inline MyHashTable( const MyHashTable &orig ) : WCValHashTable( orig ) {}; inline MyHashTable &operator=( const MyHashTable &orig ) { return( WCValHashTable::operator=( orig ) ); }
The insert() public member function inserts a value into the hash. If the insertion is successful, a non-zero is returned. A zero is returned if the insertion fails.
WCExcept::out_of_memory, WCValHashSet<Type>::operator =(), WCValHashTable<Type>::operator =()
determine whether or not the hash object is empty
#include <wchash.h> public: int isEmpty() const;
The isEmpty() public member function is used to determine if the hash is empty.
This function returns zero if the hash object contains at least one entry, non-zero if it's empty.
WCValHashSet<Type>::buckets(), WCValHashTable<Type>::buckets(), WCValHashSet<Type>::entries(), WCValHashTable<Type>::entries()
count the occurrences of an element in the hash object
#include <wchash.h> public: unsigned occurrencesOf( const Type & ) const;
The occurrencesOf() public member function is used to return the current number of elements stored in the hash that are equivalent to the passed value. Note that equivalence is based on the equivalence operator of the element type.
The occurrencesOf() public member function returns the number of elements in the hash.
WCValHashTable<Type>::buckets(), WCValHashTable<Type>::entries(), WCValHashTable<Type>::find(), WCValHashTable<Type>::isEmpty()
set the value of a hash object
#include <wchash.h> public: WCValHashSet & operator =(const WCValHashSet &); WCValHashTable & operator =( const WCValHashTable &);
The operator =() public member function is the assignment operator for the WCValHashTable<Type> and WCValHashSet<Type> classes. The left hand side hash is first cleared using the clear() member function, and then the right hand side hash is copied. The hash function, exception trap states, and all of the hash elements are copied.
If an allocation failure occurs when creating the buckets, the table is created with zero buckets, and the out_of_memory exception is thrown, if it's enabled. If there isn't enough memory to copy all of the values or pointers in the hash, then only some are be copied, and the out_of_memory exception is thrown, if it's enabled. The number of entries correctly reflects the number copied.
The operator =() public member function assigns the left hand side hash to be a copy of the right hand side.
WCExcept::out_of_memory, WCValHashSet<Type>::clear(), WCValHashTable<Type>::clear()
determine whether or not two hash objects are equivalent
#include <wchash.h> public: int operator ==( const WCValHashSet & ) const; int operator ==( const WCValHashTable & ) const;
The operator ==() public member function is the equivalence operator for the WCValHashTable<Type> and WCValHashSet<Type> classes. Two hash objects are equivalent if they're the same object and share the same address.
A TRUE (non-zero) value is returned if the left hand side and right hand side hashes are the same object. A FALSE (zero) value is returned otherwise.
remove an element from the hash table
#include <wchash.h> public: int remove( const Type & );
The remove() public member function is used to remove the specified element from the hash. If an equivalent element is found, a non-zero value is returned. Zero is returned if the element isn't found. If the hash is a table and there's more than one element equivalent to the specified element, then the first equivalent element added to the table is removed.
Note that equivalence is based on the equivalence operator of the element type.
The element is removed from the hash if it's found.
remove all occurrences of an element from the hash object
#include <wchash.h> public: unsigned removeAll( const Type & );
The removeAll() public member function is used to remove all elements equivalent to the specified element from the hash. Zero is returned if no equivalent elements are found. Note that equivalence is based on the equivalence operator of the element type.
All equivalent elements are removed from the hash.
change the number of buckets in the hash object
#include <wchash.h> public: void resize( unsigned );
The resize() public member function is used to change the number of buckets contained in the hash.
If the new number is larger than the previous hash size, then the hash function is used on all of the stored elements to determine in which bucket they should be stored. Entries aren't destroyed or created in the process of being moved. If there isn't enough memory to resize the hash, the out_of_memory exception is thrown, if it's enabled, and the hash contains the number of buckets it contained before the resize.
If the new number is zero, then the zero_buckets exception is thrown, if it's enabled, and no resize is performed. The hash is guaranteed to contain the same number of entries after the resize.
The hash is resized to the new number of buckets.