00001
00031 #ifndef __adevs_cellspace_h_
00032 #define __adevs_cellspace_h_
00033 #include "adevs.h"
00034 #include <cstdlib>
00035
00036 namespace adevs
00037 {
00038
00044 template <class X> class CellEvent
00045 {
00046 public:
00048 CellEvent(){ x = y = z = 0; }
00050 CellEvent(const CellEvent<X>& src):
00051 x(src.x),y(src.y),z(src.z),value(src.value){}
00053 const CellEvent& operator=(const CellEvent<X>& src)
00054 {
00055 x = src.x; y = src.y; z = src.z; value = src.value;
00056 return *this;
00057 }
00059 long int x;
00061 long int y;
00063 long int z;
00065 X value;
00066 };
00067
00079 template <class X, class T = double> class CellSpace: public Network<CellEvent<X>,T>
00080 {
00081 public:
00083 typedef Devs<CellEvent<X>,T> Cell;
00085 CellSpace(long int width, long int height = 1, long int depth = 1);
00087 void add(Cell* model, long int x, long int y = 0, long int z = 0)
00088 {
00089 space[x][y][z] = model;
00090 model->setParent(this);
00091 }
00093 const Cell* getModel(long int x, long int y = 0, long int z = 0) const
00094 {
00095 return space[x][y][z];
00096 }
00098 Cell* getModel(long int x, long int y = 0, long int z = 0)
00099 {
00100 return space[x][y][z];
00101 }
00103 long int getWidth() const { return w; }
00105 long int getHeight() const { return h; }
00107 long int getDepth() const { return d; }
00109 void getComponents(Set<Cell*>& c);
00111 void route(const CellEvent<X>& event, Cell* model,
00112 Bag<Event<CellEvent<X>,T> >& r);
00114 ~CellSpace();
00115 private:
00116 long int w, h, d;
00117 Cell**** space;
00118 };
00119
00120
00121 template <class X, class T>
00122 CellSpace<X,T>::CellSpace(long int width, long int height, long int depth):
00123 Network<CellEvent<X>,T>()
00124 {
00125 w = width;
00126 h = height;
00127 d = depth;
00128
00129 space = new Cell***[w];
00130 for (long int x = 0; x < w; x++)
00131 {
00132 space[x] = new Cell**[h];
00133 for (long int y = 0; y < h; y++)
00134 {
00135 space[x][y] = new Cell*[h];
00136 for (long int z = 0; z < d; z++)
00137 {
00138 space[x][y][z] = NULL;
00139 }
00140 }
00141 }
00142 }
00143
00144
00145 template <class X, class T>
00146 CellSpace<X,T>::~CellSpace()
00147 {
00148 for (long int x = 0; x < w; x++)
00149 {
00150 for (long int y = 0; y < h; y++)
00151 {
00152 for (long int z = 0; z < d; z++)
00153 {
00154 if (space[x][y][z] != NULL)
00155 {
00156 delete space[x][y][z];
00157 }
00158 }
00159 delete [] space[x][y];
00160 }
00161 delete [] space[x];
00162 }
00163 delete [] space;
00164 }
00165
00166
00167 template <class X, class T>
00168 void CellSpace<X,T>::getComponents(Set<Cell*>& c)
00169 {
00170
00171 for (long int x = 0; x < w; x++)
00172 {
00173 for (long int y = 0; y < h; y++)
00174 {
00175 for (long int z = 0; z < d; z++)
00176 {
00177 if (space[x][y][z] != NULL)
00178 {
00179 c.insert(space[x][y][z]);
00180 }
00181 }
00182 }
00183 }
00184 }
00185
00186
00187 template <class X, class T>
00188 void CellSpace<X,T>::route(
00189 const CellEvent<X>& event, Cell* model, Bag<Event<CellEvent<X>,T> >& r)
00190 {
00191 Cell* target = NULL;
00192
00193 if (event.x >= 0 && event.x < w &&
00194 event.y >= 0 && event.y < h &&
00195 event.z >= 0 && event.z < d)
00196 {
00197
00198 target = space[event.x][event.y][event.z];
00199 }
00200 else
00201 {
00202
00203 target = this;
00204 }
00205
00206 if (target != NULL)
00207 {
00208
00209 Event<CellEvent<X> > io(target,event);
00210 r.insert(io);
00211 }
00212 }
00213
00214 }
00215
00216 #endif