delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
VariableSet.cpp
1
6
7#include "delpi/symbolic/VariableSet.h"
8
9#include <limits>
10#include <ostream>
11#include <vector>
12
13#include "delpi/util/error.h"
14
15#define VarSetIndex(var) (var.id() - min_id_)
16#define IdSetIndex(id) (id - min_id_)
17
18namespace delpi {
19
20bool VariableSet::Insert(const Id id) {
21 if (min_id_ == std::numeric_limits<Id>::max()) {
22 DELPI_ASSERT(vars_.empty(), "VariableSet is not empty but min_id is not set");
23 min_id_ = id;
24 vars_.emplace_back(true);
25 return true;
26 }
27 if (id < min_id_) {
28 vars_.resize(vars_.size() + min_id_ - id, false);
29 std::rotate(vars_.begin(), vars_.begin() + static_cast<std::int64_t>(vars_.size() - (min_id_ - id)), vars_.end());
30 min_id_ = id;
31 vars_.front() = true;
32 return true;
33 }
34 if (id >= min_id_ + vars_.size()) {
35 vars_.resize(id - min_id_ + 1);
36 vars_.back() = true;
37 return true;
38 }
39 const bool was_present = vars_.at(IdSetIndex(id));
40 vars_.at(IdSetIndex(id)) = true;
41 return !was_present;
42}
43std::size_t VariableSet::size() const { return std::ranges::count(vars_, true); }
44bool VariableSet::empty() const {
45 return std::ranges::all_of(vars_, [](const bool var) { return !var; });
46}
47std::vector<Variable> VariableSet::variables() const {
48 std::vector<Variable> vars;
49 vars.reserve(vars_.size());
50 for (std::size_t i = 0; i < vars_.size(); ++i) {
51 if (vars_.at(i)) vars.emplace_back(min_id_ + i);
52 }
53 return vars;
54}
56 if (Contains(id)) return Variable{id};
57 DELPI_OUT_OF_RANGE_FMT("Variable with id {} not found in the set", id);
58}
59bool VariableSet::Contains(const Id id) const {
60 return id >= min_id_ && IdSetIndex(id) < vars_.size() && vars_.at(IdSetIndex(id));
61}
62bool VariableSet::Remove(const Id id) {
63 if (!Contains(id)) return false;
64 vars_.at(IdSetIndex(id)) = false;
65 return true;
66}
68 vars_.clear();
69 min_id_ = std::numeric_limits<Id>::max();
70}
71
72std::ostream& operator<<(std::ostream& os, const VariableSet& var_set) {
73 os << "{";
74 bool is_first = true;
75 for (const Variable var : var_set.variables()) {
76 os << (is_first ? "" : ", ") << var;
77 is_first = false;
78 }
79 return os << "}";
80}
81
82} // namespace delpi
Set of variables optimised for fast access at the cost of memory.
Definition VariableSet.h:33
Id min_id_
Minimum id of the variables in the set.
bool empty() const
@checked{empty, variable set}
bool Remove(const Variable &var)
Remove a var from the set.
bool Contains(const Variable &var) const
Check if the var is in the set.
Definition VariableSet.h:87
void Clear()
Clear the set of all variables.
std::vector< bool > vars_
Vector tracking the presence of each variable in the set.
Variable Get(Id id) const
Use the id to get the variable in the set, if it exists.
bool Insert(const Variable &var)
Insert a var to the set.
Definition VariableSet.h:95
Real symbolic variable.
Definition Variable.h:20
Global namespace for the delpi library.