Wildmeshing Toolkit
AttributeFlatCache.hxx
Go to the documentation of this file.
1 #include <wmtk/Types.hpp>
4 #include "AttributeFlatCache.hpp"
5 
7 
8 
9 template <typename T>
11  : m_buffer(64)
12  , m_indices(32)
13 {}
14 template <typename T>
16 
17 
18 template <typename T>
20 {
21  m_buffer.clear();
22  m_indices.clear();
23 }
24 
25 
26 template <typename T>
27 template <typename Derived>
29  int64_t index,
30  const Eigen::MatrixBase<Derived>& value)
31 {
32  // basically try_emplace but optimizes to avoid accessing the pointed-to value
33 
34  size_t dim = value.size();
35  size_t old_size = m_buffer.size();
36 
37  //assert(old_size % dim == 0);
38  //assert(old_size / value.size() == m_indices.size());
39  // m_indices.emplace_back(index, m_indices.size());
40  m_indices.emplace_back(index, old_size);
41 
42 
43  m_buffer.resize(old_size + dim);
44  std::copy(value.begin(), value.end(), m_buffer.begin() + old_size);
45  // assert(dim * m_buffer.size() == m_indices.size());
46 }
47 
48 template <typename T>
49 inline void AttributeFlatCache<T>::try_caching(int64_t index, const T& value)
50 {
51  //assert(m_buffer.size() == m_indices.size());
52  m_indices.emplace_back(index, m_buffer.size());
53  m_buffer.emplace_back(value);
54  //assert(m_buffer.size() == m_indices.size());
55 }
56 
57 
58 template <typename T>
59 inline void AttributeFlatCache<T>::apply_to(Attribute<T>& attribute) const
60 {
61  for (auto it = m_indices.crbegin(); it != m_indices.crend(); ++it) {
62  const auto& [global, local] = *it;
63  auto a = attribute.vector_attribute(global);
64  auto b = attribute.const_vector_attribute_from_start(local, m_buffer);
65  a = b;
66  }
67 }
68 template <typename T>
70 {
71  size_t offset = other.m_buffer.size();
72  std::copy(m_buffer.begin(), m_buffer.end(), std::back_inserter(other.m_buffer));
73  std::transform(
74  m_indices.begin(),
75  m_indices.end(),
76  std::back_inserter(other.m_indices),
77  [offset](const auto& pr) {
78  const auto& [a, b] = pr;
79  return std::make_pair(a, b + offset);
80  });
81 }
82 
83 template <typename T>
84 inline void AttributeFlatCache<T>::apply_to(const Attribute<T>& attribute, std::vector<T>& other)
85  const
86 {
87  for (auto it = m_indices.crbegin(); it != m_indices.crend(); ++it) {
88  const auto& [global, local] = *it;
89  auto a = attribute.vector_attribute(global, other);
90  auto b = attribute.const_vector_attribute_from_start(local, m_buffer);
91  a = b;
92  }
93 }
94 
95 template <typename T>
96 inline auto AttributeFlatCache<T>::get_value(int64_t index, size_t dim) const -> const T*
97 {
98  for (auto iit = m_indices.cbegin(); iit != m_indices.cend(); ++iit) {
99  const auto& [global, local] = *iit;
100  if (global == index) {
101  return m_buffer.data() + local;
102  }
103  }
104  return nullptr;
105 }
106 } // 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)
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 try_caching(int64_t index, const Eigen::MatrixBase< Derived > &value)
void apply_to(Attribute< T > &attribute) const
std::vector< std::pair< size_t, size_t > > m_indices
const T * get_value(int64_t index, size_t dim) const