|
JHashtable
/*
This library was downloaded from: http://www.mike95.com
This library is copyright. It may freely be used for personal purposes
if the restriction listed below is adhered to.
Author: Michael Olivero
Email: mike95@mike95.com
//===============================
//Start of Restriction Definition
//===============================
Anyone can have full use of the library provided they keep this complete comment
with the source. Also I would like to ask if any changes are made to the
code for efficiency reasons, please let me know so I may look into your change and
likewise incorporate it into library. If a suggestion makes it into the library,
your credits will be added to this information.
Authors of Computer related books are welcome to include this source code as part
of their publishing, provided credit the Author and make note of where the source
code was obtained from: http://www.mike95.com
//=============================
//End of Restriction Definition
//=============================
Description:
Visit http://www.mike95.com/c_plusplus/classes/JHashtable/
This class is a based on the Java JHashtable class and as such contains all the public
member functions of it's Java equivalent. Unlike Java, typecasts are not necessary
since C++ allows template enstatiation of types at compile time.
Note: Since java has hashItem() as a member of the base Object class, all
Java classes are inheritly hashable. Since the template parameter types do
not necessarily have to have a built in hashing function, the user of the class
must specify a hash function by calling setHashFunction() passing a pointer
to the hash function.
The has function must be declared as the following:
UINT function( const KeyType& );
Where:
function = any name you choose to use for the function name
KeyType = the type used for the key in the construction of the JHashtable object.
Example:
UINT myHash( const int& )
{
//your hashing code here for a key of type int.
}
//The following people have contributed to the solution
//of bugs or additional features in this library
//=====================================================
//Jeremy Friesner, email: jaf@chem.ucsd.edu
//Roxana Arama, email:roxanaa@tlc.ro
*/
#ifndef COM_MIKE95_JHASHTABLE_H
#include "M95_types.h"
template <class KeyType, class ObjType>
class JHashtable
{
public:
JHashtable( UINT initialCapacity = 101, float loadFactor = 0.5f );
virtual ~JHashtable();
//Inspectors
//==========
UINT size() const { return m_count; }
bool isEmpty() const { return m_count == 0; }
bool contains( const ObjType& value ) const;
bool containsKey( const KeyType& key ) const;
const ObjType& get( const KeyType& key );
//Modifiers
//=========
void rehash();
ObjType put( const KeyType& key, const ObjType& value );
ObjType remove( const KeyType& key );
void clear();
//C++ specific user hash function
void setHashFunction( UINT(*func)(const KeyType& key) ) { UserHash = func; }
private:
//type for Entries
struct JHashtableEntry
{
UINT hash;
KeyType key;
ObjType value;
JHashtableEntry* next;
};
//Member variables
UINT m_count; //the size of the elements in the hashtable
UINT m_tableSize; //the size of the table.
UINT m_threshold;
float m_loadFactor;
JHashtableEntry** m_table;
//Helper functions
JHashtableEntry* FindEntryByKey( const KeyType& key );
JHashtableEntry* GetHashRow( const KeyType& key );
UINT nextPrime( UINT start );
UINT (*UserHash)( const KeyType& key );
ObjType NULL_ITEM; //used for returns of not found
};
#endif
|
|
|