22 #include "lfVectorIterator.h"
27 template<
class T,
int32_t nSlots >
31 setZero( slots_, nSlots *
sizeof( T* ));
34 template<
class T,
int32_t nSlots >
39 setZero( slots_, nSlots *
sizeof( T* ));
41 for( int32_t i = 0; i <= s; ++i )
42 slots_[ i ] =
new T[ 1<<i ];
45 template<
class T,
int32_t nSlots >
50 setZero( slots_, nSlots *
sizeof( T* ));
52 for( int32_t i = 0; i <= s; ++i )
54 const size_t sz = 1<<i;
55 slots_[ i ] =
new T[ sz ];
56 for(
size_t j = 0; size_ < n && j < sz ; ++j )
62 LBASSERTINFO( size_ == n, size_ <<
" != " << n );
65 template<
class T,
int32_t nSlots >
73 template<
class T,
int32_t nSlots >
74 template<
int32_t fromSlots >
82 template<
class T,
int32_t nSlots >
85 for(
size_t i=0; i < nSlots; ++i )
98 for( int32_t i = 0; i < nSlots; ++i )
102 const size_t sz = 1<<i;
104 slots_[ i ] =
new T[ sz ];
106 for(
size_t j = 0; size_ < from.size_ && j < sz ; ++j )
108 slots_[ i ][ j ] = from.slots_[ i ][ j ];
112 else if( slots_[ i ] )
114 delete [] slots_[ i ];
119 LBASSERTINFO( size_ == from.size_, size_ <<
" != " << from.size_ );
123 template<
class T,
int32_t nSlots >
129 if( size() != rhs.
size() )
134 for( ; it != end() && rhsIt != end(); ++it, ++rhsIt )
143 #ifdef LB_GCC_4_6_OR_LATER // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51721
144 # ifndef LB_GCC_4_8 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56824
145 # pragma GCC diagnostic push
147 # pragma GCC diagnostic ignored "-Warray-bounds"
150 template<
class T,
int32_t nSlots >
154 LBASSERTINFO( size_ >= i, size_ <<
" < " << i );
157 const size_t index = i ^ ( size_t( 1 )<<slot );
159 LBASSERTINFO( slot >=0 && slot < nSlots, slot );
160 LBASSERT( slots_[ slot ] );
161 LBASSERT( index < (1ull<<slot) );
162 return slots_[ slot ][ index ];
165 template<
class T,
int32_t nSlots >
168 LBASSERTINFO( size_ > i, size_ <<
" <= " << i );
171 const size_t index = i ^ ( size_t( 1 )<<slot );
173 LBASSERTINFO( slot >=0 && slot < nSlots, slot );
174 LBASSERT( slots_[ slot ] );
175 LBASSERT( index < (1ull<<slot) );
176 return slots_[ slot ][ index ];
179 #ifdef LB_GCC_4_6_OR_LATER
181 # pragma GCC diagnostic pop
185 template<
class T,
int32_t nSlots >
188 LBASSERT( !empty( ));
189 return slots_[ 0 ][ 0 ];
192 template<
class T,
int32_t nSlots >
195 LBASSERT( !empty( ));
196 return (*
this)[ size_ - 1 ];
199 template<
class T,
int32_t nSlots >
203 while( newSize > size( ))
204 push_back_unlocked_( item );
207 template<
class T,
int32_t nSlots >
211 push_back_unlocked_( item );
214 template<
class T,
int32_t nSlots >
221 (*this)[size_] = T();
225 template<
class T,
int32_t nSlots >
234 (*this)[size_] = T();
242 LBASSERT( pos.container_ ==
this );
243 if( pos.container_ !=
this || pos.i_ >= size_ )
248 #pragma warning (push)
249 #pragma warning (disable: 4996) // unchecked iterators
250 std::copy( pos+1, end()+1, pos );
251 #pragma warning (pop)
252 (*this)[size_] = T();
261 for(
size_t i = size_; i != 0 ; --i )
263 if( (*
this)[i-1] == element )
267 #pragma warning (push)
268 #pragma warning (disable: 4996) // unchecked iterators
269 std::copy( pos+1, end()+1, pos );
270 #pragma warning (pop)
279 template<
class T,
int32_t nSlots >
283 while( size_ > newSize )
286 (*this)[size_] = T();
290 while( size_ < newSize )
291 push_back_unlocked_( value );
294 template<
class T,
int32_t nSlots >
301 (*this)[size_] = T();
303 for( int32_t i = 0; i < nSlots; ++i )
305 delete [] slots_[ i ];
316 template<
class T,
int32_t nSlots >
317 template<
int32_t fromSlots >
320 setZero( slots_, nSlots *
sizeof( T* ));
323 for( int32_t i = 0; i < nSlots; ++i )
325 if( i >= fromSlots || !from.slots_[i] )
327 LBASSERTINFO( size_ == from.size_,
328 size_ <<
" != " << from.size_ );
332 const size_t sz = 1<<i;
333 slots_[ i ] =
new T[ sz ];
334 for(
size_t j = 0; size_ < from.size_ && j < sz ; ++j )
336 slots_[ i ][ j ] = from.slots_[ i ][ j ];
342 template<
class T,
int32_t nSlots >
343 void LFVector< T, nSlots >::push_back_unlocked_(
const T& item )
345 const size_t i = size_ + 1;
347 if( slot < 0 || slot >= nSlots )
349 LBASSERTINFO( slot >= 0 && slot < nSlots, slot );
350 LBTHROW( std::runtime_error(
"LFVector full" ));
353 const size_t sz = ( size_t( 1 ) << slot );
355 if( !slots_[ slot ] )
356 slots_[ slot ] =
new T[ sz ];
358 const ssize_t index = i ^ sz;
359 slots_[ slot ][ index ] = item;
363 template<
class T,
int32_t nSlots >
364 void LFVector< T, nSlots >::trim_()
367 if( nextSlot < nSlots && slots_[ nextSlot ] )
369 delete [] slots_[ nextSlot ];
370 slots_[ nextSlot ] = 0;
374 template<
class T,
int32_t nSlots >
inline typename
380 template<
class T,
int32_t nSlots >
inline typename
386 template<
class T,
int32_t nSlots >
inline typename
392 template<
class T,
int32_t nSlots >
inline typename
399 template<
class T,
int32_t nSlots >
template<
class Archive >
401 const unsigned int )
const
404 for(
size_t i = 0; i < size_; ++i )
408 template<
class T,
int32_t nSlots >
template<
class Archive >
409 inline void LFVector< T, nSlots >::load( Archive& ar,
415 LBASSERT( size_ == newSize );
417 for(
size_t i = 0; i < size_; ++i )
423 std::ostream& operator << ( std::ostream& os, const LFVector< T >& v )
425 os <<
className( &v ) <<
" size " << v.size() <<
" [ ";
428 if( i.getPosition() > 255 )
435 return os <<
']' << std::endl;
An iterator for LFVector.
STL-like vector implementation providing certain thread-safety guarantees.
std::string className(const T *object)
Print the RTTI name of the given class.
int32_t getIndexOfLastBit(T value)
LFVector & operator=(const LFVector &from)
void push_back(const T &item, bool lock=true)
Add an element to the vector.
ScopedWrite getWriteLock()
bool operator==(const LFVector &rhs) const
void pop_back()
Remove the last element (STL version).
void clear()
Clear the vector and all storage.
const_iterator end() const
void resize(const size_t size, const T &value=T())
Resize the vector.
ScopedMutex< Lock, WriteOp > ScopedWrite
A scoped mutex for a write operation.
void expand(const size_t newSize, const T &item=T())
Resize the vector to at least the given size.
const_iterator begin() const
#define LBTHROW(exc)
Log a std::exception if topic LOG_EXCEPTION is set before throwing exception.
iterator erase(typename LFVector< T, nSlots >::iterator pos)
Remove an element.