Main Page | Class Hierarchy | Class List | File List | Class Members

/home/nutarojj/Desktop/adevs/include/adevs_cellspace.h

00001 /***************
00002 Copyright (C) 2000-2006 by James Nutaro
00003 
00004 This library is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU Lesser General Public
00006 License as published by the Free Software Foundation; either
00007 version 2 of the License, or (at your option) any later version.
00008 
00009 This library is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 Lesser General Public License for more details.
00013 
00014 You should have received a copy of the GNU Lesser General Public
00015 License along with this library; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 Bugs, comments, and questions can be sent to nutaro@gmail.com
00019 ***************/
00020 #ifndef __adevs_cellspace_h_
00021 #define __adevs_cellspace_h_
00022 #include "adevs.h"
00023 #include <cstdlib>
00024 
00025 namespace adevs
00026 {
00027 
00033 template <class X> class CellEvent
00034 {
00035         public:
00037                 CellEvent(){ x = y = z = 0; }
00039                 CellEvent(const CellEvent<X>& src):
00040                 x(src.x),y(src.y),z(src.z),value(src.value){}
00042                 const CellEvent& operator=(const CellEvent<X>& src)
00043                 {
00044                         x = src.x; y = src.y; z = src.z; value = src.value;
00045                         return *this;
00046                 }
00048                 long int x;
00050                 long int y;
00052                 long int z;
00054                 X value;
00055 };
00056 
00066 template <class X> class CellSpace: public Network<CellEvent<X> >
00067 {
00068         public:
00070                 typedef Devs<CellEvent<X> > Cell;
00072                 CellSpace(long int width, long int height = 1, long int depth = 1);
00074                 void add(Cell* model, long int x, long int y = 0, long int z = 0) 
00075                 {
00076                         space[x][y][z] = model;
00077                         model->setParent(this);
00078                 }
00080                 const Cell* getModel(long int x, long int y = 0, long int z = 0) const
00081                 {
00082                         return space[x][y][z];
00083                 }
00085                 Cell* getModel(long int x, long int y = 0, long int z = 0)
00086                 {
00087                         return space[x][y][z];
00088                 }
00090                 long int getWidth() const { return w; }
00092                 long int getHeight() const { return h; }
00094                 long int getDepth() const { return d; }
00095                 // Get the model component set
00096                 void getComponents(Set<Cell*>& c);
00097                 // Event routing method
00098                 void route(const CellEvent<X>& event, Cell* model, 
00099                 Bag<Event<CellEvent<X> > >& r);
00101                 ~CellSpace();
00102         private:        
00103                 long int w, h, d;
00104                 Cell**** space;
00105 };
00106 
00107 // Implementation of constructor
00108 template <class X>
00109 CellSpace<X>::CellSpace(long int width, long int height, long int depth):
00110 Network<CellEvent<X> >()
00111 {
00112         w = width;
00113         h = height;
00114         d = depth;
00115         // Allocate space for the cells and set the entries to NULL
00116         space = new Cell***[w];
00117         for (long int x = 0; x < w; x++)
00118         {
00119                 space[x] = new Cell**[h];
00120                 for (long int y = 0; y < h; y++)
00121                 {
00122                         space[x][y] = new Cell*[h];
00123                         for (long int z = 0; z < d; z++)
00124                         {
00125                                 space[x][y][z] = NULL;
00126                         }
00127                 }
00128         }
00129 }
00130 
00131 // Implementation of destructor
00132 template <class X>
00133 CellSpace<X>::~CellSpace()
00134 {
00135         for (long int x = 0; x < w; x++)
00136         {
00137                 for (long int y = 0; y < h; y++)
00138                 {
00139                         for (long int z = 0; z < d; z++)
00140                         {
00141                                 if (space[x][y][z] != NULL)
00142                                 {
00143                                         delete space[x][y][z];
00144                                 }
00145                         }
00146                         delete [] space[x][y];
00147                 }
00148                 delete [] space[x];
00149         }
00150         delete [] space;
00151 }
00152 
00153 // Implementation of the getComponents() method
00154 template <class X>
00155 void CellSpace<X>::getComponents(Set<Cell*>& c)
00156 {
00157         // Add all non-null entries to the set c
00158         for (long int x = 0; x < w; x++)
00159         {
00160                 for (long int y = 0; y < h; y++)
00161                 {
00162                         for (long int z = 0; z < d; z++)
00163                         {
00164                                 if (space[x][y][z] != NULL)
00165                                 {
00166                                         c.insert(space[x][y][z]);
00167                                 }
00168                         }
00169                 }
00170         }
00171 }
00172 
00173 // Event routing function for the net_exec
00174 template <class X>
00175 void CellSpace<X>::route(
00176 const CellEvent<X>& event, Cell* model, Bag<Event<CellEvent<X> > >& r)
00177 {
00178         Cell* target = NULL;
00179         // If the target cell is inside of the cellspace
00180         if (event.x >= 0 && event.x < w &&  // check x dimension
00181         event.y >= 0 && event.y < h && // check y dimension
00182         event.z >= 0 && event.z < d) // check z dimension
00183         {
00184                 // Get the interior target
00185                 target = space[event.x][event.y][event.z];
00186         }
00187         else
00188         {
00189                 // Otherwise, the event becomes an external output from the cellspace
00190                 target = this;
00191         }
00192         // If the target exists
00193         if (target != NULL)
00194         {
00195                 // Add an appropriate event to the receiver bag
00196                 Event<CellEvent<X> > io(target,event);
00197                 r.insert(io);
00198         }
00199 }
00200 
00201 } // end of namespace
00202 
00203 #endif

Generated on Mon Jun 1 09:53:42 2009 for adevs by  doxygen 1.3.9.1