-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsorting7.cpp
More file actions
60 lines (48 loc) · 1.75 KB
/
sorting7.cpp
File metadata and controls
60 lines (48 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "common.h"
int sorting7(unsigned size, sortingConstraints constraints) {
z3::context context;
z3::solver s(context, z3::solver::simple());
exprNetwork network(context, 0);
struct sort : multiSort {
exprNetwork& n;
z3::solver& s;
sort(exprNetwork& n, z3::solver& s) : n(n), s(s) {}
void add(z3::expr_vector& in, z3::expr_vector& out) override {
n.increase(in.size());
for (unsigned i = 0; i < in.size(); i++) {
n.add(i, in[i]);
}
// odd-even sort: O(n*log(n)^2)
for (unsigned p = 1; p < in.size(); p <<= 1) {
for (unsigned k = p; k >= 1; k >>= 1) {
for (unsigned j = k % p; j < in.size() - k; j += 2 * k) {
for (unsigned i = 0; i < MIN(k, in.size() - j - k); i++) {
if ((i + j) / (2 * p) == (i + j + k) / (2 * p)) {
n.addComparision(i + j, i + j + k);
}
}
}
}
}
const auto& outputExpr = n.getCurrentOutputs();
for (unsigned i = 0; i < outputExpr.size(); i++) {
s.add(out[i] == outputExpr[i]);
}
}
};
sort sort(network, s);
applyConstraints(s, size, sort, constraints);
z3::check_result result = s.check();
printStatistics(s);
if (constraints & outputReverse) {
assert(result == z3::unsat);
}
else if (!(constraints & pseudoBoolean)) {
z3::model m = s.get_model();
checkSorting(m, network.getInputs(), network.getOutputs(), constraints);
}
else {
assert(result == z3::sat);
}
return -1;
}