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 <wmtk/threading/enumerable_thread_specific.hpp>
7
8#include <algorithm>
9#include <array>
10#include <cassert>
11#include <map>
12#include <optional>
13#include <unordered_map>
14#include <vector>
15
16namespace wmtk {
22{
23public:
24 virtual ~AbstractAttributeContainer() = default;
25 virtual void move(size_t from, size_t to) {};
26 virtual void resize(size_t) = 0;
27 virtual void clear() = 0;
28 virtual void rollback() = 0;
29 virtual void begin_protect() = 0;
30 virtual void end_protect() = 0;
31};
32
33
34template <typename T>
36{
37 void move(size_t from, size_t to) override
38 {
39 if (from == to) return;
40 m_attributes[to] = std::move(m_attributes[from]);
41 }
42 // In the preallocated model this sets the storage capacity: it is called
43 // (single-threaded) at init / consolidation with the reserved size. It is
44 // grow-only so live data below `s` is never dropped. During operations the
45 // storage is never resized -- operations only fail when they run out of the
46 // preallocated slots.
47 void resize(size_t s) override
48 {
49 if (s > m_attributes.size()) m_attributes.resize(s);
50 }
51 void clear() override { m_attributes.clear(); }
52
53 bool assign(size_t to, T&& val) // always use this in OP_after
54 {
55 m_attributes[to] = val;
56 if (recording.local()) m_rollback_list.local()[to] = val;
57 // TODO: are locks necessary? not now.
58 return true;
59 }
64 void rollback() override
65 {
66 for (auto& [i, v] : m_rollback_list.local()) {
67 m_attributes[i] = std::move(v);
68 }
70 }
75 void begin_protect() override
76 {
77 m_rollback_list.local().clear();
78 recording.local() = true;
79 };
84 void end_protect() override
85 {
86 m_rollback_list.local().clear();
87 recording.local() = false;
88 }
89
90 const T& at(size_t i) const { return m_attributes[i]; }
91
92 const T& operator[](size_t i) const { return at(i); }
93
94 T& operator[](size_t i)
95 {
96 if (recording.local()) {
97 m_rollback_list.local().emplace(i, m_attributes[i]);
98 }
99 return m_attributes[i];
100 }
101
102
103 size_t size() const { return m_attributes.size(); }
105 // Plain preallocated storage: never grows during operations.
106 std::vector<T> m_attributes;
108};
109
132{
133public:
134 void add(AbstractAttributeContainer* c) { m_children.push_back(c); }
135
136 void move(size_t from, size_t to) override
137 {
138 for (auto* c : m_children) c->move(from, to);
139 }
140 void resize(size_t s) override
141 {
142 for (auto* c : m_children) c->resize(s);
143 }
144 void clear() override
145 {
146 for (auto* c : m_children) c->clear();
147 }
148 void rollback() override
149 {
150 for (auto* c : m_children) c->rollback();
151 }
152 void begin_protect() override
153 {
154 for (auto* c : m_children) c->begin_protect();
155 }
156 void end_protect() override
157 {
158 for (auto* c : m_children) c->end_protect();
159 }
160
161private:
162 std::vector<AbstractAttributeContainer*> m_children;
163};
164} // namespace wmtk
serving as buffers for attributes data that can be modified by operations
Definition AttributeCollection.hpp:22
Several attribute collections for the same simplex type, behind one container.
Definition AttributeCollection.hpp:132
Definition enumerable_thread_specific.hpp:26
Definition AttributeCollection.hpp:36
void rollback() override
retrieve the protected attribute data on operation-fail
Definition AttributeCollection.hpp:64
void end_protect() override
clear local buffers and finish recording
Definition AttributeCollection.hpp:84
void begin_protect() override
clean local buffers for attribute, and start recording
Definition AttributeCollection.hpp:75