Lunchbox  1.17.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 <algorithm> // used inline
25 #include <lunchbox/bitOperation.h> // used inline
26 #include <lunchbox/debug.h> // used inline
27 #include <lunchbox/os.h> // bzero()
28 #include <lunchbox/scopedMutex.h> // member
29 #include <lunchbox/serializable.h>
30 #include <lunchbox/spinLock.h> // member
31 #include <stdexcept>
32 
33 namespace lunchbox
34 {
53 template <class T, int32_t nSlots = 32>
54 class LFVector
55 {
56 public:
57  using ScopedWrite = std::unique_lock<SpinLock>;
58  typedef T value_type;
59 
61  LFVector();
62 
64  explicit LFVector(const size_t n);
65 
67  LFVector(const size_t n, const T& t);
68 
70  explicit LFVector(const LFVector& from);
71 
73  template <int32_t fromSlots>
74  explicit LFVector(const LFVector<T, fromSlots>& from);
75 
77  ~LFVector();
78 
80  LFVector& operator=(const LFVector& from);
81 
83  bool operator==(const LFVector& rhs) const;
84 
86  bool operator!=(const LFVector& rhs) const { return !(*this == rhs); }
87  bool empty() const { return size_ == 0; }
88  size_t size() const { return size_; }
89 
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 
129  void expand(const size_t newSize, const T& item = T());
130 
144  void push_back(const T& item, bool lock = true);
145 
154  void pop_back();
155 
169  bool pop_back(T& element);
170 
184  iterator erase(typename LFVector<T, nSlots>::iterator pos);
185 
198  iterator erase(const T& element);
199 
210  void resize(const size_t size, const T& value = T());
211 
220  void clear();
221 
223  ScopedWrite getWriteLock();
224 
225 private:
226  LB_SERIALIZABLE
227 
228  T* slots_[nSlots];
229  size_t size_;
230  mutable SpinLock lock_;
231 
232  template <int32_t fromSlots>
233  void assign_(const LFVector<T, fromSlots>& from);
234 
235  void push_back_unlocked_(const T& item);
236 
237  void trim_();
238 };
239 
241 template <class T>
242 std::ostream& operator<<(std::ostream& os, const LFVector<T>& v);
243 }
244 
245 #include "lfVector.ipp" // template implementation
246 
247 #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:54
LFVector & operator=(const LFVector &from)
Definition: lfVector.ipp:89
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:318
LFVectorIterator< LFVector< T, nSlots >, T > iterator
Iterator over the vector elements.
Definition: lfVector.h:102
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:220
void clear()
Clear the vector and all storage.
Definition: lfVector.ipp:302
const_iterator end() const
Definition: lfVector.ipp:389
bool operator!=(const LFVector &rhs) const
Definition: lfVector.h:86
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
void resize(const size_t size, const T &value=T())
Resize the vector.
Definition: lfVector.ipp:287
void expand(const size_t newSize, const T &item=T())
Resize the vector to at least the given size.
Definition: lfVector.ipp:200
const_iterator begin() const
Definition: lfVector.ipp:382
iterator erase(typename LFVector< T, nSlots >::iterator pos)
Remove an element.
Definition: lfVector.ipp:245