-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCnf.h
More file actions
64 lines (51 loc) · 1.91 KB
/
Cnf.h
File metadata and controls
64 lines (51 loc) · 1.91 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
#pragma once
#include <string>
#include <unordered_map>
#include <unordered_set>
using Valuation = std::unordered_map<std::string, bool>;
// hash codes for Literal and Clause, so those can be used in hashed containers
class Literal;
class Clause;
namespace std
{
template<> struct hash<Literal> { std::size_t operator()(const Literal &l) const noexcept; };
template<> struct hash<Clause> { std::size_t operator()(const Clause &c) const noexcept; };
}
class Literal {
public:
Literal(const std::string &name, bool neg = false);
Literal(const char *name, bool neg = false);
static Literal Lit(const std::string &name);
static Literal Not(const std::string &name);
static Literal Not(const Literal &lit);
static Literal fromString(const std::string &s);
std::string name() const { return m_name; }
bool neg() const { return m_neg; }
bool isSatisfied(const Valuation &v) const;
std::string toString() const;
Literal operator!() const;
Literal operator-() const { return ! *this; }
bool operator==(const Literal &other) const;
bool operator!=(const Literal &other) const { return !(*this == other); }
bool operator<(const Literal &other) const { return make_pair(name(), neg()) < make_pair(name(), neg()); }
private:
std::string m_name;
bool m_neg;
};
class Clause : public std::unordered_set<Literal> {
public:
using unordered_set::unordered_set;
static Clause fromString(const std::string &s);
bool isSatisfied(const Valuation &v) const;
std::string toString() const;
};
class Cnf : public std::unordered_set<Clause> {
public:
using unordered_set::unordered_set;
static Cnf fromString(const std::string &s);
bool isSatisfied(const Valuation &v) const;
std::string toString() const;
};
std::ostream& operator<< (std::ostream& os, const Literal &l);
std::ostream& operator<< (std::ostream& os, const std::unordered_set<Literal> &c);
std::ostream& operator<< (std::ostream& os, const std::unordered_set<Clause> &cnf);