adevs
adevs_cellspace.h
1 
31 #ifndef __adevs_cellspace_h_
32 #define __adevs_cellspace_h_
33 #include "adevs.h"
34 #include <cstdlib>
35 
36 namespace adevs
37 {
38 
44 template <class X> class CellEvent
45 {
46  public:
48  CellEvent(){ x = y = z = 0; }
50  CellEvent(const CellEvent<X>& src):
51  x(src.x),y(src.y),z(src.z),value(src.value){}
53  const CellEvent& operator=(const CellEvent<X>& src)
54  {
55  x = src.x; y = src.y; z = src.z; value = src.value;
56  return *this;
57  }
59  long int x;
61  long int y;
63  long int z;
65  X value;
66 };
67 
79 template <class X, class T = double> class CellSpace: public Network<CellEvent<X>,T>
80 {
81  public:
83  typedef Devs<CellEvent<X>,T> Cell;
85  CellSpace(long int width, long int height = 1, long int depth = 1);
87  void add(Cell* model, long int x, long int y = 0, long int z = 0)
88  {
89  space[x][y][z] = model;
90  model->setParent(this);
91  }
93  const Cell* getModel(long int x, long int y = 0, long int z = 0) const
94  {
95  return space[x][y][z];
96  }
98  Cell* getModel(long int x, long int y = 0, long int z = 0)
99  {
100  return space[x][y][z];
101  }
103  long int getWidth() const { return w; }
105  long int getHeight() const { return h; }
107  long int getDepth() const { return d; }
109  void getComponents(Set<Cell*>& c);
111  void route(const CellEvent<X>& event, Cell* model,
112  Bag<Event<CellEvent<X>,T> >& r);
114  ~CellSpace();
115  private:
116  long int w, h, d;
117  Cell**** space;
118 };
119 
120 // Implementation of constructor
121 template <class X, class T>
122 CellSpace<X,T>::CellSpace(long int width, long int height, long int depth):
123 Network<CellEvent<X>,T>()
124 {
125  w = width;
126  h = height;
127  d = depth;
128  // Allocate space for the cells and set the entries to NULL
129  space = new Cell***[w];
130  for (long int x = 0; x < w; x++)
131  {
132  space[x] = new Cell**[h];
133  for (long int y = 0; y < h; y++)
134  {
135  space[x][y] = new Cell*[h];
136  for (long int z = 0; z < d; z++)
137  {
138  space[x][y][z] = NULL;
139  }
140  }
141  }
142 }
143 
144 // Implementation of destructor
145 template <class X, class T>
147 {
148  for (long int x = 0; x < w; x++)
149  {
150  for (long int y = 0; y < h; y++)
151  {
152  for (long int z = 0; z < d; z++)
153  {
154  if (space[x][y][z] != NULL)
155  {
156  delete space[x][y][z];
157  }
158  }
159  delete [] space[x][y];
160  }
161  delete [] space[x];
162  }
163  delete [] space;
164 }
165 
166 // Implementation of the getComponents() method
167 template <class X, class T>
169 {
170  // Add all non-null entries to the set c
171  for (long int x = 0; x < w; x++)
172  {
173  for (long int y = 0; y < h; y++)
174  {
175  for (long int z = 0; z < d; z++)
176  {
177  if (space[x][y][z] != NULL)
178  {
179  c.insert(space[x][y][z]);
180  }
181  }
182  }
183  }
184 }
185 
186 // Event routing function for the net_exec
187 template <class X, class T>
189 const CellEvent<X>& event, Cell* model, Bag<Event<CellEvent<X>,T> >& r)
190 {
191  Cell* target = NULL;
192  // If the target cell is inside of the cellspace
193  if (event.x >= 0 && event.x < w && // check x dimension
194  event.y >= 0 && event.y < h && // check y dimension
195  event.z >= 0 && event.z < d) // check z dimension
196  {
197  // Get the interior target
198  target = space[event.x][event.y][event.z];
199  }
200  else
201  {
202  // Otherwise, the event becomes an external output from the cellspace
203  target = this;
204  }
205  // If the target exists
206  if (target != NULL)
207  {
208  // Add an appropriate event to the receiver bag
209  Event<CellEvent<X> > io(target,event);
210  r.insert(io);
211  }
212 }
213 
214 } // end of namespace
215 
216 #endif
long int getWidth() const
Get the width of the CellSpace.
Definition: adevs_cellspace.h:103
long int z
The z coordinate of the event target.
Definition: adevs_cellspace.h:63
void getComponents(Set< Cell * > &c)
Get the model's set of components.
Definition: adevs_cellspace.h:168
Devs< CellEvent< X >, T > Cell
A component model in the CellSpace.
Definition: adevs_cellspace.h:83
Definition: adevs_set.h:42
void add(Cell *model, long int x, long int y=0, long int z=0)
Insert a model at the x,y,z position.
Definition: adevs_cellspace.h:87
long int getHeight() const
Get the height of the CellSpace.
Definition: adevs_cellspace.h:105
long int x
The x coordinate of the event target.
Definition: adevs_cellspace.h:59
const CellEvent & operator=(const CellEvent< X > &src)
Assignment operator.
Definition: adevs_cellspace.h:53
Definition: adevs_models.h:46
void setParent(Network< X, T > *parent)
Definition: adevs_models.h:96
Definition: adevs_cellspace.h:79
void route(const CellEvent< X > &event, Cell *model, Bag< Event< CellEvent< X >, T > > &r)
Route events within the Cellspace.
Definition: adevs_cellspace.h:188
~CellSpace()
Destructor; this destroys the components as well.
Definition: adevs_cellspace.h:146
X value
The event value.
Definition: adevs_cellspace.h:65
Cell * getModel(long int x, long int y=0, long int z=0)
Get a mutable version of the model at x,y,z.
Definition: adevs_cellspace.h:98
CellSpace(long int width, long int height=1, long int depth=1)
Create an Width x Height x Depth CellSpace with NULL entries in the cell locations.
Definition: adevs_cellspace.h:122
Definition: adevs_cellspace.h:44
long int getDepth() const
Get the depth of the CellSpace.
Definition: adevs_cellspace.h:107
long int y
The y coordinate of the event target.
Definition: adevs_cellspace.h:61
CellEvent()
Default constructor. Sets x = y = z = 0.
Definition: adevs_cellspace.h:48
Definition: adevs_models.h:63
const Cell * getModel(long int x, long int y=0, long int z=0) const
Get the model at location x,y,z.
Definition: adevs_cellspace.h:93
Definition: adevs_models.h:142
CellEvent(const CellEvent< X > &src)
Copy constructor.
Definition: adevs_cellspace.h:50
Definition: adevs_bag.h:45