Lunchbox
1.11.0
|
STL-like vector implementation providing certain thread-safety guarantees. More...
#include <lfVector.h>
Public Types | |
typedef ScopedFastWrite | ScopedWrite |
typedef T | value_type |
typedef LFVectorIterator < LFVector< T, nSlots >, T > | iterator |
Iterator over the vector elements. More... | |
typedef LFVectorIterator < const LFVector< T, nSlots > , const T > | const_iterator |
Iterator over the const vector elements. More... | |
Public Member Functions | |
LFVector () | |
LFVector (const size_t n) | |
LFVector (const size_t n, const T &t) | |
LFVector (const LFVector &from) | |
template<int32_t fromSlots> | |
LFVector (const LFVector< T, fromSlots > &from) | |
~LFVector () | |
LFVector & | operator= (const LFVector &from) |
bool | operator== (const LFVector &rhs) const |
bool | operator!= (const LFVector &rhs) const |
bool | empty () const |
size_t | size () const |
T & | operator[] (size_t i) |
const T & | operator[] (size_t i) const |
T & | front () |
T & | back () |
const_iterator | begin () const |
const_iterator | end () const |
iterator | begin () |
iterator | end () |
void | expand (const size_t newSize, const T &item=T()) |
Resize the vector to at least the given size. More... | |
void | push_back (const T &item, bool lock=true) |
Add an element to the vector. More... | |
void | pop_back () |
Remove the last element (STL version). More... | |
bool | pop_back (T &element) |
Remove the last element (atomic version). More... | |
iterator | erase (typename LFVector< T, nSlots >::iterator pos) |
Remove an element. More... | |
iterator | erase (const T &element) |
Remove the last occurence of the given element. More... | |
void | resize (const size_t size, const T &value=T()) |
Resize the vector. More... | |
void | clear () |
Clear the vector and all storage. More... | |
ScopedWrite | getWriteLock () |
STL-like vector implementation providing certain thread-safety guarantees.
All operations not modifying the vector size are lock-free and wait-free. All operations modifying the vector size are serialized using a spin lock. The interaction of operations is documented in the corresponding modify operation.
Undocumented methods behave like the STL implementation. The number of slots (default 32) sets the maximum elements the vector may hold to 2^nSlots-1. Each slot needs one pointer additional storage. Naturally it should never be set higher than 64.
Not all std::vector methods are implemented. Serializable using boost.serialization.
Example:
Definition at line 53 of file lfVector.h.
typedef LFVectorIterator<const LFVector<T, nSlots>, const T> lunchbox::LFVector< T, nSlots >::const_iterator |
typedef LFVectorIterator< LFVector< T, nSlots >, T > lunchbox::LFVector< T, nSlots >::iterator |
lunchbox::LFVector< T, nSlots >::LFVector | ( | ) |
Definition at line 28 of file lfVector.ipp.
References lunchbox::setZero().
|
explicit |
Definition at line 35 of file lfVector.ipp.
References lunchbox::getIndexOfLastBit(), and lunchbox::setZero().
lunchbox::LFVector< T, nSlots >::LFVector | ( | const size_t | n, |
const T & | t | ||
) |
Definition at line 46 of file lfVector.ipp.
References lunchbox::getIndexOfLastBit(), and lunchbox::setZero().
|
explicit |
Definition at line 66 of file lfVector.ipp.
|
explicit |
Definition at line 75 of file lfVector.ipp.
lunchbox::LFVector< T, nSlots >::~LFVector | ( | ) |
Definition at line 83 of file lfVector.ipp.
T & lunchbox::LFVector< T, nSlots >::back | ( | ) |
Definition at line 193 of file lfVector.ipp.
|
inline |
Definition at line 375 of file lfVector.ipp.
Referenced by lunchbox::LFVector< T, nSlots >::operator==().
|
inline |
Definition at line 387 of file lfVector.ipp.
void lunchbox::LFVector< T, nSlots >::clear | ( | ) |
Clear the vector and all storage.
Thread-safe with other write operations. By nature not thread-safe with read operations.
Definition at line 295 of file lfVector.ipp.
|
inline |
Definition at line 87 of file lfVector.h.
|
inline |
Definition at line 381 of file lfVector.ipp.
|
inline |
Definition at line 393 of file lfVector.ipp.
LFVector< T, nSlots >::iterator lunchbox::LFVector< T, nSlots >::erase | ( | typename LFVector< T, nSlots >::iterator | pos | ) |
Remove an element.
A concurrent read on the item or any following item is not thread save. The vector's size is decremented first. Returns end() if the element can't be removed, i.e., the iterator is past end() or not for this vector.
pos | the element to remove |
Definition at line 240 of file lfVector.ipp.
LFVector< T, nSlots >::iterator lunchbox::LFVector< T, nSlots >::erase | ( | const T & | element | ) |
Remove the last occurence of the given element.
A concurrent read on the item or any following item is not thread save. The vector's size is decremented first. Returns end() if the element can't be removed, i.e., the vector does not contain the element.
element | the element to remove |
Definition at line 258 of file lfVector.ipp.
void lunchbox::LFVector< T, nSlots >::expand | ( | const size_t | newSize, |
const T & | item = T( ) |
||
) |
Resize the vector to at least the given size.
In contrast to resize(), expand() only increases the size of the vector, allowing concurrent resize operations on the same vector. Completely thread-save with read operations. Existing end() iterators will keep pointing to the old end of the vector. The size is updated after all elements have been inserted, so size() followed by a read is thread-safe. In contrast to while( vector.size() < newSize ) vector.insert( item );
this method's operation is atomic with other writes.
newSize | the minimum new size. |
item | the element to insert. |
std::runtime_error | if the vector is full |
Definition at line 200 of file lfVector.ipp.
T & lunchbox::LFVector< T, nSlots >::front | ( | ) |
Definition at line 186 of file lfVector.ipp.
LFVector< T, nSlots >::ScopedWrite lunchbox::LFVector< T, nSlots >::getWriteLock | ( | ) |
Definition at line 311 of file lfVector.ipp.
|
inline |
Definition at line 85 of file lfVector.h.
LFVector< T, nSlots > & lunchbox::LFVector< T, nSlots >::operator= | ( | const LFVector< T, nSlots > & | from | ) |
Definition at line 90 of file lfVector.ipp.
bool lunchbox::LFVector< T, nSlots >::operator== | ( | const LFVector< T, nSlots > & | rhs | ) | const |
Definition at line 124 of file lfVector.ipp.
References lunchbox::LFVector< T, nSlots >::begin(), and lunchbox::LFVector< T, nSlots >::size().
T & lunchbox::LFVector< T, nSlots >::operator[] | ( | size_t | i | ) |
Definition at line 151 of file lfVector.ipp.
References lunchbox::getIndexOfLastBit().
const T & lunchbox::LFVector< T, nSlots >::operator[] | ( | size_t | i | ) | const |
Definition at line 166 of file lfVector.ipp.
References lunchbox::getIndexOfLastBit().
void lunchbox::LFVector< T, nSlots >::pop_back | ( | ) |
Remove the last element (STL version).
A concurrent read on the removed item produces undefined results, in particular end() and back().
Definition at line 215 of file lfVector.ipp.
bool lunchbox::LFVector< T, nSlots >::pop_back | ( | T & | element | ) |
Remove the last element (atomic version).
A concurrent read on the removed item produces undefined results, in particular end() and back(). The last element is assigned to the given output element if the vector is not empty. If the vector is empty, element is not touched and false is returned. The whole operation is atomic with other operations changing the size of the vector.
element | the item receiving the value which was stored at the end. |
Definition at line 226 of file lfVector.ipp.
void lunchbox::LFVector< T, nSlots >::push_back | ( | const T & | item, |
bool | lock = true |
||
) |
Add an element to the vector.
Completely thread-save with read operations. Existing end() iterators will keep pointing to the old end of the vector. The size is updated after the element is inserted, so size() followed by a read is thread-safe.
item | the element to insert. |
lock | true for internal lock, false if locked with getWriteLock() |
std::runtime_error | if the vector is full |
Definition at line 208 of file lfVector.ipp.
void lunchbox::LFVector< T, nSlots >::resize | ( | const size_t | size, |
const T & | value = T( ) |
||
) |
Resize the vector.
Thread-safe with other write operations. Shrinking is not thread-safe with concurrent reads on the removed elements and produces undefined results.
std::runtime_error | if the vector is full |
Definition at line 280 of file lfVector.ipp.
|
inline |
Definition at line 88 of file lfVector.h.
Referenced by lunchbox::LFVector< T, nSlots >::operator==().