adevs
Main Page
Namespaces
Classes
Files
File List
include
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>
146
CellSpace<X,T>::~CellSpace
()
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>
168
void
CellSpace<X,T>::getComponents
(
Set<Cell*>
& c)
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>
188
void
CellSpace<X,T>::route
(
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
Generated by
1.8.3.1