/home/enzo/treballs/fib/pfc/nanocomp/src/truthTable.cpp

Go to the documentation of this file.
00001 /*
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  *   This program is distributed in the hope that it will be useful,       *
00009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00011  *   GNU General Public License for more details.                          *
00012  *                                                                         *
00013  *   You should have received a copy of the GNU General Public License     *
00014  *   along with this program; if not, write to the                         *
00015  *   Free Software Foundation, Inc.,                                       *
00016  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00017                                                                           */
00018 
00019 // $Revision: 1.7 $
00020 
00021 #include "truthTable.hpp"
00022 #include <math.h>
00023 #include <iostream>
00024 
00026 
00031 TruthTable::TruthTable(wxString name, int inputs, int outputs)
00032 {
00033     table.resize((int)(pow(2, inputs)));
00034     //We have 2^inputs outputs, so we must
00035     //initialize 2^input false values
00036     for (unsigned int i = 0; i < table.size(); i++)
00037     {
00038         table[i].resize(outputs);
00039         for (unsigned int j = 0; j < table[i].size(); j++)
00040         {
00041             table[i][j] = false;
00042         }
00043     }
00044     
00045     this->inputs = inputs;
00046     this->outputs = outputs;
00047     this->name = name;
00048 }
00049 
00050 
00052 
00055 TruthTable::TruthTable()
00056 {
00057     inputs = 0;
00058     outputs = 0;
00059 }
00060 
00061 
00063 TruthTable::~TruthTable()
00064 {
00065 }
00066 
00067 
00069 
00073 vector<bool> TruthTable::getOutput(vector<bool> input)
00074 {
00075     return table[getIndex(input)];
00076 }
00077 
00078 
00080 
00088 int TruthTable::getIndex(vector<bool> input)
00089 {
00090     int result = 0;
00091     int base = 1;
00092     for (unsigned int i = 0; i < input.size(); i++)
00093     {
00094         if (input[input.size() - 1 - i])
00095         {
00096             result += base;
00097         } 
00098         base *= 2;
00099     }
00100     return result;
00101 }
00102 
00103 
00105 
00110 void TruthTable::setOutput(vector<bool> input, vector<bool> output)
00111 {
00112     table[getIndex(input)] = output;
00113 }
00114 
00115 
00117 
00120 int TruthTable::getInputs()
00121 {
00122     return inputs;
00123 }
00124 
00125 
00127 
00130 int TruthTable::getOutputs()
00131 {
00132     return outputs;
00133 }
00134 
00135 
00137 
00141 wxString TruthTable::getOutput(wxString input)
00142 {
00143     vector<bool> vInput = stringToVector(input);
00144     return vectorToString(getOutput(vInput));
00145 }
00146 
00147 
00149 
00155 bool TruthTable::getOutput(vector<bool> input, int output)
00156 {
00157     return getOutput(input)[output];
00158 }
00159 
00160 
00162 
00168 bool TruthTable::getOutput(wxString input, int output)
00169 {
00170     vector<bool> vInput = stringToVector(input);
00171     return getOutput(vInput)[output];
00172 }
00173     
00174     
00176 
00181 void TruthTable::setOutput(wxString input, wxString output)
00182 {
00183     vector<bool> vInput = stringToVector(input);
00184     vector<bool> vOutput = stringToVector(output);
00185     setOutput(vInput, vOutput);
00186 }
00187 
00188 
00190 
00198 int TruthTable::getIndex(wxString input)
00199 {
00200     vector<bool> vInput = stringToVector(input);
00201     return getIndex(vInput);
00202 }
00203 
00204 
00206 
00211 vector <bool> TruthTable::stringToVector(wxString input)
00212 {
00213     vector<bool> result(input.length());
00214     for (unsigned int i = 0; i < input.length(); i++)
00215     {
00216         if (input[i] == '0')
00217         {
00218             result[i] = false;
00219         }
00220         else
00221         {
00222             result[i] = true;
00223         }
00224     }
00225     
00226     return result;
00227 }
00228 
00229 
00231 
00236 wxString TruthTable::vectorToString(vector<bool> input)
00237 {
00238     wxString result= _("");
00239     for (unsigned int i = 0; i < input.size(); i++)
00240     {
00241         if (input[i])
00242         {
00243             result += '1';
00244         }
00245         else
00246         {
00247             result += '0';
00248         }
00249     }
00250     
00251     return result;
00252 }
00253 
00254 
00256 
00260 vector< vector<bool> > TruthTable::getTable()
00261 {
00262     return table;
00263 }
00264 
00265 
00267 
00270 void TruthTable::copyTable(TruthTable newTable)
00271 {
00272     table = newTable.getTable();
00273     name = newTable.getName();
00274     inputs = newTable.getInputs();
00275     outputs = newTable.getOutputs();
00276 }
00277 
00278 
00280 
00283 wxString TruthTable::getName()
00284 {
00285     return name;
00286 }
00287 
00288 
00290 
00293 void TruthTable::setName(wxString newName)
00294 {
00295     name = newName;
00296 }
00297 
00298 
00300 
00303 void TruthTable::setTable(vector< vector<bool> > table)
00304 {
00305     this->table = table;
00306 }

Generated on Fri Sep 1 23:55:14 2006 for NanoComp by  doxygen 1.4.6