delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
Variable.cpp
1
6#include "delpi/symbolic/Variable.h"
7
8#include <atomic>
9#include <limits>
10#include <ostream>
11#include <string>
12#include <utility>
13#include <vector>
14
15#include "delpi/util/error.h"
16
17namespace delpi {
18std::vector<std::string> Variable::names_{{"dummy"}};
19const Variable::Id Variable::dummy_id{std::numeric_limits<Id>::max()};
20
21Variable::Id Variable::GetNextId() {
22 static std::atomic<Id> next_id{0};
23 const std::size_t counter = next_id.fetch_add(1);
24 return counter;
25}
26Variable::Variable(const Id id) : id_{id} {
27 DELPI_ASSERT(id < names_.size(), "The id is out of bounds. Make sure the variable had been created before.");
28}
29
30Variable::Variable(std::string name) : id_{GetNextId()} {
31 DELPI_ASSERT(id_ < std::numeric_limits<Id>::max(), "The ID of the variable has reached the maximum value.");
32 names_.push_back(std::move(name));
33 DELPI_ASSERT(names_.size() == id_ + 2u, "The size of the names vector is not equal to the ID + dummy string.");
34}
35
36std::ostream &operator<<(std::ostream &os, const Variable &var) { return os << var.name(); }
37
38} // namespace delpi
Real symbolic variable.
Definition Variable.h:20
static std::vector< std::string > names_
Names of all existing variables.
Definition Variable.h:67
Id id_
Unique identifier.
Definition Variable.h:74
static const Id dummy_id
ID of the dummy variable.
Definition Variable.h:24
Variable()
Construct a new dummy variable object.
Definition Variable.h:34
static Id GetNextId()
Get the next unique identifier for a variable.
Definition Variable.cpp:21
Global namespace for the delpi library.