SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
SampleComparator.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Sample.h"
4 #include "../common.h"
5 #include "../Settings.h"
6 
7 namespace smtrat {
8 namespace cad {
9 
10 /**
11  * Contains comparison operators for samples and associated helpers.
12  * The comparators implement a `less-than` operators and large samples are considered first.
13  * Hence if `lhs` has greater priority than `rhs`, the result should be `false`.
14  */
15 namespace sample_compare {
16 
17  struct level {};
18  struct size {};
19  struct absvalue {};
20  struct type {};
21 
22  template<typename It>
23  auto get(const It& it, level) {
24  return it.depth();
25  }
26  template<typename It>
27  auto get(const It& it, size) {
28  return carl::bitsize(it->value());
29  }
30  template<typename It>
31  auto get(const It& it, absvalue) {
32  return carl::abs(it->value());
33  }
34  template<typename It>
35  auto get(const It& it, type) {
36  if (!it->value().is_numeric()) return 0;
37  if (!carl::is_integer(it->value())) return 1;
38  return 2;
39  }
40 
41  /**
42  * Compares the criterion given by `t` of two samples `lhs` and `rhs` using a comparator `f`.
43  * Returns `0` if the values are the same, `-1` if `lhs` should be used first and `1` if `rhs` should be used first.
44  */
45  template<typename It, typename tag, typename F>
46  int compareCriterion(const It& lhs, const It& rhs, tag t, F&& f) {
47  auto l = get(lhs, t);
48  auto r = get(rhs, t);
49  if (l == r) {
50  SMTRAT_LOG_TRACE("smtrat.cad.samplecompare", "Comparing " << l << " < " << r << " -> 0");
51  return 0;
52  }
53  SMTRAT_LOG_TRACE("smtrat.cad.samplecompare", "Comparing " << l << " < " << r << " ? " << (f(l, r) ? -1 : 1));
54  return f(l, r) ? -1 : 1;
55  }
56 
57  template<typename It>
58  bool compare(const It& lhs, const It& rhs) {
59  return lhs < rhs;
60  }
61  template<typename It, typename tag, typename F, typename... Tail>
62  bool compare(const It& lhs, const It& rhs) {
63  int res = compareCriterion(lhs, rhs, tag{}, F{});
64  if (res != 0) return res > 0;
65  return compare<It, Tail...>(lhs, rhs);
66  }
67 
68  template<typename It, typename... Args>
70  bool operator()(const It& lhs, const It& rhs) const {
71  auto res = compare<It, Args...>(lhs, rhs);
72  SMTRAT_LOG_TRACE("smtrat.cad.samplecompare", *lhs << " < " << *rhs << " ? " << res);
73  return res;
74  }
75  };
76 
77  using lt = std::less<>;
78  using gt = std::greater<>;
79 
80  template<typename Iterator, SampleCompareStrategy Strategy>
81  struct SampleComparator {};
82 
83  template<typename Iterator>
85  SampleComparator_impl<Iterator, size, lt> {};
86 
87  //template<typename Iterator>
88  //struct SampleComparator<Iterator, SampleCompareStrategy::Type>:
89  // SampleComparator_impl<Iterator, type, gt, size, lt> {};
90 
91  template<typename Iterator>
93  bool operator()(const Iterator& lhs, const Iterator& rhs) const {
94  SMTRAT_LOG_TRACE("smtrat.cad.samplecompare", *lhs << " < " << *rhs << "?");
95  auto c = compare(lhs, rhs);
96  auto r = reference(lhs, rhs);
97  SMTRAT_LOG_TRACE("smtrat.cad.samplecompare", "-> " << c << " / " << r);
98  assert(c == r);
99  return c;
100  }
101  bool reference(const Iterator& lhs, const Iterator& rhs) const {
103  auto res = sc(lhs, rhs);
104  SMTRAT_LOG_TRACE("smtrat.cad.samplecompare", *lhs << " < " << *rhs << " -> " << res);
105  return res;
106  }
107  bool compare(const Iterator& lhs, const Iterator& rhs) const {
108  bool l1 = carl::is_integer(lhs->value());
109  bool r1 = carl::is_integer(rhs->value());
110  SMTRAT_LOG_TRACE("smtrat.cad.samplecompare", lhs->value() << " < " << rhs->value() << ": Int " << r1);
111  if (l1 != r1) {
112  return r1;
113  }
114  bool l2 = lhs->value().is_numeric();
115  bool r2 = rhs->value().is_numeric();
116  SMTRAT_LOG_TRACE("smtrat.cad.samplecompare", lhs->value() << " < " << rhs->value() << ": Num " << r2);
117  if (l2 != r2) {
118  return r2;
119  }
120  std::size_t l3 = carl::bitsize(lhs->value());
121  std::size_t r3 = carl::bitsize(rhs->value());
122  SMTRAT_LOG_TRACE("smtrat.cad.samplecompare", lhs->value() << " < " << rhs->value() << ": Size (" << l3 << " / " << r3 << ") " << (l3 > r3));
123  if (l3 != r3) {
124  return l3 > r3;
125  }
126  SMTRAT_LOG_TRACE("smtrat.cad.samplecompare", lhs->value() << " < " << rhs->value() << ": Absolute " << (carl::abs(lhs->value()) >= carl::abs(rhs->value())));
127  if (carl::abs(lhs->value()) != carl::abs(rhs->value())) return carl::abs(lhs->value()) >= carl::abs(rhs->value());
128  return lhs < rhs;
129  }
130  };
131 
132  template<typename Iterator>
134  SampleComparator_impl<Iterator, type, gt> {};
135  template<typename Iterator>
137  SampleComparator_impl<Iterator, type, gt, level, gt, size, lt, absvalue, lt> {};
138  template<typename Iterator>
140  SampleComparator_impl<Iterator, type, gt, size, lt, absvalue, lt> {};
141  template<typename Iterator>
143  SampleComparator_impl<Iterator, type, gt, size, lt> {};
144  template<typename Iterator>
146  SampleComparator_impl<Iterator, level, gt, type, gt> {};
147  template<typename Iterator>
149  SampleComparator_impl<Iterator, level, gt, type, gt, absvalue, lt> {};
150  template<typename Iterator>
152  SampleComparator_impl<Iterator, level, gt, type, gt, size, lt> {};
153  template<typename Iterator>
155  SampleComparator_impl<Iterator, level, gt, type, gt, size, lt, absvalue, lt> {};
156  template<typename Iterator>
158  SampleComparator_impl<Iterator, level, gt, size, lt> {};
159  template<typename Iterator>
161  SampleComparator_impl<Iterator, size, lt> {};
162 }
163 
165 
166  template<typename Iterator, FullSampleCompareStrategy Strategy>
168 
169  template<typename Iterator>
170  struct FullSampleComparator<Iterator, FullSampleCompareStrategy::Value>: SampleComparator<Iterator, SampleCompareStrategy::Value> {};
171  template<typename Iterator>
172  struct FullSampleComparator<Iterator, FullSampleCompareStrategy::Type>: SampleComparator<Iterator, SampleCompareStrategy::Type> {};
173  template<typename Iterator>
174  struct FullSampleComparator<Iterator, FullSampleCompareStrategy::T>: SampleComparator<Iterator, SampleCompareStrategy::T> {};
175 }
176 }
bool compare(const It &lhs, const It &rhs)
int compareCriterion(const It &lhs, const It &rhs, tag t, F &&f)
Compares the criterion given by t of two samples lhs and rhs using a comparator f.
auto get(const It &it, level)
SampleCompareStrategy
Definition: Settings.h:14
FullSampleCompareStrategy
Definition: Settings.h:28
Numeric abs(const Numeric &_value)
Calculates the absolute value of the given Numeric.
Definition: Numeric.cpp:276
PositionIteratorType Iterator
Definition: Common.h:49
Class to create the formulas for axioms.
#define SMTRAT_LOG_TRACE(channel, msg)
Definition: logging.h:36
bool operator()(const It &lhs, const It &rhs) const