Adding Labyrinth3D files
[IRC.git] / Robust / src / Benchmarks / Java-Single / Labyrinth3D / Original / Normal_Java / Grid.java
1 /* =============================================================================
2  *
3  * grid.java
4  *
5  * =============================================================================
6  *
7  * Copyright (C) Stanford University, 2006.  All Rights Reserved.
8  * Author: Chi Cao Minh
9  *
10  * =============================================================================
11  *
12  * For the license of bayes/sort.h and bayes/sort.c, please see the header
13  * of the files.
14  * 
15  * ------------------------------------------------------------------------
16  * 
17  * For the license of kmeans, please see kmeans/LICENSE.kmeans
18  * 
19  * ------------------------------------------------------------------------
20  * 
21  * For the license of ssca2, please see ssca2/COPYRIGHT
22  * 
23  * ------------------------------------------------------------------------
24  * 
25  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
26  * header of the files.
27  * 
28  * ------------------------------------------------------------------------
29  * 
30  * For the license of lib/rbtree.h and lib/rbtree.c, please see
31  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
32  * 
33  * ------------------------------------------------------------------------
34  * 
35  * Unless otherwise noted, the following license applies to STAMP files:
36  * 
37  * Copyright (c) 2007, Stanford University
38  * All rights reserved.
39  * 
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions are
42  * met:
43  * 
44  *     * Redistributions of source code must retain the above copyright
45  *       notice, this list of conditions and the following disclaimer.
46  * 
47  *     * Redistributions in binary form must reproduce the above copyright
48  *       notice, this list of conditions and the following disclaimer in
49  *       the documentation and/or other materials provided with the
50  *       distribution.
51  * 
52  *     * Neither the name of Stanford University nor the names of its
53  *       contributors may be used to endorse or promote products derived
54  *       from this software without specific prior written permission.
55  * 
56  * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
57  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
66  * THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  * =============================================================================
69  */
70
71 import java.lang.Long;
72
73 public class Grid {
74     public int width;
75     public int height;
76     public int depth;
77     public int[][][] points_unaligned;
78     
79     public static int GRID_POINT_FULL;
80     public static int GRID_POINT_EMPTY;
81
82     public Grid() 
83     {
84         GRID_POINT_FULL = -2;
85         GRID_POINT_EMPTY = -1;
86     }
87     
88 /* =============================================================================
89  * grid_alloc
90  * =============================================================================
91     grid_t* grid_alloc (long width, long height, long depth);
92
93     well... need to implement
94     got stuck
95 */
96   public static Grid alloc(int width,int height,int depth) {
97     Grid grid = new Grid();
98     
99     grid.width = width;
100     grid.height = height;
101     grid.depth = depth;
102     
103     int[][][] points_unaligned = new int[width][height][depth];
104     
105     
106     for(int i=0;i<width;i++)
107       for(int j=0;j<height;j++)
108         for(int k=0;k<depth;k++)
109           points_unaligned[i][j][k]= GRID_POINT_EMPTY;
110     
111     grid.points_unaligned = points_unaligned;
112     
113     return grid;         
114   }
115
116   public static Grid scratchalloc(int width,int height,int depth) {
117     Grid grid = new Grid();
118     grid.width = width;
119     grid.height = height;
120     grid.depth = depth;
121     int[][][] points_unaligned = new int[width][height][depth];
122     grid.points_unaligned = points_unaligned;
123     return grid;         
124   }
125
126
127
128 /* =============================================================================
129  * grid_copy
130  * =============================================================================
131     void grid_copy (grid_t* dstGridPtr, grid_t* srcGridPtr);
132  */
133   public static void copy(Grid dstGridPtr,Grid srcGridPtr) {
134     if((srcGridPtr.width == dstGridPtr.width) ||
135        (srcGridPtr.height == dstGridPtr.height) ||
136        (srcGridPtr.depth == dstGridPtr.depth)) {   
137       deepArrayCopy(dstGridPtr.points_unaligned, srcGridPtr.points_unaligned);
138     }      
139   }
140  
141   //Array copy function added to replace System.deepArrayCopy in original 
142   //assumes they are of the same size
143   private static void deepArrayCopy(int[][][] dst, int[][][] src)
144   {
145           for(int i = 0; i < src.length; i++)
146                   for(int j = 0; j < src[i].length; j++)
147                           for(int k = 0; k < src[i][j].length; k++)
148                                   dst[i][j][k] = src[i][j][k];  
149   }
150
151 /* =============================================================================
152  * grid_isPointValid
153  * =============================================================================
154  bool_t grid_isPointValid (grid_t* gridPtr, long x, long y, long z);
155  */
156   public boolean isPointValid(int x,int y,int z) {
157     return x>=0 && x< width && y>=0 && y<height && z>=0 && z<depth;
158   }
159
160
161   public int getPoint(int x,int y,int z) {
162     return this.points_unaligned[x][y][z];
163   }
164
165
166 /* =============================================================================
167  * grid_isPointEmpty
168  * =============================================================================
169  bool_t grid_isPointEmpty (grid_t* gridPtr, long x, long y, long z); {
170  */
171
172   public boolean isPointEmpty(int x,int y,int z) {
173     return points_unaligned[x][y][z]==GRID_POINT_EMPTY;
174   }
175
176
177
178 /* =============================================================================
179  * grid_isPointFull
180  * =============================================================================
181  bool_t grid_isPointFull (grid_t* gridPtr, long x, long y, long z);
182  */
183   public boolean isPointFull(int x,int y,int z) {
184     return points_unaligned[x][y][z]==GRID_POINT_FULL;
185   }
186
187
188 /* =============================================================================
189  * grid_setPoint
190  * =============================================================================
191  void grid_setPoint (grid_t* gridPtr, long x, long y, long z, long value);
192  */
193   public void setPoint(int x,int y,int z,int value) {
194     points_unaligned[x][y][z] = value;
195   }
196
197
198 /* =============================================================================
199  * grid_addPath
200  * =============================================================================
201  
202 void grid_addPath (grid_t* gridPtr, vector_t* pointVectorPtr);
203 */
204   public void addPath(Vector_t pointVectorPtr) {
205     int i;
206     int n = pointVectorPtr.vector_getSize();
207     
208     for(i = 0; i < n; i++) {
209       Coordinate coordinatePtr = (Coordinate)pointVectorPtr.vector_at(i);
210       int x = coordinatePtr.x;
211       int y = coordinatePtr.y;
212       int z = coordinatePtr.z;
213
214       points_unaligned[x][y][z]=GRID_POINT_FULL;
215     }
216   }
217
218   public boolean TM_addPath(Vector_t pointVectorPtr) {
219     int i;
220     int n = pointVectorPtr.vector_getSize();
221
222     int height = this.height;
223     int width = this.width;
224     int area = height * width;
225     boolean dowrites=true;
226     for(i = 1; i < (n-1); i++) {
227       int gridPointIndex = ((Integer)(pointVectorPtr.vector_at(i))).intValue();
228       int z = gridPointIndex / area;
229       int index2d = gridPointIndex % area;
230       int y = index2d / width;
231       int x = index2d % width;        
232       if (points_unaligned[x][y][z] != GRID_POINT_EMPTY) {
233         dowrites=false;
234       }
235     }
236
237     for(i = 1; i < (n-1); i++) {
238       int gridPointIndex = ((Integer)(pointVectorPtr.vector_at(i))).intValue();
239       int z = gridPointIndex / area;
240       int index2d = gridPointIndex % area;
241       int y = index2d / width;
242       int x = index2d % width;
243       int[] array=points_unaligned[x][y];
244       if (dowrites) array[z] = GRID_POINT_FULL;
245     }
246     return !dowrites;
247   }
248
249   public int getPointIndex(int x,int y,int z) {
250     return ((z * height) + y) * width + x;
251   }
252
253   
254   public void print() {
255     int width  = this.width;
256     int height = this.height;
257     int depth  = this.depth;
258
259     for (int z = 0; z < depth; z++) {
260       System.out.println("[z ="+z+"]");
261       for (int x = 0; x < width; x++) {
262         for (int y = 0; y < height; y++) {
263           String str=String.valueOf(points_unaligned[x][y][z]);
264           for(int sp=0; sp<(4-str.length());sp++)
265             System.out.print(" ");
266           System.out.print(str);
267         }
268         System.out.println("");
269       }
270       System.out.println("");
271     }
272   }
273 }
274
275
276 /* =============================================================================
277  *
278  * End of grid.c
279  *
280  * =============================================================================
281  */