Lunchbox  1.6.0
Public Member Functions | Public Types
lunchbox::LFVector< T, nSlots > Class Template Reference

STL-like vector implementation providing certain thread-safety guarantees. More...

#include <lfVector.h>

List of all members.

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 ()
LFVectoroperator= (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.
void push_back (const T &item, bool lock=true)
 Add an element to the vector.
void pop_back ()
 Remove the last element (STL version).
bool pop_back (T &element)
 Remove the last element (atomic version).
iterator erase (typename LFVector< T, nSlots >::iterator pos)
 Remove an element.
iterator erase (const T &element)
 Remove the last occurence of the given element.
void clear ()
 Clear the vector and all storage.
ScopedWrite getWriteLock ()

Public Types

typedef ScopedFastWrite ScopedWrite
typedef LFVectorIterator
< LFVector< T >, T > 
iterator
 Iterator over the vector elements.
typedef LFVectorIterator
< const LFVector< T >, const T > 
const_iterator
 Iterator over the const vector elements.

Detailed Description

template<class T, int32_t nSlots = 32>
class lunchbox::LFVector< T, nSlots >

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.

Definition at line 57 of file lfVector.h.


Member Typedef Documentation

template<class T, int32_t nSlots = 32>
typedef LFVectorIterator< const LFVector< T >, const T > lunchbox::LFVector< T, nSlots >::const_iterator

Iterator over the const vector elements.

Version:
1.3.2

Definition at line 109 of file lfVector.h.

template<class T, int32_t nSlots = 32>
typedef LFVectorIterator< LFVector< T >, T > lunchbox::LFVector< T, nSlots >::iterator

Iterator over the vector elements.

Version:
1.3.2

Definition at line 106 of file lfVector.h.


Constructor & Destructor Documentation

template<class T , int32_t nSlots>
lunchbox::LFVector< T, nSlots >::LFVector ( )
Version:
1.3.2

Definition at line 28 of file lfVector.ipp.

template<class T , int32_t nSlots>
lunchbox::LFVector< T, nSlots >::LFVector ( const size_t  n) [explicit]
Version:
1.3.2

Definition at line 35 of file lfVector.ipp.

References lunchbox::getIndexOfLastBit().

+ Here is the call graph for this function:

template<class T, int32_t nSlots>
lunchbox::LFVector< T, nSlots >::LFVector ( const size_t  n,
const T &  t 
) [explicit]
Version:
1.3.2

Definition at line 46 of file lfVector.ipp.

References lunchbox::getIndexOfLastBit().

+ Here is the call graph for this function:

template<class T, int32_t nSlots>
lunchbox::LFVector< T, nSlots >::LFVector ( const LFVector< T, nSlots > &  from) [explicit]
Version:
1.3.2

Definition at line 66 of file lfVector.ipp.

template<class T, int32_t nSlots>
template<int32_t fromSlots>
lunchbox::LFVector< T, nSlots >::LFVector ( const LFVector< T, fromSlots > &  from) [explicit]
Version:
1.3.2

Definition at line 75 of file lfVector.ipp.

template<class T , int32_t nSlots>
lunchbox::LFVector< T, nSlots >::~LFVector ( )
Version:
1.3.2

Definition at line 83 of file lfVector.ipp.


Member Function Documentation

template<class T , int32_t nSlots>
T & lunchbox::LFVector< T, nSlots >::back ( )
Version:
1.3.2

Definition at line 190 of file lfVector.ipp.

template<class T , int32_t nSlots>
LFVector< T, nSlots >::const_iterator lunchbox::LFVector< T, nSlots >::begin ( ) const [inline]
Version:
1.3.2

Definition at line 350 of file lfVector.ipp.

Referenced by lunchbox::LFVector< T, nSlots >::operator==().

+ Here is the caller graph for this function:

template<class T , int32_t nSlots>
LFVector< T, nSlots >::iterator lunchbox::LFVector< T, nSlots >::begin ( ) [inline]
Version:
1.3.2

Definition at line 362 of file lfVector.ipp.

template<class T , int32_t nSlots>
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.

Version:
1.3.2

Definition at line 277 of file lfVector.ipp.

template<class T, int32_t nSlots = 32>
bool lunchbox::LFVector< T, nSlots >::empty ( ) const [inline]
Version:
1.3.2

Definition at line 90 of file lfVector.h.

template<class T , int32_t nSlots>
LFVector< T, nSlots >::const_iterator lunchbox::LFVector< T, nSlots >::end ( ) const [inline]
Version:
1.3.2

Definition at line 356 of file lfVector.ipp.

template<class T , int32_t nSlots>
LFVector< T, nSlots >::iterator lunchbox::LFVector< T, nSlots >::end ( ) [inline]
Version:
1.3.2

Definition at line 368 of file lfVector.ipp.

template<class T, int32_t nSlots>
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.

Parameters:
posthe element to remove
Returns:
an iterator pointing to the element after the removed element, or end() if nothing was erased.
Version:
1.3.2

Definition at line 237 of file lfVector.ipp.

template<class T, int32_t nSlots>
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.

Parameters:
elementthe element to remove
Returns:
an iterator pointing to the element after the removed element, or end() if nothing was erased.
Version:
1.3.2

Definition at line 255 of file lfVector.ipp.

template<class T, int32_t nSlots>
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.

Parameters:
newSizethe minimum new size.
itemthe element to insert.
Version:
1.3.2

Definition at line 197 of file lfVector.ipp.

template<class T , int32_t nSlots>
T & lunchbox::LFVector< T, nSlots >::front ( )
Version:
1.3.2

Definition at line 183 of file lfVector.ipp.

template<class T , int32_t nSlots>
LFVector< T, nSlots >::ScopedWrite lunchbox::LFVector< T, nSlots >::getWriteLock ( )
Returns:
the locked mutex for unlocked write operations.
Version:
1.5

Definition at line 293 of file lfVector.ipp.

template<class T, int32_t nSlots = 32>
bool lunchbox::LFVector< T, nSlots >::operator!= ( const LFVector< T, nSlots > &  rhs) const [inline]
Version:
1.3.2

Definition at line 88 of file lfVector.h.

template<class T , int32_t nSlots>
LFVector< T, nSlots > & lunchbox::LFVector< T, nSlots >::operator= ( const LFVector< T, nSlots > &  from)
Version:
1.3.2

Definition at line 90 of file lfVector.ipp.

template<class T , int32_t nSlots>
bool lunchbox::LFVector< T, nSlots >::operator== ( const LFVector< T, nSlots > &  rhs) const
Version:
1.3.2

Definition at line 124 of file lfVector.ipp.

References lunchbox::LFVector< T, nSlots >::begin(), and lunchbox::LFVector< T, nSlots >::size().

+ Here is the call graph for this function:

template<class T , int32_t nSlots>
T & lunchbox::LFVector< T, nSlots >::operator[] ( size_t  i)
Version:
1.3.2

Definition at line 150 of file lfVector.ipp.

References lunchbox::getIndexOfLastBit().

+ Here is the call graph for this function:

template<class T , int32_t nSlots>
const T & lunchbox::LFVector< T, nSlots >::operator[] ( size_t  i) const
Version:
1.3.2

Definition at line 165 of file lfVector.ipp.

References lunchbox::getIndexOfLastBit().

+ Here is the call graph for this function:

template<class T , int32_t nSlots>
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().

Version:
1.3.2

Definition at line 212 of file lfVector.ipp.

template<class T, int32_t nSlots>
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.

Parameters:
elementthe item receiving the value which was stored at the end.
Returns:
true if the vector was not empty, false if no item was popped.
Version:
1.3.2

Definition at line 223 of file lfVector.ipp.

template<class T, int32_t nSlots>
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.

Parameters:
itemthe element to insert.
locktrue for internal lock, false if locked with getWriteLock()
Version:
1.3.2

Definition at line 205 of file lfVector.ipp.

template<class T, int32_t nSlots = 32>
size_t lunchbox::LFVector< T, nSlots >::size ( ) const [inline]
Version:
1.3.2

Definition at line 91 of file lfVector.h.

Referenced by lunchbox::LFVector< T, nSlots >::operator==().

+ Here is the caller graph for this function:


The documentation for this class was generated from the following files: