Contents User Forum Source Index APIs by Task APIs by Level Data


G3D::Table< Key, Value, HashFunc > Class Template Reference

An unordered data structure mapping keys to values. More...

#include <Table.h>

List of all members.

Public Member Functions

Iterator begin () const
void clear ()
bool containsKey (const Key &key) const
size_t debugGetDeepestBucketSize () const
double debugGetLoad () const
size_t debugGetNumBuckets () const
void deleteKeys ()
void deleteValues ()
const Iterator end () const
bool get (const Key &key, Value &val) const
Value & get (const Key &key) const
void getKeys (Array< Key > &keyArray) const
Array< Key > getKeys () const
Tableoperator= (const Table< Key, Value > &h)
Value & operator[] (const Key &key) const
void remove (const Key &key)
void set (const Key &key, const Value &value)
size_t size () const
 Table (const Table< Key, Value > &h)
 Table ()
virtual ~Table ()

Classes

class  Entry
 The pairs returned by iterator. More...
class  Iterator
 C++ STL style iterator variable. More...
class  Node
 Linked list nodes used internally by HashTable.


Detailed Description

template<class Key, class Value, class HashFunc = GHashCode<Key>>
class G3D::Table< Key, Value, HashFunc >

An unordered data structure mapping keys to values.

Key must be a pointer, an int, a std::string, a class with a hashCode() method, or provide overloads for:

    template<> struct GHashCode<class Key> {
        size_t operator()(Key key) const { return reinterpret_cast<size_t>( ... ); }
    };

   bool operator==(const Key&, const Key&);
  

G3D pre-defines GHashCode functions for common types (like int and std::string). If you use a Table with a different type you must write those functions yourself. For example, an enum would use:

    template<> struct GHashCode<MyEnum> {
        size_t operator()(MyEnum key) const { return reinterpret_cast<size_t>( key ); }
    };
  

And rely on the default enum operator==.

Periodically check that debugGetLoad() is low (> 0.1). When it gets near 1.0 your hash function is badly designed and maps too many inputs to the same output.


Constructor & Destructor Documentation

template<class Key, class Value, class HashFunc = GHashCode<Key>>
G3D::Table< Key, Value, HashFunc >::Table (  )  [inline]

Creates an empty hash table.

This causes some heap allocation to occur.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
virtual G3D::Table< Key, Value, HashFunc >::~Table (  )  [inline, virtual]

Destroys all of the memory allocated by the table, but does not call delete on keys or values if they are pointers.

If you want to deallocate things that the table points at, use getKeys() and Array::deleteAll() to delete them.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
G3D::Table< Key, Value, HashFunc >::Table ( const Table< Key, Value > &  h  )  [inline]


Member Function Documentation

template<class Key, class Value, class HashFunc = GHashCode<Key>>
Iterator G3D::Table< Key, Value, HashFunc >::begin (  )  const [inline]

C++ STL style iterator method.

Returns the first Entry, which contains a key and value. Use preincrement (++entry) to get to the next element. Do not modify the table while iterating.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
void G3D::Table< Key, Value, HashFunc >::clear (  )  [inline]

Removes all elements.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
bool G3D::Table< Key, Value, HashFunc >::containsKey ( const Key &  key  )  const [inline]

Returns true if key is in the table.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
size_t G3D::Table< Key, Value, HashFunc >::debugGetDeepestBucketSize (  )  const [inline]

Returns the length of the deepest bucket.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
double G3D::Table< Key, Value, HashFunc >::debugGetLoad (  )  const [inline]

A small load (close to zero) means the hash table is acting very efficiently most of the time.

A large load (close to 1) means the hash table is acting poorly-- all operations will be very slow. A large load will result from a bad hash function that maps too many keys to the same code.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
size_t G3D::Table< Key, Value, HashFunc >::debugGetNumBuckets (  )  const [inline]

Returns the number of buckets.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
void G3D::Table< Key, Value, HashFunc >::deleteKeys (  )  [inline]

Calls delete on all of the keys.

Does not clear the table, however, so you are left with a table of dangling pointers.

Same as getKeys().deleteAll();

To delete all of the values, you may want something like

        Array<Key> keys = table.getKeys();
        Set<Value> value;
        for (int k = 0; k < keys.length(); k++) {
           value.insert(keys[k]);
        }
        value.getMembers().deleteAll();
        keys.deleteAll();
    

template<class Key, class Value, class HashFunc = GHashCode<Key>>
void G3D::Table< Key, Value, HashFunc >::deleteValues (  )  [inline]

Calls delete on all of the values.

This is unsafe-- do not call unless you know that each value appears at most once.

Does not clear the table, so you are left with a table of dangling pointers.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
const Iterator G3D::Table< Key, Value, HashFunc >::end (  )  const [inline]

C++ STL style iterator method.

Returns one after the last iterator element.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
bool G3D::Table< Key, Value, HashFunc >::get ( const Key &  key,
Value &  val 
) const [inline]

If the key is present in the table, val is set to the associated value and returns true.

If the key is not present, returns false.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
Value& G3D::Table< Key, Value, HashFunc >::get ( const Key &  key  )  const [inline]

Returns the value associated with key.

Deprecated:
Use get(key, val) or

template<class Key, class Value, class HashFunc = GHashCode<Key>>
void G3D::Table< Key, Value, HashFunc >::getKeys ( Array< Key > &  keyArray  )  const [inline]

template<class Key, class Value, class HashFunc = GHashCode<Key>>
Array<Key> G3D::Table< Key, Value, HashFunc >::getKeys (  )  const [inline]

Returns an array of all of the keys in the table.

You can iterate over the keys to get the values.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
Table& G3D::Table< Key, Value, HashFunc >::operator= ( const Table< Key, Value > &  h  )  [inline]

template<class Key, class Value, class HashFunc = GHashCode<Key>>
Value& G3D::Table< Key, Value, HashFunc >::operator[] ( const Key &  key  )  const [inline]

Short syntax for get.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
void G3D::Table< Key, Value, HashFunc >::remove ( const Key &  key  )  [inline]

Removes an element from the table if it is present.

It is an error to remove an element that isn't present.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
void G3D::Table< Key, Value, HashFunc >::set ( const Key &  key,
const Value &  value 
) [inline]

If you insert a pointer into the key or value of a table, you are responsible for deallocating the object eventually.

Inserting key into a table is O(1), but may cause a potentially slow rehashing.

template<class Key, class Value, class HashFunc = GHashCode<Key>>
size_t G3D::Table< Key, Value, HashFunc >::size (  )  const [inline]

Returns the number of keys.


The documentation for this class was generated from the following file:
Generated on Thu Aug 2 11:40:48 2007 for G3D by doxygen 1.5.2
Hosted by SourceForge.net Logo