delpi
0.0.1
DElta-complete LP solver
Toggle main menu visibility
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
18
namespace
delpi
{
19
20
bool
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
}
43
std::size_t VariableSet::size()
const
{
return
std::ranges::count(
vars_
,
true
); }
44
bool
VariableSet::empty
()
const
{
45
return
std::ranges::all_of(
vars_
, [](
const
bool
var) {
return
!var; });
46
}
47
std::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
}
55
Variable
VariableSet::Get
(Id
id
)
const
{
56
if
(
Contains
(
id
))
return
Variable
{
id
};
57
DELPI_OUT_OF_RANGE_FMT(
"Variable with id {} not found in the set"
,
id
);
58
}
59
bool
VariableSet::Contains
(
const
Id
id
)
const
{
60
return
id
>=
min_id_
&& IdSetIndex(
id
) <
vars_
.size() &&
vars_
.at(IdSetIndex(
id
));
61
}
62
bool
VariableSet::Remove
(
const
Id
id
) {
63
if
(!
Contains
(
id
))
return
false
;
64
vars_
.at(IdSetIndex(
id
)) =
false
;
65
return
true
;
66
}
67
void
VariableSet::Clear
() {
68
vars_
.clear();
69
min_id_
= std::numeric_limits<Id>::max();
70
}
71
72
std::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
delpi::VariableSet
Set of variables optimised for fast access at the cost of memory.
Definition
VariableSet.h:33
delpi::VariableSet::min_id_
Id min_id_
Minimum id of the variables in the set.
Definition
VariableSet.h:198
delpi::VariableSet::empty
bool empty() const
@checked{empty, variable set}
Definition
VariableSet.cpp:44
delpi::VariableSet::Remove
bool Remove(const Variable &var)
Remove a var from the set.
Definition
VariableSet.h:149
delpi::VariableSet::Contains
bool Contains(const Variable &var) const
Check if the var is in the set.
Definition
VariableSet.h:87
delpi::VariableSet::Clear
void Clear()
Clear the set of all variables.
Definition
VariableSet.cpp:67
delpi::VariableSet::vars_
std::vector< bool > vars_
Vector tracking the presence of each variable in the set.
Definition
VariableSet.h:199
delpi::VariableSet::Get
Variable Get(Id id) const
Use the id to get the variable in the set, if it exists.
Definition
VariableSet.cpp:55
delpi::VariableSet::Insert
bool Insert(const Variable &var)
Insert a var to the set.
Definition
VariableSet.h:95
delpi::Variable
Real symbolic variable.
Definition
Variable.h:20
delpi
Global namespace for the delpi library.
delpi
symbolic
VariableSet.cpp
Generated by
1.17.0