Wildmeshing Toolkit
Loading...
Searching...
No Matches
collector.hpp
1#pragma once
2
3#include <iterator>
4#include <mutex>
5#include <utility>
6#include <vector>
7
8namespace wmtk::threading {
15template <typename T>
17{
18 std::vector<T> m_data;
19 mutable std::mutex m_mutex;
20
21public:
22 using value_type = T;
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;
27
28 collector() = default;
29 explicit collector(std::size_t n)
30 : m_data(n)
31 {}
32 collector(std::size_t n, const T& value)
33 : m_data(n, value)
34 {}
35
36 collector(const collector& o) { m_data = o.m_data; }
37 collector& operator=(const collector& o)
38 {
39 m_data = o.m_data;
40 return *this;
41 }
42
48 void push_back(const T& v)
49 {
50 std::lock_guard<std::mutex> lock(m_mutex);
51 m_data.push_back(v);
52 }
53 void push_back(T&& v)
54 {
55 std::lock_guard<std::mutex> lock(m_mutex);
56 m_data.push_back(std::move(v));
57 }
58
64 template <typename... Args>
65 void emplace_back(Args&&... args)
66 {
67 std::lock_guard<std::mutex> lock(m_mutex);
68 m_data.emplace_back(std::forward<Args>(args)...);
69 }
70
76 void append(const std::vector<T>& v)
77 {
78 std::lock_guard<std::mutex> lock(m_mutex);
79 m_data.insert(m_data.end(), v.begin(), v.end());
80 }
86 void append(std::vector<T>&& v)
87 {
88 std::lock_guard<std::mutex> lock(m_mutex);
89 if (m_data.empty()) {
90 m_data = std::move(v);
91 } else {
92 m_data.insert(
93 m_data.end(),
94 std::make_move_iterator(v.begin()),
95 std::make_move_iterator(v.end()));
96 }
97 }
98
104 void resize(std::size_t n)
105 {
106 std::lock_guard<std::mutex> lock(m_mutex);
107 m_data.resize(n);
108 }
109 void resize(std::size_t n, const T& value)
110 {
111 std::lock_guard<std::mutex> lock(m_mutex);
112 m_data.resize(n, value);
113 }
118 void clear()
119 {
120 std::lock_guard<std::mutex> lock(m_mutex);
121 m_data.clear();
122 }
128 void reserve(std::size_t n)
129 {
130 std::lock_guard<std::mutex> lock(m_mutex);
131 m_data.reserve(n);
132 }
133
139 const std::vector<T>& data() const { return m_data; }
140
146 std::vector<T> take() { return std::exchange(m_data, std::vector<T>()); }
147
154 const_reference operator[](std::size_t i) const { return m_data[i]; }
155 // reference operator[](std::size_t i) { return m_data[i]; }
156
162 std::size_t size() const { return m_data.size(); }
168 bool empty() const { return m_data.empty(); }
169
170 // iterator begin() { return m_data.begin(); }
171 // iterator end() { return m_data.end(); }
172 const_iterator begin() const { return m_data.begin(); }
173 const_iterator end() const { return m_data.end(); }
174};
175
176} // namespace wmtk::threading
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