Painless
A framework to ease parallelization of sequential CDCL SAT solvers
Loading...
Searching...
No Matches
ClauseUtils.hpp
1#pragma once
2
3#include "containers/ClauseExchange.hpp"
4#include <cstdint>
5#include <memory>
6#include <vector>
7
12namespace ClauseUtils {
13
18#define _jenkins_rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
19
25inline size_t
26lookup3_hash(size_t key)
27{
28 size_t s1, s2;
29 s1 = s2 = 0xdeadbeef;
30 s2 ^= s1;
31 s2 -= _jenkins_rot(s1, 14);
32 key ^= s2;
33 key -= _jenkins_rot(s2, 11);
34 s1 ^= key;
35 s1 -= _jenkins_rot(key, 25);
36 s2 ^= s1;
37 s2 -= _jenkins_rot(s1, 16);
38 key ^= s2;
39 key -= _jenkins_rot(s2, 4);
40 s1 ^= key;
41 s1 -= _jenkins_rot(key, 14);
42 s2 ^= s1;
43 s2 -= _jenkins_rot(s1, 24);
44
45 return s2;
46}
47
54inline size_t
55lookup3_hash_clause(const int* clause, unsigned int size)
56{
57 if (size == 0)
58 return 0;
59 size_t hash = lookup3_hash(clause[0]);
60 for (unsigned int i = 1; i < size; i++) {
61 hash ^= lookup3_hash(clause[i]);
62 }
63 return hash;
64}
65
71std::size_t
72hashClause(const simpleClause& clause);
73
80bool
81operator==(const simpleClause& lhs, const simpleClause& rhs);
82
88int
89getLiteralsCount(const std::vector<ClauseExchangePtr>& clauses);
90
95{
96 std::size_t operator()(const simpleClause& clause) const { return hashClause(clause); }
97};
98
103{
104 std::size_t operator()(const ClauseExchange& clause) const { return lookup3_hash_clause(clause.lits, clause.size); }
105};
106
112{
113 bool operator()(const ClauseExchange& left, const ClauseExchange& right) const
114 {
115 if (left.size != right.size)
116 return false;
117
118 // Same size: check if all literals in left are in right
119 uint ri = 0; // right index
120
121 for (int lit : left) {
122
123 // Same elements (if sorted and equal will only continue => linear complexity)
124 if (lit == right[ri]) {
125 ri++;
126 continue;
127 }
128
129 // A different element: check if it is present in remaining element of right
130 if (std::find(right.begin() + ri + 1, right.end(), lit) == right.end())
131 return false;
132 }
133
134 // Same size and all literals match
135 return true;
136 }
137};
138
143{
144 std::size_t operator()(const ClauseExchangePtr& clause) const { return ClauseExchangeHash()(*clause); }
145};
146
151{
152 bool operator()(const ClauseExchangePtr& left, const ClauseExchangePtr& right) const
153 {
154 return ClauseExchangeEqual()(*left, *right);
155 }
156};
157
158} // namespace ClauseUtils
Represents an exchangeable clause with flexible array member.
Definition ClauseExchange.hpp:42
int lits[0]
Flexible array member for storing clause literals (must be last)
Definition ClauseExchange.hpp:48
int * begin()
Get iterator to the beginning of the clause.
Definition ClauseExchange.hpp:111
int * end()
Get iterator to the end of the clause.
Definition ClauseExchange.hpp:117
unsigned int size
Size of the clause.
Definition ClauseExchange.hpp:46
Helping Functions for Clause Management (Hash and Equality)
Definition ClauseUtils.hpp:12
size_t lookup3_hash_clause(const int *clause, unsigned int size)
Computes a hash value for a clause using the Jenkins lookup3 algorithm.
Definition ClauseUtils.hpp:55
size_t lookup3_hash(size_t key)
Computes a hash value for a single key using the Jenkins lookup3 algorithm.
Definition ClauseUtils.hpp:26
bool operator==(const simpleClause &lhs, const simpleClause &rhs)
Equality operator for simple clauses.
std::size_t hashClause(const simpleClause &clause)
Computes a hash value for a simple clause.
int getLiteralsCount(const std::vector< ClauseExchangePtr > &clauses)
Calculates the total number of literals in a vector of clauses.
Equality functor for ClauseExchange objects.
Definition ClauseUtils.hpp:112
Hash functor for ClauseExchange objects.
Definition ClauseUtils.hpp:103
Equality functor for ClauseExchangePtr objects.
Definition ClauseUtils.hpp:151
Hash functor for ClauseExchangePtr objects.
Definition ClauseUtils.hpp:143
Hash functor for simple clauses.
Definition ClauseUtils.hpp:95