A vector which guarantees stable indices and features O(1) deletion. More...
#include <StableVector.hpp>
Public Types | |
using | ElementType = ElemT |
using | HandleType = HandleT |
Public Member Functions | |
StableVectorIterator< HandleType, ElementType > | begin () const |
Returns an iterator to the first element of this vector. More... | |
void | clear () |
Removes all elements from the vector. More... | |
StableVectorIterator< HandleType, ElementType > | end () const |
Returns an iterator to the element after the last element of this vector. More... | |
void | erase (HandleType handle) |
Mark the element behind the given handle as deleted. More... | |
boost::optional< ElementType & > | get (HandleType handle) |
Returns the element referred to by handle . More... | |
boost::optional< const ElementType & > | get (HandleType handle) const |
Returns the element referred to by handle . More... | |
void | increaseSize (HandleType upTo) |
Increases the size of the vector to the length of upTo . More... | |
void | increaseSize (HandleType upTo, const ElementType &elem) |
Increases the size of the vector to the length of upTo by inserting copies of elem . More... | |
HandleType | nextHandle () const |
The handle which would be returned by calling push now. More... | |
size_t | numUsed () const |
Number of non-deleted elements. More... | |
ElementType & | operator[] (HandleType handle) |
Returns the element referred to by handle . More... | |
const ElementType & | operator[] (HandleType handle) const |
Returns the element referred to by handle . More... | |
HandleType | push (const ElementType &elem) |
Adds the given element to the vector. More... | |
HandleType | push (ElementType &&elem) |
Adds the given element by moving from it. More... | |
void | reserve (size_t newCap) |
Increase the capacity of the vector to a value that's greater or equal to newCap. More... | |
void | set (HandleType handle, const ElementType &elem) |
Set a value for the existing handle . More... | |
void | set (HandleType handle, ElementType &&elem) |
Set a value for the existing handle by moving from elem . More... | |
size_t | size () const |
Absolute size of the vector (including deleted elements). More... | |
StableVector () | |
Creates an empty StableVector. More... | |
StableVector (size_t countElements, const ElementType &defaultValue) | |
Creates a StableVector with countElements many copies of defaultValue . More... | |
StableVector (size_t countElements, const boost::shared_array< ElementType > &sharedArray) | |
Private Member Functions | |
void | checkAccess (HandleType handle) const |
Assert that the requested handle is not deleted or throw an exception otherwise. More... | |
Private Attributes | |
vector< boost::optional< ElementType > > | m_elements |
Vector for stored elements. More... | |
size_t | m_usedCount |
Count of used elements in elements vector. More... | |
A vector which guarantees stable indices and features O(1) deletion.
This is basically a wrapper for the std::vector, which marks an element as deleted but does not actually delete it. This means that indices are never invalidated. When inserting an element, you get its index (its so called "handle") back. This handle can later be used to access the element. This remains true regardless of other insertions and deletions happening in between.
USE WITH CAUTION: This NEVER frees memory of deleted values (except on its own destruction and can get very large if used incorrectly! If deletions in your use-case are far more numerous than insertions, this data structure is probably not fitting your needs. The memory requirement of this class is O(n_p) where n_p is the number of push()
calls.
HandleT | This handle type contains the actual index. It has to be derived from BaseHandle ! |
ElemT | Type of elements in the vector. |
Definition at line 104 of file StableVector.hpp.
using lvr2::StableVector< HandleT, ElemT >::ElementType = ElemT |
Definition at line 113 of file StableVector.hpp.
using lvr2::StableVector< HandleT, ElemT >::HandleType = HandleT |
Definition at line 114 of file StableVector.hpp.
|
inline |
Creates an empty StableVector.
Definition at line 119 of file StableVector.hpp.
lvr2::StableVector< HandleT, ElemT >::StableVector | ( | size_t | countElements, |
const ElementType & | defaultValue | ||
) |
Creates a StableVector with countElements
many copies of defaultValue
.
The elements are stored contiguously in the vectors, thus the valid indices of these elements are 0 to countElements
- 1.
lvr2::StableVector< HandleT, ElemT >::StableVector | ( | size_t | countElements, |
const boost::shared_array< ElementType > & | sharedArray | ||
) |
StableVectorIterator<HandleType, ElementType> lvr2::StableVector< HandleT, ElemT >::begin | ( | ) | const |
Returns an iterator to the first element of this vector.
This iterator auto skips deleted elements and returns handles to the valid elements.
|
private |
Assert that the requested handle is not deleted or throw an exception otherwise.
void lvr2::StableVector< HandleT, ElemT >::clear | ( | ) |
Removes all elements from the vector.
StableVectorIterator<HandleType, ElementType> lvr2::StableVector< HandleT, ElemT >::end | ( | ) | const |
Returns an iterator to the element after the last element of this vector.
void lvr2::StableVector< HandleT, ElemT >::erase | ( | HandleType | handle | ) |
boost::optional<ElementType&> lvr2::StableVector< HandleT, ElemT >::get | ( | HandleType | handle | ) |
Returns the element referred to by handle
.
Returns none
if the element was deleted or if the handle is out of bounds.
boost::optional<const ElementType&> lvr2::StableVector< HandleT, ElemT >::get | ( | HandleType | handle | ) | const |
Returns the element referred to by handle
.
Returns none
if the element was deleted or if the handle is out of bounds.
void lvr2::StableVector< HandleT, ElemT >::increaseSize | ( | HandleType | upTo | ) |
Increases the size of the vector to the length of upTo
.
This means that the next call to push()
after calling resize(upTo)
will return exactly the upTo
handle. All elements that are inserted by this method are marked as deleted and thus aren't initialized. They can be set later with set()
.
If upTo
is already a valid handle, this method will panic!
void lvr2::StableVector< HandleT, ElemT >::increaseSize | ( | HandleType | upTo, |
const ElementType & | elem | ||
) |
Increases the size of the vector to the length of upTo
by inserting copies of elem
.
This means that the next call to push()
after calling resize(upTo)
will return exactly the upTo
handle.
If upTo
is already a valid handle, this method will panic!
HandleType lvr2::StableVector< HandleT, ElemT >::nextHandle | ( | ) | const |
The handle which would be returned by calling push
now.
size_t lvr2::StableVector< HandleT, ElemT >::numUsed | ( | ) | const |
Number of non-deleted elements.
ElementType& lvr2::StableVector< HandleT, ElemT >::operator[] | ( | HandleType | handle | ) |
Returns the element referred to by handle
.
If handle
is out of bounds or the element was deleted, this method will throw an exception in debug mode and has UB in release mode. Use get()
instead to gracefully handle the absence of an element.
const ElementType& lvr2::StableVector< HandleT, ElemT >::operator[] | ( | HandleType | handle | ) | const |
Returns the element referred to by handle
.
If handle
is out of bounds or the element was deleted, this method will throw an exception in debug mode and has UB in release mode. Use get()
instead to gracefully handle the absence of an element.
HandleType lvr2::StableVector< HandleT, ElemT >::push | ( | const ElementType & | elem | ) |
Adds the given element to the vector.
HandleType lvr2::StableVector< HandleT, ElemT >::push | ( | ElementType && | elem | ) |
Adds the given element by moving from it.
void lvr2::StableVector< HandleT, ElemT >::reserve | ( | size_t | newCap | ) |
Increase the capacity of the vector to a value that's greater or equal to newCap.
If newCap is greater than the current capacity, new storage is allocated, otherwise the method does nothing.
newCap | new capacity of the vector |
void lvr2::StableVector< HandleT, ElemT >::set | ( | HandleType | handle, |
const ElementType & | elem | ||
) |
void lvr2::StableVector< HandleT, ElemT >::set | ( | HandleType | handle, |
ElementType && | elem | ||
) |
size_t lvr2::StableVector< HandleT, ElemT >::size | ( | ) | const |
Absolute size of the vector (including deleted elements).
|
private |
Vector for stored elements.
Definition at line 281 of file StableVector.hpp.
|
private |
Count of used elements in elements vector.
Definition at line 278 of file StableVector.hpp.