SAT Solver Template
Loading...
Searching...
No Matches
Clause.hpp
Go to the documentation of this file.
1/**
2* @author Tim Luchterhand
3* @date 26.11.24
4* @file Clause.hpp
5* @brief Contains the class Clause that consists of one or more literals
6*/
7
8#ifndef CLAUSE_HPP
9#define CLAUSE_HPP
10
11#include <vector>
12#include <ostream>
13
14#include "util/concepts.hpp"
16
17namespace sat {
18
19 /**
20 * Concept modeling a clause. A clause is some range of literals, i.e. it must have accessible members
21 * begin() and end() that return an iterator to the first or pas the end elements respectively. For example
22 * std::vector<Literal> satisfies this concept.
23 */
24 template<typename T>
25 concept clause_like = concepts::typed_range<T, Literal>;
26
27
28 /**
29 * @brief Clause class with watch literals.
30 * @details @copybrief
31 * In order for it to model the clause_like concept, you must implement
32 * the begin() and end() member functions. I recommend that you store the Literals in a private member of type
33 * std::vector<Literal>. Then, to implement begin() and end(), you can simply return the iterators returned from
34 * std::vector<Literal>::begin() and end() respectively
35 */
36 class Clause {
37 // @TODO Private members here
38 public:
39
40 /**
41 * Default CTor. If you want, you can remove it or adapt it to your needs
42 */
43 Clause() = default;
44
45 /**
46 * CTor
47 * @param literals list of literals of the clause
48 * @TODO The tests require a single argument constructor. But you can add more arguments with default values
49 * @TODO (for example a flag that tells the constructor if the literals are already sorted)
50 * @TODO or even further constructors if you want.
51 */
52 Clause(std::vector<Literal> literals);
53
54 /*
55 * @TODO if you want, you can declare additional constructors here
56 */
57
58
59 /**
60 * Gets the watcher rank of the given Literal
61 * @param l
62 * @return 0 if first watcher, 1 if second watcher, -1 if no watcher
63 */
64 short getRank(Literal l) const;
65
66 /**
67 * Gets the index of the watcher with the given rank
68 * @param rank rank of the watcher
69 * @return first watcher index if rank is 0, second watcher index otherwise
70 */
71 std::size_t getIndex(short rank) const;
72
73 /**
74 * Sets the given literal as watcher
75 * @param l Literal to be the new watcher
76 * @param watcherNo number of the watcher to be replaced
77 * @return true if setting watcher was successful, false if literal is not contained in clause
78 */
79 bool setWatcher(Literal l, short watcherNo);
80
81
82 /**
83 * Get the watch literal identified by the given rank
84 * @param rank rank of the watcher in {0, 1}
85 * @return watch literal
86 */
87 Literal getWatcherByRank(short rank) const;
88
89 /*
90 * For your information: The following two functions (begin and end) enable the class Clause to be used in a
91 * range based for-loop (foreach loop). This also makes it so that Clause models clause_like
92 * Clause c;
93 * for (auto literal : c) { ... }
94 * If you don't want to use a std::vector as the literal container, you need to change the return type of the
95 * functions
96 */
97
98 /**
99 * Iterator to first Literal in the clause
100 * @return
101 */
102 auto begin() const -> std::vector<Literal>::const_iterator;
103 // change if necessary ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
104
105 /**
106 * Past-the-end iterator
107 * @return
108 */
109 auto end() const -> std::vector<Literal>::const_iterator;
110 // change if necessary ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111
112 /**
113 * Array subscript operator
114 * @param index index for access
115 * @return Literal at given index
116 */
117 Literal operator[](std::size_t index) const;
118
119 /**
120 * Whether the clause is empty
121 * @return
122 */
123 bool isEmpty() const;
124
125 /**
126 * size of the clause
127 * @return
128 */
129 std::size_t size() const;
130
131 /**
132 * Whether the other clause contains the same literals (independent of ordering)
133 * @param other clause to compare to
134 * @return true if both clauses contain exactly the same literals.
135 * @TODO for your implementation you need to either sort the literals or implement your own logic for the order
136 * @TODO independent comparison
137 */
138 bool sameLiterals(const Clause &other) const;
139
140 };
141}
142
143
144#endif //CLAUSE_HPP
Clause class with watch literals.
Definition Clause.hpp:36
auto end() const -> std::vector< Literal >::const_iterator
Definition Clause.cpp:36
short getRank(Literal l) const
Definition Clause.cpp:20
Literal getWatcherByRank(short rank) const
Definition Clause.cpp:52
bool sameLiterals(const Clause &other) const
Definition Clause.cpp:56
bool setWatcher(Literal l, short watcherNo)
Definition Clause.cpp:28
Literal operator[](std::size_t index) const
Definition Clause.cpp:44
auto begin() const -> std::vector< Literal >::const_iterator
Definition Clause.cpp:32
std::size_t getIndex(short rank) const
Definition Clause.cpp:24
bool isEmpty() const
Definition Clause.cpp:40
std::size_t size() const
Definition Clause.cpp:48
Clause(std::vector< Literal > literals)
Definition Clause.cpp:16
Clause()=default
Structure representing a literal in a CNF-SAT problem.
Definition basic_structures.hpp:56
#define NOT_IMPLEMENTED
Definition exception.hpp:26
Definition basic_structures.cpp:10