dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
BoundIterator.cpp
1
6#include "BoundIterator.h"
7
8#include <algorithm>
9#include <compare>
10#include <ostream>
11#include <vector>
12
13#include "dlinear/util/exception.h"
14
15namespace dlinear {
16
17const typename BoundIterator::vector_type BoundIterator::default_empty_vector_{};
18
20 : begin_bounds_it_(default_empty_vector_.cend()),
21 bounds_it_(default_empty_vector_.cend()),
22 end_bounds_it_(default_empty_vector_.cend()),
23 begin_nq_bounds_it_(default_empty_vector_.cend()),
24 nq_bounds_it_(default_empty_vector_.cend()),
25 end_nq_bounds_it_(default_empty_vector_.cend()) {
26 DLINEAR_ASSERT(begin_bounds_it_ <= bounds_it_ && bounds_it_ <= end_bounds_it_, "Bounds iterator is out of bounds.");
28 "Non-equal bounds iterator is out of bounds.");
29}
30
31BoundIterator::BoundIterator(BoundIterator::internal_iterator begin_bounds_it,
32 BoundIterator::internal_iterator end_bounds_it,
33 BoundIterator::internal_iterator begin_nq_bounds_it,
34 BoundIterator::internal_iterator end_nq_bounds_it)
35 : begin_bounds_it_(begin_bounds_it),
36 bounds_it_(begin_bounds_it),
37 end_bounds_it_(std::max(end_bounds_it, begin_bounds_it)),
38 begin_nq_bounds_it_(begin_nq_bounds_it),
39 nq_bounds_it_(begin_nq_bounds_it),
40 end_nq_bounds_it_(std::max(end_nq_bounds_it, begin_nq_bounds_it)) {
41 DLINEAR_ASSERT(begin_bounds_it_ <= bounds_it_ && bounds_it_ <= end_bounds_it_, "Bounds iterator is out of bounds.");
43 "Non-equal bounds iterator is out of bounds.");
44}
45
46BoundIterator::BoundIterator(std::pair<BoundIterator::internal_iterator, BoundIterator::internal_iterator> bounds,
47 std::pair<BoundIterator::internal_iterator, BoundIterator::internal_iterator> nq_bounds)
48 : begin_bounds_it_(bounds.first),
49 bounds_it_(bounds.first),
50 end_bounds_it_(std::max(bounds.second, bounds.first)),
51 begin_nq_bounds_it_(nq_bounds.first),
52 nq_bounds_it_(nq_bounds.first),
53 end_nq_bounds_it_(std::max(nq_bounds.second, nq_bounds.first)) {
54 DLINEAR_ASSERT(begin_bounds_it_ <= bounds_it_ && bounds_it_ <= end_bounds_it_, "Bounds iterator is out of bounds.");
56 "Non-equal bounds iterator is out of bounds.");
57}
58BoundIterator::BoundIterator(BoundIterator::internal_iterator begin_bounds_it,
59 BoundIterator::internal_iterator end_bounds_it)
60 : begin_bounds_it_(begin_bounds_it),
61 bounds_it_(begin_bounds_it),
62 end_bounds_it_(std::max(end_bounds_it, begin_bounds_it)),
63 begin_nq_bounds_it_(default_empty_vector_.cend()),
64 nq_bounds_it_(default_empty_vector_.cend()),
65 end_nq_bounds_it_(default_empty_vector_.cend()) {
66 DLINEAR_ASSERT(begin_bounds_it_ <= bounds_it_ && bounds_it_ <= end_bounds_it_, "Bounds iterator is out of bounds.");
68 "Non-equal bounds iterator is out of bounds.");
69}
70BoundIterator::BoundIterator(std::pair<BoundIterator::internal_iterator, BoundIterator::internal_iterator> bounds)
71 : begin_bounds_it_(bounds.first),
72 bounds_it_(bounds.first),
73 end_bounds_it_(std::max(bounds.second, bounds.first)),
74 begin_nq_bounds_it_(default_empty_vector_.cend()),
75 nq_bounds_it_(default_empty_vector_.cend()),
76 end_nq_bounds_it_(default_empty_vector_.cend()) {
77 DLINEAR_ASSERT(begin_bounds_it_ <= bounds_it_ && bounds_it_ <= end_bounds_it_, "Bounds iterator is out of bounds.");
79 "Non-equal bounds iterator is out of bounds.");
80}
81
82BoundIterator &BoundIterator::operator++() {
84 ++bounds_it_;
85 } else if (nq_bounds_it_ != end_nq_bounds_it_) {
87 }
88 return *this;
89}
90
91const BoundIterator BoundIterator::operator++(int) {
92 BoundIterator tmp = *this;
93 ++*this;
94 return tmp;
95}
96
97BoundIterator &BoundIterator::operator--() {
100 } else if (bounds_it_ != begin_bounds_it_) {
101 --bounds_it_;
102 }
103 return *this;
104}
105const BoundIterator BoundIterator::operator--(int) {
106 BoundIterator tmp = *this;
107 --*this;
108 return tmp;
109}
110typename BoundIterator::value_type BoundIterator::operator[](int i) const {
111 const int distance = std::distance(begin_bounds_it_, end_bounds_it_);
112 return i < distance ? *(bounds_it_ + i) : *(nq_bounds_it_ + i - distance);
113}
119void BoundIterator::explanation(LiteralSet &explanation) const {
120 auto [bound_it, end_bound_it] = bounds();
121 for (; bound_it != end_bound_it; ++bound_it) {
122 explanation.insert(bound_it->explanation.begin(), bound_it->explanation.end());
123 explanation.insert(bound_it->theory_literal);
124 }
125 auto [nq_bound_it, end_nq_bound_it] = nq_bounds();
126 for (; nq_bound_it != end_nq_bound_it; ++nq_bound_it) {
127 explanation.insert(nq_bound_it->explanation.begin(), nq_bound_it->explanation.end());
128 explanation.insert(nq_bound_it->theory_literal);
129 }
130}
131std::set<LiteralSet> BoundIterator::explanations(const std::optional<Literal> &lit) const {
132 std::set<LiteralSet> explanations;
134 return explanations;
135}
136void BoundIterator::explanations(std::set<LiteralSet> &explanations, const std::optional<Literal> &lit) const {
137 auto [bound_it, end_bound_it] = bounds();
138 for (; bound_it != end_bound_it; ++bound_it) {
140 if (lit.has_value()) explanation.insert(lit.value());
141 explanation.insert(bound_it->explanation.begin(), bound_it->explanation.end());
142 explanation.insert(bound_it->theory_literal);
144 }
145 auto [nq_bound_it, end_nq_bound_it] = nq_bounds();
146 for (; nq_bound_it != end_nq_bound_it; ++nq_bound_it) {
148 if (lit.has_value()) explanation.insert(lit.value());
149 explanation.insert(nq_bound_it->explanation.begin(), nq_bound_it->explanation.end());
150 explanation.insert(nq_bound_it->theory_literal);
152 }
153}
154
155std::ostream &operator<<(std::ostream &os, const BoundIterator &violation) {
156 BoundIterator it{violation.bounds(), violation.nq_bounds()};
157 os << "BoundIterator{";
158 for (size_t i = 0; it; ++it, ++i) {
159 os << *it;
160 if (i + 1 < it.size()) {
161 os << ", ";
162 }
163 }
164 return os << "}";
165}
166
167} // namespace dlinear
BoundIterator class.
const internal_iterator end_nq_bounds_it_
End iterator of the non-equal bounds.
internal_iterator nq_bounds_it_
Iterator to the current non-equal bound.
std::pair< internal_iterator, internal_iterator > nq_bounds() const
Return the pair of iterators to the non-equal bounds.
std::pair< internal_iterator, internal_iterator > bounds() const
Return the pair of iterators to the bounds.
LiteralSet explanation() const
Produce and explanation formed by all the theory literals present in the violation.
internal_iterator bounds_it_
Iterator to the current bound.
static const vector_type default_empty_vector_
Default empty vector. Used for default construction.
const internal_iterator begin_nq_bounds_it_
Begin iterator to the first non-equal bound.
BoundIterator()
Construct an empty iterator.
std::set< LiteralSet > explanations(const std::optional< Literal > &lit={}) const
Produce a set of explanations.
const internal_iterator end_bounds_it_
End iterator of the bounds.
const internal_iterator begin_bounds_it_
Begin iterator to the first bound.
Global namespace for the dlinear library.
std::set< Literal > LiteralSet
A set of literals.
Definition literal.h:28
STL namespace.