iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nrt::IdTable< ValueType, IdType > Class Template Reference

#include <nrt/Core/Design/IdTable.H>

template<class ValueType, class IdType = size_t>
class nrt::IdTable< ValueType, IdType >

A table used to store linked value and id types supporting lookups on both.

The IdTable class provides a way to associate a given value with an Id. Looking up a value by its id, or an id by its value are both very fast operations because the IdTable keeps track of both forward and reverse lookup tables.

IdTable keeps track of the number of "users" for each Id. Each time create() is called, a user count is incremented, and each time remove() is called it is decremented. If the count becomes zero during a remove(), the Id is destroyed.

Note
Care must be taken when using this structure in a multithreaded environment. If not locked properly, the internal structures could get messed-up.
Examples:
tests/test-IdTable.C.

Definition at line 58 of file IdTable.H.

Public Member Functions

IdType create (ValueType const &value)
 Create a new entry by value (or just increment user count if entry exists), and return the Id.
 
ValueType get (IdType const &id) const
 Get a value by its id. More...
 
IdType get (ValueType const &value) const
 Get an id by value. More...
 
IdType getcreate (ValueType const &value)
 Create a new entry by value if it does not already exist, and return the associated Id. More...
 
size_t users (IdType const &id) const
 Get the user count for a given Id, or zero if it does not exist.
 
size_t users (ValueType const &value) const
 Get the user count for a given value, or zero if it does not exist.
 
size_t remove (IdType const &id)
 Decrement the user count and, if zero, delete the Id and associated value. More...
 
size_t remove (ValueType const &value)
 Decrement the user count and, if zero, delete the value and associated Id. More...
 
void clear ()
 Clear the entire IdTable.
 

Member Function Documentation

template<class ValueType , class IdType>
ValueType nrt::IdTable< ValueType, IdType >::get ( IdType const &  id) const
inline

Get a value by its id.

Exceptions
IdNotFoundExceptionThrown if id does not exist in the table.

Definition at line 94 of file IdTableImpl.H.

template<class ValueType, class IdType>
IdType nrt::IdTable< ValueType, IdType >::get ( ValueType const &  value) const
inline

Get an id by value.

Exceptions
ValueNotFoundExceptionThrown if value does not exist in the table.

Definition at line 104 of file IdTableImpl.H.

template<class ValueType, class IdType >
IdType nrt::IdTable< ValueType, IdType >::getcreate ( ValueType const &  value)
inline

Create a new entry by value if it does not already exist, and return the associated Id.

Note that this does not increment the user count if the Id already existed

Definition at line 114 of file IdTableImpl.H.

template<class ValueType , class IdType>
size_t nrt::IdTable< ValueType, IdType >::remove ( IdType const &  id)

Decrement the user count and, if zero, delete the Id and associated value.

Returns
the new number of users after the decrement.
Exceptions
IdNotFoundExceptionThrown if value does not exist in the table.

Definition at line 141 of file IdTableImpl.H.

template<class ValueType, class IdType>
size_t nrt::IdTable< ValueType, IdType >::remove ( ValueType const &  value)

Decrement the user count and, if zero, delete the value and associated Id.

Returns
the new number of users after the decrement.
Exceptions
ValueNotFoundExceptionThrown if value does not exist in the table.

Definition at line 164 of file IdTableImpl.H.


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