Wildmeshing Toolkit
AttributeTransactionStack.hxx
Go to the documentation of this file.
1 
2 #include <wmtk/Types.hpp>
6 
7 
9 
10 
11 template <typename T>
13  : m_buffer(64)
14  , m_indices(32)
15  , m_buffer_end(0)
16  , m_indices_end(0)
17 {}
18 template <typename T>
20 
21 
22 template <typename T>
24 {
25  // m_indices_end = m_transaction_starts.back();
26  // m_buffer_end = m_indices[m_indices_end].second;
27 }
28 
29 template <typename T>
31 {
32  // check sizes
33  if (m_indices_end + 1 >= m_indices.size()) {
34  assert(m_indices.size() > 2);
35  m_indices.resize(m_indices.size() * 1.75);
36  }
37 
38  if (m_buffer_end + data_size >= m_buffer.size()) {
39  assert(m_buffer.size() > 2);
40  m_buffer.resize(m_buffer.size() * 1.75);
41  }
42 }
43 
44 template <typename T>
45 template <typename Derived>
47  int64_t index,
48  const Eigen::MatrixBase<Derived>& value)
49 {
50  // basically try_emplace but optimizes to avoid accessing the pointed-to value
51 
52  size_t dim = value.size();
53 
54 
55  update_buffer_sizes_for_add(dim);
56 
57  // assert(old_size % dim == 0);
58  //assert(old_size / value.size() == m_indices.size());
59  // m_indices.emplace_back(index, m_indices.size());
60  m_indices[m_indices_end] = {index, m_buffer_end};
61 
62 
63  assert(m_buffer.size() >= m_buffer_end + dim);
64  // m_buffer.resize(m_buffer_end + dim);
65  std::copy(value.begin(), value.end(), m_buffer.begin() + m_buffer_end);
66  m_buffer_end += dim;
67  m_indices_end++;
68  assert(m_buffer_end == m_indices_end * dim);
69 }
70 
71 template <typename T>
72 inline void AttributeTransactionStack<T>::try_caching(int64_t index, const T& value)
73 {
74  update_buffer_sizes_for_add(1);
75  // assert(m_buffer.size() == m_indices.size());
76  m_indices[m_indices_end] = {index, m_buffer_end};
77  m_buffer[m_buffer_end] = value;
78 
79  m_buffer_end++;
80  m_indices_end++;
81  assert(m_buffer_end == m_indices_end);
82 }
83 
84 
85 template <typename T>
87 {
88  apply_to(attribute, attribute.m_data);
89 }
90 
91 template <typename T>
93  const Attribute<T>& attribute,
94  std::vector<T>& other) const
95 {
96  assert(at_current_scope());
97  for (auto it = final_transaction_rbegin();
98  it != transaction_start_rend(current_transaction_index() - 1);
99  ++it) {
100  const auto& [global, local] = *it;
101  auto a = attribute.vector_attribute(global, other);
102  auto b = attribute.const_vector_attribute_from_start(local, m_buffer);
103  a = b;
104  }
105 }
106 
107 template <typename T>
108 inline auto AttributeTransactionStack<T>::get_value(int64_t index) const -> const T*
109 {
110  for (auto it = transaction_start_begin(current_transaction_index());
111  it != final_transaction_end();
112  ++it) {
113  const auto& [global_index, local_index] = *it;
114  if (global_index == index) {
115  const T* ptr = m_buffer.data() + local_index;
116  return ptr;
117  }
118  }
119  return nullptr;
120 }
121 
122 template <typename T>
124 {
125  assert(at_current_scope()); // must only be called on leaf node
126 
127  m_transaction_starts.emplace_back(m_indices_end);
128  change_to_current_scope();
129 }
130 template <typename T>
131 inline void AttributeTransactionStack<T>::pop(Attribute<T>& attribute, bool preserve_changes)
132 {
133  assert(at_current_scope()); // must only be called on leaf node
134  // TODO consider re-enabling
135  if (preserve_changes) {
136  clear();
137  } else {
138  apply_to(attribute);
139  }
140 
141 
142  m_transaction_starts.pop_back();
143 
144  change_to_current_scope();
145  if (empty()) {
146  m_indices_end = 0;
147  m_buffer_end = 0;
148  }
149 }
150 
151 
152 template <typename T>
154 {
155  return m_transaction_starts.empty();
156 }
157 
158 
159 template <typename T>
161 {
162  assert(at_current_scope());
163  assert(!empty());
164  apply_to(attr);
165 }
166 
167 template <typename T>
169 {
170  // if the previous is a nullptr it's fine
171  assert(!at_current_scope());
172  m_current_transaction_index++;
173 }
174 
175 template <typename T>
177 {
178  if (at_current_scope()) {
179  assert(!empty());
180  }
181  m_current_transaction_index--;
182 }
183 template <typename T>
185 {
186  m_current_transaction_index = m_transaction_starts.size();
187 }
188 template <typename T>
190 {
191  assert(m_current_transaction_index <= m_transaction_starts.size());
192  return m_current_transaction_index == m_transaction_starts.size();
193 }
194 template <typename T>
196 {
197  return at_current_scope();
198 }
199 
200 } // namespace wmtk::attribute::internal
This class stores data of type T in a vector.
Definition: Attribute.hpp:32
MapResult< D > vector_attribute(const int64_t index)
std::vector< T > m_data
Definition: Attribute.hpp:181
ConstMapResult< D > const_vector_attribute_from_start(const int64_t index, const std::vector< T > &data) const
Accesses the attribute using the specified vector as the underlying data This is internally used by t...
void pop(Attribute< T > &attribute, bool preserve_changes)
bool at_current_scope() const
checks that we are viewing the active state of the attribute
void try_caching(int64_t index, const Eigen::MatrixBase< Derived > &value)