carl  24.04
Computer ARithmetic Library
carl::Variable Class Reference

A Variable represents an algebraic variable that can be used throughout carl. More...

#include <Variable.h>

Collaboration diagram for carl::Variable:

Public Types

using Arg = const Variable &
 Argument type for variables being function arguments. More...
 

Public Member Functions

constexpr Variable ()=default
 Default constructor, constructing a variable, which is considered as not an actual variable. More...
 
constexpr std::size_t id () const noexcept
 Retrieves the id of the variable. More...
 
constexpr VariableType type () const noexcept
 Retrieves the type of the variable. More...
 
std::string name () const
 Retrieves the name of the variable. More...
 
std::string safe_name () const
 Retrieves a unique name of the variable of the form <type><id>. More...
 
constexpr std::size_t rank () const noexcept
 Retrieves the rank of the variable. More...
 

Static Public Attributes

static constexpr std::size_t BITSIZE = CHAR_BIT * sizeof(std::size_t)
 Number of bits available for the content. More...
 
static constexpr std::size_t RESERVED_FOR_TYPE = 3
 Number of bits reserved for the type. More...
 
static constexpr std::size_t RESERVED_FOR_RANK = 4
 Number of bits reserved for the rank. More...
 
static constexpr std::size_t RESERVED = RESERVED_FOR_RANK + RESERVED_FOR_TYPE
 Overall number of bits reserved. More...
 
static constexpr std::size_t AVAILABLE = BITSIZE - RESERVED
 Number of bits available for the id. More...
 
static const Variable NO_VARIABLE = Variable()
 Instance of an invalid variable. More...
 

Private Member Functions

 Variable (std::size_t id, VariableType type=VariableType::VT_REAL, std::size_t rank=0) noexcept
 Private constructor to be used by the VariablePool. More...
 

Private Attributes

friend VariablePool
 
std::size_t mContent = 0
 The content of the variable. More...
 

Friends

Comparison operators
bool operator== (Variable lhs, Variable rhs) noexcept
 Compares two variables. More...
 
bool operator!= (Variable lhs, Variable rhs) noexcept
 Compares two variables. More...
 
bool operator< (Variable lhs, Variable rhs) noexcept
 Compares two variables. More...
 
bool operator<= (Variable lhs, Variable rhs) noexcept
 Compares two variables. More...
 
bool operator> (Variable lhs, Variable rhs) noexcept
 Compares two variables. More...
 
bool operator>= (Variable lhs, Variable rhs) noexcept
 Compares two variables. More...
 

Detailed Description

A Variable represents an algebraic variable that can be used throughout carl.

Variables are basically bitvectors that contain [rank | id | type], called content.

  • The id is the identifier of this variable.
  • The type is the variable type.
  • The rank is zero be default, but can be used to create a custom variable ordering, as the comparison operators compare the whole content. The id and the type together form a unique identifier for a variable. If the VariablePool is used to construct variables (and we advise to do so), the id's will be consecutive starting with one for each variable type. The rank is meant to change the variable order when passing a set of variables to another context, for example a function. A single variable (identified by id and type) should not occur with two different rank values in the same context and hence such a comparison should never take place.

A variable with id zero is considered invalid. It can be used as a default argument and can be compared to Variable::NO_VARIABLE. Such a variable can only be constructed using the default constructor and its content will always be zero.

Although not templated, we keep the whole class inlined for efficiency purposes. Note that this way, any decent compiler removes the overhead introduced, while having gained strong type-definitions and thus the ability to provide operator overloading.

Moreover, notice that for small classes like this, pass-by-value could be faster than pass-by-ref. However, this depends much on the capabilities of the compiler.

Definition at line 85 of file Variable.h.

Member Typedef Documentation

◆ Arg

using carl::Variable::Arg = const Variable&

Argument type for variables being function arguments.

Definition at line 89 of file Variable.h.

Constructor & Destructor Documentation

◆ Variable() [1/2]

carl::Variable::Variable ( std::size_t  id,
VariableType  type = VariableType::VT_REAL,
std::size_t  rank = 0 
)
inlineexplicitprivatenoexcept

Private constructor to be used by the VariablePool.

Parameters
idThe identifier of the variable.
typeThe type.
rankThe rank.

Definition at line 105 of file Variable.h.

Here is the call graph for this function:

◆ Variable() [2/2]

constexpr carl::Variable::Variable ( )
constexprdefault

Default constructor, constructing a variable, which is considered as not an actual variable.

Such an invalid variable is stored in NO_VARIABLE, so use this if you need a default value for a variable.

Member Function Documentation

◆ id()

constexpr std::size_t carl::Variable::id ( ) const
inlineconstexprnoexcept

Retrieves the id of the variable.

Returns
Variable id.

Definition at line 123 of file Variable.h.

Here is the caller graph for this function:

◆ name()

std::string carl::Variable::name ( ) const

Retrieves the name of the variable.

Returns
Variable name.

Definition at line 8 of file Variable.cpp.

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

◆ rank()

constexpr std::size_t carl::Variable::rank ( ) const
inlineconstexprnoexcept

Retrieves the rank of the variable.

Returns
Variable rank.

Definition at line 153 of file Variable.h.

Here is the caller graph for this function:

◆ safe_name()

std::string carl::Variable::safe_name ( ) const

Retrieves a unique name of the variable of the form <type><id>.

While <type> consists of lowercase letters, <id> is a decimal number. This unique name is meant to be used wherever a unique but notationally simple identifier is required, for example when interfacing with other systems.

Returns
Variable name.

Definition at line 11 of file Variable.cpp.

Here is the call graph for this function:

◆ type()

constexpr VariableType carl::Variable::type ( ) const
inlineconstexprnoexcept

Retrieves the type of the variable.

Returns
Variable type.

Definition at line 131 of file Variable.h.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( Variable  lhs,
Variable  rhs 
)
friend

Compares two variables.

Note that for performance reasons, we compare the whole content of the variable (including the rank).

Note that the variable order is not the order of the variable id. We consider variables greater, if they are defined earlier, i.e. if they have a smaller id. Hence, the variables order and the order of the variable ids are reversed.

Parameters
lhsFirst variable.
rhsSecond variable.
Returns
lhs ~ rhs, ~ being the relation that is checked.

Definition at line 173 of file Variable.h.

◆ operator<

bool operator< ( Variable  lhs,
Variable  rhs 
)
friend

Compares two variables.

Note that for performance reasons, we compare the whole content of the variable (including the rank).

Note that the variable order is not the order of the variable id. We consider variables greater, if they are defined earlier, i.e. if they have a smaller id. Hence, the variables order and the order of the variable ids are reversed.

Parameters
lhsFirst variable.
rhsSecond variable.
Returns
lhs ~ rhs, ~ being the relation that is checked.

Definition at line 176 of file Variable.h.

◆ operator<=

bool operator<= ( Variable  lhs,
Variable  rhs 
)
friend

Compares two variables.

Note that for performance reasons, we compare the whole content of the variable (including the rank).

Note that the variable order is not the order of the variable id. We consider variables greater, if they are defined earlier, i.e. if they have a smaller id. Hence, the variables order and the order of the variable ids are reversed.

Parameters
lhsFirst variable.
rhsSecond variable.
Returns
lhs ~ rhs, ~ being the relation that is checked.

Definition at line 179 of file Variable.h.

◆ operator==

bool operator== ( Variable  lhs,
Variable  rhs 
)
friend

Compares two variables.

Note that for performance reasons, we compare the whole content of the variable (including the rank).

Note that the variable order is not the order of the variable id. We consider variables greater, if they are defined earlier, i.e. if they have a smaller id. Hence, the variables order and the order of the variable ids are reversed.

Parameters
lhsFirst variable.
rhsSecond variable.
Returns
lhs ~ rhs, ~ being the relation that is checked.

Definition at line 170 of file Variable.h.

◆ operator>

bool operator> ( Variable  lhs,
Variable  rhs 
)
friend

Compares two variables.

Note that for performance reasons, we compare the whole content of the variable (including the rank).

Note that the variable order is not the order of the variable id. We consider variables greater, if they are defined earlier, i.e. if they have a smaller id. Hence, the variables order and the order of the variable ids are reversed.

Parameters
lhsFirst variable.
rhsSecond variable.
Returns
lhs ~ rhs, ~ being the relation that is checked.

Definition at line 182 of file Variable.h.

◆ operator>=

bool operator>= ( Variable  lhs,
Variable  rhs 
)
friend

Compares two variables.

Note that for performance reasons, we compare the whole content of the variable (including the rank).

Note that the variable order is not the order of the variable id. We consider variables greater, if they are defined earlier, i.e. if they have a smaller id. Hence, the variables order and the order of the variable ids are reversed.

Parameters
lhsFirst variable.
rhsSecond variable.
Returns
lhs ~ rhs, ~ being the relation that is checked.

Definition at line 185 of file Variable.h.

Field Documentation

◆ AVAILABLE

constexpr std::size_t carl::Variable::AVAILABLE = BITSIZE - RESERVED
staticconstexpr

Number of bits available for the id.

Definition at line 200 of file Variable.h.

◆ BITSIZE

constexpr std::size_t carl::Variable::BITSIZE = CHAR_BIT * sizeof(std::size_t)
staticconstexpr

Number of bits available for the content.

Definition at line 191 of file Variable.h.

◆ mContent

std::size_t carl::Variable::mContent = 0
private

The content of the variable.

In order to keep a variable object small, this is the only data member. All other data (like names or alike) are stored in the VariablePool.

Definition at line 97 of file Variable.h.

◆ NO_VARIABLE

const Variable carl::Variable::NO_VARIABLE = Variable()
static

Instance of an invalid variable.

Definition at line 203 of file Variable.h.

◆ RESERVED

constexpr std::size_t carl::Variable::RESERVED = RESERVED_FOR_RANK + RESERVED_FOR_TYPE
staticconstexpr

Overall number of bits reserved.

Definition at line 197 of file Variable.h.

◆ RESERVED_FOR_RANK

constexpr std::size_t carl::Variable::RESERVED_FOR_RANK = 4
staticconstexpr

Number of bits reserved for the rank.

Definition at line 195 of file Variable.h.

◆ RESERVED_FOR_TYPE

constexpr std::size_t carl::Variable::RESERVED_FOR_TYPE = 3
staticconstexpr

Number of bits reserved for the type.

Definition at line 193 of file Variable.h.

◆ VariablePool

friend carl::Variable::VariablePool
private

Definition at line 86 of file Variable.h.


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