Hashtable< KeyType, ValueType, HashFunctorType > Class Template Reference

This is a handy templated Hashtable class, rather similar to Java's java.util.Hashtable, but with a number of enhancements. More...

#include <Hashtable.h>

List of all members.

Public Types

enum  { AUTOSORT_DISABLED = 0, AUTOSORT_BY_KEY, AUTOSORT_BY_VALUE }
 Tokens for the various auto-sort modes we know how to do. More...
typedef int(*) KeyCompareFunc (const KeyType &key1, const KeyType &key2, void *cookie)
 This is the signature of the type of callback function that you can pass into the SetKeyCompareFunction() and SortByKey() methods.
typedef int(*) ValueCompareFunc (const ValueType &key1, const ValueType &key2, void *cookie)
 This is the signature of the type of callback function that you can pass into the SetValueCompareFunction() and SortByValue() methods.

Public Member Functions

 Hashtable ()
 Default constructor.
 Hashtable (uint32 initialCapacity)
 Explicit constructor.
 Hashtable (const Hashtable< KeyType, ValueType, HashFunctorType > &rhs)
 Copy Constructor.
Hashtable< KeyType, ValueType,
HashFunctorType > & 
operator= (const Hashtable< KeyType, ValueType, HashFunctorType > &rhs)
 Assignment operator.
bool operator== (const Hashtable< KeyType, ValueType, HashFunctorType > &rhs) const
 Equality operator.
bool operator!= (const Hashtable< KeyType, ValueType, HashFunctorType > &rhs) const
 Returns the opposite of the equality operator.
 ~Hashtable ()
 Destructor.
uint32 GetNumItems () const
 Returns the number of items stored in the table.
bool IsEmpty () const
 Convenience method; Returns true iff the table is empty (i.e.
bool HasItems () const
 Convenience method; Returns true iff the table is non-empty (i.e.
bool ContainsKey (const KeyType &key) const
 Returns true iff the table contains a mapping with the given key.
bool ContainsValue (const ValueType &value) const
 Returns true iff the table contains a mapping with the given value.
int32 IndexOfKey (const KeyType &key) const
 Returns the given key's position in the hashtable's linked list, or -1 if the key wasn't found.
int32 IndexOfValue (const ValueType &value, bool searchBackwards=false) const
 Returns the index of the first (or last) occurrance of (value), or -1 if (value) does not exist in this Hashtable.
status_t GetValue (const KeyType &key, ValueType &setValue) const
 Attempts to retrieve the associated value from the table for a given key.
ValueType * GetValue (const KeyType &key) const
 Retrieve a pointer to the associated value object for the given key.
status_t GetKey (const KeyType &lookupKey, KeyType &setKey) const
 Given a lookup key, returns the a copy of the actual key as held by the table.
const KeyType * GetKey (const KeyType &lookupKey) const
 Given a key, returns a pointer to the actual key object in this table that matches that key, or NULL if there is no matching key.
HashtableIterator< KeyType,
ValueType, HashFunctorType > 
GetIterator (uint32 flags=0) const
 Get an iterator for use with this table.
HashtableIterator< KeyType,
ValueType, HashFunctorType > 
GetIteratorAt (const KeyType &startAt, uint32 flags=0) const
 Get an iterator for use with this table, starting at the given entry.
const KeyType * GetKeyAt (uint32 index) const
 Returns a pointer to the (index)'th key in this Hashtable.
status_t GetKeyAt (uint32 index, KeyType &retKey) const
 Returns the (index)'th key in this Hashtable.
status_t Put (const KeyType &key, const ValueType &value, ValueType &setPreviousValue, bool *optSetReplaced=NULL)
 Places the given (key, value) mapping into the table.
status_t Put (const KeyType &key, const ValueType &value)
 Places the given (key, value) mapping into the table.
status_t Put (const Hashtable< KeyType, ValueType, HashFunctorType > &pairs)
 Convenience method: For each key/value pair in the passed-in-table, Put()'s that key/value pair into this table.
status_t Remove (const KeyType &key)
 Removes a mapping from the table.
status_t Remove (const KeyType &key, ValueType &setRemovedValue)
 Removes the mapping with the given (key) and places its value into (setRemovedValue).
uint32 Remove (const Hashtable< KeyType, ValueType, HashFunctorType > &pairs)
 Convenience method: For each key in the passed-in-table, removes that key (and its associated value) from this table.
status_t RemoveFirst ()
 Convenience method: Removes the first key/value mapping in the table.
status_t RemoveFirst (KeyType &setRemovedKey)
 Convenience method: Removes the first key/value mapping in the table and places the removed key into (setRemovedKey).
status_t RemoveFirst (KeyType &setRemovedKey, ValueType &setRemovedValue)
 Convenience method: Removes the first key/value mapping in the table and places its key into (setRemovedKey) and its value into (setRemovedValue).
status_t RemoveLast ()
 Convenience method: Removes the last key/value mapping in the table.
status_t RemoveLast (KeyType &setRemovedKey)
 Convenience method: Removes the last key/value mapping in the table and places the removed key into (setRemovedKey).
status_t RemoveLast (KeyType &setRemovedKey, ValueType &setRemovedValue)
 Convenience method: Removes the last key/value mapping in the table and places its key into (setRemovedKey) and its value into (setRemovedValue).
void Clear (bool releaseCachedData=false)
 Removes all mappings from the hash table.
status_t Reposition (const KeyType &key)
 Moves the specified key/value pair so that it is in the correct position based on the current sort-by-value ordering.
void SetAutoSortMode (int mode, bool sortNow=true)
 This method can be used to activate or deactivate auto-sorting on this Hashtable.
int GetAutoSortMode () const
 Returns this hash table's current auto-sort mode.
void SetCompareCookie (void *cookie)
 This method can be used to set the "cookie value" that will be passed in to the comparison functions specified by SetKeyCompareFunction() and SetValueCompareFunction().
void * GetCompareCookie () const
 Returns the current comparison cookie, as previously set by SetCompareCookie().
void SetKeyCompareFunction (KeyCompareFunc func)
 You can call this to set a custom function to use to compare keys.
KeyCompareFunc GetKeyCompareFunction () const
 Returns the current comparison function used for comparing keys.
void SetValueCompareFunction (ValueCompareFunc func)
 You can call this to set a custom function to use to compare values.
ValueCompareFunc GetValueCompareFunction () const
 Returns the current comparison function used for comparing values.
void SwapContents (Hashtable< KeyType, ValueType, HashFunctorType > &swapMe)
 This method doesn anhefficient zero-copy swap of this hash table's contents with those of (swapMe).
status_t MoveToFront (const KeyType &moveMe)
 Moves the given entry to the head of the HashtableIterator traversal sequence.
status_t MoveToBack (const KeyType &moveMe)
 Moves the given entry to the tail of the HashtableIterator traversal sequence.
status_t MoveToBefore (const KeyType &moveMe, const KeyType &toBeforeMe)
 Moves the given entry to the spot just in front of the other specified entry in the HashtableIterator traversal sequence.
status_t MoveToBehind (const KeyType &moveMe, const KeyType &toBehindMe)
 Moves the given entry to the spot just behind the other specified entry in the HashtableIterator traversal sequence.
status_t MoveToTable (const KeyType &moveMe, Hashtable< KeyType, ValueType > &toTable)
 Convenience method: Moves an item from this table to another table.
status_t CopyToTable (const KeyType &moveMe, Hashtable< KeyType, ValueType > &toTable) const
 Convenience method: Copies an item from this table to another table.
status_t Sort ()
 Forcefully sorts the iteration traversal list of this table using the preferred sorting mode.
void SortByKey (KeyCompareFunc func, void *optCompareCookie=NULL)
 Forcefully sorts the iteration traversal list of this table using the given key comparison function.
void SortByValue (ValueCompareFunc func, void *optCompareCookie=NULL)
 Forcefully sorts the iteration traversal list of this table using the given value comparison function.
status_t Get (const KeyType &key, ValueType &setValue) const
 Convenience synonym for GetValue().
ValueType * Get (const KeyType &key) const
 Convenience synonym for GetValue().
ValueType * GetOrPut (const KeyType &key, const ValueType &defValue)
 Convenience method -- returns a pointer to the value specified by (key), or if no such value exists, it will Put() a (key,value) pair in the Hashtable, and then return a pointer to the newly-placed value.
ValueType * GetOrPut (const KeyType &key)
 Convenience method -- returns a pointer to the value specified by (key), or if no such value exists, it will Put() a (key,value) pair in the Hashtable, using a default value (as specified by ValueType's default constructor) and then return a pointer to the newly-placed value.
ValueType GetWithDefault (const KeyType &key) const
 Similar to Get(), except that if the specified key is not found, the ValueType's default value is returned.
ValueType GetWithDefault (const KeyType &key, const ValueType &defaultValue) const
 Similar to Get(), except that if the specified key is not found, the specified default value is returned.
ValueType * PutAndGet (const KeyType &key, const ValueType &value=ValueType())
 Places the given (key, value) mapping into the table.
const KeyType * GetFirstKey () const
 Convenience method.
const KeyType * GetLastKey () const
 Convenience method.
ValueType * GetFirstValue () const
 Convenience method.
ValueType * GetLastValue () const
 Convenience method.
status_t EnsureSize (uint32 newTableSize)
 This method resizes the Hashtable larger if necessary, so that it has at least (newTableSize) entries in it.
uint32 GetNumAllocatedSlots () const
 Returns the number of table-slots that we currently have allocated.

Friends

class HashtableIterator< KeyType, ValueType, HashFunctorType >
 Our hashtable iterator class needs access to our internals to register itself with us.

Classes

class  HashtableEntry


Detailed Description

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
class Hashtable< KeyType, ValueType, HashFunctorType >

This is a handy templated Hashtable class, rather similar to Java's java.util.Hashtable, but with a number of enhancements.

Enhancements include: Iterator objects that are safe to use even if the table is modified or deleted during a traversal (not thread-safe though), traversal ordering is maintained at all times and may optionally be sorted or auto-sorted, and the traversal order may be manually changed as well.

The Hashtable class uses a HashFunctor template argument to specify how the hash code for a given KeyType object can be computed. For built-in numeric types (e.g. int, long, etc) the default HashFunctor can be used. For more complex types (e.g. class objects), you will need to either create your own custom HashFunctor, or add a () operator to your class that returns a uint32 HashCode. The best way to do a custom HashFunctor is to specialize the default HashFunctor type for your class; check the example in util/String.h for how to do this.

Definition at line 334 of file Hashtable.h.


Member Typedef Documentation

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
typedef int(*) Hashtable< KeyType, ValueType, HashFunctorType >::KeyCompareFunc(const KeyType &key1, const KeyType &key2, void *cookie)

This is the signature of the type of callback function that you can pass into the SetKeyCompareFunction() and SortByKey() methods.

The function should work like strcmp(), return a negative value if (key1) is less than (key2), 0 if the two keys are equal, or a positive value if (key1) is greater than (key2).

Parameters:
key1 The first key.
key2 The second key.
cookie A user-defined value that was passed in to the Sort() or SetCompareCookie() method.
Returns:
An integer indicating which key is "larger", as defined above.

Definition at line 346 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
typedef int(*) Hashtable< KeyType, ValueType, HashFunctorType >::ValueCompareFunc(const ValueType &key1, const ValueType &key2, void *cookie)

This is the signature of the type of callback function that you can pass into the SetValueCompareFunction() and SortByValue() methods.

The function should work like strcmp(), return a negative value if (value1) is less than (value2), 0 if the two values are equal, or a positive value if (value1) is greater than (value2).

Parameters:
value1 The first value.
value2 The second value.
cookie A user-defined value that was passed in to the Sort() or SetCompareCookie() method.
Returns:
An integer indicating which value is "larger", as defined above.

Definition at line 357 of file Hashtable.h.


Member Enumeration Documentation

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
anonymous enum

Tokens for the various auto-sort modes we know how to do.

Enumerator:
AUTOSORT_DISABLED  This token means that no automatic sorting of the Hashtable should be done (this is most efficient).
AUTOSORT_BY_KEY  This token means that the Hashtable should be kept sorted based on the keys in the key-value pairs.
AUTOSORT_BY_VALUE  This token means that the Hashtable should be kept sorted based on the values of the key-value pairs.

Definition at line 360 of file Hashtable.h.


Constructor & Destructor Documentation

template<class KeyType, class ValueType, class HashFunctorType>
Hashtable< KeyType, ValueType, HashFunctorType >::Hashtable (  ) 

Default constructor.

Creates a standard, non-sorting Hashtable.

Definition at line 1107 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
Hashtable< KeyType, ValueType, HashFunctorType >::Hashtable ( uint32  initialCapacity  )  [explicit]

Explicit constructor.

Creates a standard, non-sorting Hashtable that will pre-allocate the specified number of key-value slots on its first call to Put().

Parameters:
initialCapacity Specifies the number of table slots to initially pre-allocate when Put() is called.

Definition at line 1114 of file Hashtable.h.


Member Function Documentation

template<class KeyType, class ValueType, class HashFunctorType>
bool Hashtable< KeyType, ValueType, HashFunctorType >::operator== ( const Hashtable< KeyType, ValueType, HashFunctorType > &  rhs  )  const

Equality operator.

Returns true iff both hash tables contains the same set of keys and values. Note that the ordering of the keys is NOT taken into account!

Definition at line 1152 of file Hashtable.h.

References Hashtable< KeyType, ValueType, HashFunctorType >::GetEntry(), and Hashtable< KeyType, ValueType, HashFunctorType >::GetNumItems().

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
bool Hashtable< KeyType, ValueType, HashFunctorType >::IsEmpty (  )  const [inline]

Convenience method; Returns true iff the table is empty (i.e.

if GetNumItems() is zero).

Definition at line 396 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
bool Hashtable< KeyType, ValueType, HashFunctorType >::HasItems (  )  const [inline]

Convenience method; Returns true iff the table is non-empty (i.e.

if GetNumItems() is non-zero).

Definition at line 399 of file Hashtable.h.

Referenced by Hashtable< KeyType, ValueType, HashFunctorType >::Clear(), Hashtable< KeyType, ValueType, HashFunctorType >::MoveToBefore(), and Hashtable< KeyType, ValueType, HashFunctorType >::MoveToBehind().

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
bool Hashtable< KeyType, ValueType, HashFunctorType >::ContainsKey ( const KeyType &  key  )  const [inline]

Returns true iff the table contains a mapping with the given key.

(O(1) search time)

Definition at line 402 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
bool Hashtable< KeyType, ValueType, HashFunctorType >::ContainsValue ( const ValueType &  value  )  const

Returns true iff the table contains a mapping with the given value.

(O(n) search time)

Definition at line 1175 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
int32 Hashtable< KeyType, ValueType, HashFunctorType >::IndexOfKey ( const KeyType &  key  )  const

Returns the given key's position in the hashtable's linked list, or -1 if the key wasn't found.

O(n) count time (if the key exists, O(1) if it doesn't)

Definition at line 1188 of file Hashtable.h.

References Hashtable< KeyType, ValueType, HashFunctorType >::GetNumItems().

template<class KeyType, class ValueType, class HashFunctorType>
int32 Hashtable< KeyType, ValueType, HashFunctorType >::IndexOfValue ( const ValueType &  value,
bool  searchBackwards = false 
) const

Returns the index of the first (or last) occurrance of (value), or -1 if (value) does not exist in this Hashtable.

Note that the search is O(N).

Parameters:
value The value to search for.
searchBackwards If set to true, the table will be searched in reverse order. and the index returned (if valid) will be the index of the last instance of (value) in the table, rather than the first.
Returns:
The index into the table, or -1 if (value) doesn't exist in the table's set of values.

Definition at line 1209 of file Hashtable.h.

References Hashtable< KeyType, ValueType, HashFunctorType >::GetNumItems().

template<class KeyType, class ValueType, class HashFunctorType>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::GetValue ( const KeyType &  key,
ValueType &  setValue 
) const

Attempts to retrieve the associated value from the table for a given key.

(O(1) lookup time)

Parameters:
key The key to use to look up a value.
setValue On success, the associated value is copied into this object.
Returns:
B_NO_ERROR on success, B_ERROR if their was no value found for the given key.

Definition at line 1259 of file Hashtable.h.

Referenced by Hashtable< IPAddressAndPort, PacketTunnelIOGateway::ReceiveState >::Get().

template<class KeyType, class ValueType, class HashFunctorType>
ValueType * Hashtable< KeyType, ValueType, HashFunctorType >::GetValue ( const KeyType &  key  )  const

Retrieve a pointer to the associated value object for the given key.

(O(1) lookup time)

Parameters:
key The key to use to look up a value.
Returns:
A pointer to the internally held value object for the given key, or NULL of no object was found. Note that this object is only guaranteed to remain valid as long as the Hashtable remains unchanged.

Definition at line 1272 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::GetKey ( const KeyType &  lookupKey,
KeyType &  setKey 
) const

Given a lookup key, returns the a copy of the actual key as held by the table.

This method is only useful in rare situations where the hashing or comparison functions are such that lookupKeys and held keys are not guaranteed equivalent.

Parameters:
lookupKey The key used to look up the held key object.
setKey On success, the actual key held in the table is placed here.
Returns:
B_NO_ERROR on success, or B_ERROR on failure.

Definition at line 1288 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
const KeyType * Hashtable< KeyType, ValueType, HashFunctorType >::GetKey ( const KeyType &  lookupKey  )  const

Given a key, returns a pointer to the actual key object in this table that matches that key, or NULL if there is no matching key.

This method is only useful in rare situations where the hashing or comparison functions are such that lookup keys and held keys are not guaranteed equivalent.

Parameters:
lookupKey The key used to look up the key object
Returns:
A pointer to an internally held key object on success, or NULL on failure.

Definition at line 1280 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
HashtableIterator<KeyType,ValueType,HashFunctorType> Hashtable< KeyType, ValueType, HashFunctorType >::GetIterator ( uint32  flags = 0  )  const [inline]

Get an iterator for use with this table.

Parameters:
flags A bit-chord of HTIT_FLAG_* constants (see above). Defaults to zero, for default behaviour.
Returns:
an iterator object that can be used to examine the items in the hash table, starting at the specified key. If the specified key is not in this table, an empty iterator will be returned.

Definition at line 458 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
HashtableIterator<KeyType,ValueType,HashFunctorType> Hashtable< KeyType, ValueType, HashFunctorType >::GetIteratorAt ( const KeyType &  startAt,
uint32  flags = 0 
) const [inline]

Get an iterator for use with this table, starting at the given entry.

Parameters:
startAt The key in this table to start the iteration at.
flags A bit-chord of HTIT_FLAG_* constants (see above). Defaults to zero, for default behaviour.
Returns:
an iterator object that can be used to examine the items in the hash table, starting at the specified key. If the specified key is not in this table, an empty iterator will be returned.

Definition at line 469 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
const KeyType * Hashtable< KeyType, ValueType, HashFunctorType >::GetKeyAt ( uint32  index  )  const

Returns a pointer to the (index)'th key in this Hashtable.

(This Hashtable class keeps its entries in a well-defined order) Note that this method is an O(N) operation, so for iteration, always use GetIterator() instead.

Parameters:
index Index of the key to return a pointer to. Should be in the range [0, GetNumItems()-1].
Returns:
Pointer to the key at position (index) on success, or NULL on failure (bad index?)

Definition at line 1238 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::GetKeyAt ( uint32  index,
KeyType &  retKey 
) const

Returns the (index)'th key in this Hashtable.

(This Hashtable class keeps its entries in a well-defined order) Note that this method is an O(N) operation, so for iteration, always use GetIterator() instead.

Parameters:
index Index of the key to return a pointer to. Should be in the range [0, GetNumItems()-1].
retKey On success, the contents of the (index)'th key will be written into this object.
Returns:
B_NO_ERROR on success, or B_ERROR on failure.

Definition at line 1246 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::Put ( const KeyType &  key,
const ValueType &  value,
ValueType &  setPreviousValue,
bool *  optSetReplaced = NULL 
) [inline]

Places the given (key, value) mapping into the table.

Any previous entry with a key of (key) will be replaced. (average O(1) insertion time, unless auto-sorting is enabled, in which case it becomes O(N) insertion time for keys that are not already in the table)

Parameters:
key The key that the new value is to be associated with.
value The value to associate with the new key.
setPreviousValue If there was a previously existing value associated with (key), it will be copied into this object.
optSetReplaced If set non-NULL, this boolean will be set to true if (setPreviousValue) was written into, false otherwise.
Returns:
B_NO_ERROR If the operation succeeded, B_ERROR if it failed (out of memory?)

Definition at line 500 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::Put ( const KeyType &  key,
const ValueType &  value 
) [inline]

Places the given (key, value) mapping into the table.

Any previous entry with a key of (key) will be replaced. (average O(1) insertion time, unless auto-sorting is enabled, in which case it becomes O(N) insertion time for keys that are not already in the table)

Parameters:
key The key that the new value is to be associated with.
value The value to associate with the new key.
Returns:
B_NO_ERROR If the operation succeeded, B_ERROR if it failed (out of memory?)

Definition at line 509 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::Put ( const Hashtable< KeyType, ValueType, HashFunctorType > &  pairs  ) 

Convenience method: For each key/value pair in the passed-in-table, Put()'s that key/value pair into this table.

Any existing items in this table with the same key as any in (pairs) will be overwritten.

Parameters:
pairs A table full of items to Put() into this table.
Returns:
B_NO_ERROR on success, or B_ERROR on failue (out of memory?)

Definition at line 1875 of file Hashtable.h.

References Hashtable< KeyType, ValueType, HashFunctorType >::_iterHead.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::Remove ( const KeyType &  key  )  [inline]

Removes a mapping from the table.

(O(1) removal time)

Parameters:
key The key of the key-value mapping to remove.
Returns:
B_NO_ERROR if a key was found and the mapping removed, or B_ERROR if the key wasn't found.

Definition at line 523 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::Remove ( const KeyType &  key,
ValueType &  setRemovedValue 
) [inline]

Removes the mapping with the given (key) and places its value into (setRemovedValue).

(O(1) removal time)

Parameters:
key The key of the key-value mapping to remove.
setRemovedValue On success, the removed value is copied into this object.
Returns:
B_NO_ERROR if a key was found and the mapping removed, or B_ERROR if the key wasn't found.

Definition at line 530 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
uint32 Hashtable< KeyType, ValueType, HashFunctorType >::Remove ( const Hashtable< KeyType, ValueType, HashFunctorType > &  pairs  ) 

Convenience method: For each key in the passed-in-table, removes that key (and its associated value) from this table.

Parameters:
pairs A table containing keys that should be removed from this table.
Returns:
the number of items actually removed from this table.

Definition at line 1891 of file Hashtable.h.

References Hashtable< KeyType, ValueType, HashFunctorType >::_iterHead, Hashtable< KeyType, ValueType, HashFunctorType >::Clear(), and Hashtable< KeyType, ValueType, HashFunctorType >::GetNumItems().

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::RemoveFirst (  )  [inline]

Convenience method: Removes the first key/value mapping in the table.

(O(1) removal time)

Returns:
B_NO_ERROR if the first mapping was removed, or B_ERROR if this table was empty.

Definition at line 541 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::RemoveFirst ( KeyType &  setRemovedKey  )  [inline]

Convenience method: Removes the first key/value mapping in the table and places the removed key into (setRemovedKey).

(O(1) removal time)

Parameters:
setRemovedKey On success, the removed key is copied into this object.
Returns:
B_NO_ERROR if the first mapping was removed and the key placed into (setRemovedKey), or B_ERROR if the table was empty.

Definition at line 548 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::RemoveFirst ( KeyType &  setRemovedKey,
ValueType &  setRemovedValue 
) [inline]

Convenience method: Removes the first key/value mapping in the table and places its key into (setRemovedKey) and its value into (setRemovedValue).

(O(1) removal time)

Parameters:
setRemovedKey On success, the removed key is copied into this object.
setRemovedValue On success, the removed value is copied into this object.
Returns:
B_NO_ERROR if the first mapping was removed and the key and value placed into the arguments, or B_ERROR if the table was empty.

Definition at line 561 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::RemoveLast (  )  [inline]

Convenience method: Removes the last key/value mapping in the table.

(O(1) removal time)

Returns:
B_NO_ERROR if the last mapping was removed, or B_ERROR if the table was empty.

Definition at line 571 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::RemoveLast ( KeyType &  setRemovedKey  )  [inline]

Convenience method: Removes the last key/value mapping in the table and places the removed key into (setRemovedKey).

(O(1) removal time)

Parameters:
setRemovedKey On success, the removed key is copied into this object.
Returns:
B_NO_ERROR if the last mapping was removed and the key placed into (setRemovedKey), or B_ERROR if the table was empty.

Definition at line 578 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::RemoveLast ( KeyType &  setRemovedKey,
ValueType &  setRemovedValue 
) [inline]

Convenience method: Removes the last key/value mapping in the table and places its key into (setRemovedKey) and its value into (setRemovedValue).

(O(1) removal time)

Parameters:
setRemovedKey On success, the removed key is copied into this object.
setRemovedValue On success, the removed value is copied into this object.
Returns:
B_NO_ERROR if the last mapping was removed and the key and value placed into the arguments, or B_ERROR if the table was empty.

Definition at line 591 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
void Hashtable< KeyType, ValueType, HashFunctorType >::Clear ( bool  releaseCachedData = false  ) 

Removes all mappings from the hash table.

(O(N) clear time)

Parameters:
releaseCachedData If set true, we will immediately free any buffers we may contain. Otherwise, we'll keep them around in case they can be re-used later on.

Definition at line 1821 of file Hashtable.h.

References Hashtable< KeyType, ValueType, HashFunctorType >::HasItems().

Referenced by PathMatcher::Clear(), Hashtable< KeyType, ValueType, HashFunctorType >::operator=(), Hashtable< KeyType, ValueType, HashFunctorType >::Remove(), and Hashtable< KeyType, ValueType, HashFunctorType >::~Hashtable().

template<class KeyType, class ValueType, class HashFunctorType>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::Reposition ( const KeyType &  key  ) 

Moves the specified key/value pair so that it is in the correct position based on the current sort-by-value ordering.

The only time you would need to call this is if the Hashtable has been set to automatic-sort-by-value mode (see SetAutoSortMode()) and you have done an in-place modification of this key's value that might have affected the key's correct position in the sort ordering. If you are not using AUTOSORT_BY_VALUE mode, or if you are only doing Put()'s and Get()'s, and never modifying ValueType objects in-place within the table, then calling this method is not necessary and will have no effect.

Parameters:
key The key object of the key/value pair that may need to be repositioned.
Returns:
B_NO_ERROR on success, or B_ERROR if (key) was not found in the table, or if the table is not in AUTOSORT_BY_VALUE mode with a valid value-compare function.

Definition at line 1682 of file Hashtable.h.

References Hashtable< KeyType, ValueType, HashFunctorType >::AUTOSORT_BY_VALUE.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
void Hashtable< KeyType, ValueType, HashFunctorType >::SetAutoSortMode ( int  mode,
bool  sortNow = true 
) [inline]

This method can be used to activate or deactivate auto-sorting on this Hashtable.

If active, auto-sorting ensures that whenever Put() is called, the new/updated item is moved to the correct place in the iterator traversal list. Note that using auto-sort means that Put() becomes an O(N) operation instead of O(1). Default mode is AUTOSORT_DISABLED.

Parameters:
mode Either AUTOSORT_DISABLED, AUTOSORT_BY_KEY, or AUTOSORT_BY_VALUE.
sortNow If true, Sort() will be called to ensure that the table is in a sorted state. You can avoid an immediate sort by specifying this parameter as false, but be aware that auto-sorting won't sort properly if the contents of the table aren't already in in a correctly sorted state--so you may then need to call Sort() before auto-sorting will sort properly.

Definition at line 630 of file Hashtable.h.

Referenced by Hashtable< KeyType, ValueType, HashFunctorType >::EnsureSize().

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
void Hashtable< KeyType, ValueType, HashFunctorType >::SetCompareCookie ( void *  cookie  )  [inline]

This method can be used to set the "cookie value" that will be passed in to the comparison functions specified by SetKeyCompareFunction() and SetValueCompareFunction().

This value will be passed along during whenever a user-defined compare function is called implicitely.

Definition at line 640 of file Hashtable.h.

Referenced by Hashtable< KeyType, ValueType, HashFunctorType >::EnsureSize().

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
void* Hashtable< KeyType, ValueType, HashFunctorType >::GetCompareCookie (  )  const [inline]

Returns the current comparison cookie, as previously set by SetCompareCookie().

Default value is NULL.

Definition at line 643 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
void Hashtable< KeyType, ValueType, HashFunctorType >::SetKeyCompareFunction ( KeyCompareFunc  func  )  [inline]

You can call this to set a custom function to use to compare keys.

By default, the Key class's '==' operator is used to compare keys, and no key sorting is done. But for some key types (e.g. const char *'s) '==' is not terribly useful, and in any case it doesn't handle sorting properly, so if you like you you can supply your own key-comparison-function here.

See also:
KeyCompareFunc for a description of the callback function's required semantics.
Parameters:
func The new comparison function to use for this table, or NULL to revert to no key-sorting-function.

Definition at line 652 of file Hashtable.h.

Referenced by Hashtable< KeyType, ValueType, HashFunctorType >::EnsureSize().

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
KeyCompareFunc Hashtable< KeyType, ValueType, HashFunctorType >::GetKeyCompareFunction (  )  const [inline]

Returns the current comparison function used for comparing keys.

Default value is NULL.

Definition at line 655 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
void Hashtable< KeyType, ValueType, HashFunctorType >::SetValueCompareFunction ( ValueCompareFunc  func  )  [inline]

You can call this to set a custom function to use to compare values.

By default, no value sorting is done. If you wish to enable auto-sort-by-value, you will need to a value-comparison-function here.

See also:
ValueCompareFunc for a description of the callback function's required semantics.
Parameters:
func The new comparison function to use for this table, or NULL to revert to no value-sorting-function.

Definition at line 662 of file Hashtable.h.

Referenced by Hashtable< KeyType, ValueType, HashFunctorType >::EnsureSize().

template<class KeyType, class ValueType, class HashFunctorType = HashFunctor<KeyType>>
ValueCompareFunc Hashtable< KeyType, ValueType, HashFunctorType >::GetValueCompareFunction (  )  const [inline]

Returns the current comparison function used for comparing values.

Default value is NULL.

Definition at line 665 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
void Hashtable< KeyType, ValueType, HashFunctorType >::SwapContents ( Hashtable< KeyType, ValueType, HashFunctorType > &  swapMe  ) 

This method doesn anhefficient zero-copy swap of this hash table's contents with those of (swapMe).

That is to say, when this method returns, (swapMe) will be identical to the old state of this Hashtable, and this Hashtable will be identical to the old state of (swapMe). Any active iterators present for either table will swap owners also, becoming associated with the other table.

Parameters:
swapMe The table whose contents and iterators are to be swapped with this table's.

Definition at line 1485 of file Hashtable.h.

References Hashtable< KeyType, ValueType, HashFunctorType >::_autoSortMode, Hashtable< KeyType, ValueType, HashFunctorType >::_compareCookie, Hashtable< KeyType, ValueType, HashFunctorType >::_count, Hashtable< KeyType, ValueType, HashFunctorType >::_freeHead, Hashtable< KeyType, ValueType, HashFunctorType >::_iterHead, Hashtable< KeyType, ValueType, HashFunctorType >::_iterList, Hashtable< KeyType, ValueType, HashFunctorType >::_iterTail, HashtableIterator< KeyType, ValueType, HashFunctorType >::_nextIter, HashtableIterator< KeyType, ValueType, HashFunctorType >::_owner, Hashtable< KeyType, ValueType, HashFunctorType >::_table, Hashtable< KeyType, ValueType, HashFunctorType >::_tableSize, Hashtable< KeyType, ValueType, HashFunctorType >::_userKeyCompareFunc, and Hashtable< KeyType, ValueType, HashFunctorType >::_userValueCompareFunc.

Referenced by Hashtable< KeyType, ValueType, HashFunctorType >::EnsureSize().

template<class KeyType, class ValueType, class HashFunctorType>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::MoveToFront ( const KeyType &  moveMe  ) 

Moves the given entry to the head of the HashtableIterator traversal sequence.

Note that calling this method is generally a bad idea of the table is in auto-sort mode, as it is likely to unsort the traversal ordering and thus break auto-sorting. However, calling Sort() will restore the sort-order and make auto-sorting work again)

Parameters:
moveMe Key of the item to be moved to the front of the sequence.
Returns:
B_NO_ERROR on success, or B_ERROR if (moveMe) was not found in the table.

Definition at line 1548 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::MoveToBack ( const KeyType &  moveMe  ) 

Moves the given entry to the tail of the HashtableIterator traversal sequence.

Note that calling this method is generally a bad idea of the table is in auto-sort mode, as it is likely to unsort the traversal ordering and thus break auto-sorting. However, calling Sort() will restore the sort-order and make auto-sorting work again)

Parameters:
moveMe Key of the item to be moved to the end of the sequence.
Returns:
B_NO_ERROR on success, or B_ERROR if (moveMe) was not found in the table.

Definition at line 1558 of file Hashtable.h.

template<class KeyType, class ValueType, class HashFunctorType>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::MoveToBefore ( const KeyType &  moveMe,
const KeyType &  toBeforeMe 
)

Moves the given entry to the spot just in front of the other specified entry in the HashtableIterator traversal sequence.

Note that calling this method is generally a bad idea of the table is in auto-sort mode, as it is likely to unsort the traversal ordering and thus break auto-sorting. However, calling Sort() will restore the sort-order and make auto-sorting work again)

Parameters:
moveMe Key of the item to be moved.
toBeforeMe Key of the item that (moveMe) should be placed in front of.
Returns:
B_NO_ERROR on success, or B_ERROR if (moveMe) was not found in the table, or was the same item as (toBeforeMe).

Definition at line 1568 of file Hashtable.h.

References Hashtable< KeyType, ValueType, HashFunctorType >::HasItems().

template<class KeyType, class ValueType, class HashFunctorType>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::MoveToBehind ( const KeyType &  moveMe,
const KeyType &  toBehindMe 
)

Moves the given entry to the spot just behind the other specified entry in the HashtableIterator traversal sequence.

Note that calling this method is generally a bad idea of the table is in auto-sort mode, as it is likely to unsort the traversal ordering and thus break auto-sorting. However, calling Sort() will restore the sort-order and make auto-sorting work again)

Parameters:
moveMe Key of the item to be moved.
toBehindMe Key of the item that (moveMe) should be placed behind.
Returns:
B_NO_ERROR on success, or B_ERROR if (moveMe) was not found in the table, or was the same item as (toBehindMe).

Definition at line 1583 of file Hashtable.h.

References Hashtable< KeyType, ValueType, HashFunctorType >::HasItems().

template<class KeyType, class ValueType, class HashFunctorType>
status_t Hashtable< KeyType, ValueType, HashFunctorType >::MoveToTable ( const KeyType &  moveMe,
Hashtable< KeyType, ValueType > &  toTable 
)

Convenience method: Moves an item from this table to another table.

Parameters:
moveMe The key in this table of the item that should be moved.
toTable The table that the item should be in when this operation completes.
Returns:
B_NO_ERROR on success, or B_ERROR on failure (out of memory, or (moveMe) was not found in this table. Note that trying to move an item into its own table will simply return B_NO_ERROR with no side effects.

Definition at line 1534 of file Hashtable.h.

References Hashtable< KeyType, ValueType, HashFunctorType >::PutAux().