Wildmeshing Toolkit
Loading...
Searching...
No Matches
AttributeCollection.hpp
1#pragma once
2
3#include <wmtk/utils/VectorUtils.h>
4#include <wmtk/utils/Logger.hpp>
5
6#include <tbb/concurrent_vector.h>
7#include <tbb/enumerable_thread_specific.h>
8
9#include <algorithm>
10#include <array>
11#include <cassert>
12#include <map>
13#include <optional>
14#include <unordered_map>
15#include <vector>
16
17namespace wmtk {
23{
24public:
25 virtual ~AbstractAttributeContainer() = default;
26 virtual void move(size_t from, size_t to) {};
27 virtual void resize(size_t) = 0;
28 virtual void clear() = 0;
29 virtual void rollback() = 0;
30 virtual void begin_protect() = 0;
31 virtual void end_protect() = 0;
32};
33
34
35template <typename T>
37{
38 void move(size_t from, size_t to) override
39 {
40 if (from == to) return;
41 m_attributes[to] = std::move(m_attributes[from]);
42 }
43 void resize(size_t s) override
44 {
45 m_attributes.grow_to_at_least(s);
46 // if (m_attributes.size() > s) {
47 // m_attributes.resize(s);
48 // m_attributes.shrink_to_fit();
49 // }
50 // TODO: in Concurrent, vertex partition id, vertex mutex should be part of attribute
51 }
52 void clear() override { m_attributes.clear(); }
53
54 bool assign(size_t to, T&& val) // always use this in OP_after
55 {
56 m_attributes[to] = val;
57 if (recording.local()) m_rollback_list.local()[to] = val;
58 // TODO: are locks necessary? not now.
59 return true;
60 }
65 void rollback() override
66 {
67 for (auto& [i, v] : m_rollback_list.local()) {
68 m_attributes[i] = std::move(v);
69 }
71 }
76 void begin_protect() override
77 {
78 m_rollback_list.local().clear();
79 recording.local() = true;
80 };
85 void end_protect() override
86 {
87 m_rollback_list.local().clear();
88 recording.local() = false;
89 }
90
91 const T& at(size_t i) const { return m_attributes[i]; }
92
93 const T& operator[](size_t i) const { return at(i); }
94
95 T& operator[](size_t i)
96 {
97 if (recording.local()) {
98 m_rollback_list.local().emplace(i, m_attributes[i]);
99 }
100 return m_attributes[i];
101 }
102
103
104 size_t size() const { return m_attributes.size(); }
105 tbb::enumerable_thread_specific<std::unordered_map<size_t, T>> m_rollback_list;
106 // experimenting with tbb, could be templated as well.
107 tbb::concurrent_vector<T> m_attributes;
108 tbb::enumerable_thread_specific<bool> recording{false};
109};
110} // namespace wmtk
serving as buffers for attributes data that can be modified by operations
Definition AttributeCollection.hpp:23
Definition AttributeCollection.hpp:37
void rollback() override
retrieve the protected attribute data on operation-fail
Definition AttributeCollection.hpp:65
void end_protect() override
clear local buffers and finish recording
Definition AttributeCollection.hpp:85
void begin_protect() override
clean local buffers for attribute, and start recording
Definition AttributeCollection.hpp:76