delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
delpi::VariableSet Class Reference

Set of variables optimised for fast access at the cost of memory. More...

#include <VariableSet.h>

Public Member Functions

template<TypedIterable< Variable > T>
 VariableSet (const T &vars)
 Construct a new variable set from a range of vars.
template<std::same_as< Variable >... Vars>
 VariableSet (const Variable &var, const Vars &... vars)
 Construct a new variable set from a sequence of variables.
bool empty () const
 @checked{empty, variable set}
Variable Get (Id id) const
 Use the id to get the variable in the set, if it exists.
bool Contains (const Variable &var) const
 Check if the var is in the set.
bool Insert (const Variable &var)
 Insert a var to the set.
template<TypedIterable< Variable > T>
void Insert (const T &vars)
 Insert a range of vars to the set.
template<std::same_as< Variable >... Vars>
void Insert (const Variable &var, const Vars &... vars)
 Insert a sequence of variables to the set.
bool Remove (const Variable &var)
 Remove a var from the set.
template<TypedIterable< Variable > T>
void Remove (const T &vars)
 Remove a range of vars from the set.
template<std::same_as< Variable >... Vars>
void Remove (const Variable &var, const Vars &... vars)
 Remove a sequence of variables from the set.
void Clear ()
 Clear the set of all variables.

Private Member Functions

bool Contains (Id id) const
 Check if the variable with the given id is in the set.
bool Insert (Id id)
 Insert a variable with the given id to the set.
bool Remove (Id id)
 Remove a variable with the given id from the set.

Private Attributes

Id min_id_
 Minimum id of the variables in the set.
std::vector< bool > vars_
 Vector tracking the presence of each variable in the set.

Detailed Description

Set of variables optimised for fast access at the cost of memory.

Instead of using a hash set, it uses a vector of booleans to track the presence of each variable. The vector contains \( m \) elements, where \( m \) is the number obtained by subtracting the minimum id of the variables in the set 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 set of variables. For instance, checking if the set is empty or iterating over all the variables is linear in \( m \), not in the number of variables. @tip If you know the variable indexes are sparse, use a standard set instead.

Definition at line 33 of file VariableSet.h.

Constructor & Destructor Documentation

◆ VariableSet() [1/2]

template<TypedIterable< Variable > T>
delpi::VariableSet::VariableSet ( const T & vars)
inlineexplicit

Construct a new variable set from a range of vars.

Template Parameters
Tgeneric iterable containing variables
Parameters
varsrange of variables to add to the set

Definition at line 45 of file VariableSet.h.

◆ VariableSet() [2/2]

template<std::same_as< Variable >... Vars>
delpi::VariableSet::VariableSet ( const Variable & var,
const Vars &... vars )
inlineexplicit

Construct a new variable set from a sequence of variables.

@tip This method will add the variables in the set one by one. Using VariableSet(T) can be more efficient for large sets.

Template Parameters
Varssequence of variables to add to the set
Parameters
varfirst variable to add to the set
varsremaining variables to add to the set

Definition at line 57 of file VariableSet.h.

Member Function Documentation

◆ Contains() [1/2]

bool delpi::VariableSet::Contains ( const Variable & var) const
inlinenodiscard

Check if the var is in the set.

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

Definition at line 87 of file VariableSet.h.

◆ Contains() [2/2]

bool delpi::VariableSet::Contains ( Id id) const
nodiscardprivate

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

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

Definition at line 59 of file VariableSet.cpp.

◆ Get()

Variable delpi::VariableSet::Get ( Id id) const
nodiscard

Use the id to get the variable in the set, if it exists.

Parameters
idid of the variable to get
Returns
variable with the given id, if it exists
Exceptions
DelpiOutOfRangeExceptionif the id is not in the set

Definition at line 55 of file VariableSet.cpp.

◆ Insert() [1/4]

template<TypedIterable< Variable > T>
void delpi::VariableSet::Insert ( const T & vars)
inline

Insert a range of vars to the set.

Only variables not already in the set will be added.

Template Parameters
Tgeneric iterable containing variables
Parameters
varsrange of variables to add

Definition at line 103 of file VariableSet.h.

◆ Insert() [2/4]

bool delpi::VariableSet::Insert ( const Variable & var)
inline

Insert a var to the set.

If the variable is already in the set, nothing happens.

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

Definition at line 95 of file VariableSet.h.

◆ Insert() [3/4]

template<std::same_as< Variable >... Vars>
void delpi::VariableSet::Insert ( const Variable & var,
const Vars &... vars )
inline

Insert a sequence of variables to the set.

Only variables not already in the set will be added. @tip This method will add the variables in the set one by one. Using Insert(T) can be more efficient for large sets.

Template Parameters
Varssequence of variables to add
Parameters
varfirst variable to add
varsremaining variables to add

Definition at line 138 of file VariableSet.h.

◆ Insert() [4/4]

bool delpi::VariableSet::Insert ( Id id)
private

Insert a variable with the given id to the set.

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

Definition at line 20 of file VariableSet.cpp.

◆ Remove() [1/4]

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

Remove a range of vars from the set.

Only variables in the set will be removed.

Template Parameters
Tgeneric iterable containing variables
Parameters
varsrange of variables to remove

Definition at line 157 of file VariableSet.h.

◆ Remove() [2/4]

bool delpi::VariableSet::Remove ( const Variable & var)
inline

Remove a var from the set.

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

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

Definition at line 149 of file VariableSet.h.

◆ Remove() [3/4]

template<std::same_as< Variable >... Vars>
void delpi::VariableSet::Remove ( const Variable & var,
const Vars &... vars )
inline

Remove a sequence of variables from the set.

Only variables in the set will be removed.

Template Parameters
Varssequence of variables to remove
Parameters
varfirst variable to remove
varsremaining variables to remove

Definition at line 168 of file VariableSet.h.

◆ Remove() [4/4]

bool delpi::VariableSet::Remove ( Id id)
private

Remove a variable with the given id from the set.

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

Definition at line 62 of file VariableSet.cpp.


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