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;
32 virtual void reset_from(size_t first) = 0;
33 virtual void clear() = 0;
34 virtual void rollback() = 0;
35 virtual void begin_protect() = 0;
36 virtual void end_protect() = 0;
40 virtual void append_recorded(std::vector<size_t>& out) {}
41};
42
43
44template <typename T>
46{
47 void move(size_t from, size_t to) override
48 {
49 if (from == to) return;
50 m_attributes[to] = std::move(m_attributes[from]);
51 }
52 // In the preallocated model this sets the storage capacity: it is called
53 // (single-threaded) at init / consolidation with the reserved size. It is
54 // grow-only so live data below `s` is never dropped. During operations the
55 // storage is never resized -- operations only fail when they run out of the
56 // preallocated slots.
57 void resize(size_t s) override
58 {
59 if (s > m_attributes.size()) m_attributes.resize(s);
60 }
61 void reset_from(size_t first) override
62 {
63 for (size_t i = first; i < m_attributes.size(); ++i) m_attributes[i] = T{};
64 }
65 void clear() override { m_attributes.clear(); }
66
71 void rollback() override
72 {
73 for (auto& [i, v] : m_rollback_list.local()) {
74 m_attributes[i] = std::move(v);
75 }
77 }
82 void begin_protect() override
83 {
84 m_rollback_list.local().clear();
85 recording.local() = true;
86 };
91 void end_protect() override
92 {
93 m_rollback_list.local().clear();
94 recording.local() = false;
95 }
96 void append_recorded(std::vector<size_t>& out) override
97 {
98 for (const auto& kv : m_rollback_list.local()) out.push_back(kv.first);
99 }
100
101 const T& at(size_t i) const { return m_attributes[i]; }
102
103 const T& operator[](size_t i) const { return at(i); }
104
105 T& operator[](size_t i)
106 {
107 if (recording.local()) {
108 m_rollback_list.local().emplace(i, m_attributes[i]);
109 }
110 return m_attributes[i];
111 }
112
113
114 size_t size() const { return m_attributes.size(); }
116 // Plain preallocated storage: never grows during operations.
117 std::vector<T> m_attributes;
119};
120
143{
144public:
145 void add(AbstractAttributeContainer* c) { m_children.push_back(c); }
146
147 void move(size_t from, size_t to) override
148 {
149 for (auto* c : m_children) c->move(from, to);
150 }
151 void resize(size_t s) override
152 {
153 for (auto* c : m_children) c->resize(s);
154 }
155 void reset_from(size_t first) override
156 {
157 for (auto* c : m_children) c->reset_from(first);
158 }
159 void clear() override
160 {
161 for (auto* c : m_children) c->clear();
162 }
163 void rollback() override
164 {
165 for (auto* c : m_children) c->rollback();
166 }
167 void begin_protect() override
168 {
169 for (auto* c : m_children) c->begin_protect();
170 }
171 void end_protect() override
172 {
173 for (auto* c : m_children) c->end_protect();
174 }
175 void append_recorded(std::vector<size_t>& out) override
176 {
177 for (auto* c : m_children) c->append_recorded(out);
178 }
179
180private:
181 std::vector<AbstractAttributeContainer*> m_children;
182};
183} // namespace wmtk
serving as buffers for attributes data that can be modified by operations
Definition AttributeCollection.hpp:22
virtual void append_recorded(std::vector< size_t > &out)
Definition AttributeCollection.hpp:40
virtual void reset_from(size_t first)=0
Several attribute collections for the same simplex type, behind one container.
Definition AttributeCollection.hpp:143
void reset_from(size_t first) override
Definition AttributeCollection.hpp:155
void append_recorded(std::vector< size_t > &out) override
Definition AttributeCollection.hpp:175
Definition enumerable_thread_specific.hpp:37
Definition AttributeCollection.hpp:46
void reset_from(size_t first) override
Definition AttributeCollection.hpp:61
void rollback() override
retrieve the protected attribute data on operation-fail
Definition AttributeCollection.hpp:71
void end_protect() override
clear local buffers and finish recording
Definition AttributeCollection.hpp:91
void append_recorded(std::vector< size_t > &out) override
Definition AttributeCollection.hpp:96
void begin_protect() override
clean local buffers for attribute, and start recording
Definition AttributeCollection.hpp:82