Lunchbox  1.12.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
lfVector.h
1 
2 /* Copyright (c) 2011-2015, 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 #include <stdexcept>
32 
33 namespace lunchbox
34 {
53 template< class T, int32_t nSlots = 32 > class LFVector
54 {
55 public:
57  typedef T value_type;
58 
60  LFVector();
61 
63  explicit LFVector( const size_t n );
64 
66  LFVector( const size_t n, const T& t );
67 
69  explicit LFVector( const LFVector& from );
70 
72  template< int32_t fromSlots >
73  explicit LFVector( const LFVector< T, fromSlots >& from );
74 
76  ~LFVector();
77 
79  LFVector& operator = ( const LFVector& from );
80 
82  bool operator == ( const LFVector& rhs ) const;
83 
85  bool operator != ( const LFVector& rhs ) const { return !(*this == rhs); }
86 
87  bool empty() const { return size_ == 0; }
88  size_t size() const { return size_; }
89 
91  T& operator[]( size_t i );
92 
94  const T& operator[]( size_t i ) const;
95 
97  T& front();
98 
100  T& back();
101 
104 
107 
108  const_iterator begin() const;
109  const_iterator end() const;
110  iterator begin();
111  iterator end();
112 
130  void expand( const size_t newSize, const T& item = T( ));
131 
145  void push_back( const T& item, bool lock = true );
146 
155  void pop_back();
156 
170  bool pop_back( T& element );
171 
185  iterator erase( typename LFVector< T, nSlots >::iterator pos );
186 
199  iterator erase( const T& element );
200 
211  void resize( const size_t size, const T& value = T( ));
212 
221  void clear();
222 
224  ScopedWrite getWriteLock();
225 
226 private:
227  LB_SERIALIZABLE
228 
229  T* slots_[ nSlots ];
230  size_t size_;
231  mutable SpinLock lock_;
232 
233  template< int32_t fromSlots >
234  void assign_( const LFVector< T, fromSlots >& from );
235 
236  void push_back_unlocked_( const T& item );
237 
238  void trim_();
239 };
240 
242 template< class T >
243 std::ostream& operator << ( std::ostream& os, const LFVector< T >& v );
244 
245 }
246 
247 #include "lfVector.ipp" // template implementation
248 
249 #endif // LUNCHBOX_LFVECTOR_H
An iterator for LFVector.
A fast lock for uncontended memory access.
Definition: spinLock.h:38
bool empty() const
Definition: lfVector.h:87
size_t size() const
Definition: lfVector.h:88
STL-like vector implementation providing certain thread-safety guarantees.
Definition: lfVector.h:53
LFVector & operator=(const LFVector &from)
Definition: lfVector.ipp:90
T & operator[](size_t i)
Definition: lfVector.ipp:151
void push_back(const T &item, bool lock=true)
Add an element to the vector.
Definition: lfVector.ipp:208
ScopedWrite getWriteLock()
Definition: lfVector.ipp:311
LFVectorIterator< const LFVector< T, nSlots >, const T > const_iterator
Iterator over the const vector elements.
Definition: lfVector.h:106
bool operator==(const LFVector &rhs) const
Definition: lfVector.ipp:124
void pop_back()
Remove the last element (STL version).
Definition: lfVector.ipp:215
void clear()
Clear the vector and all storage.
Definition: lfVector.ipp:295
const_iterator end() const
Definition: lfVector.ipp:381
bool operator!=(const LFVector &rhs) const
Definition: lfVector.h:85
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
void resize(const size_t size, const T &value=T())
Resize the vector.
Definition: lfVector.ipp:280
void expand(const size_t newSize, const T &item=T())
Resize the vector to at least the given size.
Definition: lfVector.ipp:200
A scoped mutex.
Definition: scopedMutex.h:59
const_iterator begin() const
Definition: lfVector.ipp:375
LFVectorIterator< LFVector< T, nSlots >, T > iterator
Iterator over the vector elements.
Definition: lfVector.h:103
iterator erase(typename LFVector< T, nSlots >::iterator pos)
Remove an element.
Definition: lfVector.ipp:240