DiSMEC++
hash_vector.h
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #ifndef DISMEC_HASH_VECTOR_H
7 #define DISMEC_HASH_VECTOR_H
8 
9 #include <memory>
10 #include "matrix_types.h"
11 
12 namespace dismec
13 {
14  class VectorHash;
15 
43  class HashVector {
44  public:
50  explicit HashVector(DenseRealVector data);
51 
57  [[nodiscard]] const DenseRealVector& get() const { return m_Data; }
59  const DenseRealVector* operator->() const { return &m_Data; }
60 
76  [[nodiscard]] VectorHash hash() const;
77 
85  template<class Derived>
87  update_id();
88  m_Data = expr;
89  return *this;
90  }
91 
99  update_id();
100  return m_Data;
101  }
102  private:
103  void update_id();
104 
105  std::size_t m_UniqueID;
107  };
108 
109 
118  class VectorHash {
119  public:
125  VectorHash();
126 
132  bool operator==(const VectorHash& other) const {
133  return m_UniqueID == other.m_UniqueID;
134  }
136  bool operator!=(const VectorHash& other) const {
137  return !(*this == other);
138  }
139  private:
144  VectorHash(std::size_t id) : m_UniqueID(id) {}
145  std::size_t m_UniqueID;
146 
147  friend class HashVector;
148  };
149 
150  // overload vector operators
151  template<class T>
152  auto operator+(const HashVector& vec, T&& other) {
153  return vec.get() + other;
154  }
155 
156  template<class T>
157  auto operator+(T&& other, const HashVector& vec) {
158  return other + vec.get() ;
159  }
160 
161  template<class T>
162  auto operator+=(T&& other, const HashVector& vec) {
163  return other += vec.get() ;
164  }
165 
166  template<class T>
167  auto operator*(const HashVector& vec, T&& other) {
168  return vec.get() * other;
169  }
170 
171  template<class T>
172  auto operator*(T&& other, const HashVector& vec) {
173  return other * vec.get() ;
174  }
175 
176  template<class T>
177  auto operator*=(T&& other, const HashVector& vec) {
178  return other *= vec.get() ;
179  }
180 
181  class CacheHelper {
182  public:
183  explicit CacheHelper(Eigen::Index size) : m_Input(), m_Output(size) {}
184 
185  template<class F>
186  const DenseRealVector& update(const HashVector& input, F&& function) {
187  if(input.hash() == m_Input) {
188  return m_Output;
189  }
190  function(input.get(), m_Output);
191  m_Input = input.hash();
192  return m_Output;
193  }
194 
195  void invalidate() {
196  m_Input = {};
197  }
198  private:
201  };
202 }
203 #endif //DISMEC_HASH_VECTOR_H
const DenseRealVector & update(const HashVector &input, F &&function)
Definition: hash_vector.h:186
VectorHash m_Input
Definition: hash_vector.h:199
DenseRealVector m_Output
Definition: hash_vector.h:200
CacheHelper(Eigen::Index size)
Definition: hash_vector.h:183
An Eigen vector with versioning information, to implement simple caching of results.
Definition: hash_vector.h:43
VectorHash hash() const
Gets the unique id of this vector.
Definition: hash_vector.cpp:45
HashVector & operator=(const Eigen::EigenBase< Derived > &expr)
Update the contents of this vector.
Definition: hash_vector.h:86
std::size_t m_UniqueID
Definition: hash_vector.h:105
const DenseRealVector * operator->() const
constant access to vector data.
Definition: hash_vector.h:59
const DenseRealVector & get() const
Gets a constant reference to the data of this vector.
Definition: hash_vector.h:57
DenseRealVector m_Data
Definition: hash_vector.h:106
HashVector(DenseRealVector data)
Creates a new hash vector from the given DenseRealVector.
Definition: hash_vector.cpp:36
DenseRealVector & modify()
Gets non-const access to the underlying data.
Definition: hash_vector.h:98
A unique identifier for a HashVector.
Definition: hash_vector.h:118
VectorHash()
Default constructor. Initializes the VectorHash to special marker.
Definition: hash_vector.cpp:49
std::size_t m_UniqueID
Definition: hash_vector.h:145
VectorHash(std::size_t id)
Creates a VectorHash for the given ID.
Definition: hash_vector.h:144
bool operator==(const VectorHash &other) const
Checks that two hashes are equal.
Definition: hash_vector.h:132
bool operator!=(const VectorHash &other) const
negation of the equality check
Definition: hash_vector.h:136
Main namespace in which all types, classes, and functions are defined.
Definition: app.h:15
auto operator*=(T &&other, const HashVector &vec)
Definition: hash_vector.h:177
auto operator*(const HashVector &vec, T &&other)
Definition: hash_vector.h:167
types::DenseVector< real_t > DenseRealVector
Any dense, real values vector.
Definition: matrix_types.h:40
auto operator+=(T &&other, const HashVector &vec)
Definition: hash_vector.h:162
label_id_t operator+(label_id_t a, std::ptrdiff_t b)
Definition: types.h:34