carl  24.04
Computer ARithmetic Library
carl::VariablePool Class Reference

This class generates new variables and stores human-readable names for them. More...

#include <VariablePool.h>

Inheritance diagram for carl::VariablePool:
Collaboration diagram for carl::VariablePool:

Public Member Functions

Variable get_fresh_persistent_variable (VariableType type=VariableType::VT_REAL) noexcept
 
Variable get_fresh_persistent_variable (const std::string &name, VariableType type=VariableType::VT_REAL)
 
void clear () noexcept
 Clears everything already created in this pool. More...
 
Variable find_variable_with_name (const std::string &name) const noexcept
 Searches in the friendly names list for a variable with the given name. More...
 
std::string get_name (Variable v, bool variableName=true) const
 Get a human-readable name for the given variable. More...
 
void set_name (Variable v, const std::string &name)
 Add a name for a given Variable. More...
 
void set_prefix (std::string prefix="_") noexcept
 Sets the prefix used when printing anonymous variables. More...
 

Static Public Member Functions

static VariablePoolgetInstance ()
 Returns the single instance of this class by reference. More...
 

Protected Member Functions

 VariablePool () noexcept
 Private default constructor. More...
 
Variable get_fresh_variable (VariableType type=VariableType::VT_REAL) noexcept
 Get a variable which was not used before. More...
 
Variable get_fresh_variable (const std::string &name, VariableType type=VariableType::VT_REAL)
 Get a variable with was not used before and set a name for it. More...
 

Private Member Functions

std::size_t & nextID (VariableType vt) noexcept
 
std::size_t nextID (VariableType vt) const noexcept
 

Private Attributes

friend Singleton< VariablePool >
 
std::array< std::size_t, static_cast< std::size_t >VariableType::TYPE_SIZE)> mNextIDs
 Contains the id of the next variable to be created for each type. More...
 
std::mutex freshVarMutex
 Mutex for calling get_fresh_variable(). More...
 
std::mutex set_nameMutex
 Mutex for calling setVariableName(). More...
 
std::vector< std::pair< Variable, std::string > > mPersistentVariables
 Contains persistent variables that are restored after clear was called. More...
 
std::map< Variable, std::string > mVariableNames
 Stores human-readable names for variables that can be set via setVariableName(). More...
 
std::string mVariablePrefix
 Stores a prefix for printing variables that have no human-readable name. More...
 

Friends

Variable fresh_variable (VariableType vt) noexcept
 
Variable fresh_variable (const std::string &name, VariableType vt)
 

Detailed Description

This class generates new variables and stores human-readable names for them.

As we want only a single unique VariablePool and need global access to it, it is implemented as a singleton.

All methods that modify the pool, that are getInstance(), get_fresh_variable() and set_name(), are thread-safe.

Definition at line 27 of file VariablePool.h.

Constructor & Destructor Documentation

◆ VariablePool()

carl::VariablePool::VariablePool ( )
protectednoexcept

Private default constructor.

Definition at line 15 of file VariablePool.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ clear()

void carl::VariablePool::clear ( )
inlinenoexcept

Clears everything already created in this pool.

Definition at line 109 of file VariablePool.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_variable_with_name()

Variable carl::VariablePool::find_variable_with_name ( const std::string &  name) const
noexcept

Searches in the friendly names list for a variable with the given name.

Parameters
nameThe friendly variable name to look for.
Returns
The first variable with that friendly name.

Definition at line 56 of file VariablePool.cpp.

Here is the caller graph for this function:

◆ get_fresh_persistent_variable() [1/2]

Variable carl::VariablePool::get_fresh_persistent_variable ( const std::string &  name,
VariableType  type = VariableType::VT_REAL 
)

Definition at line 49 of file VariablePool.cpp.

Here is the call graph for this function:

◆ get_fresh_persistent_variable() [2/2]

Variable carl::VariablePool::get_fresh_persistent_variable ( VariableType  type = VariableType::VT_REAL)
noexcept

Definition at line 40 of file VariablePool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_fresh_variable() [1/2]

Variable carl::VariablePool::get_fresh_variable ( const std::string &  name,
VariableType  type = VariableType::VT_REAL 
)
protected

Get a variable with was not used before and set a name for it.

This method is thread-safe.

Parameters
nameName for the new variable.
typeType for the new variable.
Returns
A new variable.

Definition at line 31 of file VariablePool.cpp.

Here is the call graph for this function:

◆ get_fresh_variable() [2/2]

Variable carl::VariablePool::get_fresh_variable ( VariableType  type = VariableType::VT_REAL)
protectednoexcept

Get a variable which was not used before.

This method is thread-safe.

Parameters
typeType for the new variable.
Returns
A new variable.

Definition at line 21 of file VariablePool.cpp.

Here is the caller graph for this function:

◆ get_name()

std::string carl::VariablePool::get_name ( Variable  v,
bool  variableName = true 
) const

Get a human-readable name for the given variable.

If the given Variable is Variable::NO_VARIABLE, "NO_VARIABLE" is returned. If friendlyVarName is true, the name that was set via setVariableName() for this Variable, if there is any, is returned. Otherwise "x_<id>" is returned, id being the internal id of the Variable.

Parameters
vVariable.
variableNameFlag, if a name set via setVariableName shall be considered.
Returns
Some name for the Variable.

Definition at line 63 of file VariablePool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getInstance()

static VariablePool & carl::Singleton< VariablePool >::getInstance ( )
inlinestaticinherited

Returns the single instance of this class by reference.

If there is no instance yet, a new one is created.

Definition at line 45 of file Singleton.h.

◆ nextID() [1/2]

std::size_t carl::VariablePool::nextID ( VariableType  vt) const
inlineprivatenoexcept

Definition at line 51 of file VariablePool.h.

◆ nextID() [2/2]

std::size_t& carl::VariablePool::nextID ( VariableType  vt)
inlineprivatenoexcept

Definition at line 47 of file VariablePool.h.

Here is the caller graph for this function:

◆ set_name()

void carl::VariablePool::set_name ( Variable  v,
const std::string &  name 
)

Add a name for a given Variable.

This method is thread-safe.

Parameters
vVariable.
nameSome string naming the variable.

Definition at line 90 of file VariablePool.cpp.

Here is the caller graph for this function:

◆ set_prefix()

void carl::VariablePool::set_prefix ( std::string  prefix = "_")
inlinenoexcept

Sets the prefix used when printing anonymous variables.

The default is "_", hence they look like "_x_5".

Parameters
prefixPrefix for anonymous variable names.

Definition at line 154 of file VariablePool.h.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ fresh_variable [1/2]

Variable fresh_variable ( const std::string &  name,
VariableType  vt 
)
friend

Definition at line 182 of file VariablePool.h.

◆ fresh_variable [2/2]

Variable fresh_variable ( VariableType  vt)
friend

Definition at line 179 of file VariablePool.h.

Field Documentation

◆ freshVarMutex

std::mutex carl::VariablePool::freshVarMutex
mutableprivate

Mutex for calling get_fresh_variable().

Definition at line 40 of file VariablePool.h.

◆ mNextIDs

std::array<std::size_t, static_cast<std::size_t>VariableType::TYPE_SIZE)> carl::VariablePool::mNextIDs
private

Contains the id of the next variable to be created for each type.

As such, is also a counter of the variables that exist.

Definition at line 35 of file VariablePool.h.

◆ mPersistentVariables

std::vector<std::pair<Variable, std::string> > carl::VariablePool::mPersistentVariables
private

Contains persistent variables that are restored after clear was called.

Definition at line 59 of file VariablePool.h.

◆ mVariableNames

std::map<Variable, std::string> carl::VariablePool::mVariableNames
private

Stores human-readable names for variables that can be set via setVariableName().

Definition at line 64 of file VariablePool.h.

◆ mVariablePrefix

std::string carl::VariablePool::mVariablePrefix
private

Stores a prefix for printing variables that have no human-readable name.

Definition at line 69 of file VariablePool.h.

◆ set_nameMutex

std::mutex carl::VariablePool::set_nameMutex
mutableprivate

Mutex for calling setVariableName().

Definition at line 45 of file VariablePool.h.

◆ Singleton< VariablePool >

Definition at line 28 of file VariablePool.h.


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