Painless
A framework to ease parallelization of sequential CDCL SAT solvers
Loading...
Searching...
No Matches
ClauseExchange.hpp
1#pragma once
2
3#include <algorithm>
4#include <atomic>
5#include <boost/intrusive_ptr.hpp>
6#include <cstdlib>
7#include <cstring>
8#include <sstream>
9#include <stdexcept>
10#include <vector>
11
13using simpleClause = std::vector<int>;
14
15// Forward declaration of ClauseExchange
16class ClauseExchange;
17
25using ClauseExchangePtr = boost::intrusive_ptr<ClauseExchange>;
26
42{
43 public:
44 unsigned int lbd;
45 int from;
46 unsigned int size;
47 std::atomic<unsigned int> refCounter;
48 int lits[0];
49
57 static ClauseExchangePtr create(const unsigned int size, const unsigned int lbd = 0, const int from = -1);
58
68 static ClauseExchangePtr create(const int* begin, const int* end, const unsigned int lbd = 0, const int from = -1);
69
78 static ClauseExchangePtr create(const std::vector<int>& v_cls, const unsigned int lbd = 0, const int from = -1);
79
83 ~ClauseExchange() = default;
84
90 int& operator[](unsigned int index)
91 {
92 assert(index < size && "Index out of bounds");
93 return lits[index];
94 }
95
101 const int& operator[](unsigned int index) const
102 {
103 assert(index < size && "Index out of bounds");
104 return lits[index];
105 }
106
111 int* begin() { return lits; }
112
117 int* end() { return lits + size; }
118
123 const int* begin() const { return lits; }
124
129 const int* end() const { return lits + size; }
130
134 void sortLiterals() { std::sort(begin(), end()); }
135
139 void sortLiteralsDescending() { std::sort(begin(), end(), std::greater<int>()); }
140
145 std::string toString() const;
146
152 {
153 refCounter.fetch_add(1, std::memory_order_relaxed);
154 return this;
155 }
156
162 static ClauseExchangePtr fromRawPtr(ClauseExchange* ptr) { return ClauseExchangePtr(ptr, false); }
163
164 private:
171 ClauseExchange(unsigned int size, unsigned int lbd, int from);
172};
173
178inline void
179intrusive_ptr_add_ref(ClauseExchange* ce)
180{
181 ce->refCounter.fetch_add(1, std::memory_order_relaxed);
182}
183
188inline void
189intrusive_ptr_release(ClauseExchange* ce)
190{
191 if (ce->refCounter.fetch_sub(1, std::memory_order_acq_rel) == 1) {
192 ce->~ClauseExchange();
193 std::free(ce);
194 }
195}
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
static ClauseExchangePtr create(const int *begin, const int *end, const unsigned int lbd=0, const int from=-1)
Create a new ClauseExchange object using pointers.
ClauseExchange * toRawPtr()
Convert to a raw pointer and increment the reference count.
Definition ClauseExchange.hpp:151
const int * end() const
Get const iterator to the end of the clause.
Definition ClauseExchange.hpp:129
int & operator[](unsigned int index)
Access clause literal by index.
Definition ClauseExchange.hpp:90
int * begin()
Get iterator to the beginning of the clause.
Definition ClauseExchange.hpp:111
void sortLiteralsDescending()
Sort the literals in descending order.
Definition ClauseExchange.hpp:139
static ClauseExchangePtr fromRawPtr(ClauseExchange *ptr)
Create an intrusive_ptr from a raw pointer.
Definition ClauseExchange.hpp:162
int from
Source identifier of the clause.
Definition ClauseExchange.hpp:45
std::atomic< unsigned int > refCounter
Counter for intrusive_ptr copies and raw pointer conversions.
Definition ClauseExchange.hpp:47
int * end()
Get iterator to the end of the clause.
Definition ClauseExchange.hpp:117
std::string toString() const
Convert the clause to a string representation.
static ClauseExchangePtr create(const std::vector< int > &v_cls, const unsigned int lbd=0, const int from=-1)
Create a new ClauseExchange object from a vector of literals.
~ClauseExchange()=default
Destructor.
void sortLiterals()
Sort the literals in ascending order.
Definition ClauseExchange.hpp:134
static ClauseExchangePtr create(const unsigned int size, const unsigned int lbd=0, const int from=-1)
Create a new ClauseExchange object.
const int * begin() const
Get const iterator to the beginning of the clause.
Definition ClauseExchange.hpp:123
unsigned int lbd
Literal Block Distance (LBD) of the clause.
Definition ClauseExchange.hpp:44
const int & operator[](unsigned int index) const
Access clause literal by index (const version).
Definition ClauseExchange.hpp:101
unsigned int size
Size of the clause.
Definition ClauseExchange.hpp:46