Skip to content

Commit e19c108

Browse files
Refactor constraint pointer types: update shared_ptr usage to const in var class methods for improved type safety
1 parent 59a58e5 commit e19c108

3 files changed

Lines changed: 21 additions & 21 deletions

File tree

include/linspire.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ namespace linspire
182182
*
183183
* @return A constant reference to a vector of shared pointers to constraint objects representing the last conflict explanation.
184184
*/
185-
[[nodiscard]] const std::vector<std::shared_ptr<constraint>> &get_conflict() const noexcept { return cnfl; }
185+
[[nodiscard]] const std::vector<std::shared_ptr<const constraint>> &get_conflict() const noexcept { return cnfl; }
186186

187187
friend std::string to_string(const solver &s) noexcept;
188188
friend json::json to_json(const solver &s) noexcept;
@@ -201,11 +201,11 @@ namespace linspire
201201

202202
void new_row(const utils::var x, utils::lin &&l) noexcept;
203203

204-
std::vector<var> vars; // index is the variable id
205-
std::unordered_map<std::string, utils::var> exprs; // the expressions (string to numeric variable) for which already exist slack variables..
206-
std::map<utils::var, utils::lin> tableau; // variable -> expression
207-
std::vector<std::set<utils::var>> t_watches; // for each variable `v`, a set of tableau rows watching `v`..
208-
std::vector<std::shared_ptr<constraint>> cnfl; // the last conflict explanation..
204+
std::vector<var> vars; // index is the variable id
205+
std::unordered_map<std::string, utils::var> exprs; // the expressions (string to numeric variable) for which already exist slack variables..
206+
std::map<utils::var, utils::lin> tableau; // basic variable -> expression
207+
std::vector<std::set<utils::var>> t_watches; // for each variable `v`, a set of tableau rows watching `v`..
208+
std::vector<std::shared_ptr<const constraint>> cnfl; // the last conflict explanation..
209209
};
210210

211211
class constraint

include/var.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ namespace linspire
2525
friend json::json to_json(const var &x) noexcept;
2626

2727
private:
28-
void set_lb(const utils::inf_rational &v, std::shared_ptr<constraint> reason = nullptr) noexcept;
29-
void unset_lb(const utils::inf_rational &v, std::shared_ptr<constraint> reason) noexcept;
30-
void set_ub(const utils::inf_rational &v, std::shared_ptr<constraint> reason = nullptr) noexcept;
31-
void unset_ub(const utils::inf_rational &v, std::shared_ptr<constraint> reason) noexcept;
28+
void set_lb(const utils::inf_rational &v, std::shared_ptr<const constraint> reason = nullptr) noexcept;
29+
void unset_lb(const utils::inf_rational &v, std::shared_ptr<const constraint> reason) noexcept;
30+
void set_ub(const utils::inf_rational &v, std::shared_ptr<const constraint> reason = nullptr) noexcept;
31+
void unset_ub(const utils::inf_rational &v, std::shared_ptr<const constraint> reason) noexcept;
3232

3333
private:
34-
const utils::var id; // the id of this variable in the solver..
35-
std::map<utils::inf_rational, std::set<std::shared_ptr<constraint>>> lbs, ubs; // the lower and upper bounds with their reasons..
36-
utils::inf_rational val; // the current value of this variable..
34+
const utils::var id; // the id of this variable in the solver..
35+
std::map<utils::inf_rational, std::set<std::shared_ptr<const constraint>>> lbs, ubs; // the lower and upper bounds with their reasons..
36+
utils::inf_rational val; // the current value of this variable..
3737
};
3838

3939
[[nodiscard]] std::string to_string(const var &x) noexcept;

src/var.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ namespace linspire
99
utils::inf_rational var::get_lb() const noexcept { return lbs.empty() ? utils::rational::negative_infinite : lbs.rbegin()->first; }
1010
utils::inf_rational var::get_ub() const noexcept { return ubs.empty() ? utils::rational::positive_infinite : ubs.begin()->first; }
1111

12-
void var::set_lb(const utils::inf_rational &v, std::shared_ptr<constraint> reason) noexcept
12+
void var::set_lb(const utils::inf_rational &v, std::shared_ptr<const constraint> reason) noexcept
1313
{
1414
assert(v <= get_ub()); // we cannot set a lower bound greater than the current upper bound..
1515
if (reason)
1616
{ // we add a new lower bound `v` with the given reason..
1717
if (auto it = lbs.find(v); it != lbs.end())
1818
it->second.insert(reason); // we add the reason to the existing lower bound `v`..
1919
else
20-
lbs.emplace(v, std::set<std::shared_ptr<constraint>>{reason}); // we create a new lower bound `v`..
20+
lbs.emplace(v, std::set<std::shared_ptr<const constraint>>{reason}); // we create a new lower bound `v`..
2121
}
2222
else
2323
{ // we remove all the lower bounds that are less than `v`..
2424
auto it = lbs.upper_bound(v);
2525
lbs.erase(lbs.begin(), it);
26-
lbs.emplace(v, std::set<std::shared_ptr<constraint>>());
26+
lbs.emplace(v, std::set<std::shared_ptr<const constraint>>());
2727
}
2828
}
29-
void var::unset_lb(const utils::inf_rational &v, std::shared_ptr<constraint> reason) noexcept
29+
void var::unset_lb(const utils::inf_rational &v, std::shared_ptr<const constraint> reason) noexcept
3030
{
3131
assert(lbs.find(v) != lbs.end());
3232
assert(reason);
@@ -36,24 +36,24 @@ namespace linspire
3636
lbs.erase(it);
3737
}
3838

39-
void var::set_ub(const utils::inf_rational &v, std::shared_ptr<constraint> reason) noexcept
39+
void var::set_ub(const utils::inf_rational &v, std::shared_ptr<const constraint> reason) noexcept
4040
{
4141
assert(v >= get_lb()); // we cannot set an upper bound less than the current lower bound..
4242
if (reason)
4343
{ // we add a new upper bound `v` with the given reason..
4444
if (auto it = ubs.find(v); it != ubs.end())
4545
it->second.insert(reason); // we add the reason to the existing upper bound `v`..
4646
else
47-
ubs.emplace(v, std::set<std::shared_ptr<constraint>>{reason}); // we create a new upper bound `v`..
47+
ubs.emplace(v, std::set<std::shared_ptr<const constraint>>{reason}); // we create a new upper bound `v`..
4848
}
4949
else
5050
{ // we remove all the upper bounds that are greater than `v`..
5151
auto it = ubs.lower_bound(v);
5252
ubs.erase(it, ubs.end());
53-
ubs.emplace(v, std::set<std::shared_ptr<constraint>>());
53+
ubs.emplace(v, std::set<std::shared_ptr<const constraint>>());
5454
}
5555
}
56-
void var::unset_ub(const utils::inf_rational &v, std::shared_ptr<constraint> reason) noexcept
56+
void var::unset_ub(const utils::inf_rational &v, std::shared_ptr<const constraint> reason) noexcept
5757
{
5858
assert(ubs.find(v) != ubs.end());
5959
assert(reason);

0 commit comments

Comments
 (0)