00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00096 void getComponents(Set<Cell*>& c);
00097
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
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
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
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
00154 template <class X>
00155 void CellSpace<X>::getComponents(Set<Cell*>& c)
00156 {
00157
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
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
00180 if (event.x >= 0 && event.x < w &&
00181 event.y >= 0 && event.y < h &&
00182 event.z >= 0 && event.z < d)
00183 {
00184
00185 target = space[event.x][event.y][event.z];
00186 }
00187 else
00188 {
00189
00190 target = this;
00191 }
00192
00193 if (target != NULL)
00194 {
00195
00196 Event<CellEvent<X> > io(target,event);
00197 r.insert(io);
00198 }
00199 }
00200
00201 }
00202
00203 #endif