├── GUI └── KMapSolver │ ├── main.cpp │ ├── dialog.h │ ├── KMapSolver.pro │ ├── dialog.cpp │ ├── graycodehandler.h │ ├── include │ ├── lepton │ │ ├── windowsIncludes.h │ │ ├── Exception.h │ │ ├── CustomFunction.h │ │ ├── Parser.h │ │ ├── ExpressionProgram.h │ │ ├── CompiledExpression.h │ │ ├── ExpressionTreeNode.h │ │ └── ParsedExpression.h │ └── Lepton.h │ ├── dialog.ui │ ├── karnaughmap.h │ ├── src │ ├── MSVC_erfc.h │ ├── ExpressionProgram.cpp │ ├── ExpressionTreeNode.cpp │ └── CompiledExpression.cpp │ ├── expressionhandler.h │ ├── filterkmapresults.h │ ├── operation.h │ ├── setkmap.h │ └── comparekmapterms.h ├── .gitignore ├── README.md ├── Console └── KMapSolver │ ├── KMapSolver.pro │ ├── main.cpp │ ├── graycodehandler.h │ ├── include │ ├── lepton │ │ ├── windowsIncludes.h │ │ ├── Exception.h │ │ ├── CustomFunction.h │ │ ├── Parser.h │ │ ├── ExpressionProgram.h │ │ ├── CompiledExpression.h │ │ ├── ExpressionTreeNode.h │ │ └── ParsedExpression.h │ └── Lepton.h │ ├── karnaughmap.h │ ├── src │ ├── MSVC_erfc.h │ ├── ExpressionProgram.cpp │ ├── ExpressionTreeNode.cpp │ └── CompiledExpression.cpp │ ├── expressionhandler.h │ ├── filterkmapresults.h │ ├── operation.h │ ├── setkmap.h │ └── comparekmapterms.h └── LICENSE.txt /GUI/KMapSolver/main.cpp: -------------------------------------------------------------------------------- 1 | #include "dialog.h" 2 | #include 3 | 4 | 5 | 6 | int main (int argc, char* argv[]) 7 | { 8 | QApplication app(argc, argv); 9 | Dialog d; 10 | d.show(); 11 | return app.exec(); 12 | 13 | }//end main 14 | -------------------------------------------------------------------------------- /GUI/KMapSolver/dialog.h: -------------------------------------------------------------------------------- 1 | #ifndef DIALOG_H 2 | #define DIALOG_H 3 | 4 | #include 5 | 6 | namespace Ui { 7 | class Dialog; 8 | } 9 | 10 | class Dialog : public QDialog 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit Dialog(QWidget *parent = 0); 16 | ~Dialog(); 17 | 18 | private slots: 19 | void on_minimizeButton_clicked(); 20 | 21 | void on_clearButton_clicked(); 22 | 23 | private: 24 | Ui::Dialog *ui; 25 | }; 26 | 27 | #endif // DIALOG_H 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ objects and libs 2 | 3 | *.slo 4 | *.lo 5 | *.o 6 | *.a 7 | *.la 8 | *.lai 9 | *.so 10 | *.dll 11 | *.dylib 12 | 13 | # Qt-es 14 | 15 | /.qmake.cache 16 | /.qmake.stash 17 | *.pro.user 18 | *.pro.user.* 19 | *.qbs.user 20 | *.qbs.user.* 21 | *.moc 22 | moc_*.cpp 23 | qrc_*.cpp 24 | ui_*.h 25 | Makefile* 26 | *build-* 27 | 28 | # QtCreator 29 | 30 | *.autosave 31 | 32 | # QtCtreator Qml 33 | *.qmlproject.user 34 | *.qmlproject.user.* 35 | 36 | # QtCtreator CMake 37 | CMakeLists.txt.user 38 | 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KMapSolver 2 | Qt C++ and Lepton Based K-Map solver for unlimited variables 3 | 4 | # Demo 5 | 6 | ## 1. Simplify: `(A + C)(AD + AD) + AC + C` 7 | 8 | ![1](http://i.imgur.com/8eMw0Wx.png) 9 | 10 | ## 2. Simplify: `A(A + B) + (B + AA)(A + B)` 11 | 12 | ![2](http://i.imgur.com/8LRLDTm.png) 13 | 14 | ## 3. Simplify: `a'b'c + a'bc' + ab'c' + ab'c + abc' + abc` 15 | 16 | ![3](http://i.imgur.com/TnmL3CF.png) 17 | 18 | # Check Solution 19 | 20 | 1, 2: http://sandbox.mc.edu/~bennet/cs110/boolalg/simple.html 21 | 22 | 3: http://babbage.cs.qc.cuny.edu/courses/Minimize/ 23 | 24 | # Reference: 25 | 26 | [1]. Thanks to [Abdelrahman Elzedy](http://www.codeproject.com/script/Membership/View.aspx?mid=9729999) for the core [KMap Minimizer Program for infinite variables](http://www.codeproject.com/Articles/649849/A-Cplusplus-Karnaugh-Map-Minimizer-Infinite-Variab). 27 | 28 | [2]. [Lepton Math Parser for C++](https://simtk.org/projects/lepton) 29 | -------------------------------------------------------------------------------- /Console/KMapSolver/KMapSolver.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | CONFIG += console c++11 3 | CONFIG -= app_bundle 4 | QT += core 5 | QT -= gui 6 | 7 | SOURCES += main.cpp \ 8 | src/CompiledExpression.cpp \ 9 | src/ExpressionProgram.cpp \ 10 | src/ExpressionTreeNode.cpp \ 11 | src/Operation.cpp \ 12 | src/ParsedExpression.cpp \ 13 | src/Parser.cpp \ 14 | src/MSVC_erfc.h 15 | 16 | HEADERS += \ 17 | comparekmapterms.h \ 18 | filterkmapresults.h \ 19 | operation.h \ 20 | setkmap.h \ 21 | karnaughmap.h \ 22 | graycodehandler.h \ 23 | include/Lepton.h \ 24 | include/lepton/CompiledExpression.h \ 25 | include/lepton/CustomFunction.h \ 26 | include/lepton/Exception.h \ 27 | include/lepton/ExpressionProgram.h \ 28 | include/lepton/ExpressionTreeNode.h \ 29 | include/lepton/Operation.h \ 30 | include/lepton/ParsedExpression.h \ 31 | include/lepton/Parser.h \ 32 | include/lepton/windowsIncludes.h \ 33 | expressionhandler.h 34 | 35 | -------------------------------------------------------------------------------- /GUI/KMapSolver/KMapSolver.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | TARGET = KMapSolver 6 | TEMPLATE = app 7 | 8 | SOURCES += main.cpp \ 9 | src/CompiledExpression.cpp \ 10 | src/ExpressionProgram.cpp \ 11 | src/ExpressionTreeNode.cpp \ 12 | src/Operation.cpp \ 13 | src/ParsedExpression.cpp \ 14 | src/Parser.cpp \ 15 | src/MSVC_erfc.h \ 16 | dialog.cpp 17 | 18 | HEADERS += \ 19 | comparekmapterms.h \ 20 | filterkmapresults.h \ 21 | operation.h \ 22 | setkmap.h \ 23 | karnaughmap.h \ 24 | graycodehandler.h \ 25 | include/Lepton.h \ 26 | include/lepton/CompiledExpression.h \ 27 | include/lepton/CustomFunction.h \ 28 | include/lepton/Exception.h \ 29 | include/lepton/ExpressionProgram.h \ 30 | include/lepton/ExpressionTreeNode.h \ 31 | include/lepton/Operation.h \ 32 | include/lepton/ParsedExpression.h \ 33 | include/lepton/Parser.h \ 34 | include/lepton/windowsIncludes.h \ 35 | expressionhandler.h \ 36 | dialog.h 37 | 38 | FORMS += \ 39 | dialog.ui 40 | 41 | -------------------------------------------------------------------------------- /GUI/KMapSolver/dialog.cpp: -------------------------------------------------------------------------------- 1 | #include "dialog.h" 2 | #include "ui_dialog.h" 3 | 4 | #include "filterkmapresults.h" 5 | #include "operation.h" 6 | #include "karnaughmap.h" 7 | #include "graycodehandler.h" 8 | #include "include/Lepton.h" 9 | #include "expressionhandler.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | Dialog::Dialog(QWidget *parent) : 17 | QDialog(parent), 18 | ui(new Ui::Dialog) 19 | { 20 | ui->setupUi(this); 21 | setWindowTitle("KMap Solver"); 22 | 23 | } 24 | 25 | Dialog::~Dialog() 26 | { 27 | delete ui; 28 | } 29 | 30 | void Dialog::on_minimizeButton_clicked() 31 | { 32 | KarnaughMap k; 33 | std::string expr = ui->expressionLineEdit->text().toUpper().toStdString(); 34 | k.setType(countVariable(expr)); 35 | k.setOnesPosition(getOnesPosition(expr)); 36 | k.setResultType(true); 37 | std::string res = k.getResult(); 38 | cout << "Result: " << res << endl; 39 | ui->resultLineEdit->setText(QString::fromStdString(res)); 40 | } 41 | 42 | void Dialog::on_clearButton_clicked() 43 | { 44 | ui->expressionLineEdit->clear(); 45 | ui->resultLineEdit->clear(); 46 | } 47 | -------------------------------------------------------------------------------- /Console/KMapSolver/main.cpp: -------------------------------------------------------------------------------- 1 | #include "filterkmapresults.h" 2 | #include "operation.h" 3 | #include "karnaughmap.h" 4 | #include "graycodehandler.h" 5 | #include "include/Lepton.h" 6 | #include "expressionhandler.h" 7 | #include 8 | 9 | int main () 10 | { 11 | KarnaughMap k; 12 | vector on{0, 2, 13, 9, 15, 11}; 13 | // vector dcare {6, 12, 13}; 14 | k.setType(4); 15 | k.setResultType(true); 16 | k.setOnesPosition(on); 17 | // k.setDontCarePosition(dcare); 18 | 19 | cout << k.getResult() << endl; 20 | 21 | 22 | //// Kmap k1; 23 | // KarnaughMap k; 24 | // vector on1{0, 1, 7, 6}; 25 | //// on1.push_back(0); 26 | //// on1.push_back(1); 27 | //// on1.push_back(2); 28 | // vector dc; 29 | //// dc.push_back(3); 30 | // k.setType(3); 31 | // k.setResultType(true); 32 | // k.setOnesPosition(on1); 33 | // cout << endl; 34 | // cout << k.getResult() << endl; 35 | //// vector gc = generateGrayCodeSequence(3); 36 | // std::string expr = "AB+C'D'+EF"; 37 | 38 | // std::map variables; 39 | 40 | // variables["A"] = 1; 41 | // variables["B"] = 1; 42 | // variables["C"] = 0; 43 | // variables["D"] = 0; 44 | // variables["E"] = 1; 45 | // variables["F"] = 1; 46 | 47 | // cout << evaluateExpression(variables, expr) << endl; 48 | 49 | // cout << countVariable(expr) << endl; 50 | 51 | std::string expresso = "A'B'C+ABC'+B'C'"; 52 | getOnesPosition(expresso); 53 | 54 | }//end main 55 | -------------------------------------------------------------------------------- /GUI/KMapSolver/graycodehandler.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAYCODEHANDLER_H 2 | #define GRAYCODEHANDLER_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::string; 9 | using std::cout; 10 | using std::endl; 11 | 12 | typedef unsigned int _ui; 13 | 14 | vector generateGrayCodeSequence(int n, bool debug=false) 15 | { 16 | // 'arr' will store all generated codes 17 | vector arr; 18 | 19 | // base case 20 | if (n <= 0) 21 | return arr; 22 | 23 | // start with one-bit pattern 24 | arr.push_back("0"); 25 | arr.push_back("1"); 26 | 27 | // Every iteration of this loop generates 2*i codes from previously 28 | // generated i codes. 29 | int i, j; 30 | for (i = 2; i < (1<= 0 ; j--) 35 | arr.push_back(arr[j]); 36 | 37 | // append 0 to the first half 38 | for (j = 0 ; j < i ; j++) 39 | arr[j] = "0" + arr[j]; 40 | 41 | // append 1 to the second half 42 | for (j = i ; j < 2*i ; j++) 43 | arr[j] = "1" + arr[j]; 44 | } 45 | 46 | // print contents of arr[] 47 | if (debug) 48 | for (i = 0 ; i < arr.size() ; i++ ) 49 | cout << arr[i] << endl; 50 | 51 | return arr; 52 | } 53 | 54 | vector <_ui> getDecimalGrayCodeSequence(int n) 55 | { 56 | vector seq = generateGrayCodeSequence(n); 57 | vector <_ui> decimalSeq; 58 | char *end; 59 | 60 | for (string s : seq){ 61 | decimalSeq.push_back(strtoull(s.c_str(), &end, 2)); 62 | } 63 | 64 | return decimalSeq; 65 | } 66 | 67 | _ui strBinaryToDecimal(string s) 68 | { 69 | char *end; 70 | return strtoull(s.c_str(), &end, 2); 71 | } 72 | 73 | #endif // GRAYCODEHANDLER_H 74 | -------------------------------------------------------------------------------- /Console/KMapSolver/graycodehandler.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAYCODEHANDLER_H 2 | #define GRAYCODEHANDLER_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::string; 9 | using std::cout; 10 | using std::endl; 11 | 12 | typedef unsigned int _ui; 13 | 14 | vector generateGrayCodeSequence(int n, bool debug=false) 15 | { 16 | // 'arr' will store all generated codes 17 | vector arr; 18 | 19 | // base case 20 | if (n <= 0) 21 | return arr; 22 | 23 | // start with one-bit pattern 24 | arr.push_back("0"); 25 | arr.push_back("1"); 26 | 27 | // Every iteration of this loop generates 2*i codes from previously 28 | // generated i codes. 29 | int i, j; 30 | for (i = 2; i < (1<= 0 ; j--) 35 | arr.push_back(arr[j]); 36 | 37 | // append 0 to the first half 38 | for (j = 0 ; j < i ; j++) 39 | arr[j] = "0" + arr[j]; 40 | 41 | // append 1 to the second half 42 | for (j = i ; j < 2*i ; j++) 43 | arr[j] = "1" + arr[j]; 44 | } 45 | 46 | // print contents of arr[] 47 | if (debug) 48 | for (i = 0 ; i < arr.size() ; i++ ) 49 | cout << arr[i] << endl; 50 | 51 | return arr; 52 | } 53 | 54 | vector <_ui> getDecimalGrayCodeSequence(int n) 55 | { 56 | vector seq = generateGrayCodeSequence(n); 57 | vector <_ui> decimalSeq; 58 | char *end; 59 | 60 | for (string s : seq){ 61 | decimalSeq.push_back(strtoull(s.c_str(), &end, 2)); 62 | } 63 | 64 | return decimalSeq; 65 | } 66 | 67 | _ui strBinaryToDecimal(string s) 68 | { 69 | char *end; 70 | return strtoull(s.c_str(), &end, 2); 71 | } 72 | 73 | #endif // GRAYCODEHANDLER_H 74 | -------------------------------------------------------------------------------- /GUI/KMapSolver/include/lepton/windowsIncludes.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_WINDOW_INCLUDE_H_ 2 | #define LEPTON_WINDOW_INCLUDE_H_ 3 | 4 | /* 5 | * Shared libraries are messy in Visual Studio. We have to distinguish three 6 | * cases: 7 | * (1) this header is being used to build the Lepton shared library 8 | * (dllexport) 9 | * (2) this header is being used by a *client* of the Lepton shared 10 | * library (dllimport) 11 | * (3) we are building the Lepton static library, or the client is 12 | * being compiled with the expectation of linking with the 13 | * Lepton static library (nothing special needed) 14 | * In the CMake script for building this library, we define one of the symbols 15 | * Lepton_BUILDING_{SHARED|STATIC}_LIBRARY 16 | * Client code normally has no special symbol defined, in which case we'll 17 | * assume it wants to use the shared library. However, if the client defines 18 | * the symbol LEPTON_USE_STATIC_LIBRARIES we'll suppress the dllimport so 19 | * that the client code can be linked with static libraries. Note that 20 | * the client symbol is not library dependent, while the library symbols 21 | * affect only the Lepton library, meaning that other libraries can 22 | * be clients of this one. However, we are assuming all-static or all-shared. 23 | */ 24 | 25 | #ifdef _MSC_VER 26 | // We don't want to hear about how sprintf is "unsafe". 27 | #pragma warning(disable:4996) 28 | #if defined(LEPTON_BUILDING_SHARED_LIBRARY) 29 | #define LEPTON_EXPORT __declspec(dllexport) 30 | // Keep MS VC++ quiet about lack of dll export of private members. 31 | #pragma warning(disable:4251) 32 | #elif defined(LEPTON_BUILDING_STATIC_LIBRARY) || defined(LEPTON_USE_STATIC_LIBRARIES) 33 | #define LEPTON_EXPORT 34 | #else 35 | #define LEPTON_EXPORT __declspec(dllimport) // i.e., a client of a shared library 36 | #endif 37 | #else 38 | #define LEPTON_EXPORT // Linux, Mac 39 | #endif 40 | 41 | #endif // LEPTON_WINDOW_INCLUDE_H_ 42 | -------------------------------------------------------------------------------- /Console/KMapSolver/include/lepton/windowsIncludes.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_WINDOW_INCLUDE_H_ 2 | #define LEPTON_WINDOW_INCLUDE_H_ 3 | 4 | /* 5 | * Shared libraries are messy in Visual Studio. We have to distinguish three 6 | * cases: 7 | * (1) this header is being used to build the Lepton shared library 8 | * (dllexport) 9 | * (2) this header is being used by a *client* of the Lepton shared 10 | * library (dllimport) 11 | * (3) we are building the Lepton static library, or the client is 12 | * being compiled with the expectation of linking with the 13 | * Lepton static library (nothing special needed) 14 | * In the CMake script for building this library, we define one of the symbols 15 | * Lepton_BUILDING_{SHARED|STATIC}_LIBRARY 16 | * Client code normally has no special symbol defined, in which case we'll 17 | * assume it wants to use the shared library. However, if the client defines 18 | * the symbol LEPTON_USE_STATIC_LIBRARIES we'll suppress the dllimport so 19 | * that the client code can be linked with static libraries. Note that 20 | * the client symbol is not library dependent, while the library symbols 21 | * affect only the Lepton library, meaning that other libraries can 22 | * be clients of this one. However, we are assuming all-static or all-shared. 23 | */ 24 | 25 | #ifdef _MSC_VER 26 | // We don't want to hear about how sprintf is "unsafe". 27 | #pragma warning(disable:4996) 28 | #if defined(LEPTON_BUILDING_SHARED_LIBRARY) 29 | #define LEPTON_EXPORT __declspec(dllexport) 30 | // Keep MS VC++ quiet about lack of dll export of private members. 31 | #pragma warning(disable:4251) 32 | #elif defined(LEPTON_BUILDING_STATIC_LIBRARY) || defined(LEPTON_USE_STATIC_LIBRARIES) 33 | #define LEPTON_EXPORT 34 | #else 35 | #define LEPTON_EXPORT __declspec(dllimport) // i.e., a client of a shared library 36 | #endif 37 | #else 38 | #define LEPTON_EXPORT // Linux, Mac 39 | #endif 40 | 41 | #endif // LEPTON_WINDOW_INCLUDE_H_ 42 | -------------------------------------------------------------------------------- /GUI/KMapSolver/dialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Dialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 305 10 | 99 11 | 12 | 13 | 14 | Dialog 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Expression 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | Qt::Horizontal 37 | 38 | 39 | 40 | 40 41 | 20 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | Minimize 50 | 51 | 52 | 53 | 54 | 55 | 56 | Clear 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | Minimized 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /GUI/KMapSolver/karnaughmap.h: -------------------------------------------------------------------------------- 1 | #ifndef KARNAUGHMAP_H 2 | #define KARNAUGHMAP_H 3 | 4 | #include "filterkmapresults.h" 5 | #include "operation.h" 6 | 7 | class KarnaughMap : public FilterKmapTerms, public SOPtoPOS 8 | { 9 | private: 10 | int temp; 11 | int temp1; 12 | int temp2; 13 | int temp3; 14 | 15 | char tempChar; 16 | vector result; 17 | vector < vector > filterResults; 18 | std::string res; 19 | 20 | public: 21 | void clear() 22 | { 23 | tempChar = NULL; 24 | result.clear(); 25 | filterResults.clear(); 26 | ones.clear(); 27 | dCare.clear(); 28 | } 29 | 30 | // Number of variables 31 | void setType(int t) 32 | { 33 | type = t; 34 | } 35 | 36 | // SOP or POS [True for SOP, false for POS] 37 | void setResultType(bool s) 38 | { 39 | SOP = s; 40 | } 41 | 42 | // Getting position of ones 43 | void setOnesPosition(vector onesPosition) 44 | { 45 | ones = onesPosition; 46 | } 47 | 48 | // Getting position of don't cares 49 | void setDontCarePosition(vector dontCarePosition) 50 | { 51 | dCare = dontCarePosition; 52 | } 53 | 54 | std::string getResult(void) 55 | { 56 | result = minimize(ones, dCare); //Solving by minimizing 57 | 58 | if( result.size() != 1 || //not full or empty maps 59 | ( result[0] != 48 && result[0] != 49 ) ) 60 | { 61 | filterResults.clear(); 62 | filterResults = filter(result,ones); //filter result from unessential terms 63 | 64 | for(int temp = 0; temp < filterResults.size(); temp++) 65 | { 66 | if(filterResults.size() > 1){ 67 | res += "\n" + std::to_string(temp + 1) + " - "; 68 | } 69 | 70 | if(SOP == false) convSopToPos(filterResults[temp]); 71 | 72 | for(int temp1 = 0; temp1 < filterResults[temp].size(); temp1++){ 73 | res += filterResults[temp][temp1]; 74 | } 75 | } 76 | 77 | } 78 | else{ 79 | res += result[0] + "\n"; 80 | } 81 | return res; 82 | } 83 | }; 84 | 85 | #endif // KARNAUGHMAP_H 86 | -------------------------------------------------------------------------------- /Console/KMapSolver/karnaughmap.h: -------------------------------------------------------------------------------- 1 | #ifndef KARNAUGHMAP_H 2 | #define KARNAUGHMAP_H 3 | 4 | #include "filterkmapresults.h" 5 | #include "operation.h" 6 | 7 | class KarnaughMap : public FilterKmapTerms, public SOPtoPOS 8 | { 9 | private: 10 | int temp; 11 | int temp1; 12 | int temp2; 13 | int temp3; 14 | 15 | char tempChar; 16 | vector result; 17 | vector < vector > filterResults; 18 | std::string res; 19 | 20 | public: 21 | void clear() 22 | { 23 | tempChar = NULL; 24 | result.clear(); 25 | filterResults.clear(); 26 | ones.clear(); 27 | dCare.clear(); 28 | } 29 | 30 | // Number of variables 31 | void setType(int t) 32 | { 33 | type = t; 34 | } 35 | 36 | // SOP or POS [True for SOP, false for POS] 37 | void setResultType(bool s) 38 | { 39 | SOP = s; 40 | } 41 | 42 | // Getting position of ones 43 | void setOnesPosition(vector onesPosition) 44 | { 45 | ones = onesPosition; 46 | } 47 | 48 | // Getting position of don't cares 49 | void setDontCarePosition(vector dontCarePosition) 50 | { 51 | dCare = dontCarePosition; 52 | } 53 | 54 | std::string getResult(void) 55 | { 56 | result = minimize(ones, dCare); //Solving by minimizing 57 | 58 | if( result.size() != 1 || //not full or empty maps 59 | ( result[0] != 48 && result[0] != 49 ) ) 60 | { 61 | filterResults.clear(); 62 | filterResults = filter(result,ones); //filter result from unessential terms 63 | 64 | for(int temp = 0; temp < filterResults.size(); temp++) 65 | { 66 | if(filterResults.size() > 1){ 67 | res += "\n" + std::to_string(temp + 1) + " - "; 68 | } 69 | 70 | if(SOP == false) convSopToPos(filterResults[temp]); 71 | 72 | for(int temp1 = 0; temp1 < filterResults[temp].size(); temp1++){ 73 | res += filterResults[temp][temp1]; 74 | } 75 | } 76 | 77 | } 78 | else{ 79 | res += result[0] + "\n"; 80 | } 81 | return res; 82 | } 83 | }; 84 | 85 | #endif // KARNAUGHMAP_H 86 | -------------------------------------------------------------------------------- /GUI/KMapSolver/include/Lepton.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_H_ 2 | #define LEPTON_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "lepton/CompiledExpression.h" 36 | #include "lepton/CustomFunction.h" 37 | #include "lepton/ExpressionProgram.h" 38 | #include "lepton/ExpressionTreeNode.h" 39 | #include "lepton/Operation.h" 40 | #include "lepton/ParsedExpression.h" 41 | #include "lepton/Parser.h" 42 | 43 | #endif /*LEPTON_H_*/ 44 | -------------------------------------------------------------------------------- /Console/KMapSolver/include/Lepton.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_H_ 2 | #define LEPTON_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "lepton/CompiledExpression.h" 36 | #include "lepton/CustomFunction.h" 37 | #include "lepton/ExpressionProgram.h" 38 | #include "lepton/ExpressionTreeNode.h" 39 | #include "lepton/Operation.h" 40 | #include "lepton/ParsedExpression.h" 41 | #include "lepton/Parser.h" 42 | 43 | #endif /*LEPTON_H_*/ 44 | -------------------------------------------------------------------------------- /GUI/KMapSolver/src/MSVC_erfc.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_MSVC_ERFC_H_ 2 | #define LEPTON_MSVC_ERFC_H_ 3 | 4 | /* 5 | * Up to version 11 (VC++ 2012), Microsoft does not support the 6 | * standard C99 erf() and erfc() functions so we have to fake them here. 7 | * These were added in version 12 (VC++ 2013), which sets _MSC_VER=1800 8 | * (VC11 has _MSC_VER=1700). 9 | */ 10 | 11 | #if defined(_MSC_VER) 12 | #define M_PI 3.14159265358979323846264338327950288 13 | 14 | #if _MSC_VER <= 1700 // 1700 is VC11, 1800 is VC12 15 | /*************************** 16 | * erf.cpp 17 | * author: Steve Strand 18 | * written: 29-Jan-04 19 | ***************************/ 20 | 21 | #include 22 | 23 | static const double rel_error= 1E-12; //calculate 12 significant figures 24 | //you can adjust rel_error to trade off between accuracy and speed 25 | //but don't ask for > 15 figures (assuming usual 52 bit mantissa in a double) 26 | 27 | static double erfc(double x); 28 | 29 | static double erf(double x) 30 | //erf(x) = 2/sqrt(pi)*integral(exp(-t^2),t,0,x) 31 | // = 2/sqrt(pi)*[x - x^3/3 + x^5/5*2! - x^7/7*3! + ...] 32 | // = 1-erfc(x) 33 | { 34 | static const double two_sqrtpi= 1.128379167095512574; // 2/sqrt(pi) 35 | if (fabs(x) > 2.2) { 36 | return 1.0 - erfc(x); //use continued fraction when fabs(x) > 2.2 37 | } 38 | double sum= x, term= x, xsqr= x*x; 39 | int j= 1; 40 | do { 41 | term*= xsqr/j; 42 | sum-= term/(2*j+1); 43 | ++j; 44 | term*= xsqr/j; 45 | sum+= term/(2*j+1); 46 | ++j; 47 | } while (fabs(term)/sum > rel_error); 48 | return two_sqrtpi*sum; 49 | } 50 | 51 | 52 | static double erfc(double x) 53 | //erfc(x) = 2/sqrt(pi)*integral(exp(-t^2),t,x,inf) 54 | // = exp(-x^2)/sqrt(pi) * [1/x+ (1/2)/x+ (2/2)/x+ (3/2)/x+ (4/2)/x+ ...] 55 | // = 1-erf(x) 56 | //expression inside [] is a continued fraction so '+' means add to denominator only 57 | { 58 | static const double one_sqrtpi= 0.564189583547756287; // 1/sqrt(pi) 59 | if (fabs(x) < 2.2) { 60 | return 1.0 - erf(x); //use series when fabs(x) < 2.2 61 | } 62 | // Don't look for x==0 here! 63 | if (x < 0) { //continued fraction only valid for x>0 64 | return 2.0 - erfc(-x); 65 | } 66 | double a=1, b=x; //last two convergent numerators 67 | double c=x, d=x*x+0.5; //last two convergent denominators 68 | double q1, q2= b/d; //last two convergents (a/c and b/d) 69 | double n= 1.0, t; 70 | do { 71 | t= a*n+b*x; 72 | a= b; 73 | b= t; 74 | t= c*n+d*x; 75 | c= d; 76 | d= t; 77 | n+= 0.5; 78 | q1= q2; 79 | q2= b/d; 80 | } while (fabs(q1-q2)/q2 > rel_error); 81 | return one_sqrtpi*exp(-x*x)*q2; 82 | } 83 | 84 | #endif // _MSC_VER <= 1700 85 | #endif // _MSC_VER 86 | 87 | #endif // LEPTON_MSVC_ERFC_H_ 88 | -------------------------------------------------------------------------------- /Console/KMapSolver/src/MSVC_erfc.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_MSVC_ERFC_H_ 2 | #define LEPTON_MSVC_ERFC_H_ 3 | 4 | /* 5 | * Up to version 11 (VC++ 2012), Microsoft does not support the 6 | * standard C99 erf() and erfc() functions so we have to fake them here. 7 | * These were added in version 12 (VC++ 2013), which sets _MSC_VER=1800 8 | * (VC11 has _MSC_VER=1700). 9 | */ 10 | 11 | #if defined(_MSC_VER) 12 | #define M_PI 3.14159265358979323846264338327950288 13 | 14 | #if _MSC_VER <= 1700 // 1700 is VC11, 1800 is VC12 15 | /*************************** 16 | * erf.cpp 17 | * author: Steve Strand 18 | * written: 29-Jan-04 19 | ***************************/ 20 | 21 | #include 22 | 23 | static const double rel_error= 1E-12; //calculate 12 significant figures 24 | //you can adjust rel_error to trade off between accuracy and speed 25 | //but don't ask for > 15 figures (assuming usual 52 bit mantissa in a double) 26 | 27 | static double erfc(double x); 28 | 29 | static double erf(double x) 30 | //erf(x) = 2/sqrt(pi)*integral(exp(-t^2),t,0,x) 31 | // = 2/sqrt(pi)*[x - x^3/3 + x^5/5*2! - x^7/7*3! + ...] 32 | // = 1-erfc(x) 33 | { 34 | static const double two_sqrtpi= 1.128379167095512574; // 2/sqrt(pi) 35 | if (fabs(x) > 2.2) { 36 | return 1.0 - erfc(x); //use continued fraction when fabs(x) > 2.2 37 | } 38 | double sum= x, term= x, xsqr= x*x; 39 | int j= 1; 40 | do { 41 | term*= xsqr/j; 42 | sum-= term/(2*j+1); 43 | ++j; 44 | term*= xsqr/j; 45 | sum+= term/(2*j+1); 46 | ++j; 47 | } while (fabs(term)/sum > rel_error); 48 | return two_sqrtpi*sum; 49 | } 50 | 51 | 52 | static double erfc(double x) 53 | //erfc(x) = 2/sqrt(pi)*integral(exp(-t^2),t,x,inf) 54 | // = exp(-x^2)/sqrt(pi) * [1/x+ (1/2)/x+ (2/2)/x+ (3/2)/x+ (4/2)/x+ ...] 55 | // = 1-erf(x) 56 | //expression inside [] is a continued fraction so '+' means add to denominator only 57 | { 58 | static const double one_sqrtpi= 0.564189583547756287; // 1/sqrt(pi) 59 | if (fabs(x) < 2.2) { 60 | return 1.0 - erf(x); //use series when fabs(x) < 2.2 61 | } 62 | // Don't look for x==0 here! 63 | if (x < 0) { //continued fraction only valid for x>0 64 | return 2.0 - erfc(-x); 65 | } 66 | double a=1, b=x; //last two convergent numerators 67 | double c=x, d=x*x+0.5; //last two convergent denominators 68 | double q1, q2= b/d; //last two convergents (a/c and b/d) 69 | double n= 1.0, t; 70 | do { 71 | t= a*n+b*x; 72 | a= b; 73 | b= t; 74 | t= c*n+d*x; 75 | c= d; 76 | d= t; 77 | n+= 0.5; 78 | q1= q2; 79 | q2= b/d; 80 | } while (fabs(q1-q2)/q2 > rel_error); 81 | return one_sqrtpi*exp(-x*x)*q2; 82 | } 83 | 84 | #endif // _MSC_VER <= 1700 85 | #endif // _MSC_VER 86 | 87 | #endif // LEPTON_MSVC_ERFC_H_ 88 | -------------------------------------------------------------------------------- /GUI/KMapSolver/include/lepton/Exception.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_EXCEPTION_H_ 2 | #define LEPTON_EXCEPTION_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include 36 | #include 37 | 38 | namespace Lepton { 39 | 40 | /** 41 | * This class is used for all exceptions thrown by Lepton. 42 | */ 43 | 44 | class Exception : public std::exception { 45 | public: 46 | Exception(const std::string& message) : message(message) { 47 | } 48 | ~Exception() throw() { 49 | } 50 | const char* what() const throw() { 51 | return message.c_str(); 52 | } 53 | private: 54 | std::string message; 55 | }; 56 | 57 | } // namespace Lepton 58 | 59 | #endif /*LEPTON_EXCEPTION_H_*/ 60 | -------------------------------------------------------------------------------- /Console/KMapSolver/include/lepton/Exception.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_EXCEPTION_H_ 2 | #define LEPTON_EXCEPTION_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include 36 | #include 37 | 38 | namespace Lepton { 39 | 40 | /** 41 | * This class is used for all exceptions thrown by Lepton. 42 | */ 43 | 44 | class Exception : public std::exception { 45 | public: 46 | Exception(const std::string& message) : message(message) { 47 | } 48 | ~Exception() throw() { 49 | } 50 | const char* what() const throw() { 51 | return message.c_str(); 52 | } 53 | private: 54 | std::string message; 55 | }; 56 | 57 | } // namespace Lepton 58 | 59 | #endif /*LEPTON_EXCEPTION_H_*/ 60 | -------------------------------------------------------------------------------- /GUI/KMapSolver/include/lepton/CustomFunction.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_CUSTOM_FUNCTION_H_ 2 | #define LEPTON_CUSTOM_FUNCTION_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "windowsIncludes.h" 36 | 37 | namespace Lepton { 38 | 39 | /** 40 | * This class is the interface for defining your own function that may be included in expressions. 41 | * To use it, create a concrete subclass that implements all of the virtual methods for each new function 42 | * you want to define. Then when you call Parser::parse() to parse an expression, pass a map of 43 | * function names to CustomFunction objects. 44 | */ 45 | 46 | class LEPTON_EXPORT CustomFunction { 47 | public: 48 | virtual ~CustomFunction() { 49 | } 50 | /** 51 | * Get the number of arguments this function exprects. 52 | */ 53 | virtual int getNumArguments() const = 0; 54 | /** 55 | * Evaluate the function. 56 | * 57 | * @param arguments the array of argument values 58 | */ 59 | virtual double evaluate(const double* arguments) const = 0; 60 | /** 61 | * Evaluate a derivative of the function. 62 | * 63 | * @param arguments the array of argument values 64 | * @param derivOrder an array specifying the number of times the function has been differentiated 65 | * with respect to each of its arguments. For example, the array {0, 2} indicates 66 | * a second derivative with respect to the second argument. 67 | */ 68 | virtual double evaluateDerivative(const double* arguments, const int* derivOrder) const = 0; 69 | /** 70 | * Create a new duplicate of this object on the heap using the "new" operator. 71 | */ 72 | virtual CustomFunction* clone() const = 0; 73 | }; 74 | 75 | } // namespace Lepton 76 | 77 | #endif /*LEPTON_CUSTOM_FUNCTION_H_*/ 78 | -------------------------------------------------------------------------------- /Console/KMapSolver/include/lepton/CustomFunction.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_CUSTOM_FUNCTION_H_ 2 | #define LEPTON_CUSTOM_FUNCTION_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "windowsIncludes.h" 36 | 37 | namespace Lepton { 38 | 39 | /** 40 | * This class is the interface for defining your own function that may be included in expressions. 41 | * To use it, create a concrete subclass that implements all of the virtual methods for each new function 42 | * you want to define. Then when you call Parser::parse() to parse an expression, pass a map of 43 | * function names to CustomFunction objects. 44 | */ 45 | 46 | class LEPTON_EXPORT CustomFunction { 47 | public: 48 | virtual ~CustomFunction() { 49 | } 50 | /** 51 | * Get the number of arguments this function exprects. 52 | */ 53 | virtual int getNumArguments() const = 0; 54 | /** 55 | * Evaluate the function. 56 | * 57 | * @param arguments the array of argument values 58 | */ 59 | virtual double evaluate(const double* arguments) const = 0; 60 | /** 61 | * Evaluate a derivative of the function. 62 | * 63 | * @param arguments the array of argument values 64 | * @param derivOrder an array specifying the number of times the function has been differentiated 65 | * with respect to each of its arguments. For example, the array {0, 2} indicates 66 | * a second derivative with respect to the second argument. 67 | */ 68 | virtual double evaluateDerivative(const double* arguments, const int* derivOrder) const = 0; 69 | /** 70 | * Create a new duplicate of this object on the heap using the "new" operator. 71 | */ 72 | virtual CustomFunction* clone() const = 0; 73 | }; 74 | 75 | } // namespace Lepton 76 | 77 | #endif /*LEPTON_CUSTOM_FUNCTION_H_*/ 78 | -------------------------------------------------------------------------------- /GUI/KMapSolver/include/lepton/Parser.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_PARSER_H_ 2 | #define LEPTON_PARSER_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "windowsIncludes.h" 36 | #include 37 | #include 38 | #include 39 | 40 | namespace Lepton { 41 | 42 | class CustomFunction; 43 | class ExpressionTreeNode; 44 | class Operation; 45 | class ParsedExpression; 46 | class ParseToken; 47 | 48 | /** 49 | * This class provides the main interface for parsing expressions. 50 | */ 51 | 52 | class LEPTON_EXPORT Parser { 53 | public: 54 | /** 55 | * Parse a mathematical expression and return a representation of it as an abstract syntax tree. 56 | */ 57 | static ParsedExpression parse(const std::string& expression); 58 | /** 59 | * Parse a mathematical expression and return a representation of it as an abstract syntax tree. 60 | * 61 | * @param customFunctions a map specifying user defined functions that may appear in the expression. 62 | * The key are function names, and the values are corresponding CustomFunction objects. 63 | */ 64 | static ParsedExpression parse(const std::string& expression, const std::map& customFunctions); 65 | private: 66 | static std::string trim(const std::string& expression); 67 | static std::vector tokenize(const std::string& expression); 68 | static ParseToken getNextToken(const std::string& expression, int start); 69 | static ExpressionTreeNode parsePrecedence(const std::vector& tokens, int& pos, const std::map& customFunctions, 70 | const std::map& subexpressionDefs, int precedence); 71 | static Operation* getOperatorOperation(const std::string& name); 72 | static Operation* getFunctionOperation(const std::string& name, const std::map& customFunctions); 73 | }; 74 | 75 | } // namespace Lepton 76 | 77 | #endif /*LEPTON_PARSER_H_*/ 78 | -------------------------------------------------------------------------------- /Console/KMapSolver/include/lepton/Parser.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_PARSER_H_ 2 | #define LEPTON_PARSER_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "windowsIncludes.h" 36 | #include 37 | #include 38 | #include 39 | 40 | namespace Lepton { 41 | 42 | class CustomFunction; 43 | class ExpressionTreeNode; 44 | class Operation; 45 | class ParsedExpression; 46 | class ParseToken; 47 | 48 | /** 49 | * This class provides the main interface for parsing expressions. 50 | */ 51 | 52 | class LEPTON_EXPORT Parser { 53 | public: 54 | /** 55 | * Parse a mathematical expression and return a representation of it as an abstract syntax tree. 56 | */ 57 | static ParsedExpression parse(const std::string& expression); 58 | /** 59 | * Parse a mathematical expression and return a representation of it as an abstract syntax tree. 60 | * 61 | * @param customFunctions a map specifying user defined functions that may appear in the expression. 62 | * The key are function names, and the values are corresponding CustomFunction objects. 63 | */ 64 | static ParsedExpression parse(const std::string& expression, const std::map& customFunctions); 65 | private: 66 | static std::string trim(const std::string& expression); 67 | static std::vector tokenize(const std::string& expression); 68 | static ParseToken getNextToken(const std::string& expression, int start); 69 | static ExpressionTreeNode parsePrecedence(const std::vector& tokens, int& pos, const std::map& customFunctions, 70 | const std::map& subexpressionDefs, int precedence); 71 | static Operation* getOperatorOperation(const std::string& name); 72 | static Operation* getFunctionOperation(const std::string& name, const std::map& customFunctions); 73 | }; 74 | 75 | } // namespace Lepton 76 | 77 | #endif /*LEPTON_PARSER_H_*/ 78 | -------------------------------------------------------------------------------- /GUI/KMapSolver/expressionhandler.h: -------------------------------------------------------------------------------- 1 | #ifndef EXPRESSIONHANDLER_H 2 | #define EXPRESSIONHANDLER_H 3 | 4 | #include "include/Lepton.h" 5 | #include 6 | #include 7 | #include 8 | #include "graycodehandler.h" 9 | 10 | using std::string; 11 | using std::vector; 12 | 13 | using std::cout; 14 | using std::endl; 15 | 16 | vector literals{"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"}; 17 | 18 | 19 | 20 | string getInvertedExpression(char variable) 21 | { 22 | string var(1, variable); 23 | return "(1-" + var + ")"; 24 | } 25 | 26 | unsigned int countVariable(QString expression) 27 | { 28 | set charSet; 29 | for (char c : expression.toStdString()){ 30 | if (!ispunct(c) && !isdigit(c)) 31 | charSet.insert(charSet.begin(), c); 32 | } 33 | set ::iterator it = charSet.end(); 34 | it--; 35 | return ((*it) - 'A' + 1); 36 | } 37 | 38 | unsigned int countVariable(std::string expression) 39 | { 40 | set charSet; 41 | for (char c : expression){ 42 | if (!ispunct(c) && !isdigit(c)) 43 | charSet.insert(charSet.begin(), c); 44 | } 45 | set ::iterator it = charSet.end(); 46 | it--; 47 | return ((*it) - 'A' + 1); 48 | } 49 | 50 | std::string parseExpression(std::string expr) 51 | { 52 | std::string::iterator it = expr.begin(); 53 | 54 | //Deals with the inverted part 55 | while (it != expr.end()) 56 | { 57 | if ((*it) == '\''){ 58 | expr.erase(it); 59 | it--; 60 | string inv_expr = getInvertedExpression((*it)); 61 | expr.erase(it); 62 | expr.insert(it, inv_expr.begin(), inv_expr.end()); 63 | it = expr.begin(); 64 | } else { 65 | it++; 66 | } 67 | } 68 | 69 | cout << expr << endl; 70 | 71 | it = expr.begin(); 72 | // inserts '*' where needed 73 | while (it != expr.end()){ 74 | if (isalpha(*it) || (*it) == ')'){ 75 | it++; 76 | if (isalpha(*it) || (*it) == '('){ 77 | expr.insert(it, '*'); 78 | it = expr.begin(); 79 | it++; 80 | } 81 | } else { 82 | it++; 83 | } 84 | } 85 | return expr; 86 | } 87 | 88 | void parseExpression(QString expression) 89 | { 90 | std::string expr = expression.toStdString(); 91 | std::string::iterator it = expr.begin(); 92 | 93 | //Deals with the inverted part 94 | while (it != expr.end()) 95 | { 96 | if ((*it) == '\''){ 97 | expr.erase(it); 98 | it--; 99 | string inv_expr = getInvertedExpression((*it)); 100 | expr.erase(it); 101 | expr.insert(it, inv_expr.begin(), inv_expr.end()); 102 | it = expr.begin(); 103 | } else { 104 | it++; 105 | } 106 | } 107 | 108 | cout << expr << endl; 109 | 110 | it = expr.begin(); 111 | // inserts '*' where needed 112 | while (it != expr.end()){ 113 | if (isalpha(*it) || (*it) == ')'){ 114 | it++; 115 | if (isalpha(*it) || (*it) == '('){ 116 | expr.insert(it, '*'); 117 | it = expr.begin(); 118 | it++; 119 | } 120 | } else { 121 | it++; 122 | } 123 | } 124 | cout << expr << endl; 125 | } 126 | 127 | unsigned int evaluateExpression(map variables, std::string expr) 128 | { 129 | std::string parsedExpr = parseExpression(expr); 130 | cout << "parsed expr: " << parsedExpr << endl; 131 | double value = Lepton::Parser::parse(parsedExpr).evaluate(variables); 132 | if (value >= 1.0) 133 | return 1; 134 | return 0; 135 | } 136 | 137 | vector getOnesPosition(std::string expression){ 138 | map vars; 139 | vector _ones; 140 | int vc = countVariable(expression); 141 | vector grayCodes = generateGrayCodeSequence(vc); 142 | 143 | // 100 144 | for (string graycode: grayCodes){ 145 | for (int i = 0; i < graycode.size(); i++){ 146 | std::string value(1, graycode[i]); 147 | vars[literals[i]] = atoi(value.c_str()); 148 | } 149 | 150 | int exprValue = evaluateExpression(vars, expression); 151 | if (exprValue > 0){ 152 | _ones.push_back(strBinaryToDecimal(graycode)); 153 | } 154 | } 155 | 156 | for (int v : _ones){ 157 | cout << "val : " << v << endl; 158 | } 159 | 160 | return _ones; 161 | } 162 | 163 | #endif // EXPRESSIONHANDLER_H 164 | -------------------------------------------------------------------------------- /Console/KMapSolver/expressionhandler.h: -------------------------------------------------------------------------------- 1 | #ifndef EXPRESSIONHANDLER_H 2 | #define EXPRESSIONHANDLER_H 3 | 4 | #include "include/Lepton.h" 5 | #include 6 | #include 7 | #include 8 | #include "graycodehandler.h" 9 | 10 | using std::string; 11 | using std::vector; 12 | 13 | using std::cout; 14 | using std::endl; 15 | 16 | vector literals{"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"}; 17 | 18 | 19 | 20 | string getInvertedExpression(char variable) 21 | { 22 | string var(1, variable); 23 | return "(1-" + var + ")"; 24 | } 25 | 26 | unsigned int countVariable(QString expression) 27 | { 28 | set charSet; 29 | for (char c : expression.toStdString()){ 30 | if (!ispunct(c) && !isdigit(c)) 31 | charSet.insert(charSet.begin(), c); 32 | } 33 | set ::iterator it = charSet.end(); 34 | it--; 35 | return ((*it) - 'A' + 1); 36 | } 37 | 38 | unsigned int countVariable(std::string expression) 39 | { 40 | set charSet; 41 | for (char c : expression){ 42 | if (!ispunct(c) && !isdigit(c)) 43 | charSet.insert(charSet.begin(), c); 44 | } 45 | set ::iterator it = charSet.end(); 46 | it--; 47 | return ((*it) - 'A' + 1); 48 | } 49 | 50 | std::string parseExpression(std::string expr) 51 | { 52 | std::string::iterator it = expr.begin(); 53 | 54 | //Deals with the inverted part 55 | while (it != expr.end()) 56 | { 57 | if ((*it) == '\''){ 58 | expr.erase(it); 59 | it--; 60 | string inv_expr = getInvertedExpression((*it)); 61 | expr.erase(it); 62 | expr.insert(it, inv_expr.begin(), inv_expr.end()); 63 | it = expr.begin(); 64 | } else { 65 | it++; 66 | } 67 | } 68 | 69 | cout << expr << endl; 70 | 71 | it = expr.begin(); 72 | // inserts '*' where needed 73 | while (it != expr.end()){ 74 | if (isalpha(*it) || (*it) == ')'){ 75 | it++; 76 | if (isalpha(*it) || (*it) == '('){ 77 | expr.insert(it, '*'); 78 | it = expr.begin(); 79 | it++; 80 | } 81 | } else { 82 | it++; 83 | } 84 | } 85 | return expr; 86 | } 87 | 88 | void parseExpression(QString expression) 89 | { 90 | std::string expr = expression.toStdString(); 91 | std::string::iterator it = expr.begin(); 92 | 93 | //Deals with the inverted part 94 | while (it != expr.end()) 95 | { 96 | if ((*it) == '\''){ 97 | expr.erase(it); 98 | it--; 99 | string inv_expr = getInvertedExpression((*it)); 100 | expr.erase(it); 101 | expr.insert(it, inv_expr.begin(), inv_expr.end()); 102 | it = expr.begin(); 103 | } else { 104 | it++; 105 | } 106 | } 107 | 108 | cout << expr << endl; 109 | 110 | it = expr.begin(); 111 | // inserts '*' where needed 112 | while (it != expr.end()){ 113 | if (isalpha(*it) || (*it) == ')'){ 114 | it++; 115 | if (isalpha(*it) || (*it) == '('){ 116 | expr.insert(it, '*'); 117 | it = expr.begin(); 118 | it++; 119 | } 120 | } else { 121 | it++; 122 | } 123 | } 124 | cout << expr << endl; 125 | } 126 | 127 | unsigned int evaluateExpression(map variables, std::string expr) 128 | { 129 | std::string parsedExpr = parseExpression(expr); 130 | cout << "parsed expr: " << parsedExpr << endl; 131 | double value = Lepton::Parser::parse(parsedExpr).evaluate(variables); 132 | if (value >= 1.0) 133 | return 1; 134 | return 0; 135 | } 136 | 137 | vector getOnesPosition(std::string expression){ 138 | map vars; 139 | vector _ones; 140 | int vc = countVariable(expression); 141 | vector grayCodes = generateGrayCodeSequence(vc); 142 | 143 | // 100 144 | for (string graycode: grayCodes){ 145 | for (int i = 0; i < graycode.size(); i++){ 146 | std::string value(1, graycode[i]); 147 | vars[literals[i]] = atoi(value.c_str()); 148 | } 149 | 150 | int exprValue = evaluateExpression(vars, expression); 151 | if (exprValue > 0){ 152 | _ones.push_back(strBinaryToDecimal(graycode)); 153 | } 154 | } 155 | 156 | for (int v : _ones){ 157 | cout << "val : " << v << endl; 158 | } 159 | 160 | return _ones; 161 | } 162 | 163 | #endif // EXPRESSIONHANDLER_H 164 | -------------------------------------------------------------------------------- /GUI/KMapSolver/include/lepton/ExpressionProgram.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_EXPRESSION_PROGRAM_H_ 2 | #define LEPTON_EXPRESSION_PROGRAM_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "ExpressionTreeNode.h" 36 | #include "windowsIncludes.h" 37 | #include 38 | #include 39 | #include 40 | 41 | namespace Lepton { 42 | 43 | class ParsedExpression; 44 | 45 | /** 46 | * An ExpressionProgram is a linear sequence of Operations for evaluating an expression. The evaluation 47 | * is done with a stack. The arguments to each Operation are first taken off the stack in order, then it is 48 | * evaluated and the result is pushed back onto the stack. At the end, the stack contains a single value, 49 | * which is the value of the expression. 50 | * 51 | * An ExpressionProgram is created by calling createProgram() on a ParsedExpression. 52 | */ 53 | 54 | class LEPTON_EXPORT ExpressionProgram { 55 | public: 56 | ExpressionProgram(); 57 | ExpressionProgram(const ExpressionProgram& program); 58 | ~ExpressionProgram(); 59 | ExpressionProgram& operator=(const ExpressionProgram& program); 60 | /** 61 | * Get the number of Operations that make up this program. 62 | */ 63 | int getNumOperations() const; 64 | /** 65 | * Get an Operation in this program. 66 | */ 67 | const Operation& getOperation(int index) const; 68 | /** 69 | * Get the size of the stack needed to execute this program. This is the largest number of elements present 70 | * on the stack at any point during evaluation. 71 | */ 72 | int getStackSize() const; 73 | /** 74 | * Evaluate the expression. If the expression involves any variables, this method will throw an exception. 75 | */ 76 | double evaluate() const; 77 | /** 78 | * Evaluate the expression. 79 | * 80 | * @param variables a map specifying the values of all variables that appear in the expression. If any 81 | * variable appears in the expression but is not included in this map, an exception 82 | * will be thrown. 83 | */ 84 | double evaluate(const std::map& variables) const; 85 | private: 86 | friend class ParsedExpression; 87 | ExpressionProgram(const ParsedExpression& expression); 88 | void buildProgram(const ExpressionTreeNode& node); 89 | std::vector operations; 90 | int maxArgs, stackSize; 91 | }; 92 | 93 | } // namespace Lepton 94 | 95 | #endif /*LEPTON_EXPRESSION_PROGRAM_H_*/ 96 | -------------------------------------------------------------------------------- /Console/KMapSolver/include/lepton/ExpressionProgram.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_EXPRESSION_PROGRAM_H_ 2 | #define LEPTON_EXPRESSION_PROGRAM_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "ExpressionTreeNode.h" 36 | #include "windowsIncludes.h" 37 | #include 38 | #include 39 | #include 40 | 41 | namespace Lepton { 42 | 43 | class ParsedExpression; 44 | 45 | /** 46 | * An ExpressionProgram is a linear sequence of Operations for evaluating an expression. The evaluation 47 | * is done with a stack. The arguments to each Operation are first taken off the stack in order, then it is 48 | * evaluated and the result is pushed back onto the stack. At the end, the stack contains a single value, 49 | * which is the value of the expression. 50 | * 51 | * An ExpressionProgram is created by calling createProgram() on a ParsedExpression. 52 | */ 53 | 54 | class LEPTON_EXPORT ExpressionProgram { 55 | public: 56 | ExpressionProgram(); 57 | ExpressionProgram(const ExpressionProgram& program); 58 | ~ExpressionProgram(); 59 | ExpressionProgram& operator=(const ExpressionProgram& program); 60 | /** 61 | * Get the number of Operations that make up this program. 62 | */ 63 | int getNumOperations() const; 64 | /** 65 | * Get an Operation in this program. 66 | */ 67 | const Operation& getOperation(int index) const; 68 | /** 69 | * Get the size of the stack needed to execute this program. This is the largest number of elements present 70 | * on the stack at any point during evaluation. 71 | */ 72 | int getStackSize() const; 73 | /** 74 | * Evaluate the expression. If the expression involves any variables, this method will throw an exception. 75 | */ 76 | double evaluate() const; 77 | /** 78 | * Evaluate the expression. 79 | * 80 | * @param variables a map specifying the values of all variables that appear in the expression. If any 81 | * variable appears in the expression but is not included in this map, an exception 82 | * will be thrown. 83 | */ 84 | double evaluate(const std::map& variables) const; 85 | private: 86 | friend class ParsedExpression; 87 | ExpressionProgram(const ParsedExpression& expression); 88 | void buildProgram(const ExpressionTreeNode& node); 89 | std::vector operations; 90 | int maxArgs, stackSize; 91 | }; 92 | 93 | } // namespace Lepton 94 | 95 | #endif /*LEPTON_EXPRESSION_PROGRAM_H_*/ 96 | -------------------------------------------------------------------------------- /GUI/KMapSolver/include/lepton/CompiledExpression.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_COMPILED_EXPRESSION_H_ 2 | #define LEPTON_COMPILED_EXPRESSION_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2013 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "ExpressionTreeNode.h" 36 | #include "windowsIncludes.h" 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | namespace Lepton { 43 | 44 | class Operation; 45 | class ParsedExpression; 46 | 47 | /** 48 | * A CompiledExpression is a highly optimized representation of an expression for cases when you want to evaluate 49 | * it many times as quickly as possible. You should treat it as an opaque object; none of the internal representation 50 | * is visible. 51 | * 52 | * A CompiledExpression is created by calling createCompiledExpression() on a ParsedExpression. 53 | * 54 | * WARNING: CompiledExpression is NOT thread safe. You should never access a CompiledExpression from two threads at 55 | * the same time. 56 | */ 57 | 58 | class LEPTON_EXPORT CompiledExpression { 59 | public: 60 | CompiledExpression(); 61 | CompiledExpression(const CompiledExpression& expression); 62 | ~CompiledExpression(); 63 | CompiledExpression& operator=(const CompiledExpression& expression); 64 | /** 65 | * Get the names of all variables used by this expression. 66 | */ 67 | const std::set& getVariables() const; 68 | /** 69 | * Get a reference to the memory location where the value of a particular variable is stored. This can be used 70 | * to set the value of the variable before calling evaluate(). 71 | */ 72 | double& getVariableReference(const std::string& name); 73 | /** 74 | * Evaluate the expression. The values of all variables should have been set before calling this. 75 | */ 76 | double evaluate() const; 77 | private: 78 | friend class ParsedExpression; 79 | CompiledExpression(const ParsedExpression& expression); 80 | void compileExpression(const ExpressionTreeNode& node, std::vector >& temps); 81 | int findTempIndex(const ExpressionTreeNode& node, std::vector >& temps); 82 | std::vector > arguments; 83 | std::vector target; 84 | std::vector operation; 85 | std::map variableIndices; 86 | std::set variableNames; 87 | mutable std::vector workspace; 88 | mutable std::vector argValues; 89 | std::map dummyVariables; 90 | }; 91 | 92 | } // namespace Lepton 93 | 94 | #endif /*LEPTON_COMPILED_EXPRESSION_H_*/ 95 | -------------------------------------------------------------------------------- /Console/KMapSolver/include/lepton/CompiledExpression.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_COMPILED_EXPRESSION_H_ 2 | #define LEPTON_COMPILED_EXPRESSION_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2013 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "ExpressionTreeNode.h" 36 | #include "windowsIncludes.h" 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | namespace Lepton { 43 | 44 | class Operation; 45 | class ParsedExpression; 46 | 47 | /** 48 | * A CompiledExpression is a highly optimized representation of an expression for cases when you want to evaluate 49 | * it many times as quickly as possible. You should treat it as an opaque object; none of the internal representation 50 | * is visible. 51 | * 52 | * A CompiledExpression is created by calling createCompiledExpression() on a ParsedExpression. 53 | * 54 | * WARNING: CompiledExpression is NOT thread safe. You should never access a CompiledExpression from two threads at 55 | * the same time. 56 | */ 57 | 58 | class LEPTON_EXPORT CompiledExpression { 59 | public: 60 | CompiledExpression(); 61 | CompiledExpression(const CompiledExpression& expression); 62 | ~CompiledExpression(); 63 | CompiledExpression& operator=(const CompiledExpression& expression); 64 | /** 65 | * Get the names of all variables used by this expression. 66 | */ 67 | const std::set& getVariables() const; 68 | /** 69 | * Get a reference to the memory location where the value of a particular variable is stored. This can be used 70 | * to set the value of the variable before calling evaluate(). 71 | */ 72 | double& getVariableReference(const std::string& name); 73 | /** 74 | * Evaluate the expression. The values of all variables should have been set before calling this. 75 | */ 76 | double evaluate() const; 77 | private: 78 | friend class ParsedExpression; 79 | CompiledExpression(const ParsedExpression& expression); 80 | void compileExpression(const ExpressionTreeNode& node, std::vector >& temps); 81 | int findTempIndex(const ExpressionTreeNode& node, std::vector >& temps); 82 | std::vector > arguments; 83 | std::vector target; 84 | std::vector operation; 85 | std::map variableIndices; 86 | std::set variableNames; 87 | mutable std::vector workspace; 88 | mutable std::vector argValues; 89 | std::map dummyVariables; 90 | }; 91 | 92 | } // namespace Lepton 93 | 94 | #endif /*LEPTON_COMPILED_EXPRESSION_H_*/ 95 | -------------------------------------------------------------------------------- /GUI/KMapSolver/src/ExpressionProgram.cpp: -------------------------------------------------------------------------------- 1 | /* -------------------------------------------------------------------------- * 2 | * Lepton * 3 | * -------------------------------------------------------------------------- * 4 | * This is part of the Lepton expression parser originating from * 5 | * Simbios, the NIH National Center for Physics-Based Simulation of * 6 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 7 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 8 | * * 9 | * Portions copyright (c) 2009-2013 Stanford University and the Authors. * 10 | * Authors: Peter Eastman * 11 | * Contributors: * 12 | * * 13 | * Permission is hereby granted, free of charge, to any person obtaining a * 14 | * copy of this software and associated documentation files (the "Software"), * 15 | * to deal in the Software without restriction, including without limitation * 16 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 17 | * and/or sell copies of the Software, and to permit persons to whom the * 18 | * Software is furnished to do so, subject to the following conditions: * 19 | * * 20 | * The above copyright notice and this permission notice shall be included in * 21 | * all copies or substantial portions of the Software. * 22 | * * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 26 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 27 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 28 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 29 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 30 | * -------------------------------------------------------------------------- */ 31 | 32 | #include "../include/lepton/ExpressionProgram.h" 33 | #include "../include/lepton/Operation.h" 34 | #include "../include/lepton/ParsedExpression.h" 35 | 36 | using namespace Lepton; 37 | using namespace std; 38 | 39 | ExpressionProgram::ExpressionProgram() : maxArgs(0), stackSize(0) { 40 | } 41 | 42 | ExpressionProgram::ExpressionProgram(const ParsedExpression& expression) : maxArgs(0), stackSize(0) { 43 | buildProgram(expression.getRootNode()); 44 | int currentStackSize = 0; 45 | for (int i = 0; i < (int) operations.size(); i++) { 46 | int args = operations[i]->getNumArguments(); 47 | if (args > maxArgs) 48 | maxArgs = args; 49 | currentStackSize += 1-args; 50 | if (currentStackSize > stackSize) 51 | stackSize = currentStackSize; 52 | } 53 | } 54 | 55 | ExpressionProgram::~ExpressionProgram() { 56 | for (int i = 0; i < (int) operations.size(); i++) 57 | delete operations[i]; 58 | } 59 | 60 | ExpressionProgram::ExpressionProgram(const ExpressionProgram& program) { 61 | *this = program; 62 | } 63 | 64 | ExpressionProgram& ExpressionProgram::operator=(const ExpressionProgram& program) { 65 | maxArgs = program.maxArgs; 66 | stackSize = program.stackSize; 67 | operations.resize(program.operations.size()); 68 | for (int i = 0; i < (int) operations.size(); i++) 69 | operations[i] = program.operations[i]->clone(); 70 | return *this; 71 | } 72 | 73 | void ExpressionProgram::buildProgram(const ExpressionTreeNode& node) { 74 | for (int i = (int) node.getChildren().size()-1; i >= 0; i--) 75 | buildProgram(node.getChildren()[i]); 76 | operations.push_back(node.getOperation().clone()); 77 | } 78 | 79 | int ExpressionProgram::getNumOperations() const { 80 | return (int) operations.size(); 81 | } 82 | 83 | const Operation& ExpressionProgram::getOperation(int index) const { 84 | return *operations[index]; 85 | } 86 | 87 | int ExpressionProgram::getStackSize() const { 88 | return stackSize; 89 | } 90 | 91 | double ExpressionProgram::evaluate() const { 92 | return evaluate(map()); 93 | } 94 | 95 | double ExpressionProgram::evaluate(const std::map& variables) const { 96 | vector stack(stackSize+1); 97 | int stackPointer = stackSize; 98 | for (int i = 0; i < (int) operations.size(); i++) { 99 | int numArgs = operations[i]->getNumArguments(); 100 | double result = operations[i]->evaluate(&stack[stackPointer], variables); 101 | stackPointer += numArgs-1; 102 | stack[stackPointer] = result; 103 | } 104 | return stack[stackSize-1]; 105 | } 106 | -------------------------------------------------------------------------------- /Console/KMapSolver/src/ExpressionProgram.cpp: -------------------------------------------------------------------------------- 1 | /* -------------------------------------------------------------------------- * 2 | * Lepton * 3 | * -------------------------------------------------------------------------- * 4 | * This is part of the Lepton expression parser originating from * 5 | * Simbios, the NIH National Center for Physics-Based Simulation of * 6 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 7 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 8 | * * 9 | * Portions copyright (c) 2009-2013 Stanford University and the Authors. * 10 | * Authors: Peter Eastman * 11 | * Contributors: * 12 | * * 13 | * Permission is hereby granted, free of charge, to any person obtaining a * 14 | * copy of this software and associated documentation files (the "Software"), * 15 | * to deal in the Software without restriction, including without limitation * 16 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 17 | * and/or sell copies of the Software, and to permit persons to whom the * 18 | * Software is furnished to do so, subject to the following conditions: * 19 | * * 20 | * The above copyright notice and this permission notice shall be included in * 21 | * all copies or substantial portions of the Software. * 22 | * * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 26 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 27 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 28 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 29 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 30 | * -------------------------------------------------------------------------- */ 31 | 32 | #include "../include/lepton/ExpressionProgram.h" 33 | #include "../include/lepton/Operation.h" 34 | #include "../include/lepton/ParsedExpression.h" 35 | 36 | using namespace Lepton; 37 | using namespace std; 38 | 39 | ExpressionProgram::ExpressionProgram() : maxArgs(0), stackSize(0) { 40 | } 41 | 42 | ExpressionProgram::ExpressionProgram(const ParsedExpression& expression) : maxArgs(0), stackSize(0) { 43 | buildProgram(expression.getRootNode()); 44 | int currentStackSize = 0; 45 | for (int i = 0; i < (int) operations.size(); i++) { 46 | int args = operations[i]->getNumArguments(); 47 | if (args > maxArgs) 48 | maxArgs = args; 49 | currentStackSize += 1-args; 50 | if (currentStackSize > stackSize) 51 | stackSize = currentStackSize; 52 | } 53 | } 54 | 55 | ExpressionProgram::~ExpressionProgram() { 56 | for (int i = 0; i < (int) operations.size(); i++) 57 | delete operations[i]; 58 | } 59 | 60 | ExpressionProgram::ExpressionProgram(const ExpressionProgram& program) { 61 | *this = program; 62 | } 63 | 64 | ExpressionProgram& ExpressionProgram::operator=(const ExpressionProgram& program) { 65 | maxArgs = program.maxArgs; 66 | stackSize = program.stackSize; 67 | operations.resize(program.operations.size()); 68 | for (int i = 0; i < (int) operations.size(); i++) 69 | operations[i] = program.operations[i]->clone(); 70 | return *this; 71 | } 72 | 73 | void ExpressionProgram::buildProgram(const ExpressionTreeNode& node) { 74 | for (int i = (int) node.getChildren().size()-1; i >= 0; i--) 75 | buildProgram(node.getChildren()[i]); 76 | operations.push_back(node.getOperation().clone()); 77 | } 78 | 79 | int ExpressionProgram::getNumOperations() const { 80 | return (int) operations.size(); 81 | } 82 | 83 | const Operation& ExpressionProgram::getOperation(int index) const { 84 | return *operations[index]; 85 | } 86 | 87 | int ExpressionProgram::getStackSize() const { 88 | return stackSize; 89 | } 90 | 91 | double ExpressionProgram::evaluate() const { 92 | return evaluate(map()); 93 | } 94 | 95 | double ExpressionProgram::evaluate(const std::map& variables) const { 96 | vector stack(stackSize+1); 97 | int stackPointer = stackSize; 98 | for (int i = 0; i < (int) operations.size(); i++) { 99 | int numArgs = operations[i]->getNumArguments(); 100 | double result = operations[i]->evaluate(&stack[stackPointer], variables); 101 | stackPointer += numArgs-1; 102 | stack[stackPointer] = result; 103 | } 104 | return stack[stackSize-1]; 105 | } 106 | -------------------------------------------------------------------------------- /GUI/KMapSolver/include/lepton/ExpressionTreeNode.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_EXPRESSION_TREE_NODE_H_ 2 | #define LEPTON_EXPRESSION_TREE_NODE_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "windowsIncludes.h" 36 | #include 37 | #include 38 | 39 | namespace Lepton { 40 | 41 | class Operation; 42 | 43 | /** 44 | * This class represents a node in the abstract syntax tree representation of an expression. 45 | * Each node is defined by an Operation and a set of children. When the expression is 46 | * evaluated, each child is first evaluated in order, then the resulting values are passed 47 | * as the arguments to the Operation's evaluate() method. 48 | */ 49 | 50 | class LEPTON_EXPORT ExpressionTreeNode { 51 | public: 52 | /** 53 | * Create a new ExpressionTreeNode. 54 | * 55 | * @param operation the operation for this node. The ExpressionTreeNode takes over ownership 56 | * of this object, and deletes it when the node is itself deleted. 57 | * @param children the children of this node 58 | */ 59 | ExpressionTreeNode(Operation* operation, const std::vector& children); 60 | /** 61 | * Create a new ExpressionTreeNode with two children. 62 | * 63 | * @param operation the operation for this node. The ExpressionTreeNode takes over ownership 64 | * of this object, and deletes it when the node is itself deleted. 65 | * @param child1 the first child of this node 66 | * @param child2 the second child of this node 67 | */ 68 | ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child1, const ExpressionTreeNode& child2); 69 | /** 70 | * Create a new ExpressionTreeNode with one child. 71 | * 72 | * @param operation the operation for this node. The ExpressionTreeNode takes over ownership 73 | * of this object, and deletes it when the node is itself deleted. 74 | * @param child the child of this node 75 | */ 76 | ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child); 77 | /** 78 | * Create a new ExpressionTreeNode with no children. 79 | * 80 | * @param operation the operation for this node. The ExpressionTreeNode takes over ownership 81 | * of this object, and deletes it when the node is itself deleted. 82 | */ 83 | ExpressionTreeNode(Operation* operation); 84 | ExpressionTreeNode(const ExpressionTreeNode& node); 85 | ExpressionTreeNode(); 86 | ~ExpressionTreeNode(); 87 | bool operator==(const ExpressionTreeNode& node) const; 88 | bool operator!=(const ExpressionTreeNode& node) const; 89 | ExpressionTreeNode& operator=(const ExpressionTreeNode& node); 90 | /** 91 | * Get the Operation performed by this node. 92 | */ 93 | const Operation& getOperation() const; 94 | /** 95 | * Get this node's child nodes. 96 | */ 97 | const std::vector& getChildren() const; 98 | private: 99 | Operation* operation; 100 | std::vector children; 101 | }; 102 | 103 | } // namespace Lepton 104 | 105 | #endif /*LEPTON_EXPRESSION_TREE_NODE_H_*/ 106 | -------------------------------------------------------------------------------- /Console/KMapSolver/include/lepton/ExpressionTreeNode.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_EXPRESSION_TREE_NODE_H_ 2 | #define LEPTON_EXPRESSION_TREE_NODE_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "windowsIncludes.h" 36 | #include 37 | #include 38 | 39 | namespace Lepton { 40 | 41 | class Operation; 42 | 43 | /** 44 | * This class represents a node in the abstract syntax tree representation of an expression. 45 | * Each node is defined by an Operation and a set of children. When the expression is 46 | * evaluated, each child is first evaluated in order, then the resulting values are passed 47 | * as the arguments to the Operation's evaluate() method. 48 | */ 49 | 50 | class LEPTON_EXPORT ExpressionTreeNode { 51 | public: 52 | /** 53 | * Create a new ExpressionTreeNode. 54 | * 55 | * @param operation the operation for this node. The ExpressionTreeNode takes over ownership 56 | * of this object, and deletes it when the node is itself deleted. 57 | * @param children the children of this node 58 | */ 59 | ExpressionTreeNode(Operation* operation, const std::vector& children); 60 | /** 61 | * Create a new ExpressionTreeNode with two children. 62 | * 63 | * @param operation the operation for this node. The ExpressionTreeNode takes over ownership 64 | * of this object, and deletes it when the node is itself deleted. 65 | * @param child1 the first child of this node 66 | * @param child2 the second child of this node 67 | */ 68 | ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child1, const ExpressionTreeNode& child2); 69 | /** 70 | * Create a new ExpressionTreeNode with one child. 71 | * 72 | * @param operation the operation for this node. The ExpressionTreeNode takes over ownership 73 | * of this object, and deletes it when the node is itself deleted. 74 | * @param child the child of this node 75 | */ 76 | ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child); 77 | /** 78 | * Create a new ExpressionTreeNode with no children. 79 | * 80 | * @param operation the operation for this node. The ExpressionTreeNode takes over ownership 81 | * of this object, and deletes it when the node is itself deleted. 82 | */ 83 | ExpressionTreeNode(Operation* operation); 84 | ExpressionTreeNode(const ExpressionTreeNode& node); 85 | ExpressionTreeNode(); 86 | ~ExpressionTreeNode(); 87 | bool operator==(const ExpressionTreeNode& node) const; 88 | bool operator!=(const ExpressionTreeNode& node) const; 89 | ExpressionTreeNode& operator=(const ExpressionTreeNode& node); 90 | /** 91 | * Get the Operation performed by this node. 92 | */ 93 | const Operation& getOperation() const; 94 | /** 95 | * Get this node's child nodes. 96 | */ 97 | const std::vector& getChildren() const; 98 | private: 99 | Operation* operation; 100 | std::vector children; 101 | }; 102 | 103 | } // namespace Lepton 104 | 105 | #endif /*LEPTON_EXPRESSION_TREE_NODE_H_*/ 106 | -------------------------------------------------------------------------------- /GUI/KMapSolver/src/ExpressionTreeNode.cpp: -------------------------------------------------------------------------------- 1 | /* -------------------------------------------------------------------------- * 2 | * Lepton * 3 | * -------------------------------------------------------------------------- * 4 | * This is part of the Lepton expression parser originating from * 5 | * Simbios, the NIH National Center for Physics-Based Simulation of * 6 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 7 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 8 | * * 9 | * Portions copyright (c) 2009 Stanford University and the Authors. * 10 | * Authors: Peter Eastman * 11 | * Contributors: * 12 | * * 13 | * Permission is hereby granted, free of charge, to any person obtaining a * 14 | * copy of this software and associated documentation files (the "Software"), * 15 | * to deal in the Software without restriction, including without limitation * 16 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 17 | * and/or sell copies of the Software, and to permit persons to whom the * 18 | * Software is furnished to do so, subject to the following conditions: * 19 | * * 20 | * The above copyright notice and this permission notice shall be included in * 21 | * all copies or substantial portions of the Software. * 22 | * * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 26 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 27 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 28 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 29 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 30 | * -------------------------------------------------------------------------- */ 31 | 32 | #include "../include/lepton/ExpressionTreeNode.h" 33 | #include "../include/lepton/Exception.h" 34 | #include "../include/lepton/Operation.h" 35 | 36 | using namespace Lepton; 37 | using namespace std; 38 | 39 | ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const vector& children) : operation(operation), children(children) { 40 | if (operation->getNumArguments() != children.size()) 41 | throw Exception("Parse error: wrong number of arguments to function: "+operation->getName()); 42 | } 43 | 44 | ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child1, const ExpressionTreeNode& child2) : operation(operation) { 45 | children.push_back(child1); 46 | children.push_back(child2); 47 | if (operation->getNumArguments() != children.size()) 48 | throw Exception("Parse error: wrong number of arguments to function: "+operation->getName()); 49 | } 50 | 51 | ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child) : operation(operation) { 52 | children.push_back(child); 53 | if (operation->getNumArguments() != children.size()) 54 | throw Exception("Parse error: wrong number of arguments to function: "+operation->getName()); 55 | } 56 | 57 | ExpressionTreeNode::ExpressionTreeNode(Operation* operation) : operation(operation) { 58 | if (operation->getNumArguments() != children.size()) 59 | throw Exception("Parse error: wrong number of arguments to function: "+operation->getName()); 60 | } 61 | 62 | ExpressionTreeNode::ExpressionTreeNode(const ExpressionTreeNode& node) : operation(&node.getOperation() == NULL ? NULL : node.getOperation().clone()), children(node.getChildren()) { 63 | } 64 | 65 | ExpressionTreeNode::ExpressionTreeNode() : operation(NULL) { 66 | } 67 | 68 | ExpressionTreeNode::~ExpressionTreeNode() { 69 | if (operation != NULL) 70 | delete operation; 71 | } 72 | 73 | bool ExpressionTreeNode::operator!=(const ExpressionTreeNode& node) const { 74 | if (node.getOperation() != getOperation()) 75 | return true; 76 | if (getOperation().isSymmetric() && getChildren().size() == 2) { 77 | if (getChildren()[0] == node.getChildren()[0] && getChildren()[1] == node.getChildren()[1]) 78 | return false; 79 | if (getChildren()[0] == node.getChildren()[1] && getChildren()[1] == node.getChildren()[0]) 80 | return false; 81 | return true; 82 | } 83 | for (int i = 0; i < (int) getChildren().size(); i++) 84 | if (getChildren()[i] != node.getChildren()[i]) 85 | return true; 86 | return false; 87 | } 88 | 89 | bool ExpressionTreeNode::operator==(const ExpressionTreeNode& node) const { 90 | return !(*this != node); 91 | } 92 | 93 | ExpressionTreeNode& ExpressionTreeNode::operator=(const ExpressionTreeNode& node) { 94 | if (operation != NULL) 95 | delete operation; 96 | operation = node.getOperation().clone(); 97 | children = node.getChildren(); 98 | return *this; 99 | } 100 | 101 | const Operation& ExpressionTreeNode::getOperation() const { 102 | return *operation; 103 | } 104 | 105 | const vector& ExpressionTreeNode::getChildren() const { 106 | return children; 107 | } 108 | -------------------------------------------------------------------------------- /Console/KMapSolver/src/ExpressionTreeNode.cpp: -------------------------------------------------------------------------------- 1 | /* -------------------------------------------------------------------------- * 2 | * Lepton * 3 | * -------------------------------------------------------------------------- * 4 | * This is part of the Lepton expression parser originating from * 5 | * Simbios, the NIH National Center for Physics-Based Simulation of * 6 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 7 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 8 | * * 9 | * Portions copyright (c) 2009 Stanford University and the Authors. * 10 | * Authors: Peter Eastman * 11 | * Contributors: * 12 | * * 13 | * Permission is hereby granted, free of charge, to any person obtaining a * 14 | * copy of this software and associated documentation files (the "Software"), * 15 | * to deal in the Software without restriction, including without limitation * 16 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 17 | * and/or sell copies of the Software, and to permit persons to whom the * 18 | * Software is furnished to do so, subject to the following conditions: * 19 | * * 20 | * The above copyright notice and this permission notice shall be included in * 21 | * all copies or substantial portions of the Software. * 22 | * * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 26 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 27 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 28 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 29 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 30 | * -------------------------------------------------------------------------- */ 31 | 32 | #include "../include/lepton/ExpressionTreeNode.h" 33 | #include "../include/lepton/Exception.h" 34 | #include "../include/lepton/Operation.h" 35 | 36 | using namespace Lepton; 37 | using namespace std; 38 | 39 | ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const vector& children) : operation(operation), children(children) { 40 | if (operation->getNumArguments() != children.size()) 41 | throw Exception("Parse error: wrong number of arguments to function: "+operation->getName()); 42 | } 43 | 44 | ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child1, const ExpressionTreeNode& child2) : operation(operation) { 45 | children.push_back(child1); 46 | children.push_back(child2); 47 | if (operation->getNumArguments() != children.size()) 48 | throw Exception("Parse error: wrong number of arguments to function: "+operation->getName()); 49 | } 50 | 51 | ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child) : operation(operation) { 52 | children.push_back(child); 53 | if (operation->getNumArguments() != children.size()) 54 | throw Exception("Parse error: wrong number of arguments to function: "+operation->getName()); 55 | } 56 | 57 | ExpressionTreeNode::ExpressionTreeNode(Operation* operation) : operation(operation) { 58 | if (operation->getNumArguments() != children.size()) 59 | throw Exception("Parse error: wrong number of arguments to function: "+operation->getName()); 60 | } 61 | 62 | ExpressionTreeNode::ExpressionTreeNode(const ExpressionTreeNode& node) : operation(&node.getOperation() == NULL ? NULL : node.getOperation().clone()), children(node.getChildren()) { 63 | } 64 | 65 | ExpressionTreeNode::ExpressionTreeNode() : operation(NULL) { 66 | } 67 | 68 | ExpressionTreeNode::~ExpressionTreeNode() { 69 | if (operation != NULL) 70 | delete operation; 71 | } 72 | 73 | bool ExpressionTreeNode::operator!=(const ExpressionTreeNode& node) const { 74 | if (node.getOperation() != getOperation()) 75 | return true; 76 | if (getOperation().isSymmetric() && getChildren().size() == 2) { 77 | if (getChildren()[0] == node.getChildren()[0] && getChildren()[1] == node.getChildren()[1]) 78 | return false; 79 | if (getChildren()[0] == node.getChildren()[1] && getChildren()[1] == node.getChildren()[0]) 80 | return false; 81 | return true; 82 | } 83 | for (int i = 0; i < (int) getChildren().size(); i++) 84 | if (getChildren()[i] != node.getChildren()[i]) 85 | return true; 86 | return false; 87 | } 88 | 89 | bool ExpressionTreeNode::operator==(const ExpressionTreeNode& node) const { 90 | return !(*this != node); 91 | } 92 | 93 | ExpressionTreeNode& ExpressionTreeNode::operator=(const ExpressionTreeNode& node) { 94 | if (operation != NULL) 95 | delete operation; 96 | operation = node.getOperation().clone(); 97 | children = node.getChildren(); 98 | return *this; 99 | } 100 | 101 | const Operation& ExpressionTreeNode::getOperation() const { 102 | return *operation; 103 | } 104 | 105 | const vector& ExpressionTreeNode::getChildren() const { 106 | return children; 107 | } 108 | -------------------------------------------------------------------------------- /GUI/KMapSolver/include/lepton/ParsedExpression.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_PARSED_EXPRESSION_H_ 2 | #define LEPTON_PARSED_EXPRESSION_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009=2013 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "ExpressionTreeNode.h" 36 | #include "windowsIncludes.h" 37 | #include 38 | #include 39 | 40 | namespace Lepton { 41 | 42 | class CompiledExpression; 43 | class ExpressionProgram; 44 | 45 | /** 46 | * This class represents the result of parsing an expression. It provides methods for working with the 47 | * expression in various ways, such as evaluating it, getting the tree representation of the expresson, etc. 48 | */ 49 | 50 | class LEPTON_EXPORT ParsedExpression { 51 | public: 52 | /** 53 | * Create an uninitialized ParsedExpression. This exists so that ParsedExpressions can be put in STL containers. 54 | * Doing anything with it will produce an exception. 55 | */ 56 | ParsedExpression(); 57 | /** 58 | * Create a ParsedExpression. Normally you will not call this directly. Instead, use the Parser class 59 | * to parse expression. 60 | */ 61 | ParsedExpression(const ExpressionTreeNode& rootNode); 62 | /** 63 | * Get the root node of the expression's abstract syntax tree. 64 | */ 65 | const ExpressionTreeNode& getRootNode() const; 66 | /** 67 | * Evaluate the expression. If the expression involves any variables, this method will throw an exception. 68 | */ 69 | double evaluate() const; 70 | /** 71 | * Evaluate the expression. 72 | * 73 | * @param variables a map specifying the values of all variables that appear in the expression. If any 74 | * variable appears in the expression but is not included in this map, an exception 75 | * will be thrown. 76 | */ 77 | double evaluate(const std::map& variables) const; 78 | /** 79 | * Create a new ParsedExpression which produces the same result as this one, but is faster to evaluate. 80 | */ 81 | ParsedExpression optimize() const; 82 | /** 83 | * Create a new ParsedExpression which produces the same result as this one, but is faster to evaluate. 84 | * 85 | * @param variables a map specifying values for a subset of variables that appear in the expression. 86 | * All occurrences of these variables in the expression are replaced with the values 87 | * specified. 88 | */ 89 | ParsedExpression optimize(const std::map& variables) const; 90 | /** 91 | * Create a new ParsedExpression which is the analytic derivative of this expression with respect to a 92 | * particular variable. 93 | * 94 | * @param variable the variable with respect to which the derivate should be taken 95 | */ 96 | ParsedExpression differentiate(const std::string& variable) const; 97 | /** 98 | * Create an ExpressionProgram that represents the same calculation as this expression. 99 | */ 100 | ExpressionProgram createProgram() const; 101 | /** 102 | * Create a CompiledExpression that represents the same calculation as this expression. 103 | */ 104 | CompiledExpression createCompiledExpression() const; 105 | /** 106 | * Create a new ParsedExpression which is identical to this one, except that the names of some 107 | * variables have been changed. 108 | * 109 | * @param replacements a map whose keys are the names of variables, and whose values are the 110 | * new names to replace them with 111 | */ 112 | ParsedExpression renameVariables(const std::map& replacements) const; 113 | private: 114 | static double evaluate(const ExpressionTreeNode& node, const std::map& variables); 115 | static ExpressionTreeNode preevaluateVariables(const ExpressionTreeNode& node, const std::map& variables); 116 | static ExpressionTreeNode precalculateConstantSubexpressions(const ExpressionTreeNode& node); 117 | static ExpressionTreeNode substituteSimplerExpression(const ExpressionTreeNode& node); 118 | static ExpressionTreeNode differentiate(const ExpressionTreeNode& node, const std::string& variable); 119 | static double getConstantValue(const ExpressionTreeNode& node); 120 | static ExpressionTreeNode renameNodeVariables(const ExpressionTreeNode& node, const std::map& replacements); 121 | ExpressionTreeNode rootNode; 122 | }; 123 | 124 | LEPTON_EXPORT std::ostream& operator<<(std::ostream& out, const ExpressionTreeNode& node); 125 | 126 | LEPTON_EXPORT std::ostream& operator<<(std::ostream& out, const ParsedExpression& exp); 127 | 128 | } // namespace Lepton 129 | 130 | #endif /*LEPTON_PARSED_EXPRESSION_H_*/ 131 | -------------------------------------------------------------------------------- /Console/KMapSolver/include/lepton/ParsedExpression.h: -------------------------------------------------------------------------------- 1 | #ifndef LEPTON_PARSED_EXPRESSION_H_ 2 | #define LEPTON_PARSED_EXPRESSION_H_ 3 | 4 | /* -------------------------------------------------------------------------- * 5 | * Lepton * 6 | * -------------------------------------------------------------------------- * 7 | * This is part of the Lepton expression parser originating from * 8 | * Simbios, the NIH National Center for Physics-Based Simulation of * 9 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 10 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 11 | * * 12 | * Portions copyright (c) 2009=2013 Stanford University and the Authors. * 13 | * Authors: Peter Eastman * 14 | * Contributors: * 15 | * * 16 | * Permission is hereby granted, free of charge, to any person obtaining a * 17 | * copy of this software and associated documentation files (the "Software"), * 18 | * to deal in the Software without restriction, including without limitation * 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 20 | * and/or sell copies of the Software, and to permit persons to whom the * 21 | * Software is furnished to do so, subject to the following conditions: * 22 | * * 23 | * The above copyright notice and this permission notice shall be included in * 24 | * all copies or substantial portions of the Software. * 25 | * * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 29 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 30 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 31 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 32 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 33 | * -------------------------------------------------------------------------- */ 34 | 35 | #include "ExpressionTreeNode.h" 36 | #include "windowsIncludes.h" 37 | #include 38 | #include 39 | 40 | namespace Lepton { 41 | 42 | class CompiledExpression; 43 | class ExpressionProgram; 44 | 45 | /** 46 | * This class represents the result of parsing an expression. It provides methods for working with the 47 | * expression in various ways, such as evaluating it, getting the tree representation of the expresson, etc. 48 | */ 49 | 50 | class LEPTON_EXPORT ParsedExpression { 51 | public: 52 | /** 53 | * Create an uninitialized ParsedExpression. This exists so that ParsedExpressions can be put in STL containers. 54 | * Doing anything with it will produce an exception. 55 | */ 56 | ParsedExpression(); 57 | /** 58 | * Create a ParsedExpression. Normally you will not call this directly. Instead, use the Parser class 59 | * to parse expression. 60 | */ 61 | ParsedExpression(const ExpressionTreeNode& rootNode); 62 | /** 63 | * Get the root node of the expression's abstract syntax tree. 64 | */ 65 | const ExpressionTreeNode& getRootNode() const; 66 | /** 67 | * Evaluate the expression. If the expression involves any variables, this method will throw an exception. 68 | */ 69 | double evaluate() const; 70 | /** 71 | * Evaluate the expression. 72 | * 73 | * @param variables a map specifying the values of all variables that appear in the expression. If any 74 | * variable appears in the expression but is not included in this map, an exception 75 | * will be thrown. 76 | */ 77 | double evaluate(const std::map& variables) const; 78 | /** 79 | * Create a new ParsedExpression which produces the same result as this one, but is faster to evaluate. 80 | */ 81 | ParsedExpression optimize() const; 82 | /** 83 | * Create a new ParsedExpression which produces the same result as this one, but is faster to evaluate. 84 | * 85 | * @param variables a map specifying values for a subset of variables that appear in the expression. 86 | * All occurrences of these variables in the expression are replaced with the values 87 | * specified. 88 | */ 89 | ParsedExpression optimize(const std::map& variables) const; 90 | /** 91 | * Create a new ParsedExpression which is the analytic derivative of this expression with respect to a 92 | * particular variable. 93 | * 94 | * @param variable the variable with respect to which the derivate should be taken 95 | */ 96 | ParsedExpression differentiate(const std::string& variable) const; 97 | /** 98 | * Create an ExpressionProgram that represents the same calculation as this expression. 99 | */ 100 | ExpressionProgram createProgram() const; 101 | /** 102 | * Create a CompiledExpression that represents the same calculation as this expression. 103 | */ 104 | CompiledExpression createCompiledExpression() const; 105 | /** 106 | * Create a new ParsedExpression which is identical to this one, except that the names of some 107 | * variables have been changed. 108 | * 109 | * @param replacements a map whose keys are the names of variables, and whose values are the 110 | * new names to replace them with 111 | */ 112 | ParsedExpression renameVariables(const std::map& replacements) const; 113 | private: 114 | static double evaluate(const ExpressionTreeNode& node, const std::map& variables); 115 | static ExpressionTreeNode preevaluateVariables(const ExpressionTreeNode& node, const std::map& variables); 116 | static ExpressionTreeNode precalculateConstantSubexpressions(const ExpressionTreeNode& node); 117 | static ExpressionTreeNode substituteSimplerExpression(const ExpressionTreeNode& node); 118 | static ExpressionTreeNode differentiate(const ExpressionTreeNode& node, const std::string& variable); 119 | static double getConstantValue(const ExpressionTreeNode& node); 120 | static ExpressionTreeNode renameNodeVariables(const ExpressionTreeNode& node, const std::map& replacements); 121 | ExpressionTreeNode rootNode; 122 | }; 123 | 124 | LEPTON_EXPORT std::ostream& operator<<(std::ostream& out, const ExpressionTreeNode& node); 125 | 126 | LEPTON_EXPORT std::ostream& operator<<(std::ostream& out, const ParsedExpression& exp); 127 | 128 | } // namespace Lepton 129 | 130 | #endif /*LEPTON_PARSED_EXPRESSION_H_*/ 131 | -------------------------------------------------------------------------------- /GUI/KMapSolver/src/CompiledExpression.cpp: -------------------------------------------------------------------------------- 1 | /* -------------------------------------------------------------------------- * 2 | * Lepton * 3 | * -------------------------------------------------------------------------- * 4 | * This is part of the Lepton expression parser originating from * 5 | * Simbios, the NIH National Center for Physics-Based Simulation of * 6 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 7 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 8 | * * 9 | * Portions copyright (c) 2013 Stanford University and the Authors. * 10 | * Authors: Peter Eastman * 11 | * Contributors: * 12 | * * 13 | * Permission is hereby granted, free of charge, to any person obtaining a * 14 | * copy of this software and associated documentation files (the "Software"), * 15 | * to deal in the Software without restriction, including without limitation * 16 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 17 | * and/or sell copies of the Software, and to permit persons to whom the * 18 | * Software is furnished to do so, subject to the following conditions: * 19 | * * 20 | * The above copyright notice and this permission notice shall be included in * 21 | * all copies or substantial portions of the Software. * 22 | * * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 26 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 27 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 28 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 29 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 30 | * -------------------------------------------------------------------------- */ 31 | 32 | #include "../include/lepton/CompiledExpression.h" 33 | #include "../include/lepton/Operation.h" 34 | #include "../include/lepton/ParsedExpression.h" 35 | #include 36 | 37 | using namespace Lepton; 38 | using namespace std; 39 | 40 | CompiledExpression::CompiledExpression() { 41 | } 42 | 43 | CompiledExpression::CompiledExpression(const ParsedExpression& expression) { 44 | ParsedExpression expr = expression.optimize(); // Just in case it wasn't already optimized. 45 | vector > temps; 46 | compileExpression(expr.getRootNode(), temps); 47 | } 48 | 49 | CompiledExpression::~CompiledExpression() { 50 | for (int i = 0; i < (int) operation.size(); i++) 51 | if (operation[i] != NULL) 52 | delete operation[i]; 53 | } 54 | 55 | CompiledExpression::CompiledExpression(const CompiledExpression& expression) { 56 | *this = expression; 57 | } 58 | 59 | CompiledExpression& CompiledExpression::operator=(const CompiledExpression& expression) { 60 | arguments = expression.arguments; 61 | target = expression.target; 62 | variableIndices = expression.variableIndices; 63 | variableNames = expression.variableNames; 64 | workspace.resize(expression.workspace.size()); 65 | argValues.resize(expression.argValues.size()); 66 | operation.resize(expression.operation.size()); 67 | for (int i = 0; i < (int) operation.size(); i++) 68 | operation[i] = expression.operation[i]->clone(); 69 | return *this; 70 | } 71 | 72 | void CompiledExpression::compileExpression(const ExpressionTreeNode& node, vector >& temps) { 73 | if (findTempIndex(node, temps) != -1) 74 | return; // We have already processed a node identical to this one. 75 | 76 | // Process the child nodes. 77 | 78 | vector args; 79 | for (int i = 0; i < node.getChildren().size(); i++) { 80 | compileExpression(node.getChildren()[i], temps); 81 | args.push_back(findTempIndex(node.getChildren()[i], temps)); 82 | } 83 | 84 | // Process this node. 85 | 86 | if (node.getOperation().getId() == Operation::VARIABLE) { 87 | variableIndices[node.getOperation().getName()] = (int) workspace.size(); 88 | variableNames.insert(node.getOperation().getName()); 89 | } 90 | else { 91 | int stepIndex = (int) arguments.size(); 92 | arguments.push_back(vector()); 93 | target.push_back((int) workspace.size()); 94 | operation.push_back(node.getOperation().clone()); 95 | if (args.size() == 0) 96 | arguments[stepIndex].push_back(0); // The value won't actually be used. We just need something there. 97 | else { 98 | // If the arguments are sequential, we can just pass a pointer to the first one. 99 | 100 | bool sequential = true; 101 | for (int i = 1; i < args.size(); i++) 102 | if (args[i] != args[i-1]+1) 103 | sequential = false; 104 | if (sequential) 105 | arguments[stepIndex].push_back(args[0]); 106 | else { 107 | arguments[stepIndex] = args; 108 | if (args.size() > argValues.size()) 109 | argValues.resize(args.size(), 0.0); 110 | } 111 | } 112 | } 113 | temps.push_back(make_pair(node, workspace.size())); 114 | workspace.push_back(0.0); 115 | } 116 | 117 | int CompiledExpression::findTempIndex(const ExpressionTreeNode& node, vector >& temps) { 118 | for (int i = 0; i < (int) temps.size(); i++) 119 | if (temps[i].first == node) 120 | return i; 121 | return -1; 122 | } 123 | 124 | const set& CompiledExpression::getVariables() const { 125 | return variableNames; 126 | } 127 | 128 | double& CompiledExpression::getVariableReference(const string& name) { 129 | map::iterator index = variableIndices.find(name); 130 | if (index == variableIndices.end()) 131 | throw Exception("getVariableReference: Unknown variable '"+name+"'"); 132 | return workspace[index->second]; 133 | } 134 | 135 | double CompiledExpression::evaluate() const { 136 | // Loop over the operations and evaluate each one. 137 | 138 | for (int step = 0; step < operation.size(); step++) { 139 | const vector& args = arguments[step]; 140 | if (args.size() == 1) 141 | workspace[target[step]] = operation[step]->evaluate(&workspace[args[0]], dummyVariables); 142 | else { 143 | for (int i = 0; i < args.size(); i++) 144 | argValues[i] = workspace[args[i]]; 145 | workspace[target[step]] = operation[step]->evaluate(&argValues[0], dummyVariables); 146 | } 147 | } 148 | return workspace[workspace.size()-1]; 149 | } 150 | -------------------------------------------------------------------------------- /Console/KMapSolver/src/CompiledExpression.cpp: -------------------------------------------------------------------------------- 1 | /* -------------------------------------------------------------------------- * 2 | * Lepton * 3 | * -------------------------------------------------------------------------- * 4 | * This is part of the Lepton expression parser originating from * 5 | * Simbios, the NIH National Center for Physics-Based Simulation of * 6 | * Biological Structures at Stanford, funded under the NIH Roadmap for * 7 | * Medical Research, grant U54 GM072970. See https://simtk.org. * 8 | * * 9 | * Portions copyright (c) 2013 Stanford University and the Authors. * 10 | * Authors: Peter Eastman * 11 | * Contributors: * 12 | * * 13 | * Permission is hereby granted, free of charge, to any person obtaining a * 14 | * copy of this software and associated documentation files (the "Software"), * 15 | * to deal in the Software without restriction, including without limitation * 16 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 17 | * and/or sell copies of the Software, and to permit persons to whom the * 18 | * Software is furnished to do so, subject to the following conditions: * 19 | * * 20 | * The above copyright notice and this permission notice shall be included in * 21 | * all copies or substantial portions of the Software. * 22 | * * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 26 | * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 27 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 28 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 29 | * USE OR OTHER DEALINGS IN THE SOFTWARE. * 30 | * -------------------------------------------------------------------------- */ 31 | 32 | #include "../include/lepton/CompiledExpression.h" 33 | #include "../include/lepton/Operation.h" 34 | #include "../include/lepton/ParsedExpression.h" 35 | #include 36 | 37 | using namespace Lepton; 38 | using namespace std; 39 | 40 | CompiledExpression::CompiledExpression() { 41 | } 42 | 43 | CompiledExpression::CompiledExpression(const ParsedExpression& expression) { 44 | ParsedExpression expr = expression.optimize(); // Just in case it wasn't already optimized. 45 | vector > temps; 46 | compileExpression(expr.getRootNode(), temps); 47 | } 48 | 49 | CompiledExpression::~CompiledExpression() { 50 | for (int i = 0; i < (int) operation.size(); i++) 51 | if (operation[i] != NULL) 52 | delete operation[i]; 53 | } 54 | 55 | CompiledExpression::CompiledExpression(const CompiledExpression& expression) { 56 | *this = expression; 57 | } 58 | 59 | CompiledExpression& CompiledExpression::operator=(const CompiledExpression& expression) { 60 | arguments = expression.arguments; 61 | target = expression.target; 62 | variableIndices = expression.variableIndices; 63 | variableNames = expression.variableNames; 64 | workspace.resize(expression.workspace.size()); 65 | argValues.resize(expression.argValues.size()); 66 | operation.resize(expression.operation.size()); 67 | for (int i = 0; i < (int) operation.size(); i++) 68 | operation[i] = expression.operation[i]->clone(); 69 | return *this; 70 | } 71 | 72 | void CompiledExpression::compileExpression(const ExpressionTreeNode& node, vector >& temps) { 73 | if (findTempIndex(node, temps) != -1) 74 | return; // We have already processed a node identical to this one. 75 | 76 | // Process the child nodes. 77 | 78 | vector args; 79 | for (int i = 0; i < node.getChildren().size(); i++) { 80 | compileExpression(node.getChildren()[i], temps); 81 | args.push_back(findTempIndex(node.getChildren()[i], temps)); 82 | } 83 | 84 | // Process this node. 85 | 86 | if (node.getOperation().getId() == Operation::VARIABLE) { 87 | variableIndices[node.getOperation().getName()] = (int) workspace.size(); 88 | variableNames.insert(node.getOperation().getName()); 89 | } 90 | else { 91 | int stepIndex = (int) arguments.size(); 92 | arguments.push_back(vector()); 93 | target.push_back((int) workspace.size()); 94 | operation.push_back(node.getOperation().clone()); 95 | if (args.size() == 0) 96 | arguments[stepIndex].push_back(0); // The value won't actually be used. We just need something there. 97 | else { 98 | // If the arguments are sequential, we can just pass a pointer to the first one. 99 | 100 | bool sequential = true; 101 | for (int i = 1; i < args.size(); i++) 102 | if (args[i] != args[i-1]+1) 103 | sequential = false; 104 | if (sequential) 105 | arguments[stepIndex].push_back(args[0]); 106 | else { 107 | arguments[stepIndex] = args; 108 | if (args.size() > argValues.size()) 109 | argValues.resize(args.size(), 0.0); 110 | } 111 | } 112 | } 113 | temps.push_back(make_pair(node, workspace.size())); 114 | workspace.push_back(0.0); 115 | } 116 | 117 | int CompiledExpression::findTempIndex(const ExpressionTreeNode& node, vector >& temps) { 118 | for (int i = 0; i < (int) temps.size(); i++) 119 | if (temps[i].first == node) 120 | return i; 121 | return -1; 122 | } 123 | 124 | const set& CompiledExpression::getVariables() const { 125 | return variableNames; 126 | } 127 | 128 | double& CompiledExpression::getVariableReference(const string& name) { 129 | map::iterator index = variableIndices.find(name); 130 | if (index == variableIndices.end()) 131 | throw Exception("getVariableReference: Unknown variable '"+name+"'"); 132 | return workspace[index->second]; 133 | } 134 | 135 | double CompiledExpression::evaluate() const { 136 | // Loop over the operations and evaluate each one. 137 | 138 | for (int step = 0; step < operation.size(); step++) { 139 | const vector& args = arguments[step]; 140 | if (args.size() == 1) 141 | workspace[target[step]] = operation[step]->evaluate(&workspace[args[0]], dummyVariables); 142 | else { 143 | for (int i = 0; i < args.size(); i++) 144 | argValues[i] = workspace[args[i]]; 145 | workspace[target[step]] = operation[step]->evaluate(&argValues[0], dummyVariables); 146 | } 147 | } 148 | return workspace[workspace.size()-1]; 149 | } 150 | -------------------------------------------------------------------------------- /GUI/KMapSolver/filterkmapresults.h: -------------------------------------------------------------------------------- 1 | #ifndef FILTERKMAPRESULTS 2 | #define FILTERKMAPRESULTS 3 | 4 | #include "operation.h" 5 | 6 | //this class is using for filtering results and giving 7 | //all possibilites results 8 | class FilterKmapTerms : public CompareKmapTerms, public ConverteTerms, public Combination 9 | { 10 | protected: 11 | 12 | int temp, temp1, temp2, temp3; //temprature variables 13 | 14 | vector getTerm (vector &result, int pos); //getting a term form an array 15 | int getMintermCount(vector &term); 16 | int getLargestTermSize(vector &result); 17 | vector > getFilterResult(vector> &results, vector &essentialTerms); 18 | bool checkResult (vector someResult, vector ones ); 19 | vector > filter (vector &result, vector ones); 20 | 21 | };//end filterKmapTerms 22 | 23 | /*** 24 | The main function of the program that implement K-map by: 25 | 1- prompting for k-map type by its variable's number 26 | 2- prompting for one's position by getPos function 27 | 3- prompting for don't care position by getPos function 28 | 4- Minimizing and getting results 29 | */ 30 | 31 | 32 | 33 | //get a term from a vector of terms (result) 34 | vector FilterKmapTerms :: getTerm (vector &result, int pos) 35 | { 36 | int temp; 37 | vector term; 38 | for(temp = pos; temp < result.size(); temp++) 39 | { 40 | if(result[temp] != '+') 41 | term.push_back(result[temp]); 42 | else 43 | break; 44 | } 45 | 46 | return term; 47 | }//end getTerm 48 | 49 | 50 | //Determine the larget size of minterms 51 | int FilterKmapTerms :: getLargestTermSize(vector &result) 52 | { 53 | int largestSize = 0; 54 | vector term; 55 | 56 | for(int temp = 0; temp largestSize) 61 | largestSize = getMintermCount(term); 62 | 63 | }//end for 64 | 65 | return largestSize; 66 | }//end get Largest Term size 67 | 68 | //return a term's minterms count 69 | int FilterKmapTerms :: getMintermCount(vector &term) 70 | { 71 | int count = 0; //for return 72 | 73 | for(int temp = 0; temp < term.size(); temp++) 74 | { 75 | if(isalpha(term[temp]) ) //search for alphabets 76 | count++; 77 | } 78 | return count; 79 | }//end get minterms count 80 | 81 | vector > FilterKmapTerms :: filter (vector &result, vector ones) 82 | { 83 | 84 | vector term, //reading all term alone 85 | essentialTerms; //result after filtering 86 | vector> filterResult; 87 | int LargestTermSize; 88 | vector>terms; 89 | 90 | 91 | //setting essentials 92 | LargestTermSize = getLargestTermSize(result); 93 | 94 | for(int temp = 0 ; temp < result.size(); temp ++) 95 | { 96 | term = getTerm(result, temp); 97 | 98 | //term is essential 99 | if(LargestTermSize > getMintermCount(term) ) 100 | { 101 | //add + for more than one term result 102 | if(essentialTerms.size() > 0 ) 103 | { 104 | essentialTerms.push_back('+'); 105 | } 106 | for(int temp1 = 0 ; temp1 < term.size(); temp1 ++) 107 | { 108 | essentialTerms.push_back(term[temp1]); //add term to filter result 109 | result.erase(result.begin()+temp); //remove term form main result 110 | } 111 | 112 | if(result.size() > 0)//erase + for more than one term 113 | result.erase(result.begin()+temp); 114 | 115 | resultTerms--; //decrement result terms 116 | temp--; 117 | }//end if of essential terms 118 | else 119 | temp += term.size(); 120 | }//end result covering loop 121 | 122 | terms.push_back(vector () ); 123 | for(int temp = 0; temp < result.size(); temp++) 124 | { 125 | if(result[temp] == '+' && temp < result.size()) 126 | terms.push_back(vector () ); 127 | else 128 | terms[terms.size()-1].push_back(result[temp]); 129 | 130 | } 131 | 132 | filterResult = getFilterResult(terms, essentialTerms); 133 | 134 | return filterResult; 135 | }//end filter 136 | 137 | /*** 138 | * Getting terms position on k-map by: 139 | * 1- Extracting comparelified terms and return it to 140 | * it to its full-minterm terms 141 | * 2- compute its positions by compute position of 142 | * each full-minterm terms 143 | */ 144 | 145 | 146 | bool FilterKmapTerms :: checkResult (vector someResult, vector ones) 147 | { 148 | vector resultPos; //saving someResult positions in k-map 149 | 150 | resultPos = termToPos(someResult,type); 151 | 152 | for(int temp = 0; temp < resultPos.size(); temp++) 153 | { 154 | for(int temp1= 0; temp1> FilterKmapTerms ::getFilterResult(vector> &results, vector &essentialTerms) 170 | { 171 | vector> filterResult; //return variable 172 | int filterResultCount = 0; //count filter results 173 | int largestCombinationsCount = resultTerms; //set largest combinations count 174 | vector someResult; 175 | vector > possibilites; 176 | 177 | for(int temp = 1; temp <= largestCombinationsCount; temp++) 178 | { 179 | possibilites.clear(); 180 | possibilites = getCombination(results.size(), temp); 181 | 182 | 183 | /* Adding other terms with combinational possibilites */ 184 | 185 | //covering possibilites 186 | for(int temp1 = 0; temp1 < possibilites.size(); temp1++) 187 | { 188 | someResult.clear(); 189 | //add essential terms 190 | for(int temp1 = 0; temp1 < essentialTerms.size(); temp1++) 191 | someResult.push_back(essentialTerms[temp1]); 192 | 193 | //covering all term alone 194 | for(int temp2 = 0; temp2 < possibilites[temp1].size(); temp2++) 195 | { 196 | 197 | if(someResult.size() > 0) someResult.push_back('+'); //adding + for separating terms 198 | for(int temp3 = 0; temp3 < results[ possibilites[temp1][temp2] ].size(); temp3++) 199 | { 200 | someResult.push_back(results[ possibilites[temp1][temp2] ] [temp3] ); 201 | } 202 | 203 | }//end covering lonly terms 204 | 205 | if(checkResult(someResult,ones) ) 206 | { 207 | filterResult.push_back( vector () ); 208 | 209 | for(int temp1 = 0; temp1 < someResult.size(); temp1++) 210 | { 211 | filterResult[filterResult.size() - 1].push_back(someResult[temp1]); 212 | } 213 | 214 | largestCombinationsCount = temp; 215 | }//end check result 216 | 217 | }//end covering possibilties 218 | 219 | }//general loop 220 | 221 | 222 | 223 | return filterResult; 224 | }//end get Filter Result 225 | 226 | #endif 227 | -------------------------------------------------------------------------------- /Console/KMapSolver/filterkmapresults.h: -------------------------------------------------------------------------------- 1 | #ifndef FILTERKMAPRESULTS 2 | #define FILTERKMAPRESULTS 3 | 4 | #include "operation.h" 5 | 6 | //this class is using for filtering results and giving 7 | //all possibilites results 8 | class FilterKmapTerms : public CompareKmapTerms, public ConverteTerms, public Combination 9 | { 10 | protected: 11 | 12 | int temp, temp1, temp2, temp3; //temprature variables 13 | 14 | vector getTerm (vector &result, int pos); //getting a term form an array 15 | int getMintermCount(vector &term); 16 | int getLargestTermSize(vector &result); 17 | vector > getFilterResult(vector> &results, vector &essentialTerms); 18 | bool checkResult (vector someResult, vector ones ); 19 | vector > filter (vector &result, vector ones); 20 | 21 | };//end filterKmapTerms 22 | 23 | /*** 24 | The main function of the program that implement K-map by: 25 | 1- prompting for k-map type by its variable's number 26 | 2- prompting for one's position by getPos function 27 | 3- prompting for don't care position by getPos function 28 | 4- Minimizing and getting results 29 | */ 30 | 31 | 32 | 33 | //get a term from a vector of terms (result) 34 | vector FilterKmapTerms :: getTerm (vector &result, int pos) 35 | { 36 | int temp; 37 | vector term; 38 | for(temp = pos; temp < result.size(); temp++) 39 | { 40 | if(result[temp] != '+') 41 | term.push_back(result[temp]); 42 | else 43 | break; 44 | } 45 | 46 | return term; 47 | }//end getTerm 48 | 49 | 50 | //Determine the larget size of minterms 51 | int FilterKmapTerms :: getLargestTermSize(vector &result) 52 | { 53 | int largestSize = 0; 54 | vector term; 55 | 56 | for(int temp = 0; temp largestSize) 61 | largestSize = getMintermCount(term); 62 | 63 | }//end for 64 | 65 | return largestSize; 66 | }//end get Largest Term size 67 | 68 | //return a term's minterms count 69 | int FilterKmapTerms :: getMintermCount(vector &term) 70 | { 71 | int count = 0; //for return 72 | 73 | for(int temp = 0; temp < term.size(); temp++) 74 | { 75 | if(isalpha(term[temp]) ) //search for alphabets 76 | count++; 77 | } 78 | return count; 79 | }//end get minterms count 80 | 81 | vector > FilterKmapTerms :: filter (vector &result, vector ones) 82 | { 83 | 84 | vector term, //reading all term alone 85 | essentialTerms; //result after filtering 86 | vector> filterResult; 87 | int LargestTermSize; 88 | vector>terms; 89 | 90 | 91 | //setting essentials 92 | LargestTermSize = getLargestTermSize(result); 93 | 94 | for(int temp = 0 ; temp < result.size(); temp ++) 95 | { 96 | term = getTerm(result, temp); 97 | 98 | //term is essential 99 | if(LargestTermSize > getMintermCount(term) ) 100 | { 101 | //add + for more than one term result 102 | if(essentialTerms.size() > 0 ) 103 | { 104 | essentialTerms.push_back('+'); 105 | } 106 | for(int temp1 = 0 ; temp1 < term.size(); temp1 ++) 107 | { 108 | essentialTerms.push_back(term[temp1]); //add term to filter result 109 | result.erase(result.begin()+temp); //remove term form main result 110 | } 111 | 112 | if(result.size() > 0)//erase + for more than one term 113 | result.erase(result.begin()+temp); 114 | 115 | resultTerms--; //decrement result terms 116 | temp--; 117 | }//end if of essential terms 118 | else 119 | temp += term.size(); 120 | }//end result covering loop 121 | 122 | terms.push_back(vector () ); 123 | for(int temp = 0; temp < result.size(); temp++) 124 | { 125 | if(result[temp] == '+' && temp < result.size()) 126 | terms.push_back(vector () ); 127 | else 128 | terms[terms.size()-1].push_back(result[temp]); 129 | 130 | } 131 | 132 | filterResult = getFilterResult(terms, essentialTerms); 133 | 134 | return filterResult; 135 | }//end filter 136 | 137 | /*** 138 | * Getting terms position on k-map by: 139 | * 1- Extracting comparelified terms and return it to 140 | * it to its full-minterm terms 141 | * 2- compute its positions by compute position of 142 | * each full-minterm terms 143 | */ 144 | 145 | 146 | bool FilterKmapTerms :: checkResult (vector someResult, vector ones) 147 | { 148 | vector resultPos; //saving someResult positions in k-map 149 | 150 | resultPos = termToPos(someResult,type); 151 | 152 | for(int temp = 0; temp < resultPos.size(); temp++) 153 | { 154 | for(int temp1= 0; temp1> FilterKmapTerms ::getFilterResult(vector> &results, vector &essentialTerms) 170 | { 171 | vector> filterResult; //return variable 172 | int filterResultCount = 0; //count filter results 173 | int largestCombinationsCount = resultTerms; //set largest combinations count 174 | vector someResult; 175 | vector > possibilites; 176 | 177 | for(int temp = 1; temp <= largestCombinationsCount; temp++) 178 | { 179 | possibilites.clear(); 180 | possibilites = getCombination(results.size(), temp); 181 | 182 | 183 | /* Adding other terms with combinational possibilites */ 184 | 185 | //covering possibilites 186 | for(int temp1 = 0; temp1 < possibilites.size(); temp1++) 187 | { 188 | someResult.clear(); 189 | //add essential terms 190 | for(int temp1 = 0; temp1 < essentialTerms.size(); temp1++) 191 | someResult.push_back(essentialTerms[temp1]); 192 | 193 | //covering all term alone 194 | for(int temp2 = 0; temp2 < possibilites[temp1].size(); temp2++) 195 | { 196 | 197 | if(someResult.size() > 0) someResult.push_back('+'); //adding + for separating terms 198 | for(int temp3 = 0; temp3 < results[ possibilites[temp1][temp2] ].size(); temp3++) 199 | { 200 | someResult.push_back(results[ possibilites[temp1][temp2] ] [temp3] ); 201 | } 202 | 203 | }//end covering lonly terms 204 | 205 | if(checkResult(someResult,ones) ) 206 | { 207 | filterResult.push_back( vector () ); 208 | 209 | for(int temp1 = 0; temp1 < someResult.size(); temp1++) 210 | { 211 | filterResult[filterResult.size() - 1].push_back(someResult[temp1]); 212 | } 213 | 214 | largestCombinationsCount = temp; 215 | }//end check result 216 | 217 | }//end covering possibilties 218 | 219 | }//general loop 220 | 221 | 222 | 223 | return filterResult; 224 | }//end get Filter Result 225 | 226 | #endif 227 | -------------------------------------------------------------------------------- /GUI/KMapSolver/operation.h: -------------------------------------------------------------------------------- 1 | #ifndef Operation_H 2 | #define Operation_H 3 | 4 | #include "comparekmapterms.h" 5 | 6 | /* 7 | class ConverteTerms is using to: 8 | Converte terms to ist equivalent ones 9 | 10 | class Combination is using to: 11 | provide all combination's possibilites 12 | 13 | class convSopToPos is using to: 14 | converte terms from some of product type 15 | to product of some type 16 | 17 | 18 | */ 19 | 20 | 21 | class ConverteTerms 22 | { 23 | protected: 24 | 25 | vector termToPos (vector term, int &type); 26 | vector extract(vector &terms, int &type); 27 | void compTerm(vector &part,vector &terms, int &type, int &pos); 28 | 29 | }; 30 | 31 | //complete terms to be a full-minterm terms 32 | void ConverteTerms :: compTerm(vector &part,vector &terms, int &type, int &pos) 33 | { 34 | 35 | int dashCount = 0 ; 36 | vectorcopyPart; //copying part for don't cares 37 | int temp; 38 | bool edited = 0; //check edditing in first phase 39 | 40 | pos -= part.size()-1; //return to main positiion 41 | terms.erase( terms.begin()+pos,terms.begin()+pos+part.size() );//return the part from total terms 42 | 43 | //first phase 44 | for( temp = 0; temp < part.size(); temp++) 45 | { 46 | if(part[temp] == '\'')dashCount ++; 47 | 48 | else if (part[temp] != 65 + temp - dashCount) 49 | { 50 | part.insert(part.begin()+temp,65+temp-dashCount); //insert letter 51 | copyPart = part; //copy letter 52 | part.insert(part.begin()+temp+1,'\''); //insert ' to the letter 53 | terms.insert(terms.begin()+pos,copyPart.begin(),copyPart.end()); //insert part without dash 54 | terms.insert(terms.begin()+pos,'+'); //insert + 55 | terms.insert(terms.begin()+pos,part.begin(),part.end()); //insert the part with dash to be first 56 | edited = true; //this part has eddieted 57 | break; 58 | 59 | } 60 | } 61 | 62 | //losing last digit 63 | if (edited == false &&part[part.size() -1] != 65+type) 64 | { 65 | part.insert(part.begin()+temp,65+temp-dashCount); //insert a letter 66 | copyPart = part; //copy letter 67 | part.insert(part.begin()+temp+1,'\''); //insert ' to the letter 68 | terms.insert(terms.begin()+pos,copyPart.begin(),copyPart.end()); //insert part without dash 69 | terms.insert(terms.begin()+pos,'+'); //insert + 70 | terms.insert(terms.begin()+pos,part.begin(),part.end()); //insert the part with dash to be first 71 | 72 | } 73 | 74 | 75 | }//end edit part 76 | /***Stretching terms by getting its full-minterm terms by: 77 | * 1- getting comparelified terms 78 | * 2- complete it with "compTerm" Function 79 | */ 80 | vector ConverteTerms :: extract(vector &terms, int &type) 81 | { 82 | int temp = 0; 83 | vectorpart; //part of expression 84 | int dashCount = 0; 85 | 86 | //save one term in part 87 | for(temp = 0; temp < terms.size(); temp++) 88 | { 89 | 90 | if( isalpha(terms[temp]) || terms[temp] == '\'' ) 91 | { 92 | part.push_back(terms[temp]); 93 | if(terms[temp] == '\'') dashCount++; 94 | } 95 | 96 | if( (terms[temp] =='+' || temp == terms.size()-1 ) && part.size() - dashCount < type ) 97 | { 98 | if(terms[temp] =='+') 99 | temp--; //return temp to last digit 100 | compTerm(part,terms,type,temp); //complete term 101 | temp--;//return temp to the digit before part 102 | part.clear(); //clear edit to be used again 103 | dashCount=0; 104 | } 105 | 106 | else if (terms[temp] =='+' && part.size() - dashCount == type) 107 | { 108 | part.clear(); //clear part 109 | dashCount=0; //clear dash 110 | } 111 | 112 | }//end looping for 113 | return terms; 114 | }//end extr 115 | 116 | 117 | vector ConverteTerms :: termToPos (vector term, int &type) 118 | { 119 | vectorresult; //for returning 120 | int weight = 0; //default case 121 | int pos = 0; //saving position 122 | 123 | term = extract(term, type); 124 | 125 | //compute a full-minterm term positions 126 | for(int temp = term.size()-1; temp >=0 ; temp--) 127 | { 128 | if(term[temp] == '\'') //dashed digits 129 | { 130 | pos +=0; 131 | temp --; //going to the next minterm 132 | } 133 | else if ( isalpha(term[temp] ) ) //undashed digits 134 | { 135 | pos += pow(2.0, weight); 136 | } 137 | 138 | //seperators 139 | if(term [temp] == '+' || temp == 0) 140 | { 141 | result.push_back(pos); 142 | pos = 0; //default case 143 | weight = 0; //default case 144 | continue; 145 | } 146 | weight++; 147 | }//end for 148 | 149 | for(int temp = 0; temp < result.size() - 1 ; temp++) 150 | { 151 | if(result[temp] == result[temp+1]) 152 | result.erase(result.begin()+temp+1); 153 | 154 | else if (result[temp] > result[temp+1]) 155 | { 156 | swap(result[temp], result[temp+1]); 157 | temp = -1; 158 | } 159 | } 160 | 161 | return result; 162 | }//end term to pos 163 | 164 | class Combination 165 | { 166 | vector people; 167 | vector element; 168 | vector > result; 169 | int size; 170 | int count; 171 | 172 | void saveResult(const vector& v, int k, int &size) 173 | { 174 | 175 | result.push_back(vector () ); 176 | for (int i = 0; i < v.size(); ++i) 177 | { 178 | result[count].push_back(v[i]); 179 | if(result[count].size() == size) 180 | count++; 181 | } 182 | 183 | 184 | }//end save result 185 | 186 | void go(int offset, int k) 187 | { 188 | 189 | if (k == 0) { 190 | saveResult(element, k, size); 191 | return; 192 | } 193 | for (int i = offset; i <= people.size() - k; ++i) { 194 | element.push_back(people[i]); 195 | go(i+1, k-1); 196 | element.pop_back(); 197 | } 198 | }//end go 199 | 200 | 201 | public: 202 | vector> getCombination(int n, int k) 203 | { 204 | size = k; 205 | people.clear(); 206 | result.clear(); 207 | count = 0; 208 | for (int i = 0; i < n; ++i) { people.push_back(i); } 209 | go(0, k); 210 | 211 | 212 | return result; 213 | }//end setComb 214 | 215 | }; 216 | 217 | //converte from some of product to product ot sum 218 | class SOPtoPOS 219 | { 220 | protected: 221 | void convSopToPos (vector &result) 222 | { 223 | result.insert(result.begin(), '('); 224 | result.insert(result.end(), ')'); 225 | 226 | for(int temp = 0; temp < result.size(); temp++) 227 | { 228 | if(result[temp] == '+') 229 | { 230 | result.erase(result.begin()+temp); 231 | result.insert(result.begin()+temp, ')'); 232 | result.insert(result.begin()+temp+1, '.'); 233 | result.insert(result.begin()+temp+2, '('); 234 | temp ++; 235 | } 236 | else if ( temp > 0 && (isalpha(result[temp-1]) || result[temp-1] == '\'' ) && isalpha(result[temp] ) 237 | && result[temp-1] != '(' && result[temp-1] != '+' && result[temp] != ')') 238 | { 239 | result.insert(result.begin()+temp,'+'); 240 | } 241 | 242 | } 243 | }//end convSopToPos 244 | 245 | }; 246 | 247 | #endif 248 | -------------------------------------------------------------------------------- /Console/KMapSolver/operation.h: -------------------------------------------------------------------------------- 1 | #ifndef Operation_H 2 | #define Operation_H 3 | 4 | #include "comparekmapterms.h" 5 | 6 | /* 7 | class ConverteTerms is using to: 8 | Converte terms to ist equivalent ones 9 | 10 | class Combination is using to: 11 | provide all combination's possibilites 12 | 13 | class convSopToPos is using to: 14 | converte terms from some of product type 15 | to product of some type 16 | 17 | 18 | */ 19 | 20 | 21 | class ConverteTerms 22 | { 23 | protected: 24 | 25 | vector termToPos (vector term, int &type); 26 | vector extract(vector &terms, int &type); 27 | void compTerm(vector &part,vector &terms, int &type, int &pos); 28 | 29 | }; 30 | 31 | //complete terms to be a full-minterm terms 32 | void ConverteTerms :: compTerm(vector &part,vector &terms, int &type, int &pos) 33 | { 34 | 35 | int dashCount = 0 ; 36 | vectorcopyPart; //copying part for don't cares 37 | int temp; 38 | bool edited = 0; //check edditing in first phase 39 | 40 | pos -= part.size()-1; //return to main positiion 41 | terms.erase( terms.begin()+pos,terms.begin()+pos+part.size() );//return the part from total terms 42 | 43 | //first phase 44 | for( temp = 0; temp < part.size(); temp++) 45 | { 46 | if(part[temp] == '\'')dashCount ++; 47 | 48 | else if (part[temp] != 65 + temp - dashCount) 49 | { 50 | part.insert(part.begin()+temp,65+temp-dashCount); //insert letter 51 | copyPart = part; //copy letter 52 | part.insert(part.begin()+temp+1,'\''); //insert ' to the letter 53 | terms.insert(terms.begin()+pos,copyPart.begin(),copyPart.end()); //insert part without dash 54 | terms.insert(terms.begin()+pos,'+'); //insert + 55 | terms.insert(terms.begin()+pos,part.begin(),part.end()); //insert the part with dash to be first 56 | edited = true; //this part has eddieted 57 | break; 58 | 59 | } 60 | } 61 | 62 | //losing last digit 63 | if (edited == false &&part[part.size() -1] != 65+type) 64 | { 65 | part.insert(part.begin()+temp,65+temp-dashCount); //insert a letter 66 | copyPart = part; //copy letter 67 | part.insert(part.begin()+temp+1,'\''); //insert ' to the letter 68 | terms.insert(terms.begin()+pos,copyPart.begin(),copyPart.end()); //insert part without dash 69 | terms.insert(terms.begin()+pos,'+'); //insert + 70 | terms.insert(terms.begin()+pos,part.begin(),part.end()); //insert the part with dash to be first 71 | 72 | } 73 | 74 | 75 | }//end edit part 76 | /***Stretching terms by getting its full-minterm terms by: 77 | * 1- getting comparelified terms 78 | * 2- complete it with "compTerm" Function 79 | */ 80 | vector ConverteTerms :: extract(vector &terms, int &type) 81 | { 82 | int temp = 0; 83 | vectorpart; //part of expression 84 | int dashCount = 0; 85 | 86 | //save one term in part 87 | for(temp = 0; temp < terms.size(); temp++) 88 | { 89 | 90 | if( isalpha(terms[temp]) || terms[temp] == '\'' ) 91 | { 92 | part.push_back(terms[temp]); 93 | if(terms[temp] == '\'') dashCount++; 94 | } 95 | 96 | if( (terms[temp] =='+' || temp == terms.size()-1 ) && part.size() - dashCount < type ) 97 | { 98 | if(terms[temp] =='+') 99 | temp--; //return temp to last digit 100 | compTerm(part,terms,type,temp); //complete term 101 | temp--;//return temp to the digit before part 102 | part.clear(); //clear edit to be used again 103 | dashCount=0; 104 | } 105 | 106 | else if (terms[temp] =='+' && part.size() - dashCount == type) 107 | { 108 | part.clear(); //clear part 109 | dashCount=0; //clear dash 110 | } 111 | 112 | }//end looping for 113 | return terms; 114 | }//end extr 115 | 116 | 117 | vector ConverteTerms :: termToPos (vector term, int &type) 118 | { 119 | vectorresult; //for returning 120 | int weight = 0; //default case 121 | int pos = 0; //saving position 122 | 123 | term = extract(term, type); 124 | 125 | //compute a full-minterm term positions 126 | for(int temp = term.size()-1; temp >=0 ; temp--) 127 | { 128 | if(term[temp] == '\'') //dashed digits 129 | { 130 | pos +=0; 131 | temp --; //going to the next minterm 132 | } 133 | else if ( isalpha(term[temp] ) ) //undashed digits 134 | { 135 | pos += pow(2.0, weight); 136 | } 137 | 138 | //seperators 139 | if(term [temp] == '+' || temp == 0) 140 | { 141 | result.push_back(pos); 142 | pos = 0; //default case 143 | weight = 0; //default case 144 | continue; 145 | } 146 | weight++; 147 | }//end for 148 | 149 | for(int temp = 0; temp < result.size() - 1 ; temp++) 150 | { 151 | if(result[temp] == result[temp+1]) 152 | result.erase(result.begin()+temp+1); 153 | 154 | else if (result[temp] > result[temp+1]) 155 | { 156 | swap(result[temp], result[temp+1]); 157 | temp = -1; 158 | } 159 | } 160 | 161 | return result; 162 | }//end term to pos 163 | 164 | class Combination 165 | { 166 | vector people; 167 | vector element; 168 | vector > result; 169 | int size; 170 | int count; 171 | 172 | void saveResult(const vector& v, int k, int &size) 173 | { 174 | 175 | result.push_back(vector () ); 176 | for (int i = 0; i < v.size(); ++i) 177 | { 178 | result[count].push_back(v[i]); 179 | if(result[count].size() == size) 180 | count++; 181 | } 182 | 183 | 184 | }//end save result 185 | 186 | void go(int offset, int k) 187 | { 188 | 189 | if (k == 0) { 190 | saveResult(element, k, size); 191 | return; 192 | } 193 | for (int i = offset; i <= people.size() - k; ++i) { 194 | element.push_back(people[i]); 195 | go(i+1, k-1); 196 | element.pop_back(); 197 | } 198 | }//end go 199 | 200 | 201 | public: 202 | vector> getCombination(int n, int k) 203 | { 204 | size = k; 205 | people.clear(); 206 | result.clear(); 207 | count = 0; 208 | for (int i = 0; i < n; ++i) { people.push_back(i); } 209 | go(0, k); 210 | 211 | 212 | return result; 213 | }//end setComb 214 | 215 | }; 216 | 217 | //converte from some of product to product ot sum 218 | class SOPtoPOS 219 | { 220 | protected: 221 | void convSopToPos (vector &result) 222 | { 223 | result.insert(result.begin(), '('); 224 | result.insert(result.end(), ')'); 225 | 226 | for(int temp = 0; temp < result.size(); temp++) 227 | { 228 | if(result[temp] == '+') 229 | { 230 | result.erase(result.begin()+temp); 231 | result.insert(result.begin()+temp, ')'); 232 | result.insert(result.begin()+temp+1, '.'); 233 | result.insert(result.begin()+temp+2, '('); 234 | temp ++; 235 | } 236 | else if ( temp > 0 && (isalpha(result[temp-1]) || result[temp-1] == '\'' ) && isalpha(result[temp] ) 237 | && result[temp-1] != '(' && result[temp-1] != '+' && result[temp] != ')') 238 | { 239 | result.insert(result.begin()+temp,'+'); 240 | } 241 | 242 | } 243 | }//end convSopToPos 244 | 245 | }; 246 | 247 | #endif 248 | -------------------------------------------------------------------------------- /GUI/KMapSolver/setkmap.h: -------------------------------------------------------------------------------- 1 | #ifndef SETKMAP_H 2 | #define SETKMAP_H 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | 15 | //this class is using for prompting k-map details from user 16 | //and setting k-map in order to solveing it 17 | 18 | class setKmap 19 | { 20 | protected: 21 | 22 | int type, //k-map type 2,3, .... 23 | termCount, //term's count 24 | saverCount; //saving savers 25 | vectorones; //Saving ones position 26 | vectordCare; //Saving dont care position 27 | bool hasEnteredType, //check entering type of not 28 | SOP; // ture for some of product 29 | // false for product of some 30 | 31 | void guideWin (short cursor); 32 | int readInt (int &count, bool negative ); 33 | void getPos (vector &pos, string name); 34 | void setTerms (vector ones, vector > &terms); 35 | 36 | setKmap(): hasEnteredType(false) {} 37 | 38 | };//end kmap 39 | 40 | 41 | /*** 42 | * guide window helps user to solving K-map 43 | * its argument curor for moving cursor during minimize 44 | */ 45 | 46 | void setKmap ::guideWin (short cursor) 47 | { 48 | cout<<"This program has devolped to solve Karnaugh map with any number of variables\n\n" 49 | <<"Solving steps:\n"; 50 | if(cursor == 1) cout<<"==> "; else cout<<" "; 51 | cout <<"1- Enter k-map type (Number of variables)\n"; 52 | 53 | if(cursor == 2) cout<<"==> "; else cout<<" "; 54 | cout<<"2- Enter one's positions (-1 for stopping)\n"; 55 | 56 | if(cursor == 3) cout<<"==> "; else cout<<" "; 57 | cout<<"3- Enter don't care positions (-1 for stopping)\n"; 58 | 59 | if(cursor == 4) cout<<"==> "; else cout<<" "; 60 | cout<<"4- Choose result's type (SOP or POS):\n"; 61 | 62 | 63 | if(cursor == 5) cout<<"==> "; else cout<<" "; 64 | cout<<"5- Solving of your k-map\n\n"; 65 | 66 | //show type 67 | if(hasEnteredType) cout<<"Type : "< 0) 71 | { 72 | cout<<"Ones :"; 73 | for(int temp = 0; temp < ones.size(); temp++) 74 | { 75 | cout< 0) 84 | { 85 | cout<<"Don't Care :"; 86 | for(int temp = 0; temp < dCare.size(); temp++) 87 | { 88 | cout<47 && tempChar < 58) ) //positive condition 128 | { 129 | if(intNum < pow (2.0 , 31)) 130 | { 131 | intNum = intNum * 10 + (tempChar - 48); //add the value to intNum 132 | cout< 0) 149 | { 150 | cout<<"\b \b"; //remove number from screen 151 | intNum /= 10; //remove Character value 152 | count--; //decrease count 153 | 154 | if(sign == false && count == 0) //removing negative sign after writing it 155 | sign = true; //return it to positive 156 | 157 | if( count == 0 || (count == 1 && sign == false) ) 158 | hasValue = false; 159 | }//end backslach condition 160 | 161 | //other characters (wrong inputs) 162 | else 163 | cout<<"\a"; 164 | 165 | 166 | }//end while 167 | 168 | return (sign)?intNum:-1*intNum; //return integer num 169 | 170 | }//end readInt 171 | 172 | 173 | /*** 174 | * getpos (get positions) 175 | * is prompting for positions of name sending as 176 | * argument and saving them in pos's argument 177 | * if positions are locating in the k-map field 178 | * it will save it, else it will warn you 179 | * It is stilling reading until read -1 180 | */ 181 | 182 | void setKmap :: getPos (vector &pos, string name) 183 | { 184 | int position; //for reading positions 185 | int count ; //position count digits 186 | 187 | while(true) //still reading until read -1 188 | { 189 | 190 | count = 0; 191 | //prompting for one's positions 192 | cout<<"Please, enter " << name << "'s position (-1 for stopping) : "; 193 | 194 | //reading one by one 195 | position = readInt ( count ) ; 196 | 197 | //check position locations 198 | //as it is locating in this type of k-map 199 | if(position != -1 && position < pow(2.0,type)) 200 | { 201 | pos.push_back(position); //save position 202 | cout< pos, vector > &terms) 228 | 229 | { 230 | //covering ones 231 | for(int temp =0; temp < pos.size(); temp++) 232 | { 233 | 234 | //prepare a new term for a new position 235 | terms.push_back( vector () ); 236 | 237 | //covering coordinate 238 | for(int temp1 = 0; temp1 < type; temp1++) 239 | { 240 | /*save a one's coordinate*/ 241 | 242 | //save one coordinate 243 | terms[termCount].insert(terms[termCount].begin(), (pos[temp] % 2)); 244 | //remove the coordintae from its one 245 | pos[temp] /= 2; 246 | }//end covering coordinate loop 247 | 248 | termCount++; //increase terms 249 | }//end covering coordinate loop 250 | 251 | }//end setTerms 252 | 253 | #endif 254 | -------------------------------------------------------------------------------- /Console/KMapSolver/setkmap.h: -------------------------------------------------------------------------------- 1 | #ifndef SETKMAP_H 2 | #define SETKMAP_H 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | 15 | //this class is using for prompting k-map details from user 16 | //and setting k-map in order to solveing it 17 | 18 | class setKmap 19 | { 20 | protected: 21 | 22 | int type, //k-map type 2,3, .... 23 | termCount, //term's count 24 | saverCount; //saving savers 25 | vectorones; //Saving ones position 26 | vectordCare; //Saving dont care position 27 | bool hasEnteredType, //check entering type of not 28 | SOP; // ture for some of product 29 | // false for product of some 30 | 31 | void guideWin (short cursor); 32 | int readInt (int &count, bool negative ); 33 | void getPos (vector &pos, string name); 34 | void setTerms (vector ones, vector > &terms); 35 | 36 | setKmap(): hasEnteredType(false) {} 37 | 38 | };//end kmap 39 | 40 | 41 | /*** 42 | * guide window helps user to solving K-map 43 | * its argument curor for moving cursor during minimize 44 | */ 45 | 46 | void setKmap ::guideWin (short cursor) 47 | { 48 | cout<<"This program has devolped to solve Karnaugh map with any number of variables\n\n" 49 | <<"Solving steps:\n"; 50 | if(cursor == 1) cout<<"==> "; else cout<<" "; 51 | cout <<"1- Enter k-map type (Number of variables)\n"; 52 | 53 | if(cursor == 2) cout<<"==> "; else cout<<" "; 54 | cout<<"2- Enter one's positions (-1 for stopping)\n"; 55 | 56 | if(cursor == 3) cout<<"==> "; else cout<<" "; 57 | cout<<"3- Enter don't care positions (-1 for stopping)\n"; 58 | 59 | if(cursor == 4) cout<<"==> "; else cout<<" "; 60 | cout<<"4- Choose result's type (SOP or POS):\n"; 61 | 62 | 63 | if(cursor == 5) cout<<"==> "; else cout<<" "; 64 | cout<<"5- Solving of your k-map\n\n"; 65 | 66 | //show type 67 | if(hasEnteredType) cout<<"Type : "< 0) 71 | { 72 | cout<<"Ones :"; 73 | for(int temp = 0; temp < ones.size(); temp++) 74 | { 75 | cout< 0) 84 | { 85 | cout<<"Don't Care :"; 86 | for(int temp = 0; temp < dCare.size(); temp++) 87 | { 88 | cout<47 && tempChar < 58) ) //positive condition 128 | { 129 | if(intNum < pow (2.0 , 31)) 130 | { 131 | intNum = intNum * 10 + (tempChar - 48); //add the value to intNum 132 | cout< 0) 149 | { 150 | cout<<"\b \b"; //remove number from screen 151 | intNum /= 10; //remove Character value 152 | count--; //decrease count 153 | 154 | if(sign == false && count == 0) //removing negative sign after writing it 155 | sign = true; //return it to positive 156 | 157 | if( count == 0 || (count == 1 && sign == false) ) 158 | hasValue = false; 159 | }//end backslach condition 160 | 161 | //other characters (wrong inputs) 162 | else 163 | cout<<"\a"; 164 | 165 | 166 | }//end while 167 | 168 | return (sign)?intNum:-1*intNum; //return integer num 169 | 170 | }//end readInt 171 | 172 | 173 | /*** 174 | * getpos (get positions) 175 | * is prompting for positions of name sending as 176 | * argument and saving them in pos's argument 177 | * if positions are locating in the k-map field 178 | * it will save it, else it will warn you 179 | * It is stilling reading until read -1 180 | */ 181 | 182 | void setKmap :: getPos (vector &pos, string name) 183 | { 184 | int position; //for reading positions 185 | int count ; //position count digits 186 | 187 | while(true) //still reading until read -1 188 | { 189 | 190 | count = 0; 191 | //prompting for one's positions 192 | cout<<"Please, enter " << name << "'s position (-1 for stopping) : "; 193 | 194 | //reading one by one 195 | position = readInt ( count ) ; 196 | 197 | //check position locations 198 | //as it is locating in this type of k-map 199 | if(position != -1 && position < pow(2.0,type)) 200 | { 201 | pos.push_back(position); //save position 202 | cout< pos, vector > &terms) 228 | 229 | { 230 | //covering ones 231 | for(int temp =0; temp < pos.size(); temp++) 232 | { 233 | 234 | //prepare a new term for a new position 235 | terms.push_back( vector () ); 236 | 237 | //covering coordinate 238 | for(int temp1 = 0; temp1 < type; temp1++) 239 | { 240 | /*save a one's coordinate*/ 241 | 242 | //save one coordinate 243 | terms[termCount].insert(terms[termCount].begin(), (pos[temp] % 2)); 244 | //remove the coordintae from its one 245 | pos[temp] /= 2; 246 | }//end covering coordinate loop 247 | 248 | termCount++; //increase terms 249 | }//end covering coordinate loop 250 | 251 | }//end setTerms 252 | 253 | #endif 254 | -------------------------------------------------------------------------------- /GUI/KMapSolver/comparekmapterms.h: -------------------------------------------------------------------------------- 1 | #ifndef COMPAREKMAPTERMS_H 2 | #define COMPAREkMAPTERMS_H 3 | 4 | #include "setkmap.h" 5 | 6 | 7 | 8 | //This class is using for compare iputs results and provide 9 | // minmizing terms in order to filter them and get results 10 | class CompareKmapTerms : public setKmap 11 | { 12 | 13 | protected: 14 | int temp, temp1, temp2, temp3; //temprature variables 15 | int resultTerms; 16 | 17 | vector minimize (vector &ones, vector &dCare); 18 | void compare (vector < vector< int >> &terms); // comparelify 19 | void saveValue(vector &tempSave, vector> &saver, vector &hascompare, vector> &terms); 20 | void addOther (vector> &saver,vector &hascompare, int &saverCount, vector> &terms); 21 | void unRepeat (vector > &terms); 22 | vector posToTerm(vector> &terms); //converte position to its terms 23 | 24 | };//end kmap 25 | 26 | /*** 27 | The main function of the program that implement K-map by: 28 | 1- prompting for k-map type by its variable's number 29 | 2- prompting for one's position by getPos function 30 | 3- prompting for don't care position by getPos function 31 | 4- Minimizing and getting results 32 | */ 33 | 34 | /*** 35 | * minimize is the function that do minimizing process by: 36 | * 1- Save ones and don't care positions with its coordinates 37 | * in (vector) terms with "setTerms" function 38 | * 2- comparelify terms with "compare" Function 39 | * 3- Represent comparelification as alphapetical terms with posToTerm 40 | * 4- Filter unfull and unempty kmaps to getting essential 41 | * terms and remove others 42 | */ 43 | 44 | vector CompareKmapTerms :: minimize (vector &ones, vector &dCare) 45 | { 46 | vector >terms; //for saving one's coordinates 47 | vectorresult; //Minimizing results before filter 48 | 49 | termCount = 0; 50 | setTerms (ones, terms); //save one's with its coordintates in (vector) terms 51 | setTerms(dCare, terms); //save don't care with its coordinates in (vector) terms 52 | compare(terms); //compare ones 53 | unRepeat(terms); 54 | result = posToTerm(terms); //converte result to terms of variables 55 | 56 | return result; //return result 57 | }//end minimize 58 | 59 | 60 | /* 61 | * compare function (compare) 62 | * compare each two terms with each other and save 63 | * compare result in (saver) with "setValue" function 64 | * Add unsiplified terms to saver with "addOther" function 65 | */ 66 | void CompareKmapTerms ::compare (vector < vector< int >> &terms) 67 | { 68 | 69 | vectortempSave; 70 | vector >saver; //save similarty 71 | vectorhascompare; 72 | 73 | //comparelify loop 74 | for( temp3 = 0; temp3 < type && terms.size() > 1; temp3++) 75 | {//times while 76 | 77 | //hasSave = false; 78 | saverCount = 0; //default case 79 | 80 | //initializing has comparelified with false 81 | hascompare.clear(); 82 | hascompare.resize(terms.size(), false); 83 | 84 | //do essential minimizations 85 | for( temp = 0; temp < terms.size() -1 ; temp++) 86 | {//for 1 87 | for ( temp1 = temp + 1; temp1 < terms.size(); temp1 ++ ) 88 | {//for 2 89 | tempSave.clear(); //clear temp Save 90 | ////////element's loop///////////////// 91 | for (temp2 = 0; temp2 < type; temp2++) 92 | {//for 3 93 | 94 | //searching for identical coordinate and save its arrangement in tempSave 95 | // temp and temp1 cover terms 96 | //temp2 cover coordinates 97 | if( ( terms[temp][temp2] == terms[temp1][temp2] ) ) //search for mathcing element 98 | { 99 | //save identical 100 | tempSave.push_back(temp2); 101 | } 102 | 103 | }//end for 3 104 | if(tempSave.size () == type -1 ) 105 | saveValue(tempSave,saver, hascompare, terms); 106 | 107 | }//end for 2 108 | }//end for 1 109 | 110 | //add remain terms that hasn't comparelified with other 111 | addOther(saver,hascompare,saverCount, terms); 112 | 113 | //clear terms before comparelified 114 | terms.clear(); 115 | //assuming terms after comparelified 116 | terms = saver; 117 | 118 | saver.clear(); //clear saver to receive new data 119 | 120 | }//end comparelify loop 121 | }//end compare 122 | 123 | void CompareKmapTerms ::unRepeat(vector> &terms) 124 | { 125 | for(int temp = 0; temp < terms.size(); temp ++) 126 | for(int temp1 = temp + 1 ; temp1 < terms.size(); temp1++) 127 | { 128 | if(terms[temp] == terms[temp1]) //checking for repeating terms 129 | { 130 | terms.erase(terms.begin() + temp); //remove repeating terms 131 | temp1--; 132 | } 133 | } 134 | }//end unrepeat 135 | 136 | /* 137 | *save Value function 138 | * set don't care arrangements after getting identicals arrangements 139 | * converte identical coordinates from its arrangement to its values 140 | */ 141 | void CompareKmapTerms :: saveValue(vector &tempSave, vector> &saver, vector &hascompare, vector> &terms) 142 | { 143 | if(tempSave.size () == type -1 ) 144 | { 145 | 146 | saver.push_back(vector () ); //initializing terms 147 | 148 | /*set don't care arrangements after getting identicals arrangements*/ 149 | 150 | for(temp2 = 0; temp2 < terms[temp].size(); temp2++) 151 | { 152 | //don't care locates after matching coordinates 153 | if(temp2 == tempSave.size() ) 154 | tempSave.push_back(-1); 155 | 156 | //don't care locates between matching coordinates 157 | else if(temp2 != tempSave [temp2]) 158 | tempSave.insert(tempSave.begin()+temp2,-1); 159 | }//end adding don't care 160 | 161 | /*converte identical coordinates from its arrangement to its values*/ 162 | for(temp2 = 0; temp2 < tempSave.size(); temp2++) 163 | {//saver for 164 | if( tempSave[temp2] == -1) 165 | saver[saverCount].push_back(-1); 166 | else 167 | saver[saverCount].push_back(terms[temp][temp2]); 168 | }//end saver for 169 | 170 | hascompare[temp] = hascompare[temp1] = true; //assuming them as comparelified 171 | saverCount++; //increase saving comparelified terms 172 | 173 | }//end if condition 174 | }//end save values 175 | 176 | //add Un comparelified terms to terms 177 | void CompareKmapTerms :: addOther (vector> &saver,vector &hascompare, int &saverCount, vector> &terms) 178 | { 179 | //add single element 180 | for(temp2 = 0; temp2 () ); 185 | 186 | for(int temp4 = 0; temp4 < terms[temp2].size(); temp4++) 187 | { 188 | saver[saverCount].push_back(terms[temp2][temp4]); 189 | } 190 | saverCount++; 191 | } 192 | } 193 | }//end add other 194 | 195 | 196 | //represent comparelification as alphapetical terms 197 | vector CompareKmapTerms :: posToTerm(vector> &term) 198 | { 199 | vectorresult; //for returinging 200 | int smallLetter = false; //representing with small letters 201 | int digit, //for pushing back 202 | dashCount = 0; //dash cound 203 | bool isFull = true, //for checking full maps 204 | isEmpty = false; //checking empty maps 205 | 206 | resultTerms = 0; 207 | //terms loop 208 | for(temp = 0; temp < term.size(); temp++) 209 | { 210 | //minterms loop 211 | for(temp1 = 0; temp1 9) 216 | smallLetter = true; //start represent with small letter 217 | 218 | 219 | digit = temp1 + 65; 220 | if (smallLetter) digit + 22; //97 - 65 - 10 221 | // 97 a ascii, 65 A ascii, 10 numerical a 222 | //dashed minterms 223 | if(term[temp][temp1] == 0) 224 | { 225 | result.push_back(digit); //add minterm 226 | result.push_back(39); //add dash 227 | dashCount ++; //increase dash count 228 | } 229 | 230 | //undashed minterms 231 | if(term[temp][temp1] == 1) 232 | { 233 | result.push_back(digit); //add minterm 234 | } 235 | }//end minterms loop 236 | 237 | //add + for seperating between terms 238 | if(temp < term.size() -1) 239 | result.push_back('+'); 240 | 241 | resultTerms++; 242 | }//end terms loop 243 | 244 | //no function case 245 | if(ones.size() == 0) 246 | { 247 | result.push_back('0'); 248 | isEmpty = true; 249 | } 250 | //checking full map 251 | for(int temp = 0; temp < term.size() && isEmpty == false; temp++) 252 | { 253 | isFull = true; //default case 254 | for(int temp1= 0; temp1 < term[temp].size(); temp1++) 255 | { 256 | if(term[temp][temp1] != -1) 257 | isFull = false; 258 | } 259 | } 260 | 261 | //empty maps 262 | if (isEmpty) 263 | { 264 | result.clear(); 265 | result.push_back('0'); 266 | } 267 | 268 | //full-map case 269 | else if(isFull) 270 | { 271 | result.clear(); 272 | result.push_back('1'); 273 | } 274 | 275 | return result; 276 | }//end get Result 277 | 278 | 279 | #endif 280 | -------------------------------------------------------------------------------- /Console/KMapSolver/comparekmapterms.h: -------------------------------------------------------------------------------- 1 | #ifndef COMPAREKMAPTERMS_H 2 | #define COMPAREkMAPTERMS_H 3 | 4 | #include "setkmap.h" 5 | 6 | 7 | 8 | //This class is using for compare iputs results and provide 9 | // minmizing terms in order to filter them and get results 10 | class CompareKmapTerms : public setKmap 11 | { 12 | 13 | protected: 14 | int temp, temp1, temp2, temp3; //temprature variables 15 | int resultTerms; 16 | 17 | vector minimize (vector &ones, vector &dCare); 18 | void compare (vector < vector< int >> &terms); // comparelify 19 | void saveValue(vector &tempSave, vector> &saver, vector &hascompare, vector> &terms); 20 | void addOther (vector> &saver,vector &hascompare, int &saverCount, vector> &terms); 21 | void unRepeat (vector > &terms); 22 | vector posToTerm(vector> &terms); //converte position to its terms 23 | 24 | };//end kmap 25 | 26 | /*** 27 | The main function of the program that implement K-map by: 28 | 1- prompting for k-map type by its variable's number 29 | 2- prompting for one's position by getPos function 30 | 3- prompting for don't care position by getPos function 31 | 4- Minimizing and getting results 32 | */ 33 | 34 | /*** 35 | * minimize is the function that do minimizing process by: 36 | * 1- Save ones and don't care positions with its coordinates 37 | * in (vector) terms with "setTerms" function 38 | * 2- comparelify terms with "compare" Function 39 | * 3- Represent comparelification as alphapetical terms with posToTerm 40 | * 4- Filter unfull and unempty kmaps to getting essential 41 | * terms and remove others 42 | */ 43 | 44 | vector CompareKmapTerms :: minimize (vector &ones, vector &dCare) 45 | { 46 | vector >terms; //for saving one's coordinates 47 | vectorresult; //Minimizing results before filter 48 | 49 | termCount = 0; 50 | setTerms (ones, terms); //save one's with its coordintates in (vector) terms 51 | setTerms(dCare, terms); //save don't care with its coordinates in (vector) terms 52 | compare(terms); //compare ones 53 | unRepeat(terms); 54 | result = posToTerm(terms); //converte result to terms of variables 55 | 56 | return result; //return result 57 | }//end minimize 58 | 59 | 60 | /* 61 | * compare function (compare) 62 | * compare each two terms with each other and save 63 | * compare result in (saver) with "setValue" function 64 | * Add unsiplified terms to saver with "addOther" function 65 | */ 66 | void CompareKmapTerms ::compare (vector < vector< int >> &terms) 67 | { 68 | 69 | vectortempSave; 70 | vector >saver; //save similarty 71 | vectorhascompare; 72 | 73 | //comparelify loop 74 | for( temp3 = 0; temp3 < type && terms.size() > 1; temp3++) 75 | {//times while 76 | 77 | //hasSave = false; 78 | saverCount = 0; //default case 79 | 80 | //initializing has comparelified with false 81 | hascompare.clear(); 82 | hascompare.resize(terms.size(), false); 83 | 84 | //do essential minimizations 85 | for( temp = 0; temp < terms.size() -1 ; temp++) 86 | {//for 1 87 | for ( temp1 = temp + 1; temp1 < terms.size(); temp1 ++ ) 88 | {//for 2 89 | tempSave.clear(); //clear temp Save 90 | ////////element's loop///////////////// 91 | for (temp2 = 0; temp2 < type; temp2++) 92 | {//for 3 93 | 94 | //searching for identical coordinate and save its arrangement in tempSave 95 | // temp and temp1 cover terms 96 | //temp2 cover coordinates 97 | if( ( terms[temp][temp2] == terms[temp1][temp2] ) ) //search for mathcing element 98 | { 99 | //save identical 100 | tempSave.push_back(temp2); 101 | } 102 | 103 | }//end for 3 104 | if(tempSave.size () == type -1 ) 105 | saveValue(tempSave,saver, hascompare, terms); 106 | 107 | }//end for 2 108 | }//end for 1 109 | 110 | //add remain terms that hasn't comparelified with other 111 | addOther(saver,hascompare,saverCount, terms); 112 | 113 | //clear terms before comparelified 114 | terms.clear(); 115 | //assuming terms after comparelified 116 | terms = saver; 117 | 118 | saver.clear(); //clear saver to receive new data 119 | 120 | }//end comparelify loop 121 | }//end compare 122 | 123 | void CompareKmapTerms ::unRepeat(vector> &terms) 124 | { 125 | for(int temp = 0; temp < terms.size(); temp ++) 126 | for(int temp1 = temp + 1 ; temp1 < terms.size(); temp1++) 127 | { 128 | if(terms[temp] == terms[temp1]) //checking for repeating terms 129 | { 130 | terms.erase(terms.begin() + temp); //remove repeating terms 131 | temp1--; 132 | } 133 | } 134 | }//end unrepeat 135 | 136 | /* 137 | *save Value function 138 | * set don't care arrangements after getting identicals arrangements 139 | * converte identical coordinates from its arrangement to its values 140 | */ 141 | void CompareKmapTerms :: saveValue(vector &tempSave, vector> &saver, vector &hascompare, vector> &terms) 142 | { 143 | if(tempSave.size () == type -1 ) 144 | { 145 | 146 | saver.push_back(vector () ); //initializing terms 147 | 148 | /*set don't care arrangements after getting identicals arrangements*/ 149 | 150 | for(temp2 = 0; temp2 < terms[temp].size(); temp2++) 151 | { 152 | //don't care locates after matching coordinates 153 | if(temp2 == tempSave.size() ) 154 | tempSave.push_back(-1); 155 | 156 | //don't care locates between matching coordinates 157 | else if(temp2 != tempSave [temp2]) 158 | tempSave.insert(tempSave.begin()+temp2,-1); 159 | }//end adding don't care 160 | 161 | /*converte identical coordinates from its arrangement to its values*/ 162 | for(temp2 = 0; temp2 < tempSave.size(); temp2++) 163 | {//saver for 164 | if( tempSave[temp2] == -1) 165 | saver[saverCount].push_back(-1); 166 | else 167 | saver[saverCount].push_back(terms[temp][temp2]); 168 | }//end saver for 169 | 170 | hascompare[temp] = hascompare[temp1] = true; //assuming them as comparelified 171 | saverCount++; //increase saving comparelified terms 172 | 173 | }//end if condition 174 | }//end save values 175 | 176 | //add Un comparelified terms to terms 177 | void CompareKmapTerms :: addOther (vector> &saver,vector &hascompare, int &saverCount, vector> &terms) 178 | { 179 | //add single element 180 | for(temp2 = 0; temp2 () ); 185 | 186 | for(int temp4 = 0; temp4 < terms[temp2].size(); temp4++) 187 | { 188 | saver[saverCount].push_back(terms[temp2][temp4]); 189 | } 190 | saverCount++; 191 | } 192 | } 193 | }//end add other 194 | 195 | 196 | //represent comparelification as alphapetical terms 197 | vector CompareKmapTerms :: posToTerm(vector> &term) 198 | { 199 | vectorresult; //for returinging 200 | int smallLetter = false; //representing with small letters 201 | int digit, //for pushing back 202 | dashCount = 0; //dash cound 203 | bool isFull = true, //for checking full maps 204 | isEmpty = false; //checking empty maps 205 | 206 | resultTerms = 0; 207 | //terms loop 208 | for(temp = 0; temp < term.size(); temp++) 209 | { 210 | //minterms loop 211 | for(temp1 = 0; temp1 9) 216 | smallLetter = true; //start represent with small letter 217 | 218 | 219 | digit = temp1 + 65; 220 | if (smallLetter) digit + 22; //97 - 65 - 10 221 | // 97 a ascii, 65 A ascii, 10 numerical a 222 | //dashed minterms 223 | if(term[temp][temp1] == 0) 224 | { 225 | result.push_back(digit); //add minterm 226 | result.push_back(39); //add dash 227 | dashCount ++; //increase dash count 228 | } 229 | 230 | //undashed minterms 231 | if(term[temp][temp1] == 1) 232 | { 233 | result.push_back(digit); //add minterm 234 | } 235 | }//end minterms loop 236 | 237 | //add + for seperating between terms 238 | if(temp < term.size() -1) 239 | result.push_back('+'); 240 | 241 | resultTerms++; 242 | }//end terms loop 243 | 244 | //no function case 245 | if(ones.size() == 0) 246 | { 247 | result.push_back('0'); 248 | isEmpty = true; 249 | } 250 | //checking full map 251 | for(int temp = 0; temp < term.size() && isEmpty == false; temp++) 252 | { 253 | isFull = true; //default case 254 | for(int temp1= 0; temp1 < term[temp].size(); temp1++) 255 | { 256 | if(term[temp][temp1] != -1) 257 | isFull = false; 258 | } 259 | } 260 | 261 | //empty maps 262 | if (isEmpty) 263 | { 264 | result.clear(); 265 | result.push_back('0'); 266 | } 267 | 268 | //full-map case 269 | else if(isFull) 270 | { 271 | result.clear(); 272 | result.push_back('1'); 273 | } 274 | 275 | return result; 276 | }//end get Result 277 | 278 | 279 | #endif 280 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {Manash Kumar Mandal} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------