Wildmeshing Toolkit
Loading...
Searching...
No Matches
collector.hpp
1#pragma once
2
3#include <mutex>
4#include <vector>
5
6namespace wmtk::threading {
13template <typename T>
15{
16 std::vector<T> m_data;
17 mutable std::mutex m_mutex;
18
19public:
20 using value_type = T;
21 using iterator = typename std::vector<T>::iterator;
22 using const_iterator = typename std::vector<T>::const_iterator;
23 using reference = typename std::vector<T>::reference;
24 using const_reference = typename std::vector<T>::const_reference;
25
26 collector() = default;
27 explicit collector(std::size_t n)
28 : m_data(n)
29 {}
30 collector(std::size_t n, const T& value)
31 : m_data(n, value)
32 {}
33
34 collector(const collector& o) { m_data = o.m_data; }
35 collector& operator=(const collector& o)
36 {
37 m_data = o.m_data;
38 return *this;
39 }
40
46 void push_back(const T& v)
47 {
48 std::lock_guard<std::mutex> lock(m_mutex);
49 m_data.push_back(v);
50 }
51 void push_back(T&& v)
52 {
53 std::lock_guard<std::mutex> lock(m_mutex);
54 m_data.push_back(std::move(v));
55 }
56
62 template <typename... Args>
63 void emplace_back(Args&&... args)
64 {
65 std::lock_guard<std::mutex> lock(m_mutex);
66 m_data.emplace_back(std::forward<Args>(args)...);
67 }
68
74 void append(const std::vector<T>& v)
75 {
76 std::lock_guard<std::mutex> lock(m_mutex);
77 m_data.insert(m_data.end(), v.begin(), v.end());
78 }
79
85 void resize(std::size_t n)
86 {
87 std::lock_guard<std::mutex> lock(m_mutex);
88 m_data.resize(n);
89 }
90 void resize(std::size_t n, const T& value)
91 {
92 std::lock_guard<std::mutex> lock(m_mutex);
93 m_data.resize(n, value);
94 }
99 void clear()
100 {
101 std::lock_guard<std::mutex> lock(m_mutex);
102 m_data.clear();
103 }
109 void reserve(std::size_t n)
110 {
111 std::lock_guard<std::mutex> lock(m_mutex);
112 m_data.reserve(n);
113 }
114
120 const std::vector<T>& data() const { return m_data; }
121
128 const_reference operator[](std::size_t i) const { return m_data[i]; }
129 // reference operator[](std::size_t i) { return m_data[i]; }
130
136 std::size_t size() const { return m_data.size(); }
142 bool empty() const { return m_data.empty(); }
143
144 // iterator begin() { return m_data.begin(); }
145 // iterator end() { return m_data.end(); }
146 const_iterator begin() const { return m_data.begin(); }
147 const_iterator end() const { return m_data.end(); }
148};
149
150} // namespace wmtk::threading
Definition collector.hpp:15
void clear()
Clear the collector. This function is thread-safe.
Definition collector.hpp:99
bool empty() const
Check if the collector is empty. This function is NOT thread-safe.
Definition collector.hpp:142
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:128
const std::vector< T > & data() const
Get the underlying data vector. This function is NOT thread-safe.
Definition collector.hpp:120
void emplace_back(Args &&... args)
Emplace an element at the end of the collector. This function is thread-safe.
Definition collector.hpp:63
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:74
std::size_t size() const
Get the number of elements in the collector. This function is NOT thread-safe.
Definition collector.hpp:136
void reserve(std::size_t n)
Reserve space in the collector. This function is thread-safe.
Definition collector.hpp:109
void push_back(const T &v)
Push an element to the end of the collector. This function is thread-safe.
Definition collector.hpp:46
void resize(std::size_t n)
Resize the collector to contain n elements. This function is thread-safe.
Definition collector.hpp:85