-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqm.cpp
184 lines (145 loc) · 5.27 KB
/
qm.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
/**
* - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -
*
* Name : OpenQM
* Author : Andrea Bontempi
* Description : Logic Network Synthesizer
*
* - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -
*
* This file is part of OpenQM.
*
* OpenQM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenQM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this project. If not, see <http://www.gnu.org/licenses/>.
*
* - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -
*/
#include "qm.h"
#include "implicant.h"
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <sstream>
/**
* @brief Execute QM step
*
* @param implicantList List of implicants
* @return stepResult
*/
stepResult makeQMStep(std::vector<Implicant> implicantList){
// stepResult
stepResult result;
// Save list size
int implicantListSize = implicantList.size();
// Group implicants based on implicant's one numbers
std::map<int, std::vector<int>> implicantGroup;
// Max one number group
int implicantGroupMax = 0;
for(int i = 0; i < implicantListSize; ++i) {
int oneCount = implicantList[i].getOneCount();
implicantGroup[oneCount].push_back(i);
if(oneCount > implicantGroupMax) {
implicantGroupMax = oneCount;
}
}
// Implicant marked
std::set<int> marked;
// Combination and reduction of implicants
for(int i = 0; i < implicantGroupMax; ++i) {
for(int x : implicantGroup[i]) {
for(int y : implicantGroup[i+1]) {
// Reduce!
Implicant temp = implicantList[x] + implicantList[y];
// Fail?
if(temp.getStr() == "") {
continue;
}
// Mark!
marked.insert(x);
marked.insert(y);
// Save!
result.reduced.push_back(temp);
}
}
}
// Result must me unique
std::sort(result.reduced.begin(), result.reduced.end() );
result.reduced.erase(std::unique(result.reduced.begin(), result.reduced.end() ), result.reduced.end() );
// Compile exclude list
for(int i = 0; i < implicantListSize; ++i) {
if(!marked.count(i)) {
result.excluded.push_back(implicantList[i]);
}
}
return result;
}
/**
* @brief Execute QM
*
* @param implicantList List of implicants
* @param dontCareList List of don't care
* @return std::vector< Implicant, std::allocator< void > >
*/
std::vector< Implicant > makeQM(const std::vector<Implicant>& implicantList, const std::vector<Implicant>& dontCareList) {
std::vector<Implicant> solution;
std::vector<Implicant> list = implicantList;
list.insert(list.end(), dontCareList.begin(), dontCareList.end());
std::set<int> dontCareCoverage;
for(Implicant i : dontCareList) {
std::set<int> tempCoverage = i.getCoverage();
dontCareCoverage.insert(tempCoverage.begin(), tempCoverage.end());
}
stepResult result;
do {
result = makeQMStep(list);
list = result.reduced;
solution.insert(solution.end(), result.excluded.begin(), result.excluded.end());
} while(result.reduced.size() != 0);
if(dontCareCoverage.size() != 0) {
solution.erase(std::remove_if(solution.begin(), solution.end(), [dontCareCoverage](const Implicant& i) -> bool {
std::set<int> check;
std::set<int> tempCoverage = i.getCoverage();
std::set_difference(tempCoverage.begin(), tempCoverage.end(), dontCareCoverage.begin(), dontCareCoverage.end(), std::inserter(check, check.begin()));
return check.size() == 0;
}), solution.end());
}
return solution;
}
/**
* @brief Generate boolean expression from implicant list (solution of QM)
*
* @param solution Implicant list
* @return std::string
*/
std::string getBooleanExpression(std::vector<Implicant> solution){
std::stringstream result;
std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
for(auto i = solution.begin(); i != solution.end(); ++i) {
std::string implStr = i->getStr();
for(int charPos = 0; charPos <= implStr.size(); ++charPos) {
switch(implStr[charPos]) {
case '0':
result << alphabet[charPos] << "\'";
break;
case '1':
result << alphabet[charPos];
break;
default:
continue;
}
}
if(i != --solution.end()) result << " + ";
}
return result.str();
}