Wildmeshing Toolkit
Loading...
Searching...
No Matches
CachingAttribute.hpp
Go to the documentation of this file.
1#pragma once
2#include <Eigen/Core>
3#include <map>
4#include <memory>
5#include "Attribute.hpp"
6
7
8namespace wmtk::attribute {
9template <typename T>
10class Attribute;
11
12
13
14// An attribute that that can track old state (transactions)
15// A stack of transactions can be created/destroyed by push_scope/pop_scope
16//
17template <typename T>
18class CachingAttribute : public Attribute<T>
19{
20public:
21 using Scalar = T;
23 template <int D>
24 using MapResult = typename BaseType::template MapResult<D>;
25 template <int D>
26 using ConstMapResult = typename BaseType::template ConstMapResult<D>;
27
28 using BaseType::BaseType;
29
34
35 // adds a scope for a new transaction
36 void push_scope();
37 // pops the current transaction
38 // @param preserve_changes: if true the current state of the attribute is preserved, otherwise the last transaction level is popped
39 void pop_scope(bool preserve_changes);
40
41
42
43
44
45
46
47
49 bool at_current_scope() const;
50 // index of the currently active transaction
52 // the total number of transactions
53 int64_t transaction_depth() const;
54
55 // whether any transactions exist
56 bool has_transactions() const;
57
58
60 template <int D = Eigen::Dynamic>
63
64 template <int D = Eigen::Dynamic>
67 T& scalar_attribute(int64_t index);
68
70 const T& const_scalar_attribute(int64_t index) const;
71
72 template <int D = Eigen::Dynamic>
74 const T& const_vector_single_value(int64_t index, int8_t vector_index) const;
75
78 // go to the scope with active data
81
82
83
84
85
86private:
87 // raw access to a (strided) value in an attribute
88 const T* get_value(int64_t index) const;
89 // Appends data to the current cache/transaction
90 template <typename Derived>
91 void cache(int64_t index, const Eigen::MatrixBase<Derived>& value);
92 // Appends scalar data to the current cache/transaction
93 void cache(int64_t index, const T& value);
94 void apply_last_scope();
95
96public:// FUNCTIONS HERE ARE PUBLIC FOR UNIT TESTING. DO NOT USE!
97 // purely used for debug printing out the ENTIRE attribute state
98#if defined(WMTK_ENABLED_DEV_MODE)
99 void print_state(std::string_view prefix) const;
100#endif
101 // resets the entire transaction stack. should only really be called in unit tests
102 void reset();
103 // clears the current active transaction, should only really be called in unit tests
104 void clear();
105
106 void apply_cache();
107 // applyes to some other buffer that was passed in
108 void apply_cache(std::vector<T>& other) const;
109
110 const std::vector<T>& buffer() const { return m_buffer; }
111 const std::vector<std::pair<size_t, size_t>>& indices() const { return m_indices; }
112
113 // the starting index of each transaction
114 const std::vector<size_t>& transaction_starts() const { return m_transaction_starts; }
115 size_t buffer_end() const { return m_buffer_end; }
116 size_t indices_end() const { return m_indices_end; }
117
118 void emplace();
119 void pop(bool preserve_changes);
120 // go to the next most historic scope
121
122 std::vector<std::pair<size_t, size_t>>::const_iterator transaction_start_begin(
123 size_t scope_index) const;
124 std::vector<std::pair<size_t, size_t>>::const_iterator final_transaction_end() const;
125
126 std::vector<std::pair<size_t, size_t>>::const_reverse_iterator transaction_start_rend(
127 size_t scope_index) const;
128 std::vector<std::pair<size_t, size_t>>::const_reverse_iterator final_transaction_rbegin() const;
129
130 void update_buffer_sizes_for_add(size_t data_size);
131protected:
132 std::vector<T> m_buffer = std::vector<T>(64);
133 std::vector<std::pair<size_t, size_t>> m_indices = std::vector<std::pair<size_t, size_t>>(32);
134
136
137 // the starting index of each transaction
138 std::vector<size_t> m_transaction_starts;
139 size_t m_buffer_end = 0;
140 size_t m_indices_end = 0;
141};
142
143
144} // namespace wmtk::attribute
145
146
147#if !defined(WMTK_ENABLED_DEV_MODE)
148#include "CachingAttribute.hxx"
149#endif
This class stores data of type T in a vector.
Definition Attribute.hpp:22
CachingAttribute(CachingAttribute &&)=default
ConstMapResult< D > const_vector_attribute(int64_t index) const
default immutable vector access
const T & const_scalar_attribute(int64_t index) const
default immutable scalar access
std::vector< std::pair< size_t, size_t > >::const_iterator final_transaction_end() const
const std::vector< std::pair< size_t, size_t > > & indices() const
const std::vector< size_t > & transaction_starts() const
void pop(bool preserve_changes)
MapResult< D > vector_attribute(int64_t index)
default mutable vector access
bool at_current_scope() const
checks that we are viewing the active state of the attribute
CachingAttribute & operator=(CachingAttribute &&)=default
std::vector< std::pair< size_t, size_t > > m_indices
const T * get_value(int64_t index) const
typename BaseType::template MapResult< D > MapResult
std::vector< std::pair< size_t, size_t > >::const_reverse_iterator transaction_start_rend(size_t scope_index) const
CachingAttribute(const CachingAttribute &)=delete
std::vector< std::pair< size_t, size_t > >::const_reverse_iterator final_transaction_rbegin() const
void pop_scope(bool preserve_changes)
CachingAttribute & operator=(const CachingAttribute &)=delete
void cache(int64_t index, const Eigen::MatrixBase< Derived > &value)
const std::vector< T > & buffer() const
std::vector< std::pair< size_t, size_t > >::const_iterator transaction_start_begin(size_t scope_index) const
std::vector< size_t > m_transaction_starts
typename BaseType::template ConstMapResult< D > ConstMapResult
T & scalar_attribute(int64_t index)
default mutable scalar access
const T & const_vector_single_value(int64_t index, int8_t vector_index) const
specialized immutable scalar access useful for topological operations
void update_buffer_sizes_for_add(size_t data_size)