Under Original/Normal_Java/ one would find the original Labyrinth project ported...
[IRC.git] / Robust / src / Benchmarks / Java-Single / Labyrinth / mlp / rBlocked / Grid.java
1
2
3 //#define GRID_POINT_FULL -2
4 //#define GRID_POINT_EMPTY -1
5
6
7
8 public class Grid 
9 {
10         //TODO change these back into #defines up top
11         private static int GRID_POINT_FULL;
12         private static int GRID_POINT_EMPTY;
13         
14         
15     public int width;
16     public int height;
17     public int depth;
18     public int[][] points_unaligned;
19
20     public Grid() 
21     {
22         //Kept from #defines
23         GRID_POINT_FULL = -2;
24         GRID_POINT_EMPTY =-1;
25     }
26
27     
28 /* =============================================================================
29  * grid_alloc
30  * =============================================================================
31     grid_t* grid_alloc (long width, long height, long depth);
32
33     well... need to implement
34     got stuck
35  */
36     public Grid alloc(int width,int height,int depth) {
37         Grid grid = new Grid();
38
39         if(grid != null) {
40
41             grid.width = width;
42             grid.height = height;
43             grid.depth = depth;
44
45             int n = width * height * depth;
46
47             // long* points_unaligned = (long*) malloc(n*sizeof(long) + CACHE_LINE_SIZE);
48             int[][] points_unaligned = new int[n][1];
49
50             grid.points_unaligned = points_unaligned;
51             
52             for(int i=0;i<n;i++) 
53                 grid.points_unaligned[i][0] = grid.GRID_POINT_EMPTY;            
54         }
55                     
56         return grid;         
57     }
58
59
60
61 /* =============================================================================
62  * TMgrid_alloc
63  * =============================================================================
64  */
65 //grid_t* Pgrid_alloc (long width, long height, long depth);
66
67
68 /* =============================================================================
69  * grid_free
70  * =============================================================================
71     void grid_free (grid_t* gridPtr);
72 */
73     
74     //changed to void
75   public static void free(Grid gridPtr) {
76     gridPtr = null;
77   }
78
79
80 /* =============================================================================
81  * Pgrid_free
82  * =============================================================================
83   void Pgrid_free (grid_t* gridPtr);
84   */
85     
86
87
88 /* =============================================================================
89  * grid_copy
90  * =============================================================================
91     void grid_copy (grid_t* dstGridPtr, grid_t* srcGridPtr);
92  */
93     public void copy(Grid dstGridPtr, Grid srcGridPtr)
94     {
95         if((srcGridPtr.width == dstGridPtr.width) ||
96            (srcGridPtr.height == dstGridPtr.height) ||
97            (srcGridPtr.depth == dstGridPtr.depth))
98         {
99         int n = srcGridPtr.width * srcGridPtr.height * srcGridPtr.depth;
100
101         for(int i=0;i<n;i++)
102             dstGridPtr.points_unaligned[i][0] = 
103               srcGridPtr.points_unaligned[i][0];
104         }
105         else
106         {
107                 System.out.println("Grid copy failed");
108         }
109     }
110
111
112
113 /* =============================================================================
114  * grid_isPointValid
115  * =============================================================================
116  bool_t grid_isPointValid (grid_t* gridPtr, long x, long y, long z);
117  */
118     public boolean isPointValid(int x,int y,int z)
119     {
120         if(x < 0 || x >= width || 
121            y < 0 || y >= height ||
122            z < 0 || z >= depth)
123         {
124             return false;
125         }
126
127         return true;
128     }
129
130
131 /* =============================================================================
132  * grid_getPointRef
133  * =============================================================================
134 long* grid_getPointRef (grid_t* gridPtr, long x, long y, long z);
135
136     it is returning the index of the point
137 */
138     public int getPointIndex(int x,int y,int z)
139     {
140         return ((z * height) + y) * width + x;
141     }
142
143
144 /* =============================================================================
145  * grid_getPointIndices
146  * =============================================================================
147  void grid_getPointIndices (grid_t* gridPtr,
148                       long* gridPointPtr, long* xPtr, long* yPtr, long* zPtr);
149  */
150     public void getPointIndices(int gridPointIndex,int[] xPtr, int[] yPtr,int[] zPtr)
151     {
152         int height = this.height;
153         int width = this.width;
154         int area = height * width;
155         int index3d = (gridPointIndex);
156         zPtr[0] = index3d / area;
157         int index2d = index3d % area;
158         yPtr[0] = index2d / width;
159         xPtr[0] = index2d % width;        
160     }
161
162
163 /* =============================================================================
164  * grid_getPoint
165  * =============================================================================
166  long grid_getPoint (grid_t* gridPtr, long x, long y, long z);
167  */
168     public int getPoint(int x,int y,int z)
169     {
170         return this.points_unaligned[getPointIndex(x,y,z)][0];
171     }
172
173     public int getPoint(int index)
174     {
175         return this.points_unaligned[index][0];
176     }
177
178
179     
180     
181     
182     //added to support redos
183     public Coordinate getCoordinate(int index)
184     {
185         //((z * height) + y) * width + x;
186         Coordinate c = new Coordinate();
187         int x = index % width;
188         int y = (index/width)%height;
189         int z = (index/width)/height;
190         
191         //TODO remove after debugging
192         if(index != this.getPoint(x, y, z))
193                 System.out.println("getCordinate() in Grid.java FAILS...");
194         
195         return c.alloc(x, y, z);
196     }
197     
198 /* =============================================================================
199  * grid_isPointEmpty
200  * =============================================================================
201  bool_t grid_isPointEmpty (grid_t* gridPtr, long x, long y, long z);
202  */
203     public boolean isPointEmpty(int x,int y,int z)
204     {
205         int value = getPoint(x,y,z);
206         return ((value == GRID_POINT_EMPTY) ? true:false);
207     }
208
209
210
211 /* =============================================================================
212  * grid_isPointFull
213  * =============================================================================
214  bool_t grid_isPointFull (grid_t* gridPtr, long x, long y, long z);
215  */
216     public boolean isPointFull(int x,int y,int z)
217     {
218         int value = getPoint(x,y,z);
219         return ((value == GRID_POINT_FULL) ? true : false);
220     }
221
222
223 /* =============================================================================
224  * grid_setPoint
225  * =============================================================================
226  void grid_setPoint (grid_t* gridPtr, long x, long y, long z, long value);
227  */
228     public void setPoint(int x,int y,int z,int value)
229     {
230         points_unaligned[getPointIndex(x,y,z)][0] = value;
231     }
232
233
234 /* =============================================================================
235  * grid_addPath
236  * =============================================================================
237  
238 void grid_addPath (grid_t* gridPtr, vector_t* pointVectorPtr);
239 */
240     
241     public void addPath(Vector_t pointVectorPtr)
242     {
243         int i;
244         int n = pointVectorPtr.vector_getSize();
245
246         for(i = 0; i < n; i++) {
247             Coordinate coordinatePtr = (Coordinate)pointVectorPtr.vector_at(i);
248             int x = coordinatePtr.x;
249             int y = coordinatePtr.y;
250             int z = coordinatePtr.z;
251
252 //            System.out.println("x = " + x + " y = " + y + " z = " + z);
253             setPoint(x,y,z,GRID_POINT_FULL);
254         }
255     }
256      
257     public void TM_addPath(Vector_t pointVectorPtr)
258     {
259         int i;
260         int n = pointVectorPtr.vector_getSize();
261
262         for(i = 0; i < n; i++) {
263             int gridPointIndex = ((Integer)(pointVectorPtr.vector_at(i))).intValue();
264             points_unaligned[gridPointIndex][0] = GRID_POINT_FULL;            
265         }
266     }
267
268 /* =============================================================================
269  * TMgrid_addPath
270  * =============================================================================
271  TM_CALLABLE
272 void
273 TMgrid_addPath (TM_ARGDECL  grid_t* gridPtr, vector_t* pointVectorPtr);
274 */
275
276
277 /* =============================================================================
278  * grid_print
279  * =============================================================================
280 void grid_print (grid_t* gridPtr);
281 */
282     public void print()
283     {
284         int z;
285
286         for(z = 0; z < depth; z++) {
287             System.out.println("[z = " + z + "]");
288             int x;
289             for(x = 0; x < width; x++) {
290                 int y;
291                 for(y = 0; y < height; y++) {
292                     System.out.print(points_unaligned[getPointIndex(x,y,z)][0] + " ");
293                 }
294                 System.out.println("");
295             }
296             System.out.println("");
297         }
298  
299     }
300 }
301
302 /* =============================================================================
303  *
304  * End of grid.c
305  *
306  * =============================================================================
307  */