8namespace wmtk::threading {
18 std::vector<T> m_data;
19 mutable std::mutex m_mutex;
23 using iterator =
typename std::vector<T>::iterator;
24 using const_iterator =
typename std::vector<T>::const_iterator;
25 using reference =
typename std::vector<T>::reference;
26 using const_reference =
typename std::vector<T>::const_reference;
50 std::lock_guard<std::mutex> lock(m_mutex);
55 std::lock_guard<std::mutex> lock(m_mutex);
56 m_data.push_back(std::move(v));
64 template <
typename... Args>
67 std::lock_guard<std::mutex> lock(m_mutex);
68 m_data.emplace_back(std::forward<Args>(args)...);
78 std::lock_guard<std::mutex> lock(m_mutex);
79 m_data.insert(m_data.end(), v.begin(), v.end());
88 std::lock_guard<std::mutex> lock(m_mutex);
90 m_data = std::move(v);
94 std::make_move_iterator(v.begin()),
95 std::make_move_iterator(v.end()));
106 std::lock_guard<std::mutex> lock(m_mutex);
109 void resize(std::size_t n,
const T& value)
111 std::lock_guard<std::mutex> lock(m_mutex);
112 m_data.resize(n, value);
120 std::lock_guard<std::mutex> lock(m_mutex);
130 std::lock_guard<std::mutex> lock(m_mutex);
139 const std::vector<T>&
data()
const {
return m_data; }
146 std::vector<T>
take() {
return std::exchange(m_data, std::vector<T>()); }
154 const_reference
operator[](std::size_t i)
const {
return m_data[i]; }
162 std::size_t
size()
const {
return m_data.size(); }
168 bool empty()
const {
return m_data.empty(); }
172 const_iterator begin()
const {
return m_data.begin(); }
173 const_iterator end()
const {
return m_data.end(); }
Definition collector.hpp:17
void clear()
Clear the collector. This function is thread-safe.
Definition collector.hpp:118
std::vector< T > take()
Move the collected elements out, leaving the collector empty. This function is NOT thread-safe....
Definition collector.hpp:146
bool empty() const
Check if the collector is empty. This function is NOT thread-safe.
Definition collector.hpp:168
const_reference operator[](std::size_t i) const
operator[] is not thread-safe for concurrent writes, but is safe for concurrent reads after all write...
Definition collector.hpp:154
const std::vector< T > & data() const
Get the underlying data vector. This function is NOT thread-safe.
Definition collector.hpp:139
void emplace_back(Args &&... args)
Emplace an element at the end of the collector. This function is thread-safe.
Definition collector.hpp:65
void append(const std::vector< T > &v)
Append a vector of elements to the end of the collector. This function is thread-safe.
Definition collector.hpp:76
std::size_t size() const
Get the number of elements in the collector. This function is NOT thread-safe.
Definition collector.hpp:162
void reserve(std::size_t n)
Reserve space in the collector. This function is thread-safe.
Definition collector.hpp:128
void push_back(const T &v)
Push an element to the end of the collector. This function is thread-safe.
Definition collector.hpp:48
void resize(std::size_t n)
Resize the collector to contain n elements. This function is thread-safe.
Definition collector.hpp:104
void append(std::vector< T > &&v)
Append by moving. The first chunk into an empty collector is adopted outright, so a single-chunk (ser...
Definition collector.hpp:86