-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCnf.cpp
More file actions
177 lines (152 loc) · 3.55 KB
/
Cnf.cpp
File metadata and controls
177 lines (152 loc) · 3.55 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "Cnf.h"
#include <algorithm>
#include <iostream>
#include <regex>
#include <sstream>
Literal::Literal(const std::string &name, bool neg)
: m_name(name)
, m_neg(neg)
{
}
Literal::Literal(const char *name, bool neg)
: m_name(name)
, m_neg(neg)
{
}
Literal Literal::Lit(const std::string &name)
{
return Literal(name);
}
Literal Literal::Not(const std::string &name)
{
return Literal(name, true);
}
Literal Literal::Not(const Literal &lit)
{
return !lit;
}
Literal Literal::fromString(const std::string &s)
{
static std::string neg{"¬"};
if (s.size() && (s[0] == '-'))
return Literal(s.substr(1), true);
else if (!s.compare(0, neg.size(), neg))
return Literal(s.substr(neg.size()), true);
else
return Literal(s);
}
Literal Literal::operator!() const
{
return Literal(name(), !neg());
}
bool Literal::isSatisfied(const Valuation &v) const
{
return v.at(name());
}
std::string Literal::toString() const
{
return std::string(neg() ? "-" : "") + name();
}
bool Literal::operator==(const Literal &other) const
{
return name() == other.name() && neg() == other.neg();
}
bool Clause::isSatisfied(const Valuation &v) const
{
return std::any_of(cbegin(), cend(), [&](const auto &l) { return l.isSatisfied(v); });
}
std::string Clause::toString() const
{
std::stringstream ss;
ss << *this;
return ss.str();
}
Clause Clause::fromString(const std::string &s)
{
auto trimLeft = std::find_if_not(s.begin(), s.end(),
[](char c) { return std::isspace(c) || c == '('; } );
auto trimRight = std::find_if_not(s.rbegin(), s.rend(),
[](char c) { return std::isspace(c) || c == ')'; }).base();
if (trimLeft >= trimRight)
return {};
std::string trimmed{trimLeft, trimRight};
Clause c;
std::regex re{"(\\s|∨)+"};
std::transform(
std::sregex_token_iterator{trimmed.begin(), trimmed.end(), re, -1},
std::sregex_token_iterator{},
std::inserter(c, c.end()),
&Literal::fromString
);
return c;
}
bool Cnf::isSatisfied(const Valuation &v) const
{
return std::all_of(cbegin(), cend(), [&](const auto &l) { return l.isSatisfied(v); });
}
std::string Cnf::toString() const
{
std::stringstream ss;
ss << *this;
return ss.str();
}
Cnf Cnf::fromString(const std::string &s)
{
if (s.empty()) return {};
Cnf cnf;
std::regex re("[,;\n]|∧");
std::transform(
std::sregex_token_iterator{s.begin(), s.end(), re, -1},
std::sregex_token_iterator{},
std::inserter(cnf, cnf.end()),
&Clause::fromString
);
return cnf;
}
// hash codes for Literal and Clause, so those can be used in hashed containers
namespace std
{
std::size_t hash<Literal>::operator()(const Literal &l) const noexcept
{
return std::hash<std::string>{}(l.name()) + l.neg();
}
std::size_t hash<Clause>::operator()(const Clause &c) const noexcept
{
size_t h = 0;
for (const auto &l : c)
h ^= std::hash<Literal>{}(l);
return h;
}
}
// ostream printing support for Literal, Clause, Cnf
std::ostream& operator<< (std::ostream& os, const Literal &l)
{
os <<(l.neg() ? "-" : "") << l.name();
return os;
}
template<typename Range>
std::ostream& printRange(
std::ostream& os,
const Range &range,
const std::string &delim = ", ",
const std::string &left = "",
const std::string &right = ""
) {
os << left;
if (!range.empty()) {
auto i = range.cbegin();
os << *i;
for (++i; i != range.cend(); ++i)
os << delim << *i;
}
os << right;
return os;
}
std::ostream& operator<< (std::ostream& os, const std::unordered_set<Literal> &c)
{
return printRange(os, c, " ", "(", ")");
}
std::ostream& operator<< (std::ostream& os, const std::unordered_set<Clause> &cnf)
{
return printRange(os, cnf, "; ");
}