DiSMEC++
dismec::HashVector Class Reference

An Eigen vector with versioning information, to implement simple caching of results. More...

#include <hash_vector.h>

Public Member Functions

 HashVector (DenseRealVector data)
 Creates a new hash vector from the given DenseRealVector. More...
 
const DenseRealVectorget () const
 Gets a constant reference to the data of this vector. More...
 
const DenseRealVectoroperator-> () const
 constant access to vector data. More...
 
VectorHash hash () const
 Gets the unique id of this vector. More...
 
template<class Derived >
HashVectoroperator= (const Eigen::EigenBase< Derived > &expr)
 Update the contents of this vector. More...
 
DenseRealVectormodify ()
 Gets non-const access to the underlying data. More...
 

Private Member Functions

void update_id ()
 

Private Attributes

std::size_t m_UniqueID
 
DenseRealVector m_Data
 

Detailed Description

An Eigen vector with versioning information, to implement simple caching of results.

This class wraps an Eigen::Vector and adds an additional data field m_UniqueID. This field contains a unique number for each new HashVector created, and is updated whenever a potentially modifying operation is performed on the vector.

The unique id can be queried using the hash() function, which returns a VectorHash() object. The only purpose of this is to enable checks of whether a function is called with a vector argument that is the same as a previous call, without having to store and compare the contents of the vector. This works as follows (if cached doesn't need to be re-entrant):

int cached(const HashVector& vec) {
static VectorHash last_used{};
static int last_result{};
if(vec.hash() == last_used) {
return last_result;
}
last_result = compute(vec.get());
last_used = vec.hash();
}
HashVector(DenseRealVector data)
Creates a new hash vector from the given DenseRealVector.
Definition: hash_vector.cpp:36

The default-initialized VectorHash compares unequal to all other hash values.

Note
Technically, we are using an internal counter, which could overflow. On 64-bit systems, this should never occur (e.g. if you create a new HashVector every microsecond, it takes about 500k years). We don't really expect to run this code on hardware so old that std::size_t is less than 64 bit.

Definition at line 43 of file hash_vector.h.

Constructor & Destructor Documentation

◆ HashVector()

HashVector::HashVector ( DenseRealVector  data)
explicit

Creates a new hash vector from the given DenseRealVector.

This moves the content from data into the newly created HashVector and generates a new unique id.

Definition at line 36 of file hash_vector.cpp.

Member Function Documentation

◆ get()

◆ hash()

VectorHash HashVector::hash ( ) const

Gets the unique id of this vector.

This gets a unique id that is different from those of all other HashVector objects. This function returns the same value until a modifying operation is performed with the vector. Thus, for a given HashVector vec:

auto id = vec.hash();
auto twice = vec * 2; // does not modify `vec`
assert(id == vec.hash());
vec = twice; // update value and id
assert(id != vec.hash());
Returns
A VectorHash() corresponding to this vector.

Definition at line 45 of file hash_vector.cpp.

References m_UniqueID.

Referenced by dismec::objective::Regularized_SquaredHingeSVC::margin_error(), TEST_CASE(), dismec::CacheHelper::update(), dismec::objective::DenseAndSparseLinearBase::update_xtw_cache(), dismec::objective::LinearClassifierBase::update_xtw_cache(), dismec::objective::DenseAndSparseLinearBase::x_times_w(), and dismec::objective::LinearClassifierBase::x_times_w().

◆ modify()

DenseRealVector& dismec::HashVector::modify ( )
inline

Gets non-const access to the underlying data.

Since this can change the contents of the underlying vector, this updates this vectors unqiue id. However, note that you can store the returned reference, and modify the contents of the vector later on. Such changes cannot be detected, and will result in invalid unique ids.

Definition at line 98 of file hash_vector.h.

References m_Data, and update_id().

◆ operator->()

const DenseRealVector* dismec::HashVector::operator-> ( ) const
inline

constant access to vector data.

Definition at line 59 of file hash_vector.h.

References m_Data.

◆ operator=()

template<class Derived >
HashVector& dismec::HashVector::operator= ( const Eigen::EigenBase< Derived > &  expr)
inline

Update the contents of this vector.

Template Parameters
DerivedThe actual type of the Eigen expression.By accepting this in a templated version, we make sure that there are no unnecessary temporaries involved.
Parameters
exprAn eigen expression.
Returns
A reference to this.

Definition at line 86 of file hash_vector.h.

References m_Data, and update_id().

◆ update_id()

void HashVector::update_id ( )
private

Definition at line 41 of file hash_vector.cpp.

References anonymous_namespace{hash_vector.cpp}::get_next_id(), and m_UniqueID.

Referenced by modify(), and operator=().

Member Data Documentation

◆ m_Data

DenseRealVector dismec::HashVector::m_Data
private

Definition at line 106 of file hash_vector.h.

Referenced by get(), modify(), operator->(), and operator=().

◆ m_UniqueID

std::size_t dismec::HashVector::m_UniqueID
private

Definition at line 105 of file hash_vector.h.

Referenced by hash(), and update_id().


The documentation for this class was generated from the following files: