delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
delpi::VariableMap< T > Class Template Reference

Map from variables to an arbitrary type T optimised for fast access at the cost of memory. More...

#include <VariableMap.h>

Public Member Functions

template<TypedIterable< value_type > Items>
 VariableMap (const Items &items)
 Construct a new variable map from a range of items.
bool empty () const
 @checked{empty, variable map}
mapped_type At (const Variable var) const
 Use the var to get the value it is mapped to, if it exists.
bool Contains (const Variable &var) const
 Check if the var is in the map.
bool Insert (const Variable &var, const mapped_type &value)
 Insert a var to the map and set it to the value.
template<TypedIterable< value_type > I>
void Insert (const I &items)
 Insert a range of key-value pairs to the map.
bool Remove (const Variable &var)
 Remove a var from the map.
template<TypedIterable< Variable > V>
void Remove (const V &vars)
 Remove a range of vars from the map.
void Clear ()
 Clear the map of all variables.

Private Member Functions

bool Contains (const Id id) const
 Check if the variable with the given id is in the map.
bool Insert (const Id id, const T &value)
 Insert a variable with the given id to the map and set it to the value.
bool Remove (const Id id)
 Remove a variable with the given id from the map.

Private Attributes

Id min_id_
 Minimum id of the variables in the map.
std::vector< std::optional< T > > vars_
 Vector tracking the value each variable is mapped to.

Detailed Description

template<class T>
class delpi::VariableMap< T >

Map from variables to an arbitrary type T optimised for fast access at the cost of memory.

Instead of using a hash map, it uses a vector of std::optional values to track the value each variable maps to. The vector contains \( m \) elements, where \( m \) is the number obtained by subtracting the minimum id of the variables in the map from the maximum id plus 1, i.e. \( m = \text{max_id} - \text{min_id} + 1 \). Hence, the structure will be most efficient, both in terms of memory and speed, when the variables are contiguous and have a small range of ids, but it will be extremely inefficient in terms of space when the variables are sparse with respect to their ids. This also effects computing properties over the whole map of variables. For instance, checking if the map is empty or iterating over all the entries is linear in \( m \), not in the number of entries. @tip If you know the variable indexes are sparse, use a standard map instead.

Definition at line 35 of file VariableMap.h.

Constructor & Destructor Documentation

◆ VariableMap()

template<class T>
template<TypedIterable< value_type > Items>
delpi::VariableMap< T >::VariableMap ( const Items & items)
inlineexplicit

Construct a new variable map from a range of items.

Template Parameters
Itemskey-value pairs to add to the map
Parameters
itemsrange of key-value pairs to add to the map

Definition at line 52 of file VariableMap.h.

Member Function Documentation

◆ At()

template<class T>
mapped_type delpi::VariableMap< T >::At ( const Variable var) const
inlinenodiscard

Use the var to get the value it is mapped to, if it exists.

Parameters
varvariable whose value to get
Returns
value mapped to the variable, if it exists
Exceptions
DelpiOutOfRangeExceptionif the var is not in the map

Definition at line 84 of file VariableMap.h.

◆ Contains() [1/2]

template<class T>
bool delpi::VariableMap< T >::Contains ( const Id id) const
inlinenodiscardprivate

Check if the variable with the given id is in the map.

Parameters
idid of the variable to check
Returns
true if the variable is in the map
false if the variable is not in the map

Definition at line 168 of file VariableMap.h.

◆ Contains() [2/2]

template<class T>
bool delpi::VariableMap< T >::Contains ( const Variable & var) const
inlinenodiscard

Check if the var is in the map.

Parameters
varvariable to check
Returns
true if the variable is in the map
false if the variable is not in the map

Definition at line 94 of file VariableMap.h.

◆ Insert() [1/3]

template<class T>
template<TypedIterable< value_type > I>
void delpi::VariableMap< T >::Insert ( const I & items)
inline

Insert a range of key-value pairs to the map.

Existing variables will be updated with the new values.

Template Parameters
Igeneric iterable containing variables
Parameters
itemsrange of key-value pairs to add

Definition at line 111 of file VariableMap.h.

◆ Insert() [2/3]

template<class T>
bool delpi::VariableMap< T >::Insert ( const Id id,
const T & value )
inlineprivate

Insert a variable with the given id to the map and set it to the value.

Parameters
idid of the variable to add
valuevalue to set the variable to
Returns
true if the variable was added
false if the variable was already in the map

Definition at line 178 of file VariableMap.h.

◆ Insert() [3/3]

template<class T>
bool delpi::VariableMap< T >::Insert ( const Variable & var,
const mapped_type & value )
inline

Insert a var to the map and set it to the value.

Existing variables will be updated with the new value.

Parameters
varvariable to add
valuevalue to set the variable to
Returns
true if the variable was added
false if the variable was already in the map

Definition at line 103 of file VariableMap.h.

◆ Remove() [1/3]

template<class T>
bool delpi::VariableMap< T >::Remove ( const Id id)
inlineprivate

Remove a variable with the given id from the map.

Parameters
idid of the variable to remove
Returns
true if the variable was removed
false if the variable was not in the map

Definition at line 205 of file VariableMap.h.

◆ Remove() [2/3]

template<class T>
template<TypedIterable< Variable > V>
void delpi::VariableMap< T >::Remove ( const V & vars)
inline

Remove a range of vars from the map.

Only variables in the map will be removed.

Parameters
varsrange of variables to remove

Definition at line 152 of file VariableMap.h.

◆ Remove() [3/3]

template<class T>
bool delpi::VariableMap< T >::Remove ( const Variable & var)
inline

Remove a var from the map.

If the variable is not in the map, nothing happens.

Parameters
varvariable to remove
Returns
true if the variable was removed
false if the variable was not in the map

Definition at line 145 of file VariableMap.h.


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