Lunchbox  1.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 {
52 template< class T, int32_t nSlots = 32 > class LFVector
53 {
54 public:
56  typedef T value_type;
57 
59  LFVector();
60 
62  explicit LFVector( const size_t n );
63 
65  LFVector( const size_t n, const T& t );
66 
68  explicit LFVector( const LFVector& from );
69 
71  template< int32_t fromSlots >
72  explicit LFVector( const LFVector< T, fromSlots >& from );
73 
75  ~LFVector();
76 
78  LFVector& operator = ( const LFVector& from );
79 
81  bool operator == ( const LFVector& rhs ) const;
82 
84  bool operator != ( const LFVector& rhs ) const { return !(*this == rhs); }
85 
86  bool empty() const { return size_ == 0; }
87  size_t size() const { return size_; }
88 
90  T& operator[]( size_t i );
91 
93  const T& operator[]( size_t i ) const;
94 
96  T& front();
97 
99  T& back();
100 
103 
106 
107  const_iterator begin() const;
108  const_iterator end() const;
109  iterator begin();
110  iterator end();
111 
128  void expand( const size_t newSize, const T& item = T( ));
129 
142  void push_back( const T& item, bool lock = true );
143 
152  void pop_back();
153 
167  bool pop_back( T& element );
168 
183 
196  iterator erase( const T& element );
197 
207  void resize( const size_t size, const T& value = T( ));
208 
217  void clear();
218 
221 
222 private:
223  LB_SERIALIZABLE
224 
225  T* slots_[ nSlots ];
226  size_t size_;
227  mutable SpinLock lock_;
228 
229  template< int32_t fromSlots >
230  void assign_( const LFVector< T, fromSlots >& from );
231 
232  void push_back_unlocked_( const T& item );
233 
234  void trim_();
235 };
236 
238 template< class T >
239 std::ostream& operator << ( std::ostream& os, const LFVector< T >& v );
240 
241 }
242 
243 #include "lfVector.ipp" // template implementation
244 
245 #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:86
size_t size() const
Definition: lfVector.h:87
STL-like vector implementation providing certain thread-safety guarantees.
Definition: lfVector.h:52
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:105
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:376
bool operator!=(const LFVector &rhs) const
Definition: lfVector.h:84
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:370
LFVectorIterator< LFVector< T, nSlots >, T > iterator
Iterator over the vector elements.
Definition: lfVector.h:102
iterator erase(typename LFVector< T, nSlots >::iterator pos)
Remove an element.
Definition: lfVector.ipp:240