-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuineMcCluskey.cpp
661 lines (532 loc) · 21.5 KB
/
QuineMcCluskey.cpp
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// QuineMcCluskey.cpp
#include "QuineMcCluskey.h"
// Constructors :
QuineMcCluskey::QuineMcCluskey()= default;
QuineMcCluskey::QuineMcCluskey(int* mainTermsInputArray, int mainTermsInputArrayLength, bool maxTermsAreInput) {
this->numMainTerms = mainTermsInputArrayLength;
this->numAll = mainTermsInputArrayLength;
this->allTerms = mainTermsInputArray;
//Sort and delete all duplicates (allTerms)
removeDuplicatesAndSortArray(allTerms, numAll);
buildMainTerms(mainTermsInputArray);
this->bitsNum = detectBitsCount();
this->maxTermIsInput = maxTermsAreInput;
}
QuineMcCluskey::QuineMcCluskey(int* mainTermsInputArray, int mainTermsInputArrayLength, int* dontCaresInputArray, int dontCaresInputArrayLength, bool maxTermsAreInput) {
this->numMainTerms = mainTermsInputArrayLength;
// this->numDontCares = dontCaresInputArrayLength;
this->numAll = mainTermsInputArrayLength + dontCaresInputArrayLength;
// std::cout << "before the shit" << std::endl;
// if (numAll > 0) {
// std::memcpy(allTerms, mainTermsInputArray, mainTermsInputArrayLength * sizeof(int));
// std::memcpy(allTerms + mainTermsInputArrayLength, dontCaresInputArray, dontCaresInputArrayLength * sizeof(int));
// }
allTerms = new int[numAll];
std::copy(mainTermsInputArray, mainTermsInputArray + mainTermsInputArrayLength, allTerms);
std::copy(dontCaresInputArray, dontCaresInputArray + dontCaresInputArrayLength,
allTerms + mainTermsInputArrayLength);
//Sort and delete all duplicates (allTerms)
removeDuplicatesAndSortArray(allTerms, numAll);
// for (int i = 0; i < numAll; ++i) {
// std::cout << allTerms[i] << " ";
// }
// std::cout << std::endl;
//
// std::cout << "after the shit" << std::endl;
buildMainTerms(mainTermsInputArray);
this->bitsNum = detectBitsCount();
this->maxTermIsInput = maxTermsAreInput;
}
// Super Main Functions (there is just one function like this but, anyway):
// This function needs to be called before any action. ok?
// TODO Add a flag to check if the shit has been solved or not!
void QuineMcCluskey::solve() {
// Make sure that you solve it only once!
if(!isSolved) {
initialMintermsGrouping();
groupMinterms();
markWasteImplicants();
markPrimeImplicants();
extractPrimeImplicants();
markEssentialPrimeImplicants();
updatedCoveredMainTerms();
ApplyPetricksMethod();
updatedCoveredMainTerms();//this is somehow redundant but let it be, we may be needing to print it!
}
isSolved = true; //marking as solved, so the user can move on and use other methods too! (like optional functions)
}
// Main Functions :
// these functions are the main steps taken to solve the problem
void QuineMcCluskey::initialMintermsGrouping() {
int groupNum = 0;
int totalAdded = 0;
while (totalAdded < numAll) {
for (int i = 0; i < numAll; i++) {
if (countOnesInBinary(allTerms[i]) == groupNum) {
groupedTerms.push_back({{allTerms[i] }, { /*none*/ }, 0, groupNum, false, false });
totalAdded++;
}
}
groupNum++;
}
}
void QuineMcCluskey::groupMinterms() {
int stage = 1;
int startIndex;
int endIndexHolder = 0;
std::vector<int> combinedMinterms;
std::vector<int> deletedArgs;
while (true) {
int groupFromTop = -1;
int groupObserver = -1;
startIndex = endIndexHolder;
endIndexHolder = int(groupedTerms.size());
bool anyAdded = false;
for (int i = startIndex; i < endIndexHolder; i++) {
const auto upperdata = groupedTerms[i];
if (groupObserver != upperdata.groupFromTop) {
groupFromTop++;
groupObserver = upperdata.groupFromTop;
}
for (int j = i + 1; j < endIndexHolder; j++) {
const auto& lowerdata = groupedTerms[j];
if (upperdata.stage == lowerdata.stage && upperdata.groupFromTop + 1 == lowerdata.groupFromTop && isPowerOfTwo(lowerdata.termsIncluded[0] - upperdata.termsIncluded[0]) && compareVectors(upperdata.deletedArgs, lowerdata.deletedArgs, true)) {
combinedMinterms.clear();
combinedMinterms.insert(combinedMinterms.end(), upperdata.termsIncluded.begin(), upperdata.termsIncluded.end());
combinedMinterms.insert(combinedMinterms.end(), lowerdata.termsIncluded.begin(), lowerdata.termsIncluded.end());
deletedArgs.clear();
deletedArgs.insert(deletedArgs.end(), upperdata.deletedArgs.begin(), upperdata.deletedArgs.end());
deletedArgs.push_back(lowerdata.termsIncluded[0] - upperdata.termsIncluded[0]);
groupedTerms.push_back({ combinedMinterms, deletedArgs, stage, groupFromTop, false, false });
anyAdded = true;
}
}
}
if (!anyAdded)
break;
stage++;
}
}
void QuineMcCluskey::markWasteImplicants() {
int endIndexHolder = int(groupedTerms.size());
for (int i = 0; i < endIndexHolder; i++) {
auto& upperdata = groupedTerms[i];
bool isWaste = false;
for (int j = i + 1; j < endIndexHolder; j++) {
const auto& lowerdata = groupedTerms[j];
if (lowerdata.stage == upperdata.stage && lowerdata.groupFromTop == upperdata.groupFromTop && compareVectors(upperdata.termsIncluded, lowerdata.termsIncluded, false) && compareVectors(upperdata.deletedArgs, lowerdata.deletedArgs, false)) {
isWaste = true;
break;
}
if (lowerdata.stage != upperdata.stage)
break;
}
upperdata.isWaste = isWaste;
}
}
void QuineMcCluskey::markPrimeImplicants() {
int endIndexHolder = int(groupedTerms.size());
for (int i = 0; i < endIndexHolder; i++) {
auto& upperdata = groupedTerms[i];
bool existsInUpperStage = false;
for (int j = i + 1; j < endIndexHolder; j++) {
const auto& lowerdata = groupedTerms[j];
if (lowerdata.stage == upperdata.stage + 1 && isSubVector(upperdata.termsIncluded, lowerdata.termsIncluded)) {
existsInUpperStage = true;
break;
}
if (lowerdata.stage > upperdata.stage + 1)
break;
}
upperdata.isPI = !existsInUpperStage;
}
}
void QuineMcCluskey::extractPrimeImplicants() {
int endIndexHolder = int(groupedTerms.size());
for (int i = 0; i < endIndexHolder; i++) {
auto& data = groupedTerms[i];
if(data.isPI && !data.isWaste){
primeImplicants.push_back({data.termsIncluded, data.deletedArgs, -1, -1});
}
}
// Release some memory : ALERT : ONLY UNCOMMENT THIS LINE IF MEMORY IS NOT AVAILABLE, OR IS LOW
// groupedTerms.clear();
// groupedTerms.shrink_to_fit();
}
void QuineMcCluskey::markEssentialPrimeImplicants() {
int endIndexHolder = int(primeImplicants.size());
for (int i = 0; i < endIndexHolder; i++) {
auto& upperdata = primeImplicants[i];
bool isEPI = false;
for (int elem : upperdata.termsIncluded) {
if (isMainTerm(elem)){
bool found = false;
for (int j = 0; j < endIndexHolder; j++) {
if(i == j)
continue;
const auto& lowerdata = primeImplicants[j];
for (int element : lowerdata.termsIncluded) {
if (element == elem) {
found = true;
break;
}
}
if (found) {
break;
}
}
if (!found) {
isEPI = true;
break;
}
}
}
if(upperdata.isEssential == -1)
upperdata.isEssential = isEPI;
if (isEPI && upperdata.isRequired == -1)
upperdata.isRequired = isEPI;
}
}
void QuineMcCluskey::ApplyPetricksMethod() {
// going for Petricks method if needed. checking if there is any uncovered MainTerm, this should suffice right?
if(uncoveredMainTermsRemained()){
// I use index if unessential PI as it's label
// and I have a vector that holds boolean expressions and then define methods to do that bool Algebra
//looping over remaining mainTerms to create bool Expressions (I mean fill up the below vector):
std::vector<std::vector<std::string>> Expressions;
for (const auto & elem: MainTerms)
if (!elem.isCovered){
//loop over primeImplicants and push back the indexes into the shit
int hold = int(primeImplicants.size());
std::vector<std::string> temp;
for (int i = 0; i < hold; ++i)
if(!primeImplicants[i].isEssential)
for (const auto & minterm : primeImplicants[i].termsIncluded)
if (elem.term == minterm){
temp.push_back(std::to_string(i));
break;
}
Expressions.push_back(temp);
// std::cout << "or elem : [ ";
// for (std::string t: temp) {
// std::cout << t << " ";
// }
// std::cout << "]" << std::endl;
}
//by now, the initial P has been formed (Expression). now just we need to simplify it and convert it into SOP form
//The expression is now in this state : ()()()
//Now going for the simplification of Expressions :
//We multiply the one in index i with i+1, if there is any, starting from i = 0; and in each
// turn that we perform multiplication, we perform simplification too :
while (Expressions.size() > 1) {
//multiply 0 with 1 and put in OrExpressions. then
std::vector<std::string> tempResult;
for (const auto& up: Expressions[0]) {
for (const auto& down: Expressions[1]) {
tempResult.push_back(up + down);
}
}
//Simplify tempResult :
// 1. change these : 00 to 0 or 11 to 1 or 001 to 01 :
for (auto & target : tempResult) {
std::sort(target.begin(), target.end());
target.erase(std::unique(target.begin(), target.end()), target.end());
} // all of them will be sorted too! nice I guess
// 2. change these : 0 + 01 to 0 | 1 + 123 to 1 :
for (int i = 0; i < tempResult.size(); ++i) {
for (int j = 0; j < tempResult.size(); ++j) {
if (i != j && isSubstringCustom(tempResult[j], tempResult[i])){
tempResult.erase(tempResult.begin() + j); // Remove tempResult[j]
--j;
if (i > j)
--i;
}
}
}
//delete 0 and 1 and push the tempResult at the beginning (consider size changing)
if (Expressions.size() >= 2) {
Expressions[0] = tempResult; // Copy tempResult into Expressions[0]
Expressions.erase(Expressions.begin() + 1); // Erase Expressions[1]
}
// std::cout << "( ";
// for (const auto & up : tempResult) {
// std::cout << up << " ";
// }
// std::cout << ")(";
// for (const auto & up : Expressions[1]) {
// std::cout << up << " ";
// }
// std::cout << ")" << std::endl;
}
// for (const auto & up : Expressions[0]) {
// std::cout << up << " ";
// }
// std::cout << std::endl;
// In this state, Expressions[0] holds all possible combinations of choosable PI's, now, we'll just choose the first one,
// that hold less number of PI indexes. and then mark those PI indexes as required. done
// We might have multiple choices here, I'm just gonna choose the first one from the begin. ok?
std::string shortestString = Expressions[0][0];
// Iterate through the vector to find the shortest string
for (const auto& str : Expressions[0]) {
if (str.length() < shortestString.length()) {
shortestString = str;
}
}
//Now, we're going to mark the related PI'S as required :
for (const char & c: shortestString) {
int index = c - '0';
primeImplicants[index].isRequired = true;
}
//Done, now we just need to go for displaying, or creating a single expression, like ABC + A`BC and so on
}
//else, there is no need to apply this method, you can go for display part, based on the required PI's
}
void QuineMcCluskey::updatedCoveredMainTerms(){
for(auto& minterm : MainTerms){
if(!minterm.isCovered){//should it be like this?
bool isCovered = false;
for (const auto& primeI : primeImplicants){
if(primeI.isRequired == 1){
for (int elem : primeI.termsIncluded){
if(minterm.term == elem){
isCovered = true;
break;
}
}
if(isCovered)
break;
}
}
minterm.isCovered = isCovered;
}
}
}
// Sub-functions : functions used mostly in the main functions that are NOT static
void QuineMcCluskey::buildMainTerms(int* inputArray){
std::sort(inputArray, inputArray + numMainTerms); // Sort the input array first
for (int i = 0; i < numMainTerms; i++){
MainTerms.push_back({inputArray[i], false});
}
}
//bool QuineMcCluskey::uncoveredMainTermsRemained(){
// for (const auto & elem: MainTerms)
// if (!elem.isCovered)
// return true;
// return false;
//}
//
//bool QuineMcCluskey::isMainTerm(const int& target){
// for (const auto& elem : MainTerms)
// if (target == elem.term)
// return true;
// return false;
//}
bool QuineMcCluskey::uncoveredMainTermsRemained() {
return std::any_of(MainTerms.begin(), MainTerms.end(), [](const auto& elem) {
return !elem.isCovered;
});
}
bool QuineMcCluskey::isMainTerm(const int& target) {
return std::find_if(MainTerms.begin(), MainTerms.end(), [&](const auto& elem) {
return target == elem.term;
}) != MainTerms.end();
}
int QuineMcCluskey::detectBitsCount() {
int num = allTerms[numAll - 1];
// If num is already a power of 2, return it
if (num && !(num & (num - 1))) {
int power = 0;
while (num >>= 1) {
power++;
}
return power;
}
// Find the next power of 2 using bit manipulation
int power = 1;
while ((1 << power) < num) {
power++;
}
return power;
}
// Optional Functions
void QuineMcCluskey::printSimplifiedExpression() {
// Implement printing the simplified Boolean expression
}
// Optional : Getters
const std::vector<MainTerm> &QuineMcCluskey::getMainTerms() const {
if (isSolved) {
return MainTerms;
} else {
static const std::vector<MainTerm> emptyVector; // Empty vector as placeholder
return emptyVector;
}
}
const std::vector<GroupedData> &QuineMcCluskey::getGroupedTerms() const {
if (isSolved) {
return groupedTerms;
} else {
static const std::vector<GroupedData> emptyVector; // Empty vector as placeholder
return emptyVector;
}
}
const std::vector<PrimeImplicant> &QuineMcCluskey::getPrimeImplicants() const {
if (isSolved) {
return primeImplicants;
} else {
static const std::vector<PrimeImplicant> emptyVector; // Empty vector as placeholder
return emptyVector;
}
}
// Static Functions (they are static but private! :) ):
int QuineMcCluskey::countOnesInBinary(int num) {
int count = 0;
// Convert num to binary and count 1's
while (num > 0) {
if (num % 2 == 1) {
count++;
}
num /= 2;
}
return count;
}
bool QuineMcCluskey::isPowerOfTwo(int num) {
if (num <= 0) {
return false; // Negative numbers and zero are not powers of two
}
// Check if num is a power of two by counting the set bits (1s) in its binary representation
return (num & (num - 1)) == 0;
}
void QuineMcCluskey::vectorSelectionSort(std::vector<int>& vec) {
int n = int(vec.size());
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (vec[j] < vec[minIndex]) {
minIndex = j;
}
}
// Swap vec[i] and vec[minIndex]
int temp = vec[i];
vec[i] = vec[minIndex];
vec[minIndex] = temp;
}
}
bool QuineMcCluskey::isSubVector(const std::vector<int>& subVec, const std::vector<int>& mainVec) {
if (subVec.size() > mainVec.size()) {
return false;
}
for (int i = 0; i <= mainVec.size() - subVec.size(); i++) {
if (std::equal(subVec.begin(), subVec.end(), mainVec.begin() + i)) {
return true;
}
}
return false;
}
void QuineMcCluskey::removeDuplicatesAndSortArray(int* arr, int& size) {
std::sort(arr, arr + size); // Sort the array
auto it = std::unique(arr, arr + size); // Remove adjacent duplicates
size = int(std::distance(arr, it)); // Update the size of the array
}
//bool QuineMcCluskey::isSubstringCustom(const std::string& str1, const std::string& str2) {
// for (char target : str2) {
// if (str1.find(target) == std::string::npos) {
// return false; // If any character in str2 is not found in str1, return false
// }
// }
// return true; // All characters in str2 are found in str1
//}
bool QuineMcCluskey::isSubstringCustom(const std::string& str1, const std::string& str2) {
return std::all_of(str2.begin(), str2.end(), [&](char target) {
return str1.find(target) != std::string::npos;
});
}
bool QuineMcCluskey::compareVectors(const std::vector<int>& vec1, const std::vector<int>& vec2, bool considerOrder) {
if (vec1.size() != vec2.size()) {
return false; // Vectors of different sizes cannot be equal
}
if (considerOrder) {
return vec1 == vec2; // Compare vectors directly for equality with order consideration
} else {
// Sort the vectors and then compare for equality
std::vector<int> sortedVec1 = vec1;
std::vector<int> sortedVec2 = vec2;
vectorSelectionSort(sortedVec1);
vectorSelectionSort(sortedVec2);
return sortedVec1 == sortedVec2;
}
}
std::string QuineMcCluskey::getStringExpression() {
if(isSolved){
std::string Inputs[] = {
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
};
std::string result{};
// Building the F(a,b,c...) :
result += "F(";
for (int i = 0; i < bitsNum; ++i) {
result += Inputs[i] + ",";
}
result = result.substr(0, result.size() - 1); // Deleting the last ','
result += ")=";
for (const auto& elem: primeImplicants) {
if (elem.isRequired == 1){
result += maxTermIsInput ? "(" : "";
result += termToExpression(elem.termsIncluded, elem.deletedArgs); //Check isMaxTermInput and bits count.
result += maxTermIsInput ? ")" : "+";
}
}
if (!maxTermIsInput)
result.erase(result.size() - 1);
return result;
} else {
return "not solved yet!";
}
}
std::string QuineMcCluskey::termToExpression(std::vector<int> terms, std::vector<int> deletedArgs) {
std::string Inputs[] = {
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
};
//turning deletedArgs to indexes :
for (auto& arg: deletedArgs) {
arg = bitsNum - 1 - int(log2(arg));
}
std::string result = intToBinaryString(terms[0], bitsNum);
std::sort(deletedArgs.rbegin(), deletedArgs.rend());
std::string abcRepresentation{};
int counter = 0;
for (int i = 0; i < bitsNum; ++i) {
if (std::find(deletedArgs.begin(), deletedArgs.end(), i) != deletedArgs.end()) {
// Skip deleted indices
continue;
}
if(!maxTermIsInput){
if (result[i] == '1') {
abcRepresentation += Inputs[i];
} else {
abcRepresentation += Inputs[i] + "\'";
}
} else {
if (result[i] == '1') {
abcRepresentation += Inputs[i] + "\'";
} else {
abcRepresentation += Inputs[i];
}
if (counter != bitsNum-deletedArgs.size()-1){
abcRepresentation += "+";
}
}
counter++;
}
return abcRepresentation;
}
std::string QuineMcCluskey::intToBinaryString(const int& num, const int& bits) {
std::bitset<128> binary(num);
std::string binaryStr = binary.to_string();
// Trim the string to the desired number of bits
if (bits < 128) {
binaryStr = binaryStr.substr(128 - bits);
}
return binaryStr;
}