Lunchbox  1.8.0
lfVector.h
1 
2 /* Copyright (c) 2011-2013, EPFL/Blue Brain Project
3  * Stefan Eilemann <stefan.eilemann@epfl.ch>
4  *
5  * This file is part of Lunchbox <https://github.com/Eyescale/Lunchbox>
6  *
7  * This library is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Lesser General Public License version 3.0 as published
9  * by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #ifndef LUNCHBOX_LFVECTOR_H
22 #define LUNCHBOX_LFVECTOR_H
23 
24 #include <lunchbox/bitOperation.h> // used inline
25 #include <lunchbox/debug.h> // used inline
26 #include <lunchbox/os.h> // bzero()
27 #include <lunchbox/scopedMutex.h> // member
28 #include <lunchbox/serializable.h>
29 #include <lunchbox/spinLock.h> // member
30 #include <algorithm> // used inline
31 
32 namespace lunchbox
33 {
34 
50 template< class T, int32_t nSlots = 32 > class LFVector
51 {
52 public:
54  typedef T value_type;
55 
57  LFVector();
58 
60  explicit LFVector( const size_t n );
61 
63  explicit LFVector( const size_t n, const T& t );
64 
66  explicit LFVector( const LFVector& from );
67 
69  template< int32_t fromSlots >
70  explicit LFVector( const LFVector< T, fromSlots >& from );
71 
73  ~LFVector();
74 
76  LFVector& operator = ( const LFVector& from );
77 
79  bool operator == ( const LFVector& rhs ) const;
80 
82  bool operator != ( const LFVector& rhs ) const { return !(*this == rhs); }
83 
84  bool empty() const { return size_ == 0; }
85  size_t size() const { return size_; }
86 
88  T& operator[]( size_t i );
89 
91  const T& operator[]( size_t i ) const;
92 
94  T& front();
95 
97  T& back();
98 
101 
104 
105  const_iterator begin() const;
106  const_iterator end() const;
107  iterator begin();
108  iterator end();
109 
126  void expand( const size_t newSize, const T& item = T( ));
127 
140  void push_back( const T& item, bool lock = true );
141 
150  void pop_back();
151 
165  bool pop_back( T& element );
166 
181 
194  iterator erase( const T& element );
195 
205  void resize( const size_t size, const T& value = T( ));
206 
215  void clear();
216 
219 
220 private:
221  LB_SERIALIZABLE
222 
223  T* slots_[ nSlots ];
224  size_t size_;
225  mutable SpinLock lock_;
226 
227  template< int32_t fromSlots >
228  void assign_( const LFVector< T, fromSlots >& from );
229 
230  void push_back_unlocked_( const T& item );
231 
232  void trim_();
233 };
234 
236 template< class T >
237 std::ostream& operator << ( std::ostream& os, const LFVector< T >& v );
238 
239 }
240 
241 #include "lfVector.ipp" // template implementation
242 
243 #endif // LUNCHBOX_LFVECTOR_H