-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.h
165 lines (134 loc) · 4.2 KB
/
Helper.h
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
//
// Created by Akash Shrivastva on 10/18/23.
//
#ifndef DPLLSOLVER_HELPER_H
#define DPLLSOLVER_HELPER_H
#include "set"
#include "vector"
#include "algorithm"
#include "unordered_map"
#include "map"
#include <stdio.h>
#include "string"
#include "fstream"
#include "sstream"
#include "iostream"
using namespace std;
class Helper {
public:
Helper(){}
static string negation(string atom) {
if (!atom.empty()) {
if (atom[0] == '!') {
return atom.substr(1, atom.size()-1);
} else {
return "!" + atom;
}
}
return "";
}
static string getUnsignedAtom(string atom) {
string s = "";
if (!atom.empty()) {
if (atom[0] == '!') {
s = atom.substr(1, atom.size() - 1);
} else {
s = atom;
}
}
return s;
}
static vector<string> parseInputFile(string inputFile, bool &correctInputFormat) {
ifstream file(inputFile);
string line;
vector<string> atoms;
while (getline(file, line)) {
removeLeadingAndTrailingWhitespace(line);
if (line.empty()) {
continue;
}
vector<string> v = split(line, ' ');
for (int i=0; i<v.size(); i++) {
vector<string> w = split(v[i], '=');
if (w[0].length() > 2 || w[1].length() > 1) {
cout<<"Error in input file: "<<w[0]<<"="<<w[1]<<endl;
correctInputFormat = false;
break;
}
int row = w[0][0] - '0';
int col = w[0][1] - '0';
int val = w[1][0] - '0';
// perform validation for row/col/val
string atom = getAtomForSquare(row, col, val, false);
atoms.push_back(atom);
}
}
return atoms;
}
static string getAtomForSquare(int row, int col, int val, bool negate) {
string s = "n" + to_string(val) + "_" + "r" + to_string(row) + "_c" + to_string(col);
if (negate) {
s = "!" + s;
}
return s;
}
static void removeLeadingAndTrailingWhitespace(string &input) {
if (!input.empty()) {
input.erase(input.begin(), input.begin() + input.find_first_not_of(" \t"));
input.erase(std::find_if(input.rbegin(), input.rend(), [](int ch) {
return !std::isspace(ch);
}).base(), input.end());
}
}
static vector<string> split(string input, char del) {
stringstream ss(input);
string token;
vector<string> tokens;
while (!ss.eof()) {
getline(ss, token, del);
removeLeadingAndTrailingWhitespace(token);
tokens.push_back(token);
}
return tokens;
}
static vector<vector<string>> getClausesToBeRemoved(vector<vector<string>> &clauses, string literal) {
int i, j;
vector<vector<string>> removeClauses;
for (i=0; i<clauses.size(); i++) {
bool flag = false;
for (j=0; j<clauses[i].size(); j++) {
if (clauses[i][j] == literal) {
flag = true;
break;
}
}
if (flag) {
removeClauses.push_back(clauses[i]);
}
}
return removeClauses;
}
static void deleteClausesFromSet(set<vector<string>> &se,
vector<vector<string>> removeClauses) {
for (int i=0; i<removeClauses.size(); i++) {
se.erase(removeClauses[i]);
}
}
static vector<vector<string>> getClauses(set<vector<string>> &se) {
vector<vector<string>> clauses;
for (auto itr = se.begin(); itr!=se.end(); itr++) {
vector<string> clause = *itr;
clauses.push_back(clause);
}
return clauses;
}
static void printMatrix(vector<vector<int>> &matrix) {
for (int i=0; i<9; i++) {
for (int j=0; j<9; j++) {
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}
};
#endif //DPLLSOLVER_HELPER_H