DiSMEC++
hash_vector.cpp
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 #include "hash_vector.h"
7 #include <atomic>
8 
9 using namespace dismec;
10 
18 namespace {
24  std::size_t get_next_id()
25  {
27  static std::atomic<std::size_t> next_id{0};
28  static_assert(sizeof(std::size_t) >= 8, "std::size_t is expected to be at least 64 bit, so that we can be sure our unique ids never wrap around.");
29 
30  // see also https://stackoverflow.com/questions/41206861/atomic-increment-and-return-counter
31  return next_id++;
32  }
33 
34 }
35 
37  m_UniqueID(get_next_id()),
38  m_Data(std::move(data)) {
39 }
40 
43 }
44 
46  return {m_UniqueID};
47 }
48 
49 VectorHash::VectorHash() : m_UniqueID(-1) {
50 }
51 
52 #include "doctest.h"
53 
57 TEST_CASE("HashVector content") {
58  HashVector hv(DenseRealVector::Zero(3));
59 
60  CHECK(hv->coeff(0) == 0.0);
61  CHECK(hv->coeff(1) == 0.0);
62  CHECK(hv->coeff(2) == 0.0);
63 
64  hv = DenseRealVector::Ones(3);
65  CHECK(hv->coeff(0) == 1.0);
66  CHECK(hv->coeff(1) == 1.0);
67  CHECK(hv->coeff(2) == 1.0);
68 }
69 
73 TEST_CASE("HashVector versioning") {
74  VectorHash vh;
75  HashVector hv(DenseRealVector::Zero(3));
76 
77  CHECK(vh != hv.hash());
78  vh = hv.hash();
79  CHECK(vh == hv.hash());
80 
81  // non-mutating operation
82  auto content = hv.get();
83  content = hv + DenseRealVector::Ones(3);
84  CHECK(vh == hv.hash());
85  CHECK(hv->coeff(0) == 0.0);
86 
87  hv = DenseRealVector::Ones(3);
88  CHECK(vh != hv.hash());
89 }
90 
94 TEST_CASE("HashVector uniqueness") {
95  HashVector hv(DenseRealVector::Zero(3));
96  HashVector hw(DenseRealVector::Zero(3));
97 
98  CHECK( hv.hash() != hw.hash() );
99 }
100 
104 TEST_CASE("uninitialized VectorHash") {
105  VectorHash vh;
106 
107  HashVector hv(DenseRealVector::Zero(3));
108  CHECK_FALSE(vh == hv.hash());
109  CHECK_FALSE(hv.hash() == vh);
110 }
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
std::size_t m_UniqueID
Definition: hash_vector.h:105
const DenseRealVector & get() const
Gets a constant reference to the data of this vector.
Definition: hash_vector.h:57
HashVector(DenseRealVector data)
Creates a new hash vector from the given DenseRealVector.
Definition: hash_vector.cpp:36
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
TEST_CASE("HashVector content")
Definition: hash_vector.cpp:57
std::size_t get_next_id()
Get the next valid id.
Definition: hash_vector.cpp:24
Main namespace in which all types, classes, and functions are defined.
Definition: app.h:15
types::DenseVector< real_t > DenseRealVector
Any dense, real values vector.
Definition: matrix_types.h:40