Main Page | Modules | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

SLHashTable Class Reference

#include <SLHashTable.hpp>

Collaboration diagram for SLHashTable:

Collaboration graph
[legend]
List of all members.

Public Types

typedef unsigned long HashCode

Public Member Functions

 SLHashTable (unsigned long DEFAULT_LENGTH=32)
void put (const Element &key, const Element &value)
Elementget (const Element &key) const throw (SLHashTable::NotFoundException)
void remove (const Element &key)

Static Public Member Functions

HashCode computeHashCode (const char *key, unsigned long size)

Protected Member Functions

void rehash ()

Protected Attributes

boost::shared_ptr< Data > _shared_ptr

Static Protected Attributes

const unsigned long DEFAULT_LENGTH = 32
const float DEFAULT_LOAD_FACTOR = 0.75

Detailed Description

A NO-Template Hash Table object. Its structure and behaviour are a bit stollen to its Java couterpart.

Example of code using hash table :

#include "SLHashTable.hpp" SLHashTable h; // fill the table h.put(SLString("fruit"), SLString("apple")); h.put(SLString("color"), SLString("purple")); // find elements try { SLString &str = h.get(SLString("fruit")); // this is OK SLString &str2 = h.get(SLString("bad boy")); // will rise an exception } catch (SLHashTable::NotFoundException &e) { // Ooops not found }

To allow a classe to be a key or a value, this class has to implement the abstract class SLHashTable::Element.

Example for class SLString that implements SLHashTable::Element :

SLHashTable::HashCode SLString::getHashCode() const { // for the hash code use the facility from SLHAshTable return SLHashTable::computeHashCode(to_utf8(), getByteSize()); } SLString * SLString::clone() const { SLString *copy = new SLString(*this); // Copy and this share the same string data. // It is in SLHashTable::Element::clone() contract that the cloned object // must be deleted separately return copy; } bool SLString::operator==(const SLHashTable::Element &other) const { try { const SLString& casted = dynamic_cast<const SLString&>(other); return (casted == *this); // use SLString::operator== } catch (std::bad_cast) { return false; // == is false : other is not of string class } }


Member Typedef Documentation

typedef unsigned long SLHashTable::HashCode
 

This is the type for all computed hash codes


Constructor & Destructor Documentation

SLHashTable::SLHashTable unsigned long  DEFAULT_LENGTH = 32  ) 
 

Construct the hash table with an initial default capacity


Member Function Documentation

HashCode SLHashTable::computeHashCode const char *  key,
unsigned long  size
[static]
 

This static method compute a hashcode for any char * string given its size.

Parameters:
key is a char * string (may be casted from another type)
size is the size in bytes of key (not including '\0' !)
Returns:
0 if key is NULL or size is 0

Element& SLHashTable::get const Element key  )  const throw (SLHashTable::NotFoundException)
 

Retrieve an element from the hash table

Exceptions:
SLHashTable::NotFoundException when key is not found.

void SLHashTable::put const Element key,
const Element value
 

put an element associated with key into the table. Replaced values are destroyed.

void SLHashTable::rehash  )  [protected]
 

Rehash the hashtable when thresold is exceeded.

void SLHashTable::remove const Element key  ) 
 

Supress an element from the table. Its destructor will be called. If element is not found, nothing happens.


Member Data Documentation

boost::shared_ptr<Data> SLHashTable::_shared_ptr [protected]
 

Holds hashtable data

const unsigned long SLHashTable::DEFAULT_LENGTH = 32 [static, protected]
 

table length used by default

const float SLHashTable::DEFAULT_LOAD_FACTOR = 0.75 [static, protected]
 

load factor used by default


The documentation for this class was generated from the following file:
Generated on Mon Apr 19 01:03:36 2004 for segusoLand by doxygen 1.3.6-20040222