dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
Interval.h
1
10#pragma once
11
12#include <iosfwd>
13#include <string>
14
15#include "dlinear/libs/libgmp.h"
16#include "dlinear/util/definitions.h"
17
18namespace dlinear {
19
20class Interval {
21 public:
30 static Interval FromString(const std::string &s);
31 explicit Interval(const mpq_class &val) : lb_(val), ub_(val) {}
32 Interval(const mpq_class &lb, const mpq_class &ub);
33 Interval(const Interval &other) = default;
34 Interval(Interval &&other) noexcept = default;
35 Interval &operator=(const Interval &other) = default;
36 Interval &operator=(Interval &&other) noexcept = default;
37 [[nodiscard]] bool is_empty() const { return lb_ == 1 && ub_ == 0; }
38 [[nodiscard]] bool is_degenerated() const { return lb_ == ub_; }
39 [[nodiscard]] bool is_bisectable() const { return lb_ < ub_; }
40 [[nodiscard]] const mpq_class &lb() const { return lb_; }
41 [[nodiscard]] const mpq_class &ub() const { return ub_; }
42 [[nodiscard]] mpq_class mid() const { return (lb_ + ub_) / 2; }
43 [[nodiscard]] mpq_class diam() const { return is_empty() ? mpq_class(0) : mpq_class(ub_ - lb_); }
44 [[nodiscard]] std::pair<Interval, Interval> bisect(const mpq_class &p) const;
45
46 bool operator==(const Interval &other) const { return lb_ == other.lb_ && ub_ == other.ub_; }
47 Interval &operator=(const mpq_t &val) {
48 mpq_set(lb_.get_mpq_t(), val);
49 mpq_set(ub_.get_mpq_t(), val);
50 return *this;
51 }
52 Interval &operator=(const mpq_class &val) {
53 lb_ = ub_ = val;
54 return *this;
55 }
56
57 void set_empty() {
58 lb_ = 1;
59 ub_ = 0;
60 }
61
62 ARITHMETIC_OPERATORS(Interval)
63 GENERIC_ARITHMETIC_OPERATORS(Interval, mpq_class &)
64
65 std::ostream &PrintToStream(std::ostream &os, const mpq_class &ninfinity, const mpq_class &infinity) const;
66
67 private:
68 mpq_class lb_, ub_;
69};
70
71std::ostream &operator<<(std::ostream &os, const Interval &iv);
72
73} // namespace dlinear
74
75#ifdef DLINEAR_INCLUDE_FMT
76
77#include "dlinear/util/logging.h"
78
79OSTREAM_FORMATTER(dlinear::Interval);
80
81#endif
static Interval FromString(const std::string &s)
Constructs an interval from a string.
Definition Interval.cpp:105
Global namespace for the dlinear library.