-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG2.cpp
More file actions
239 lines (221 loc) · 5.79 KB
/
G2.cpp
File metadata and controls
239 lines (221 loc) · 5.79 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <iostream>
#include <vector>
#include <string>
struct BNode{
std::string val;
BNode* left;
BNode* right;
};
BNode* bt_node(std::string e,BNode* l,BNode* r){
BNode* tmp = new BNode;
tmp -> val = e;
tmp -> left = l;
tmp -> right = r;
return tmp;
}
//builds a branch off the exsisting tree
BNode* hit_0(std::string const direction,std::vector<int> const ord,int counter){
std::string name;
char x01 = direction[ord[counter]];
name = "x" + std::to_string(ord[counter] + 1);
if(x01 == 'x'){
return hit_0(direction,ord,counter+1);
}
if(ord[counter]==-1){
return bt_node("1",NULL,NULL);
}
if(x01 == '1'){
return bt_node(name,bt_node("0",NULL,NULL),hit_0(direction,ord,counter+1));
}
return bt_node(name,hit_0(direction,ord,counter+1),bt_node("0",NULL,NULL));
}
//finds the zero on the path to place the one
BNode* Build_tree(std::string const direction,std::vector<int> const ord,BNode* t,int counter){
char x01 = direction[ord[counter]];
if(t -> val != "0"){
if(x01=='0'){
t -> left = Build_tree(direction,ord,t->left,counter+1);
return t;
}
if(x01=='1'){
t -> right = Build_tree(direction,ord,t->right,counter+1);
return t;
}
return Build_tree(direction,ord,t,counter+1);
}
return hit_0(direction,ord,counter);
}
//Bases the order of the varible with the least dont cares to the most
std::vector<int> order(std::vector<std::string> in){
std::vector <int> rank;
std::vector<int> out;
for(int i=0;i<in[0].size();i++){
int counter = 0;
for(int j=0;j<in.size();j++){
if(in[j][i]=='x'){
counter++;
}
}
rank.push_back(counter);
}
for(int a = 0; a < in[0].size();a++){
int min = -1, track;
for(int k=0;k<rank.size();k++){
if(((rank[k] < min)&&(rank[k]!=-1))||(min==-1)){
min = rank[k];
track = k;
}
}
rank[track]=-1;
out.push_back(track);
}
out.push_back(-1);
return out;
}
//Checks if an order and compressed input is valid if not it will rexpand it
std::vector<std::string> checker(std::vector<std::string> r,std::vector <int> orders){
std::string tmp;
int saves,savey;
if((r[0].size()<4)||(r.size()<3)){
return r;
}
int s=0,che = 0;
while((s<r.size())&&(che==0)){
int x=s+1;
while((x<r.size())&&(che==0)){
int y =0,same = 0;
while((y<r[0].size())&&(same==0)){
same=1;
if(r[s][orders[y]]==r[x][orders[y]]){
same=0;
}
else if(r[s][orders[y]]=='x'){
che++;
saves =s;
savey =y;
}else if(r[x][orders[y]]=='x'){
che++;
saves =x;
savey =y;
}
y++;
}
x++;
}
s++;
}
if(che == 1){
tmp = r[saves];
tmp[orders[savey]]='1';
r[saves][orders[savey]]='0';
r.push_back(tmp);
return checker(r,order(r));
}
return r;
}
//reduces the inputs into rows with dont cares
std::vector<std::string> shorthand(std::vector<std::string> r){
std::vector<std::string> tmp;
for(int y=r[0].size()-1; y>=0;y--){
int i=0;
while(i<r.size()){
int j = i+1,check = 1;
while((j < r.size())&&(check!=-1)){
int x =0;
check=-1;
while((x != r[0].size())&&(check == -1)){
if((r[i][x]!=r[j][x])&&(x!=y)){
check = 0;
}
x++;
}
if(check == -1){
r[i][y]='x';
r[j]="";
}
j++;
if(r[j]==""){
j++;
}
}
if(r[i]!=""){
tmp.push_back(r[i]);
}
i++;
if(r[i]==""){
i++;
}
}
r = tmp;
tmp.clear();
}
return r;
}
// builds the tree
BNode* build_bt(const std::vector<std::string>& fvalues){
BNode* t;
t = bt_node("0",NULL,NULL);
if (fvalues.size() > 0){
std::vector<std::string> r = shorthand(fvalues);
r= checker(r,order(r));
for(int i=0;i<r.size();i++){
t=Build_tree(r[i],order(r),t,0);
}
}
return t;
}
void destroy_tree(BNode* T){
if(T != NULL){
destroy_tree(T->left);
destroy_tree(T->right);
delete T;
}
}
int label_to_idx(const std::string& label){
std::string out;
for(int i = 1; i < label.size(); i++){
out.push_back(label[i]);
}
return std::stoi(out) - 1;
}
std::string eval_bt(BNode* bt, const std::string& input){
if( (bt->left == NULL) && (bt->right == NULL) ){
return bt->val;
}
else{
int idx = label_to_idx(bt->val);
std::string input_idx;
input_idx.push_back(input[idx]);
if(input_idx == "0"){
return eval_bt(bt->left, input);
}
else{
return eval_bt(bt->right, input);
}
}
}
int n_nodes_bt(BNode* t){
if(t == NULL){
return 0;
}
else{
return 1 + n_nodes_bt(t->left) + n_nodes_bt(t->right);
}
}
class BoolTree{
public:
BoolTree(const std::vector<std::string>& fvalues){
t = build_bt(fvalues);
}
std::string eval(const std::string& s){
return eval_bt(t, s);
}
int n_nodes(){
return n_nodes_bt(t);
}
~BoolTree(){
destroy_tree(t);
}
private:
BNode* t;
};