/home/enzo/treballs/fib/pfc/nanocomp/src/rule.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.16 $
00020 
00021 //TODO: check invalid combinations of Grids => initial cannot be the same as final
00022 
00023 #include "rule.hpp"
00024 #include <iostream>
00025 
00026 using namespace std;
00027 
00029 
00033 Rule::Rule(int width, int height)
00034     : initialGrid(width, height), finalGrid(width, height)
00035 {
00036     this->height = height;
00037     this->width = width;
00038 }
00039 
00040 
00042 Rule::~Rule()
00043 {
00044 }
00045 
00046 
00048 
00051 int Rule::getWidth()
00052 {
00053     return width;
00054 }
00055 
00056 
00058 
00061 int Rule::getHeight()
00062 {
00063     return height;
00064 }
00065 
00066 
00068 
00071 Grid Rule::getInitialGrid()
00072 {
00073     return initialGrid;
00074 }
00075 
00076 
00078 
00081 Grid Rule::getFinalGrid()
00082 {
00083     return finalGrid;
00084 }
00085 
00086 
00088 
00097 bool Rule::cellChanged(int x, int y, GridType layout)
00098 {
00099     int status;
00100     Grid *grid;
00101     
00102     switch (layout)
00103     {
00104         case nINITIAL:
00105         {
00106             status = initialGrid(x, y);
00107             grid = &initialGrid;
00108             break;
00109         }
00110         case nFINAL:
00111         {
00112             status = finalGrid(x, y);
00113             grid = &finalGrid;
00114             break;
00115         }
00116     }
00117     
00118     switch (status)
00119     {
00120         case nENABLED:
00121         {
00122             (*grid)(x, y) = nDISABLED;
00123             break;
00124         }
00125         case nDISABLED:
00126         {
00127             (*grid)(x, y) = nDONTCARE;
00128             break;
00129         }
00130         case nDONTCARE:
00131         {
00132             (*grid)(x, y) = nENABLED;
00133             break;
00134         }
00135     }
00136     
00137     return true;
00138 }
00139 
00140 
00142 
00145 void Rule::setInitialGrid(Grid newGrid)
00146 {
00147     initialGrid.init(newGrid);    
00148 }
00149 
00150 
00152 
00155 void Rule::setFinalGrid(Grid newGrid)
00156 {
00157     finalGrid.init(newGrid);
00158 }
00159 
00160 
00162 
00169 bool Rule::cellChanged(int x, int y, int status, GridType layout)
00170 {
00171     Grid *grid;
00172     switch (layout)
00173     {
00174         case nINITIAL:
00175         {
00176             grid = &initialGrid;
00177             break;
00178         }
00179         case nFINAL:
00180         {
00181             grid = &finalGrid;
00182             break;
00183         }
00184     }
00185     (*grid)(x, y) = status;
00186     
00187     return true;
00188 }
00189 
00190 
00192 
00197 bool Rule::equals(Rule *rule)
00198 {
00199     if (getHeight() != rule->getHeight() || getWidth() != rule->getWidth())
00200     {
00201         return false;
00202     }
00203     Grid ruleInitialGrid = rule->getInitialGrid();
00204     Grid ruleFinalGrid = rule->getFinalGrid();
00205     for (int i = 0; i < getWidth(); i++)
00206     {
00207         for (int j = 0; j < getHeight(); j++)
00208         {
00209             if ((initialGrid(i, j) != ruleInitialGrid(i, j)) || (finalGrid(i, j) != ruleFinalGrid(i, j)))
00210             {
00211                 return false;
00212             }
00213         }
00214     }
00215     return true;
00216 }
00217 
00218 
00220 
00223 bool Rule::sameInitialFinal()
00224 {
00225     for (int i = 0; i < getWidth(); i++)
00226     {
00227         for (int j = 0; j < getHeight(); j++)
00228         {
00229             if ((initialGrid(i, j) != finalGrid(i, j)))
00230             {
00231                 return false;
00232             }
00233         }
00234     }
00235     return true;
00236 }
00237 
00238 
00240 
00246 bool Rule::valid()
00247 {
00248     for (int i = 0; i < getWidth(); i++)
00249     {
00250         for (int j = 0; j < getHeight(); j++)
00251         {
00252             if ((initialGrid(i, j) == nDONTCARE && (finalGrid(i, j) == nENABLED || finalGrid(i, j) == nDISABLED)))
00253             {
00254                 return false;
00255             }
00256         }
00257     }
00258     return true;
00259 }
00260 
00261 
00263 
00266 matrix Rule::getInitialMatrix()
00267 {
00268 return initialGrid.getCells();
00269 }
00270 
00271 
00273 
00276 matrix Rule::getFinalMatrix()
00277 {
00278     return finalGrid.getCells();
00279 }
00280 
00281 
00283 
00288 bool Rule::operator ==(Rule rule)
00289 {
00290     return ((getWidth() == rule.getWidth()) && 
00291             (getHeight() == rule.getHeight()) && 
00292             (initialGrid == rule.getInitialGrid()) && 
00293             (finalGrid == rule.getFinalGrid()));
00294 }

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