-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec211 project.cpp
More file actions
524 lines (460 loc) · 9.54 KB
/
ec211 project.cpp
File metadata and controls
524 lines (460 loc) · 9.54 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include <iostream>
#include<string>
using namespace std;
//Compiler version g++ 6.3.0
const int arraymax=40;
const int cubemax=11;
class larray;
void error(string str){
if(str=="INITTMV"){
cout<<"INITARRAY:too many variables";
exit(1);
}
else if(str=="COMBUNV"){
cout<<"COMBINE: unequal number of variables";
exit(1);
}
else if(str=="COMBAOV"){
cout<<"COMBINE:array overflow";
exit(1);
}
else if(str=="CHNGCOR"){
cout<<"CHANGECOLUMN: Column out of range";
exit(1);
}
else if(str=="DELCUNC"){
cout<<"DELETECUBE: nonexistent cube";
exit(1);
}
else if(str=="APCUAOV"){
cout<<"APPENDCUBE:array overflow";
exit(1);
}
else if(str=="SHRPUNV"){
cout<<"SHARPS: Unequal number of variable";
exit(1);
}
else if(str=="INTXAOV"){
cout<<"INTERSECTION: array overflow";
exit(1);
}
else if(str=="INTXUNV"){
cout<<"INTERSECTION: Unequal number of variable";
exit(1);
}
else if(str=="CONSAOV"){
cout<<"CONSENSUS: Array overflow";
exit(1);
}
}
class cube{
public:
char arr[cubemax];
//initcube
void initCube(){
for(int i=0;i<cubemax;i++){
arr[i]='x';
}
}
void writeCube(int ni,int no){
int n=ni+no;
cout<<"\n";
for(int i=0;i<n;i++){
if(i==ni){
cout<<" ";
}
if(arr[i]=='0'){
cout<<"0";
}
else if(arr[i]=='1'){
cout<<"1";
}
else if(i>=ni){
cout<<"-";
}
else{
cout<<"x";
}
}
}
};
bool subsume(cube,cube,int);//declaration
larray splitArray(larray,cube);
larray intersection (larray,larray);
class larray{
public:
int NoInputs,NoOutputs,NoCubes;
cube cubes[arraymax];
void initArray(int arraySize,int ninputs,int noutputs){
if(ninputs+noutputs>cubemax){
error("INITTMV");
}
else{
NoInputs=ninputs;
NoOutputs=noutputs;
NoCubes=arraySize;
for(int i=0;i<NoCubes;i++){
cubes[i].initCube();
}
}
}
void readArray(){
cin>>NoCubes;
cin>>NoInputs;
cin>>NoOutputs;
this->initArray(NoCubes,NoInputs,NoOutputs);
for(int i=0;i<NoCubes;i++){
bool err=false;
for(int j=0;j<NoInputs+NoOutputs;j++){
if(!err){
char c;
cin>>c;
while(c==' '){
cin>>c;
}
if(c!='0' && c!='1'&& c!='x'&& c!='X'){
cout<<"Invalid input found\n";
exit(1);
}
else{
cubes[i].arr[j]=tolower(c);
}
}
}
}
}
void writeArray(){
if(NoCubes==0){
cout<<"Empty Array\n";
}
else{
for(int i=0;i<NoCubes;i++){
cubes[i].writeCube(NoInputs,NoOutputs);
}
}
}
//position=0 to delete first element
void deleteCube(int position){
if(position>NoCubes-1|| position<0){
error("DELCUNC");
}
else{
for(int i=position;i<NoCubes-1;i++){
cubes[i]=cubes[i+1];
}
NoCubes--;
}
}
larray moveCube(int index){
larray temp;
temp.initArray(1,NoInputs,NoOutputs);
temp.cubes[0]=cubes[index];
this->deleteCube(index);
return temp;
}
void appendCube(cube c){
if(NoCubes+1>arraymax){
error("APCUAOV");
}
else{
cubes[NoCubes]=c;
NoCubes++;
}
}
void combine(larray b){
if(NoOutputs+NoInputs!=b.NoOutputs+b.NoInputs){
error("COMBUNV");
}
else if(NoCubes+b.NoCubes>=arraymax){
error("COMBAOV");
}
else{
for(int i=0;i<b.NoCubes;i++){
cubes[i+NoCubes]=b.cubes[i];
}
NoCubes+=b.NoCubes;
}
}
void changeColumn(int index,string str){
if(index>NoInputs+NoOutputs){
error("CHNGCOR");
}
else if(NoCubes>0){
for(int i=0;i<NoCubes;i++){
char x=cubes[i].arr[index];
if(x=='0'){
cubes[i].arr[index]=str[0];
}
else if(x=='1'){
cubes[i].arr[index]=str[1];
}
else{
cubes[i].arr[index]=str[2];
}
}
}
}
void absorb(){
if(NoCubes>0){
int nvars=NoInputs+NoOutputs;
for(int i=0;i<NoCubes-1;i++){
for(int j=i+1;j<NoCubes;){
if(subsume(cubes[i],cubes[j],nvars)){
this->deleteCube(i);
}
else if(subsume(cubes[j],cubes[i],nvars)){
this->deleteCube(j);
}
else{
j++;
}
}
}
}
}
void consensus(){
int nv=NoInputs+NoOutputs;
larray b,c,d;
cube mask,all0mask;
for(int i=NoInputs;i<nv;i++){
this->changeColumn(i,"01x");
}
mask.initCube();
if(NoOutputs>0){
all0mask.initCube();
for(int i=NoInputs;i<nv;i++){
all0mask.arr[i]='0';
}
}
for(int i=0;i<NoInputs;i++){
mask.arr[i]='1';
b=splitArray(*this,mask);
mask.arr[i]='0';
c=splitArray(*this,mask);
this->combine(b);
this->combine(c);
if(b.NoCubes>0&&c.NoCubes>0){
b.changeColumn(i,"xxx");
c.changeColumn(i,"xxx");
d=intersection(b,c);
mask.initCube();
if(NoOutputs>0&& NoCubes>0){
b=splitArray(d,all0mask);
}
this->absorb();
if(NoCubes+d.NoCubes>arraymax){
error("CONSAOV");
}
this->combine(d);
this->absorb();
}
mask.arr[i]='x';
}
}
};
bool cubex(cube &a,cube b,cube c,int size){
for(int i=0;i<size;i++){
if(b.arr[i]=='x'){
a.arr[i]=c.arr[i];
}
else if(c.arr[i]=='x'){
a.arr[i]=b.arr[i];
}
else if(b.arr[i]==c.arr[i]){
a.arr[i]=b.arr[i];
}
else {
return false;
}
}
return true;
}
larray cubeSharp(cube a,cube b,int size){
cube c;
larray x;
if(!cubex(c,b,a,size)){
x.NoCubes=1;
x.cubes[0]=a;
}
else{
x.NoCubes=0;
for(int i=0;i<size;i++){
if(a.arr[i]=='x'){
if(c.arr[i]=='0'){
x.cubes[x.NoCubes]=a;
x.cubes[x.NoCubes].arr[i]='1';
x.NoCubes++;
}else if(c.arr[i]=='1'){
x.cubes[x.NoCubes]=a;
x.cubes[x.NoCubes].arr[i]='0';
x.NoCubes++;
}
}
}
}
return x;
}
//a=b sharp c
larray sharp(larray &a,larray &b){
larray c;
int nvars=a.NoInputs+a.NoOutputs;
if(nvars!=b.NoInputs+b.NoOutputs){
error("SHRPUNV");
}
else{
larray t;
c=a;
for(int i=0;i<b.NoCubes;i++){
c.NoCubes=0;
for(int k=0;k<a.NoCubes;k++){
t=cubeSharp(a.cubes[k],b.cubes[i],nvars);
c.combine(t);
if(c.NoCubes>arraymax/2){
c.absorb();
}
}
a=c;
}
c.absorb();
}
return c;
}
larray splitArray(larray a, cube mask){
larray b;
b.initArray(0,a.NoInputs,a.NoOutputs);
int nvars=a.NoInputs+a.NoOutputs;
if(a.NoCubes>0){
int j=0;
while(j<a.NoCubes && a.NoCubes>0){
if(subsume(a.cubes[j],mask,nvars)){
b.appendCube(a.cubes[j]);
a.deleteCube(j);
}
else{
j++;
}
}
}
return b;
}
larray intersection (larray b,larray c){
larray a;
a.initArray(0,b.NoInputs,b.NoOutputs);
if(b.NoInputs+b.NoOutputs==c.NoInputs+c.NoOutputs){
int nv=b.NoInputs+b.NoOutputs;
for(int i=0;i<b.NoCubes;i++){
for(int j=0;j<c.NoCubes;j++){
if(a.NoCubes+1>arraymax){
error("INTXAOV");
}
else if(cubex(a.cubes[a.NoCubes],b.cubes[i],c.cubes[j],nv)){
a.NoCubes++;
}
if(a.NoCubes+1>arraymax/2){
a.absorb();
}
}
}
a.absorb();
}
else{
error("INTXUNV");
}
return a;
}
int cubeCost(cube a,int n){
int count=0;
for(int i=0;i<n;i++){
if(a.arr[i]!='x'){
count++;
}
}
return count;
}
bool subsume(cube a,cube b,int size){
bool sub=true;
int i=0;
while(sub && i<size){
if(a.arr[i]=='x' && (b.arr[i]=='0' || b.arr[i]=='1') ){
sub=false;
}
else if(a.arr[i]=='0'&&b.arr[i]=='1'){
sub=false;
}
else if(a.arr[i]=='1'&&b.arr[i]=='0'){
sub=false;
}
else{
i++;
}
}
return sub;
}
/*
9
4
3
0001001
0010101
0011001
0100110
0101010
1010111
1011111
1100101
1101110
9
4
1
00010
00101
00110
01001
01010
10101
10111
11001
11011
6
4
1
00101
01001
10101
10111
11001
11011
9
4
1
00000
00010
01000
01100
01110
11000
11100
0010X
0101x
7
4
1
00101
00111
01001
10111
11001
11011
11111
*/
int main()
{
cout << "Hello, Dcoder!\n";
larray f;
f.readArray();
cout<<"\nArray f\n";
f.writeArray();
f.consensus();
cout<<"\nReduced Array f\n";
f.writeArray();
}