Lunchbox  1.6.0
lfVector.h
00001 
00002 /* Copyright (c) 2011-2012, EPFL/Blue Brain Project
00003  *                          Stefan Eilemann <stefan.eilemann@epfl.ch>
00004  *
00005  * This file is part of Lunchbox <https://github.com/Eyescale/Lunchbox>
00006  *
00007  * This library is free software; you can redistribute it and/or modify it under
00008  * the terms of the GNU Lesser General Public License version 3.0 as published
00009  * by the Free Software Foundation.
00010  *
00011  * This library is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00014  * details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation, Inc.,
00018  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 
00021 #ifndef LUNCHBOX_LFVECTOR_H
00022 #define LUNCHBOX_LFVECTOR_H
00023 
00024 #include <lunchbox/bitOperation.h> // used inline
00025 #include <lunchbox/debug.h> // used inline
00026 #include <lunchbox/scopedMutex.h> // member
00027 #include <lunchbox/serializable.h>
00028 #include <lunchbox/spinLock.h> // member
00029 #include <algorithm> // used inline
00030 
00031 
00032 #ifdef _WIN32
00033 #  define lb_bzero( ptr, size ) memset( ptr, 0, size )
00034 #else
00035 #  include <strings.h>
00036 #  define lb_bzero bzero
00037 #endif
00038 
00039 namespace lunchbox
00040 {
00041 
00057 template< class T, int32_t nSlots = 32 > class LFVector
00058 {
00059 public:
00060     typedef ScopedFastWrite ScopedWrite;
00061 
00063     LFVector();
00064 
00066     explicit LFVector( const size_t n );
00067 
00069     explicit LFVector( const size_t n, const T& t );
00070 
00072     explicit LFVector( const LFVector& from );
00073 
00075     template< int32_t fromSlots >
00076     explicit LFVector( const LFVector< T, fromSlots >& from );
00077 
00079     ~LFVector();
00080 
00082     LFVector& operator = ( const LFVector& from );
00083 
00085     bool operator == ( const LFVector& rhs ) const;
00086 
00088     bool operator != ( const LFVector& rhs ) const { return !(*this == rhs); }
00089 
00090     bool empty() const { return size_ == 0; } 
00091     size_t size() const { return size_; } 
00092 
00094     T& operator[]( size_t i );
00095 
00097     const T& operator[]( size_t i ) const;
00098 
00100     T& front();
00101 
00103     T& back();
00104 
00106     typedef LFVectorIterator< LFVector< T >, T > iterator;
00107 
00109     typedef LFVectorIterator< const LFVector< T >, const T > const_iterator;
00110 
00111     const_iterator begin() const; 
00112     const_iterator end() const; 
00113     iterator begin(); 
00114     iterator end(); 
00115 
00132     void expand( const size_t newSize, const T& item = T( ));
00133 
00146     void push_back( const T& item, bool lock = true );
00147 
00156     void pop_back();
00157 
00171     bool pop_back( T& element );
00172 
00186     iterator erase( typename LFVector< T, nSlots >::iterator pos );
00187 
00200     iterator erase( const T& element );
00201 
00210     void clear();
00211 
00213     ScopedWrite getWriteLock();
00214 
00215 private:
00216     LB_SERIALIZABLE
00217 
00218     T* slots_[ nSlots ];
00219     size_t size_;
00220     mutable SpinLock lock_;
00221 
00222     template< int32_t fromSlots >
00223     void assign_( const LFVector< T, fromSlots >& from );
00224 
00225     void push_back_unlocked_( const T& item );
00226 
00227     void trim_();
00228 };
00229 
00231 template< class T >
00232 std::ostream& operator << ( std::ostream& os, const LFVector< T >& v );
00233 
00234 }
00235 
00236 #include "lfVector.ipp" // template implementation
00237 
00238 #endif // LUNCHBOX_LFVECTOR_H