Class SBCContainerIndexer#
template <class ObjectType, class HashMap, class Vector>
ClassList > SBCContainerIndexer
This template class is used to efficiently index objects. More...
#include <SBCContainerIndexer.hpp>
Classes#
Type | Name |
---|---|
class | const_iterator |
class | const_reverse_iterator |
class | iterator |
class | reverse_iterator |
Public Functions#
Type | Name |
---|---|
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. |
|
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. |
void | clear () Clears the indexer. |
bool | empty () const Returns true if and only if the indexer is empty. |
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. |
unsigned int | eraseIndex (unsigned int objectIndex) Erases object objectIndex from the indexer. |
unsigned int | eraseObject (const ObjectType & object) Erases the object from the indexer. |
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 . |
bool | hasIndex (const ObjectType & object) const Returns true if the object has an index. |
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. |
SBCContainerIndexer & | operator= (const SBCContainerIndexer & indexer) Copy assignment. |
SBCContainerIndexer & | operator= (SBCContainerIndexer && indexer) Move assignment. |
ObjectType | operator[] (unsigned int index) const Returns the object associated to the index . |
unsigned int | pop_back () Adds an object in the indexer and returns the index of the object. |
void | print () const Prints some debugging information. |
unsigned int | push_back (const ObjectType & object) Adds an object in the indexer if possible, and returns the index of the object. |
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. |
unsigned int | size () const Returns the number of indexed objects. |
virtual | ~SBCContainerIndexer () Destructs the indexer. |
Protected Attributes#
Type | Name |
---|---|
HashMap * | indexMap The hash map. |
Vector * | objectVector The object vector. |
Detailed Description#
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 values are always indexed by integers in . To achieve this, when an object with index is removed from the index, the index of object becomes . 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";
}
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
Public Functions Documentation#
function SBCContainerIndexer [1/6]#
Creates an indexer.
function SBCContainerIndexer [2/6]#
Creates an indexer with a default pre-allocated size.
function SBCContainerIndexer [3/6]#
Indexes a set of objects.
function SBCContainerIndexer [4/6]#
Indexes a vector of objects.
function SBCContainerIndexer [5/6]#
Copy constructor.
function SBCContainerIndexer [6/6]#
Move constructor.
function begin [1/2]#
Returns an iterator that points to the beginning of the indexer.
function begin [2/2]#
Returns an iterator that points to the beginning of the indexer.
function clear#
Clears the indexer.
function empty#
Returns true if and only if the indexer is empty.
function end [1/2]#
Returns an iterator that points to the end of the indexer.
function end [2/2]#
Returns an iterator that points to the end of the indexer.
function eraseIndex#
Erases object objectIndex from the indexer.
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.
function eraseObject#
Erases the object from the indexer.
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.
function getIndex [1/2]#
Returns the index associated to the object .
function getIndex [2/2]#
Returns the index associated to the object .
function getObject#
Returns the object associated to the index .
function hasIndex#
Returns true if the object has an index.
function insert#
Inserts an object in the indexer at position i
if possible, and returns the index of the object.
function operator=#
Copy assignment.
function operator=#
Move assignment.
function operator[]#
Returns the object associated to the index .
function pop_back#
Adds an object in the indexer and returns the index of the object.
function print#
Prints some debugging information.
function push_back#
Adds an object in the indexer if possible, and returns the index of the object.
function rbegin [1/2]#
Returns a reverse iterator that points to the reverse beginning of the indexer.
function rbegin [2/2]#
Returns a reverse iterator that points to the reverse beginning of the indexer.
function rend [1/2]#
Returns a reverse iterator that points to the reverse end of the indexer.
function rend [2/2]#
Returns a reverse iterator that points to the reverse end of the indexer.
function size#
Returns the number of indexed objects.
function ~SBCContainerIndexer#
Destructs the indexer.
Protected Attributes Documentation#
variable indexMap#
The hash map.
variable objectVector#
The object vector.