Turi Create  4.0
turi::value_container_mapper< Value, ValueContainer > Class Template Reference

#include <core/generics/value_container_mapper.hpp>

Public Member Functions

void reserve (size_t n)
 
size_t size () const
 
void clear ()
 
void insert (const hashkey_and_value &hv, ValueContainer *v_ptr) GL_HOT_INLINE_FLATTEN
 
void insert (const hashkey &hk, ValueContainer *v_ptr) GL_HOT_INLINE_FLATTEN
 
ValueContainer * find (const hashkey_and_value &hv) GL_HOT_INLINE_FLATTEN
 
ValueContainer * find (const hashkey &key, const Value &t) GL_HOT_INLINE_FLATTEN
 
const ValueContainer * find (const hashkey_and_value &hv) const GL_HOT_INLINE_FLATTEN
 
const ValueContainer * find (const hashkey &key, const Value &t) const GL_HOT_INLINE_FLATTEN
 
void invalidate (const hashkey_and_value &hv, ValueContainer *v_ptr) GL_HOT_INLINE_FLATTEN
 
void invalidate (const hashkey &hk, ValueContainer *v_ptr) GL_HOT_INLINE_FLATTEN
 

Detailed Description

template<typename Value, typename ValueContainer>
class turi::value_container_mapper< Value, ValueContainer >

A fast, specialized container to track lookups from a Value to a container holding that value (plus other things). Essentially, this is very optimized version of std::map<Value, ValueContainer*>, which adds the following assumptions on the API and the ValueContainer type in order to be really fast.

  1. The hash, implemented using a custom hashkey class (see below), is tracked explicitly with the value, and it is up to the user to track this. This permits caching this value and more efficient storage.
  2. The ValueContainer class must hold a hashkey_and_value container (see below); this is essentially a (hashkey, value) pair but with certain other optimizations. This must be accessible via a get_hashkey_and_value() method in the value container, which is then used by
  3. Pointers to the ValueContainer are what is stored, and it is assumed that a Value -> ValueContainer* mapping is valid if and only if the value container holds the same value. Otherwise the find() method returns a nullptr. (The invalidate function below sticks to this assumption; it just tracks things for lazy cleanup).

For a usage example, see ml/sketches/space_saving.hpp.

The value_container_mapper::hashkey class is initializable by value:

struct hashkey { hashkey(); hashkey(const Value& v);

// A bunch of internal methods... };

The value_container_mapper::hashkey_and_value is initializable either by value (in which case the hashkey is created from a hash of the value), or by hashkey and value pair. It also implements key() and value() methods to return the hashkey() and value() respectively:

struct hashkey_and_value { hashkey_and_value(); hashkey_and_value(const Value& v); hashkey_and_value(const hashkey& hk, const Value& v);

hashkey key() const; const Value& value() const;

// A bunch of internal methods... };

Definition at line 76 of file value_container_mapper.hpp.

Member Function Documentation

◆ clear()

template<typename Value, typename ValueContainer>
void turi::value_container_mapper< Value, ValueContainer >::clear ( )
inline

Clears the hash table.

Definition at line 98 of file value_container_mapper.hpp.

◆ find() [1/4]

template<typename Value, typename ValueContainer>
ValueContainer* turi::value_container_mapper< Value, ValueContainer >::find ( const hashkey_and_value hv)
inline

Returns the container associated with this key and value. If it's not present or has been invalidated, returns nullptr.

Definition at line 121 of file value_container_mapper.hpp.

◆ find() [2/4]

template<typename Value, typename ValueContainer>
ValueContainer* turi::value_container_mapper< Value, ValueContainer >::find ( const hashkey key,
const Value &  t 
)
inline

Same as above, but avoids a potential copy operation of the value if it is not stored in a hashkey_and_value instance already. This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 130 of file value_container_mapper.hpp.

◆ find() [3/4]

template<typename Value, typename ValueContainer>
const ValueContainer* turi::value_container_mapper< Value, ValueContainer >::find ( const hashkey_and_value hv) const
inline

Returns the container associated with this (key, value). If it's not present, returns nullptr. Const overload. This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 138 of file value_container_mapper.hpp.

◆ find() [4/4]

template<typename Value, typename ValueContainer>
const ValueContainer* turi::value_container_mapper< Value, ValueContainer >::find ( const hashkey key,
const Value &  t 
) const
inline

Same as above, but avoids a potential copy operation of the value if it is not stored in a hashkey_and_value instance already. Const overload. This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 147 of file value_container_mapper.hpp.

◆ insert() [1/2]

template<typename Value, typename ValueContainer>
void turi::value_container_mapper< Value, ValueContainer >::insert ( const hashkey_and_value hv,
ValueContainer *  v_ptr 
)
inline

Inserts a lookup index into the hash mapping.

Definition at line 104 of file value_container_mapper.hpp.

◆ insert() [2/2]

template<typename Value, typename ValueContainer>
void turi::value_container_mapper< Value, ValueContainer >::insert ( const hashkey hk,
ValueContainer *  v_ptr 
)
inline

Inserts a lookup index into the hash mapping. Overload that pulls the value from v_ptr. This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 114 of file value_container_mapper.hpp.

◆ invalidate() [1/2]

template<typename Value, typename ValueContainer>
void turi::value_container_mapper< Value, ValueContainer >::invalidate ( const hashkey_and_value hv,
ValueContainer *  v_ptr 
)
inline

Marks a particular value_container as invalid. It is assumed, however, that as long as ValueContainer* holds the value, it is a valid key; otherwise it is not. This function does some lazy cleanup, but may not erase the key.

Definition at line 156 of file value_container_mapper.hpp.

◆ invalidate() [2/2]

template<typename Value, typename ValueContainer>
void turi::value_container_mapper< Value, ValueContainer >::invalidate ( const hashkey hk,
ValueContainer *  v_ptr 
)
inline

Marks a particular value_container as invalid. It is assumed, however, that as long as ValueContainer* holds the value, it is a valid key; otherwise it is not. This function does some lazy cleanup, but may not erase the key.

Overload that pulls the value from v_ptr. This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 170 of file value_container_mapper.hpp.

◆ reserve()

template<typename Value, typename ValueContainer>
void turi::value_container_mapper< Value, ValueContainer >::reserve ( size_t  n)
inline

Reserves internal storage for n elements.

Definition at line 88 of file value_container_mapper.hpp.

◆ size()

template<typename Value, typename ValueContainer>
size_t turi::value_container_mapper< Value, ValueContainer >::size ( ) const
inline

Returns the current size of the hash table.

Definition at line 92 of file value_container_mapper.hpp.


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