Loading...
Searching...
No Matches
SBCContainerIndexer< ObjectType, HashMap, Vector > Class Template Reference

This template class is used to efficiently index objects. More...

The hash table

HashMap * indexMap
 The hash map.
 
Vector * objectVector
 The object vector.
 

Constructors and destructors

 SBCContainerIndexer ()
 Creates an indexer.
 
 SBCContainerIndexer (unsigned int initialSize)
 Creates an indexer with a default pre-allocated size.
 
 SBCContainerIndexer (std::set< ObjectType > &objectSet)
 Indexes a set of objects.
 
 SBCContainerIndexer (std::vector< ObjectType > &objectVector)
 Indexes a vector of objects.
 
 SBCContainerIndexer (const SBCContainerIndexer &indexer)
 Copy constructor.
 
 SBCContainerIndexer (SBCContainerIndexer &&indexer)
 Move constructor.
 
virtual ~SBCContainerIndexer ()
 Destructs the indexer.
 

Operators

SBCContainerIndexeroperator= (const SBCContainerIndexer &indexer)
 Copy assignment.
 
SBCContainerIndexeroperator= (SBCContainerIndexer &&indexer)
 Move assignment.
 

Indexing

void clear ()
 Clears the indexer.
 
unsigned int push_back (const ObjectType &object)
 Adds an object in the indexer if possible, and returns the index of the object.
 
unsigned int insert (unsigned int i, const ObjectType &object)
 Inserts an object in the indexer at position i if possible, and returns the index of the object.
 
unsigned int pop_back ()
 Adds an object in the indexer and returns the index of the object.
 
unsigned int eraseObject (const ObjectType &object)
 Erases the object from the indexer.
 
unsigned int eraseIndex (unsigned int objectIndex)
 Erases object objectIndex from the indexer.
 
bool empty () const
 Returns true if and only if the indexer is empty.
 
unsigned int size () const
 Returns the number of indexed objects.
 
bool hasIndex (const ObjectType &object) const
 Returns true if the object has an index.
 
unsigned int getIndex (const ObjectType &object) const
 Returns the index associated to the object.
 
bool getIndex (const ObjectType &object, unsigned int &index) const
 Returns the index associated to the object.
 
ObjectType getObject (unsigned int index) const
 Returns the object associated to the index.
 
ObjectType operator[] (unsigned int index) const
 Returns the object associated to the index.
 
iterator begin ()
 Returns an iterator that points to the beginning of the indexer.
 
const_iterator begin () const
 Returns an iterator that points to the beginning of the indexer.
 
iterator end ()
 Returns an iterator that points to the end of the indexer.
 
const_iterator end () const
 Returns an iterator that points to the end of the indexer.
 
reverse_iterator rbegin ()
 Returns a reverse iterator that points to the reverse beginning of the indexer.
 
const_reverse_iterator rbegin () const
 Returns a reverse iterator that points to the reverse beginning of the indexer.
 
reverse_iterator rend ()
 Returns a reverse iterator that points to the reverse end of the indexer.
 
const_reverse_iterator rend () const
 Returns a reverse iterator that points to the reverse end of the indexer.
 

Debugging

void print () const
 Prints some debugging information.
 

Detailed Description

template<class ObjectType, class HashMap = SBCContainerHashMap<ObjectType, unsigned int>, class Vector = SBCContainerVector<ObjectType>>
class SBCContainerIndexer< ObjectType, HashMap, Vector >

Fast indexing of objects is required in SAMSON because objects may operate on lists of other objects (e.g. a collision detection solver operates on a list of structural model components). An indexer provides a way to map the set of n objects to a set of ordered indices, from 0 to n-1, which can then be used for fast access to information associated to the mapped objects.

SBCContainerIndexer makes it possible to index values contiguously. Precisely, it is guaranteed that \(n\) values are always indexed by integers in \([0, n - 1]\). To achieve this, when an object with index \(i\neq n - 1\) is removed from the index, the index of object \(n - 1\) becomes \(i\). SBCContainerIndexer may be used to index values compactly and dynamically.

// create atoms
unsigned int nAtoms = 100;
SBAtom** atomArray = new SBAtom*[nAtoms];
for (unsigned int i = 0; i<nAtoms; i++) atomArray[i] = new SBAtom();
// index atoms
SBIndexer<SBAtom*>* atomIndexer = new SBIndexer<SBAtom*>();
for (unsigned int i = 0; i<nAtoms; i++)
atomIndexer->push_back(atomArray[i]);
// print indices
for (unsigned int i = 0; i<nAtoms; i++) {
if (atomIndexer->hasIndex(atomArray[i]))
std::cout << " Atom " << atomArray[i] <<
" -> " << atomIndexer->getIndex(atomArray[i]) << ".\n";
else
std::cout << " Atom " << atomArray[i] << " -> no index.\n";
}
// erase atomArray[9] from the index
atomIndexer->eraseObject(atomArray[9]);
// print indices
// atomArray[9] has no index
// atomArray[99] now has index 9
for (unsigned int i = 0; i<nAtoms; i++) {
if (atomIndexer->hasIndex(atomArray[i]))
std::cout << " Atom " << atomArray[i] <<
" -> " << atomIndexer->getIndex(atomArray[i]) << ".\n";
else
std::cout << " Atom " << atomArray[i] << " -> no index.\n";
}
// erase atom with index 2
atomIndexer->eraseIndex(2);
// print indices
// atomArray[9] has no index
// atomArray[99] now has index 9
// atomArray[2] has no index
// atomArray[98] has index 2
for (unsigned int i = 0; i<nAtoms; i++) {
if (atomIndexer->hasIndex(atomArray[i]))
std::cout << " Atom " << atomArray[i] <<
" -> " << atomIndexer->getIndex(atomArray[i]) << ".\n";
else
std::cout << " Atom " << atomArray[i] << " -> no index.\n";
}
SBMStructuralModelNodeAtom SBAtom
The short name of SBMStructuralModelNodeAtom.
Definition: SBMStructuralModelNodeAtom.hpp:666
This class describes an atom in a structural model.
Definition: SBMStructuralModelNodeAtom.hpp:34

Internally, SBCContainerIndexer uses a hash map (by default SBCContainerHashMap) to assign indices and a vector (by default SBCContainerVector) to store objects. As a result, retrieving indices takes an almost constant time, while retrieving objects takes constant time.

Short name: SBIndexer

See also
SBCContainerBuffer
SBCContainerHashMap
SBCContainerVector
SB_FOR

Member Function Documentation

◆ eraseIndex()

template<class ObjectType , class HashMap , class Vector >
unsigned int SBCContainerIndexer< ObjectType, HashMap, Vector >::eraseIndex ( unsigned int  objectIndex)

The function returns an unsigned int with the following meaning:

  • if the object was not present in the index, the returned value is the number of indexed objects.
  • else, the returned value is the index of the object that was removed, in [0,size-1]. Note that these two returned values coincide when the client (unsuccessfully) attempts to erase object with index size.

◆ eraseObject()

template<class ObjectType , class HashMap , class Vector >
unsigned int SBCContainerIndexer< ObjectType, HashMap, Vector >::eraseObject ( const ObjectType &  object)

The function returns an unsigned int with the following meaning:

  • if the object was not present in the index, the returned value is the number of indexed objects.
  • else, the returned value is the index of the object that was removed, in [0,size-1]. Note that these two returned values coincide when the client (unsuccessfully) attempts to erase object with index size.