Skip to content

Commit 081f617

Browse files
Refactor var class: replace to_string and to_json methods with friend functions; add lb and ub accessors for improved encapsulation
1 parent 7fd8812 commit 081f617

2 files changed

Lines changed: 26 additions & 25 deletions

File tree

include/linspire.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,11 @@ namespace linspire
226226
public:
227227
var(const utils::inf_rational &lb = utils::inf_rational(utils::rational::negative_infinite), const utils::inf_rational &ub = utils::inf_rational(utils::rational::positive_infinite)) noexcept;
228228

229-
[[nodiscard]] std::string to_string() const noexcept;
230-
[[nodiscard]] json::json to_json() const noexcept;
229+
[[nodiscard]] utils::inf_rational lb() const noexcept;
230+
[[nodiscard]] utils::inf_rational ub() const noexcept;
231+
232+
friend std::string to_string(const var &x) noexcept;
233+
friend json::json to_json(const var &x) noexcept;
231234

232235
private:
233236
std::map<utils::inf_rational, std::set<std::shared_ptr<constraint>>> lbs, ubs;
@@ -236,6 +239,8 @@ namespace linspire
236239

237240
[[nodiscard]] std::string to_string(const solver &s) noexcept;
238241
[[nodiscard]] json::json to_json(const solver &s) noexcept;
242+
[[nodiscard]] std::string to_string(const var &x) noexcept;
243+
[[nodiscard]] json::json to_json(const var &x) noexcept;
239244
[[nodiscard]] json::json to_json(const utils::rational &r) noexcept;
240245
[[nodiscard]] json::json to_json(const utils::inf_rational &r) noexcept;
241246
[[nodiscard]] json::json to_json(const utils::lin &l) noexcept;

src/linspire.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ namespace linspire
2828
return slack;
2929
}
3030

31-
utils::inf_rational solver::lb(const utils::var x) const noexcept { return vars[x].lbs.rbegin()->first; }
32-
utils::inf_rational solver::ub(const utils::var x) const noexcept { return vars[x].ubs.begin()->first; }
31+
utils::inf_rational solver::lb(const utils::var x) const noexcept { return vars[x].lb(); }
32+
utils::inf_rational solver::ub(const utils::var x) const noexcept { return vars[x].ub(); }
3333
utils::inf_rational solver::val(const utils::var x) const noexcept { return vars[x].val; }
3434

3535
bool solver::new_eq(const utils::lin &lhs, const utils::lin &rhs, std::shared_ptr<constraint> reason) noexcept
@@ -350,32 +350,16 @@ namespace linspire
350350
tableau.emplace(x, std::move(l));
351351
}
352352

353-
var::var(const utils::inf_rational &lb, const utils::inf_rational &ub) noexcept
354-
{
355-
assert(lb <= ub);
356-
lbs.emplace(lb, std::set<std::shared_ptr<constraint>>());
357-
ubs.emplace(ub, std::set<std::shared_ptr<constraint>>());
358-
}
353+
var::var(const utils::inf_rational &lb, const utils::inf_rational &ub) noexcept { assert(lb < ub); }
359354

360-
std::string var::to_string() const noexcept { return utils::to_string(val) + " [" + utils::to_string(lbs.rbegin()->first) + ", " + utils::to_string(ubs.begin()->first) + "]"; }
361-
362-
json::json var::to_json() const noexcept
363-
{
364-
json::json j = linspire::to_json(val);
365-
auto &lb = lbs.rbegin()->first;
366-
if (!is_infinite(lb))
367-
j["lb"] = linspire::to_json(lb);
368-
auto &ub = ubs.begin()->first;
369-
if (!is_infinite(ub))
370-
j["ub"] = linspire::to_json(ub);
371-
return j;
372-
}
355+
utils::inf_rational var::lb() const noexcept { return lbs.empty() ? utils::rational::negative_infinite : lbs.rbegin()->first; }
356+
utils::inf_rational var::ub() const noexcept { return ubs.empty() ? utils::rational::positive_infinite : ubs.begin()->first; }
373357

374358
std::string to_string(const solver &s) noexcept
375359
{
376360
std::string str;
377361
for (utils::var i = 0; i < s.vars.size(); ++i)
378-
str += "x" + std::to_string(i) + " = " + s.vars[i].to_string() + "\n";
362+
str += "x" + std::to_string(i) + " = " + to_string(s.vars.at(i)) + "\n";
379363
for (const auto &[v, expr] : s.tableau)
380364
str += "x" + std::to_string(v) + " = " + utils::to_string(expr) + "\n";
381365
return str;
@@ -386,7 +370,7 @@ namespace linspire
386370
json::json j;
387371
json::json j_vars;
388372
for (utils::var i = 0; i < s.vars.size(); ++i)
389-
j_vars["x" + std::to_string(i)] = s.vars[i].to_json();
373+
j_vars["x" + std::to_string(i)] = to_json(s.vars.at(i));
390374
j["vars"] = j_vars;
391375
json::json j_tableau;
392376
for (const auto &[v, expr] : s.tableau)
@@ -397,6 +381,18 @@ namespace linspire
397381

398382
json::json to_json(const utils::rational &r) noexcept { return json::json{{"num", r.numerator()}, {"den", r.denominator()}}; }
399383

384+
std::string to_string(const var &x) noexcept { return utils::to_string(x.val) + " [" + utils::to_string(x.lb()) + ", " + utils::to_string(x.ub()) + "]"; }
385+
386+
json::json to_json(const var &x) noexcept
387+
{
388+
json::json j = linspire::to_json(x.val);
389+
if (!x.lbs.empty())
390+
j["lb"] = linspire::to_json(x.lbs.rbegin()->first);
391+
if (!x.ubs.empty())
392+
j["ub"] = linspire::to_json(x.ubs.begin()->first);
393+
return j;
394+
}
395+
400396
json::json to_json(const utils::inf_rational &r) noexcept
401397
{
402398
json::json j = to_json(r.get_rational());

0 commit comments

Comments
 (0)