Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

HashMap< T > Class Template Reference

Lock-free HashMap with 2^n slots. More...

#include <HashMap.hpp>

List of all members.

Classes

struct  Data

Public Member Functions

 HashMap (unsigned bits)
 Constructs a hashmap with 2^bits slots.
 HashMap (const HashMap< T > &other)
 Copy constructor.
 ~HashMap ()
 Destructor.
unsigned bits () const
 Returns the lg2 of the number of slots.
unsigned size () const
 Returns the number of slots.
unsigned count () const
 Returns number of objects stored.
bool get (hash_t key, T &out) const
 Retrieves object with key.
void put (hash_t key, const T &obj)
 Stores a (key, object) pair.
void clear ()
 Clears the table.
void operator= (const HashMap< T > &other)
 Copys other's data, overwriting everything in this table.

Private Member Functions

 BOOST_CLASS_REQUIRE (T, benzene, HashMapStateConcept)
void CopyFrom (const HashMap< T > &other)

Private Attributes

unsigned m_bits
 See bits().
unsigned m_size
 See size().
unsigned m_mask
 Equal to size() - 1.
volatile unsigned m_count
 See count().
boost::scoped_array< volatile int > m_used
 Index into m_allocated at which data for this slot is located; index is equal to EMPTY_SLOT if unused.
boost::scoped_array< Datam_allocated
 Allocated space for entries in the table.

Static Private Attributes

static const int EMPTY_SLOT = -1

Detailed Description

template<typename T>
class HashMap< T >

Lock-free HashMap with 2^n slots.

Deletes and dynamic resizing are not supported. Thread-safe, so multiple threads can read/write data at the same time. Performs simple linear probing on hash collisions.

Requires gcc builtins for atomic access.

References:

Definition at line 52 of file HashMap.hpp.


Constructor & Destructor Documentation

template<typename T >
HashMap< T >::HashMap ( unsigned  bits  )  [inline]

Constructs a hashmap with 2^bits slots.

Definition at line 140 of file HashMap.hpp.

References HashMap< T >::clear().

template<typename T>
HashMap< T >::HashMap ( const HashMap< T > &  other  )  [inline]

Copy constructor.

Definition at line 152 of file HashMap.hpp.

References HashMap< T >::CopyFrom().

template<typename T >
HashMap< T >::~HashMap (  )  [inline]

Destructor.

Definition at line 164 of file HashMap.hpp.


Member Function Documentation

template<typename T >
unsigned HashMap< T >::bits (  )  const [inline]

Returns the lg2 of the number of slots.

Definition at line 169 of file HashMap.hpp.

References HashMap< T >::m_bits.

template<typename T>
HashMap< T >::BOOST_CLASS_REQUIRE ( ,
benzene  ,
HashMapStateConcept   
) [private]
template<typename T >
void HashMap< T >::clear (  )  [inline]

Clears the table.

Definition at line 241 of file HashMap.hpp.

References HashMap< T >::EMPTY_SLOT, HashMap< T >::m_count, HashMap< T >::m_size, and HashMap< T >::m_used.

Referenced by HashMap< T >::HashMap().

template<typename T>
void HashMap< T >::CopyFrom ( const HashMap< T > &  other  )  [inline, private]
template<typename T >
unsigned HashMap< T >::count (  )  const [inline]

Returns number of objects stored.

Definition at line 181 of file HashMap.hpp.

References HashMap< T >::m_count.

Referenced by MoHexPlayer::TryReuseSubtree().

template<typename T>
bool HashMap< T >::get ( hash_t  key,
T &  out 
) const [inline]

Retrieves object with key.

Performs linear probing to find key.

Returns true on success.

Definition at line 188 of file HashMap.hpp.

References HashMap< T >::EMPTY_SLOT, hash_t, HashMap< T >::m_allocated, HashMap< T >::m_mask, HashMap< T >::m_size, and HashMap< T >::m_used.

Referenced by MoHexPlayer::CopyKnowledgeData(), HexUctState::ExecuteTreeMove(), and MoHexPlayer::TryReuseSubtree().

template<typename T>
void HashMap< T >::operator= ( const HashMap< T > &  other  )  [inline]

Copys other's data, overwriting everything in this table.

Definition at line 261 of file HashMap.hpp.

References HashMap< T >::CopyFrom().

template<typename T>
void HashMap< T >::put ( hash_t  key,
const T &  obj 
) [inline]

Stores a (key, object) pair.

WILL ABORT IF TABLE IS FULL!

Issues a warning if table becomes 1/4th full.

Make sure the table is much larger (at least 4x larger) than the maximum amount of data you ever expect to store in this table. This is to reduce the number of probes on subsequent get() calls, as well as to avoid the need for dynamic resizing of the table.

This function can possibly create duplicate entries. This is because put() does not check the key of the slots it runs over while searching for an empty slot. So if two threads both write try to put() k1 at the same time, each will write to its own slot. Only the first slot written will ever be used by get().

Definition at line 211 of file HashMap.hpp.

References HashMap< T >::EMPTY_SLOT, hash_t, LogSevere(), LogWarning(), HashMap< T >::m_allocated, HashMap< T >::m_count, HashMap< T >::m_mask, HashMap< T >::m_size, and HashMap< T >::m_used.

Referenced by HexUctState::ComputeKnowledge(), and MoHexPlayer::CopyKnowledgeData().

template<typename T >
unsigned HashMap< T >::size (  )  const [inline]

Returns the number of slots.

Definition at line 175 of file HashMap.hpp.

References HashMap< T >::m_size.


Member Data Documentation

template<typename T>
const int HashMap< T >::EMPTY_SLOT = -1 [static, private]

Definition at line 108 of file HashMap.hpp.

Referenced by HashMap< T >::clear(), HashMap< T >::get(), and HashMap< T >::put().

template<typename T>
boost::scoped_array<Data> HashMap< T >::m_allocated [private]

Allocated space for entries in the table.

Definition at line 134 of file HashMap.hpp.

Referenced by HashMap< T >::CopyFrom(), HashMap< T >::get(), and HashMap< T >::put().

template<typename T>
unsigned HashMap< T >::m_bits [private]

See bits().

Definition at line 117 of file HashMap.hpp.

Referenced by HashMap< T >::bits().

template<typename T>
volatile unsigned HashMap< T >::m_count [private]
template<typename T>
unsigned HashMap< T >::m_mask [private]

Equal to size() - 1.

Used for bitmasking operations.

Definition at line 123 of file HashMap.hpp.

Referenced by HashMap< T >::get(), and HashMap< T >::put().

template<typename T>
unsigned HashMap< T >::m_size [private]
template<typename T>
boost::scoped_array<volatile int> HashMap< T >::m_used [private]

Index into m_allocated at which data for this slot is located; index is equal to EMPTY_SLOT if unused.

Definition at line 131 of file HashMap.hpp.

Referenced by HashMap< T >::clear(), HashMap< T >::CopyFrom(), HashMap< T >::get(), and HashMap< T >::put().


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


6 Jan 2011 Doxygen 1.6.3