forked from digama0/cadical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracer.cpp
160 lines (139 loc) · 4.27 KB
/
tracer.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "internal.hpp"
#include <sstream>
namespace CaDiCaL {
/*------------------------------------------------------------------------*/
Tracer::Tracer (Internal * i, File * f, bool b, bool l) :
internal (i),
file (f), binary (b), lrat (l),
added (0), deleted (0)
{
(void) internal;
LOG ("TRACER new");
}
Tracer::~Tracer () {
LOG ("TRACER delete");
delete file;
}
/*------------------------------------------------------------------------*/
// Support for binary DRAT format.
inline void Tracer::put_binary_zero () {
assert (binary);
assert (file);
file->put ((unsigned char) 0);
}
inline void Tracer::put_binary_lit (int lit) {
assert (binary);
assert (file);
assert (lit != INT_MIN);
put_binary_signed (lit);
}
inline void Tracer::put_binary_signed (int64_t n) {
put_binary_unsigned (2*abs (n) + (n < 0));
}
inline void Tracer::put_binary_unsigned (int64_t n) {
assert (binary);
assert (file);
assert (n > 0);
unsigned char ch;
while (n & ~0x7f) {
ch = (n & 0x7f) | 0x80;
file->put (ch);
n >>= 7;
}
ch = n;
file->put (ch);
}
/*------------------------------------------------------------------------*/
void Tracer::add_original_clause (int64_t id, const vector<int> & clause) {
if (!lrat) return;
if (file->closed ()) return;
LOG ("TRACER tracing addition of original clause");
if (binary) file->put ('o');
else if (lrat) file->put ("o ");
if (lrat) {
if (binary) put_binary_unsigned (id);
else file->put (id), file->put (" ");
}
for (const auto & external_lit : clause)
if (binary) put_binary_lit (external_lit);
else file->put (external_lit), file->put (' ');
if (binary) put_binary_zero ();
else file->put ("0\n");
}
void Tracer::add_derived_clause (int64_t id, const vector<int64_t> * chain, const vector<int> & clause) {
if (file->closed ()) return;
LOG ("TRACER tracing addition of derived clause");
if (binary) file->put ('a');
else if (lrat) file->put ("a ");
if (lrat) {
if (binary) put_binary_unsigned (id);
else file->put (id), file->put (" ");
}
for (const auto & external_lit : clause)
if (binary) put_binary_lit (external_lit);
else file->put (external_lit), file->put (' ');
if (lrat && chain) {
if (binary) put_binary_zero (), file->put ('l');
else file->put ("0 l ");
for (const auto & c : *chain)
if (binary) put_binary_signed (c);
else file->put (c), file->put (' ');
}
if (binary) put_binary_zero ();
else file->put ("0\n");
added++;
}
void Tracer::delete_clause (int64_t id, const vector<int> & clause) {
if (file->closed ()) return;
LOG ("TRACER tracing deletion of clause");
if (binary) file->put ('d');
else file->put ("d ");
if (lrat) {
if (binary) put_binary_unsigned (id);
else file->put (id), file->put (" ");
}
for (const auto & external_lit : clause)
if (binary) put_binary_lit (external_lit);
else file->put (external_lit), file->put (' ');
if (binary) put_binary_zero ();
else file->put ("0\n");
deleted++;
}
void Tracer::finalize_clause (int64_t id, const vector<int> & clause) {
if (!lrat) return;
if (file->closed ()) return;
LOG ("TRACER tracing finalized clause");
if (binary) file->put ('f');
else file->put ("f ");
if (binary) put_binary_unsigned (id);
else file->put (id), file->put (" ");
for (const auto & external_lit : clause)
if (binary) put_binary_lit (external_lit);
else file->put (external_lit), file->put (' ');
if (binary) put_binary_zero ();
else file->put ("0\n");
}
void Tracer::add_todo (const vector<int64_t> & vals) {
if (!lrat) return;
if (file->closed ()) return;
ostringstream ss;
for (auto c : vals) ss << " " << c;
LOG ("TRACER tracing TODO%s", ss.str ().c_str ());
if (binary) file->put ('t');
else file->put ("t ");
for (const auto & val : vals)
if (binary) put_binary_unsigned (val);
else file->put (val), file->put (' ');
if (binary) put_binary_zero ();
else file->put ("0\n");
}
/*------------------------------------------------------------------------*/
bool Tracer::closed () { return file->closed (); }
void Tracer::close () { assert (!closed ()); file->close (); }
void Tracer::flush () {
assert (!closed ());
file->flush ();
MSG ("traced %" PRId64 " added and %" PRId64 " deleted clauses",
added, deleted);
}
}