Loading...
Searching...
No Matches
SBCContainerVector< T > Class Template Reference

This template class describes an extensible array. More...

Public Member Functions

 SBCContainerVector ()
 Constructs an empty vector.
 
 SBCContainerVector (unsigned int n)
 Constructs a vector of reserved size n.
 
 SBCContainerVector (unsigned int n, unsigned int c, const T &t)
 Constructs a vector of reserved size n and current size c, filled with copies of t.
 
 SBCContainerVector (T *a, unsigned int n)
 Constructs a vector from an array.
 
 SBCContainerVector (const SBCContainerVector &vector)
 Copy constructor.
 
 SBCContainerVector (SBCContainerVector &&vector)
 Move constructor.
 
 SBCContainerVector (std::initializer_list< T > list)
 Constructor using the initializer list.
 
virtual ~SBCContainerVector ()
 Destructs the vector.
 
SBCContainerVectoroperator= (const SBCContainerVector &vector)
 Copy assignment.
 
SBCContainerVectoroperator= (SBCContainerVector &&vector)
 Move assignment.
 
SBCContainerVectoroperator= (std::initializer_list< T > list)
 Assignment using the initializer list.
 
T & getElement (unsigned int i)
 Returns a reference to element i.
 
T const & getElement (unsigned int i) const
 Returns a reference to element i.
 
T & operator[] (unsigned int i)
 Returns a reference to element i.
 
T const & operator[] (unsigned int i) const
 Returns a reference to element i.
 
T & front ()
 Returns a reference to the last element.
 
T const & front () const
 Returns a reference to the last element.
 
T & back ()
 Returns a reference to the last element.
 
T const & back () const
 Returns a reference to the last element.
 
T * getPointer (unsigned int i)
 Returns a pointer to element i.
 
T const * getPointer (unsigned int i) const
 Returns a pointer to element i.
 
bool operator== (const SBCContainerVector &other)
 Returns whether this vector is equal to an other vector.
 
bool operator!= (const SBCContainerVector &other)
 Returns whether this vector is different from an other vector.
 
void setArray (T *newArray, unsigned int newSize)
 build from array
 
void resize (unsigned int newSize)
 Resize to size n.
 
void push_back (const T &t)
 Appends t to the vector.
 
void insert (unsigned int i, const T &t)
 Inserts t at position i if possible. If an element was present at position i, it is pushed at the back of the vector.
 
void insertLinear (unsigned int i, const T &t)
 
void pop_back ()
 Removes the last element from the vector.
 
bool contains (const T &t)
 Returns true when the vector contains t.
 
void remove (unsigned int i)
 Removes element i from the vector, and replaces it with the last element.
 
void removeLinear (unsigned int i)
 
void removeLinear (unsigned int first, unsigned int last)
 
void removeAll (const T &t)
 Removes all t from the vector. The vector is reordered as when applying multiple remove operations.
 
bool empty () const
 Returns true if and only if the vector is empty.
 
void clear ()
 Empties the vector.
 
unsigned int allocatedSize () const
 Returns the allocated size (in memory) of the vector.
 
unsigned int size () const
 Returns the size of the vector.
 
iterator begin ()
 Returns an iterator that points to the beginning of the vector.
 
const_iterator begin () const
 Returns an iterator that points to the beginning of the vector.
 
iterator end ()
 Returns an iterator that points to the end of the vector.
 
const_iterator end () const
 Returns an iterator that points to the end of the vector.
 
reverse_iterator rbegin ()
 Returns a reverse iterator that points to the reverse beginning of the vector.
 
const_reverse_iterator rbegin () const
 Returns a reverse iterator that points to the reverse beginning of the vector.
 
reverse_iterator rend ()
 Returns a reverse iterator that points to the reverse end of the vector.
 
const_reverse_iterator rend () const
 Returns a reverse iterator that points to the reverse end of the vector.
 
void print (unsigned int offset=0) const
 Prints the vector.
 
unsigned int getMemoryFootPrint () const
 Returns the memory footprint of the vector.
 
void free ()
 Frees the memory allocated for the vector.
 

Detailed Description

template<class T>
class SBCContainerVector< T >

SBCContainerVector is a lightweight template class to handle vectors of values, i.e. contiguous arrays of values. Its functionality is similar to the std::vector class. SBVector provides const and non-const iterators and reverse iterators:

SBVector<SBAtom*> v;
// add atoms
for (unsigned int i = 0; i<10; i++)
v.push_back(new SBAtom(SBElement::Carbon));
// print atoms positions (version 1)
for (unsigned int i = 0; i<10; i++)
v[i]->getPosition().print();
// print atoms positions (version 2)
for (SBVector<SBAtom*>::iterator i = v.begin(); i != v.end(); i++)
(*i)->getPosition().print();
// print atoms positions (version 3)
SB_FOR(SBAtom* atom, v)
atom->getPosition().print();
#define SB_FOR(VARIABLE, CONTAINER)
A macro to easily write for loops involving containers.
Definition: SBCContainerFor.hpp:83
@ Carbon
The type of the Carbon element.
Definition: SBMElement.hpp:48
This class describes an atom in a structural model.
Definition: SBMStructuralModelNodeAtom.hpp:34

Note that, in accordance with the behavior of buffers, indexers, etc., the insert function has a amortized constant time complexity, as it pushes back the replaced element to the end of the vector.

Short name: SBVector

See also
SB_FOR

Member Function Documentation

◆ insertLinear()

template<class T >
void SBCContainerVector< T >::insertLinear ( unsigned int  i,
const T &  t 
)
inline

Inserts t at position i if possible. If an element was present at position i, this element and all following elements are moved. This operation has a linear complexity, and may invalidate iterators.

◆ removeLinear() [1/2]

template<class T >
void SBCContainerVector< T >::removeLinear ( unsigned int  first,
unsigned int  last 
)
inline

Removes all elements between (and including) positions first and last. If elements are present after position last, these elements are moved. This operation has a linear complexity, and may invalidate iterators.

◆ removeLinear() [2/2]

template<class T >
void SBCContainerVector< T >::removeLinear ( unsigned int  i)
inline

Removes element i from the vector. If elements are present after position i, these elements are moved. This operation has a linear complexity, and may invalidate iterators.