carl  24.04
Computer ARithmetic Library
VariablePool.cpp
Go to the documentation of this file.
1 /**
2  * @file VariablePool.cpp
3  * @author Sebastian Junges
4  */
5 
6 #include "VariablePool.h"
7 
8 #include "../initialize.h" // TODO get rid of this include
9 #include <carl-common/config.h>
11 
12 namespace carl
13 {
14 
16  CARL_LOG_INFO("carl.varpool", "Constructor called");
17  clear();
18  set_prefix();
19 }
20 
22  std::size_t tmp = 0;
23  {
25  tmp = nextID(type)++;
26  }
27  CARL_LOG_DEBUG("carl.varpool", "New variable of type " << type << " with id " << tmp);
28  return Variable(tmp, type);
29 }
30 
31 Variable VariablePool::get_fresh_variable(const std::string& name, VariableType type) {
32  if (name.substr(0, mVariablePrefix.size()) == mVariablePrefix) {
33  CARL_LOG_WARN("carl", "The prefix for auxiliary variable names \"" << mVariablePrefix << "\" is a prefix for the variable name \"" << name << "\".");
34  }
35  Variable tmp = get_fresh_variable(type);
36  set_name(tmp, name);
37  return tmp;
38 }
39 
41  Variable res = get_fresh_variable(type);
42  if (res.id() >= mPersistentVariables.size()) {
43  mPersistentVariables.resize(res.id()+1, std::make_pair(Variable::NO_VARIABLE, ""));
44  }
45  mPersistentVariables[res.id()] = std::make_pair(res, "");
46  return res;
47 }
48 
51  mPersistentVariables[res.id()] = std::make_pair(res, name);
52  set_name(res, name);
53  return res;
54 }
55 
56 Variable VariablePool::find_variable_with_name(const std::string& name) const noexcept {
57  for (const auto& v: mVariableNames) {
58  if (v.second == name) return v.first;
59  }
60  return Variable::NO_VARIABLE;
61 }
62 
63 std::string VariablePool::get_name(Variable v, bool variableName) const {
64  if (v.id() == 0) return "NO_VARIABLE";
65  if (variableName) {
67  auto it = mVariableNames.find(v);
68  if (it != mVariableNames.end()) {
69  return it->second;
70  }
71  }
72  switch (v.type()) {
74  return mVariablePrefix + "b_" + std::to_string(v.id());
76  return mVariablePrefix + "r_" + std::to_string(v.id());
78  return mVariablePrefix + "i_" + std::to_string(v.id());
80  return mVariablePrefix + "u_" + std::to_string(v.id());
82  return mVariablePrefix + "bv_" + std::to_string(v.id());
83  default:
84  CARL_LOG_ERROR("carl", "Variable has invalid type: " << v.type());
85  assert(false);
86  return "";
87  }
88 }
89 
90 void VariablePool::set_name(Variable v, const std::string& name) {
92  mVariableNames[v] = name;
93 }
94 
95 }
#define FRESHVAR_LOCK_GUARD
Definition: VariablePool.h:75
#define SETNAME_LOCK_GUARD
Definition: VariablePool.h:76
A small wrapper that configures logging for carl.
#define CARL_LOG_WARN(channel, msg)
Definition: carl-logging.h:41
#define CARL_LOG_ERROR(channel, msg)
Definition: carl-logging.h:40
#define CARL_LOG_DEBUG(channel, msg)
Definition: carl-logging.h:43
#define CARL_LOG_INFO(channel, msg)
Definition: carl-logging.h:42
carl is the main namespace for the library.
VariableType
Several types of variables are supported.
Definition: Variable.h:28
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
static const Variable NO_VARIABLE
Instance of an invalid variable.
Definition: Variable.h:203
constexpr std::size_t id() const noexcept
Retrieves the id of the variable.
Definition: Variable.h:123
constexpr VariableType type() const noexcept
Retrieves the type of the variable.
Definition: Variable.h:131
void set_name(Variable v, const std::string &name)
Add a name for a given Variable.
std::string get_name(Variable v, bool variableName=true) const
Get a human-readable name for the given variable.
Variable get_fresh_variable(VariableType type=VariableType::VT_REAL) noexcept
Get a variable which was not used before.
Variable get_fresh_persistent_variable(VariableType type=VariableType::VT_REAL) noexcept
Variable find_variable_with_name(const std::string &name) const noexcept
Searches in the friendly names list for a variable with the given name.
VariablePool() noexcept
Private default constructor.
std::string mVariablePrefix
Stores a prefix for printing variables that have no human-readable name.
Definition: VariablePool.h:69
void clear() noexcept
Clears everything already created in this pool.
Definition: VariablePool.h:109
std::map< Variable, std::string > mVariableNames
Stores human-readable names for variables that can be set via setVariableName().
Definition: VariablePool.h:64
void set_prefix(std::string prefix="_") noexcept
Sets the prefix used when printing anonymous variables.
Definition: VariablePool.h:154
std::vector< std::pair< Variable, std::string > > mPersistentVariables
Contains persistent variables that are restored after clear was called.
Definition: VariablePool.h:59