--- /dev/null
+/* =============================================================================
+ *
+ * coordinate.java
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * =============================================================================
+ *
+ * For the license of bayes/sort.h and bayes/sort.c, please see the header
+ * of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of kmeans, please see kmeans/LICENSE.kmeans
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of ssca2, please see ssca2/COPYRIGHT
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
+ * header of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/rbtree.h and lib/rbtree.c, please see
+ * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
+ *
+ * ------------------------------------------------------------------------
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * =============================================================================
+ */
+
+
+
+
+import java.lang.Math;
+
+public class Coordinate {
+
+ public int x;
+ public int y;
+ public int z;
+
+
+ // coordiate_alloc will be constructor
+ // coordinate_t* coordinate_alloc(long x, long y, long z)
+ public Coordinate(int x,int y,int z) {
+ this.x = x;
+ this.y = y;
+ this.z = z;
+ }
+
+ public static Coordinate alloc(int x,int y,int z) {
+ Coordinate c = new Coordinate(x,y,z);
+
+ return c;
+ }
+
+
+ // deallocate memory
+ // may not need
+ // coordinate_free
+
+ /*========================================================
+ // coordinate_isEqual
+ ==========================================================*/
+ public static boolean isEqual(Coordinate a,Coordinate b)
+ {
+ if((a.x == b.x) && (a.y == b.y) && (a.z == b.z))
+ return true;
+
+ return false;
+ }
+
+ /*==========================================================
+ *
+ * getPairDistance
+ *
+ *========================================================*/
+ private static double getPairDistance(Pair p)
+ {
+ Coordinate a = (Coordinate)p.a;
+ Coordinate b = (Coordinate)p.b;
+ int dx = a.x - b.x;
+ int dy = a.y - b.y;
+ int dz = a.z - b.z;
+ int dx2 = dx* dx;
+ int dy2 = dy* dy;
+ int dz2 = dz* dz;
+
+ return Math.sqrt((double)(dx2+dy2+dz2));
+ }
+
+
+ /*================================================
+ // coordinat_ comparePair
+ * -- For sorting in list of source/destination pairs
+ * -- Route longer paths first so they are more likely to suceed
+
+ *================================================*/
+ public static int comparePair(final Object a,final Object b)
+ {
+ double aDistance = getPairDistance((Pair)a);
+ double bDistnace = getPairDistance((Pair)b);
+
+ if(aDistance < bDistance) {
+ return 1;
+ } else if(aDistance > bDistance) {
+ return -1;
+ }
+
+ return 0;
+ }
+
+ /*=======================================================
+ * coordinate_areAdjacent
+ *=======================================================*/
+
+ public static boolean areAdjacent(Coordinate a,Coordinate b)
+ {
+ int dx = a.x - b.x;
+ int dy = a.y - b.y;
+ int dz = a.z - b.z;
+ int dx2 = dx * dx;
+ int dy2 = dy * dy;
+ int dz2 = dz * dz;
+
+ return (((dx2 + dy2 + dz2) == 1) ? true : false);
+ }
+ }
+
+ /*=====================================================
+ *
+ * End of Coordinate
+ *
+ *====================================================*/
+
+
--- /dev/null
+/* =============================================================================
+ *
+ * grid.java
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * =============================================================================
+ *
+ * For the license of bayes/sort.h and bayes/sort.c, please see the header
+ * of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of kmeans, please see kmeans/LICENSE.kmeans
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of ssca2, please see ssca2/COPYRIGHT
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
+ * header of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/rbtree.h and lib/rbtree.c, please see
+ * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
+ *
+ * ------------------------------------------------------------------------
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * =============================================================================
+ */
+
+import java.lang.Long;
+
+#define CACHE_LINE_SIZE 8
+#define GRID_POINT_FULL -2
+#define GRID_POINT_EMPTY -1
+
+public class Grid {
+ public int width;
+ public int height;
+ public int depth;
+ public int points_index;
+ public int[][] points_unaligned;
+
+
+
+/* =============================================================================
+ * grid_alloc
+ * =============================================================================
+ grid_t* grid_alloc (long width, long height, long depth);
+
+ well... need to implement
+ got stuck
+ */
+ public static Grid alloc(int width,int height,int depth) {
+ Grid grid = new Grid();
+
+ if(grid) {
+
+ grid.width = width;
+ grid.height = height;
+ grid.depth = depth;
+
+ int n = width * height * depth;
+
+ // long* points_unaligned = (long*) malloc(n*sizeof(long) + CACHE_LINE_SIZE);
+ int size = n + CACHE_LINE_SIZE;
+ int[][] points_unaligned = new int[size][1];
+
+ grid.points_unaligned = points_unaligned;
+ grid.points_index = CACHE_LINE_SIZE; // not sure it is right..
+
+ for(int i=grid.points_index;i<n;i++)
+ grid.points_unaligned[i] = GRID_POINT_EMPTY;
+ }
+
+ return grid;
+ }
+
+
+
+/* =============================================================================
+ * TMgrid_alloc
+ * =============================================================================
+ */
+//grid_t* Pgrid_alloc (long width, long height, long depth);
+
+
+/* =============================================================================
+ * grid_free
+ * =============================================================================
+ void grid_free (grid_t* gridPtr);
+*/
+ public static free(Grid gridPtr)
+ {
+ gridPtr = null;
+ }
+
+
+/* =============================================================================
+ * Pgrid_free
+ * =============================================================================
+ void Pgrid_free (grid_t* gridPtr);
+ */
+
+
+
+/* =============================================================================
+ * grid_copy
+ * =============================================================================
+ void grid_copy (grid_t* dstGridPtr, grid_t* srcGridPtr);
+ */
+ public static void copy(Grid dstGridPtr,Grid srcGridPtr)
+ {
+ if((srcGridPtr.width == dstGridPtr.width) ||
+ (srcGridPtr.height == dstGridPtr.height) ||
+ (srcGridPtr.depth == dstGridPtr.depth))
+ {
+ System.err.println("Assert in Grid_Copy");
+ System.out.exit(1);
+ }
+
+
+
+ int n = srcGridPtr.width * srcGridPtr.height * srcGridPtr.depth;
+
+ for(int i=0;i<n;i++)
+ dstGridPtr.points_unaligned[points_index + i][0] = srcGridPtr.points_unaligned[points_index + i][0];
+ }
+
+
+
+/* =============================================================================
+ * grid_isPointValid
+ * =============================================================================
+ bool_t grid_isPointValid (grid_t* gridPtr, long x, long y, long z);
+ */
+ public boolean isPointValid(int x,int y,int z)
+ {
+ if(x < 0 || x >= width ||
+ y < 0 || y >= height ||
+ z < 0 || z >= depth)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+
+/* =============================================================================
+ * grid_getPointRef
+ * =============================================================================
+long* grid_getPointRef (grid_t* gridPtr, long x, long y, long z);
+
+ it is returning the index of the point
+*/
+ public int getPointIndex(int x,int y,int z)
+ {
+ return points_index + (((z * height) + y) * width + x);
+ }
+
+
+/* =============================================================================
+ * grid_getPointIndices
+ * =============================================================================
+ void grid_getPointIndices (grid_t* gridPtr,
+ long* gridPointPtr, long* xPtr, long* yPtr, long* zPtr);
+ */
+ public void getPointIndices(int gridPointIndex,int[] xPtr, int[] yPtr,int[] zPtr)
+ {
+ int height = this.heigt;
+ int width = this.width;
+ int area = height * width;
+ int index3d = (gridPointIndex - this.points_index);
+ zPtr[0] = index3d / area;
+ long index2d = index3d % area;
+ yPtr[0] = index2d / width;
+ xPtr[0] = index2d % width;
+ }
+
+
+/* =============================================================================
+ * grid_getPoint
+ * =============================================================================
+ long grid_getPoint (grid_t* gridPtr, long x, long y, long z);
+ */
+ public int getPoint(int x,int y,int z)
+ {
+ return this.points_unaligned[getPointIndex(x,y,z)][0];
+ }
+
+ public int getPoint(int index)
+ {
+ return this.points_unaligned[index][0];
+ }
+
+
+/* =============================================================================
+ * grid_isPointEmpty
+ * =============================================================================
+ bool_t grid_isPointEmpty (grid_t* gridPtr, long x, long y, long z);
+ */
+ public boolean isPointEmpty(int x,int y,int z)
+ {
+ int value = getPoint(x,y,z)[0];
+ return ((value == GRID_POINT_EMPTY) ? true:false);
+ }
+
+
+
+/* =============================================================================
+ * grid_isPointFull
+ * =============================================================================
+ bool_t grid_isPointFull (grid_t* gridPtr, long x, long y, long z);
+ */
+ public boolean isPointFull(int x,int y,int z)
+ {
+ int value = getPoint(x,y,z)[0];
+ return ((value == GRID_POINT_FULL) ? true : false);
+ }
+
+
+/* =============================================================================
+ * grid_setPoint
+ * =============================================================================
+ void grid_setPoint (grid_t* gridPtr, long x, long y, long z, long value);
+ */
+ public void setPoint(int x,int y,int z,int value)
+ {
+ points_unaligned[getPointIndex(x,y,z)][0] = value;
+ }
+
+ public void setPoint(int index,int value)
+ {
+ points_unaligned[index][0] = value;
+ }
+
+
+/* =============================================================================
+ * grid_addPath
+ * =============================================================================
+
+void grid_addPath (grid_t* gridPtr, vector_t* pointVectorPtr);
+*/
+ public void addPath(Vector_t pointVectorPtr)
+ {
+ int i;
+ int n = pointVectorPtr.getSize();
+
+ for(i = 1; i < (n-1); i++) {
+ Coordinate coordinatePtr = (Coordinate)pointVectorPtr.vector_at(i);
+ int x = coordinatePtr.x;
+ int y = coordinatePtr.y;
+ int z = coordinatePtr.z;
+ setPoint(x,y,z,GRID_POINT_FULL);
+ }
+ }
+
+
+/* =============================================================================
+ * TMgrid_addPath
+ * =============================================================================
+ TM_CALLABLE
+void
+TMgrid_addPath (TM_ARGDECL grid_t* gridPtr, vector_t* pointVectorPtr);
+*/
+
+
+/* =============================================================================
+ * grid_print
+ * =============================================================================
+void grid_print (grid_t* gridPtr);
+*/
+ public void print()
+ {
+ int z;
+
+ for(z=0;j<depth;z++) {
+ System.out.println("[z = " + z + "]");
+ int x;
+ for(x = 0; x < width; x++) {
+ int y;
+ for(y = 0; y < height; y++) {
+ System.out.println(points_unaligned[getPointIndex(x,y,z)]);
+ }
+ System.out.println();
+ }
+ System.out.println();
+ }
+
+ }
+}
+
+/* =============================================================================
+ *
+ * End of grid.c
+ *
+ * =============================================================================
+ */
--- /dev/null
+/* =============================================================================
+ *
+ * Labyrinth.java
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * =============================================================================
+ *
+ * For the license of bayes/sort.h and bayes/sort.c, please see the header
+ * of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of kmeans, please see kmeans/LICENSE.kmeans
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of ssca2, please see ssca2/COPYRIGHT
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
+ * header of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/rbtree.h and lib/rbtree.c, please see
+ * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
+ *
+ * ------------------------------------------------------------------------
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * =============================================================================
+ */
+
+public class Labyrinth extends Thread{
+
+ static String global_inputFile;
+ static boolean global_doPrint;
+ int numThread;
+ int bendCost;
+ int xCost;
+ int yCost;
+ int zCost;
+
+
+ // For threads
+ int threadID;
+ Router.Arg routerArg;
+
+ private void setDefaultParams() {
+
+ /* default values */
+ global_inputFile = null;
+ global_doPrint = false;
+ bendCost = 1;
+ xCost = 1;
+ yCost = 1;
+ zCost = 2;
+ numThread = 1;
+ }
+
+ private void parseArg(String[] argv) {
+ int i=0;
+ String arg;
+ boolean opterr = false;
+
+ setDefaultParams();
+
+ while (i < args.length && argv[i].startswith("-")) {
+ arg = argv[i++];
+
+ // check options
+ if(arg.equals("-b")) {
+ bendCost = Integer.parseInt(argv[i++]);
+ }
+ else if(arg.equals("-x")) {
+ xCost = Integer.parseInt(argv[i++]);
+ }
+ else if(arg.equals("-y")) {
+ yCost = Integer.parseInt(argv[i++]);
+ }
+ else if(arg.equals("-z")) {
+ zCost = Integer.parseInt(argv[i++]);
+ }
+ else if(arg.equals("-t")) {
+ numThread = Integer.parseInt(argv[i++]);
+ }
+ else if(arg.equals("-i")) {
+ global_inputFile = argv[i++];
+ }
+ else if(arg.equals("-p")) {
+ global_doPrint = true;
+ }
+ else {
+ System.err.println("Non-option argument: " + argv[i]);
+ opterr = true;
+ }
+ }
+
+ if(opterr) {
+ displayUsage();
+ System.out.exit(1);
+ }
+ }
+
+ public Labyrinth(String[] argv)
+ {
+ parseArg(argv);
+ }
+
+
+ public Labyrinth(int myID,Router.Arg rArg)
+ {
+ threadID = myID;
+ routerArg = rArg;
+ }
+
+ public void run() {
+ while(true) {
+ Barrier.enterBarrier();
+ Router.solve(routerArg);
+ Barrier.enterBarrier();
+ }
+ }
+
+ public static void main(String[] argv)
+ {
+ /*
+ * Initailization
+ */
+ Labyrinth labyrinth = new Labyrinth(argv);
+
+ Barrier.setBarrier(laybyrinth.numThread);
+
+ Maze mazePtr = Maze.alloc();
+
+ int numPathToRoute = mazePtr.read(global_inputFile);
+
+ Router routerPtr = Router.alloc(laybyrinth.xCost,laybyrinth.yCost,
+ laybyrinth.zCost,laybyrinth.bendCost);
+
+ List_t pathVectorListPtr = List_t.alloc(0); // list_t.alloc(null)
+
+ /*
+ * Run transactions
+ */
+ Solve_arg routerArg = new Solve_arg(routerPtr,mazePtr,pathVectorListPtr);
+
+
+ /* Create and start thread */
+ Layrinth[] lb = new Labyrinth[numThread];
+
+ for(int i = 1; i<numThread;i++) {
+ lb[i] = new Labyrinth(i,routerArg);
+ }
+
+ for(int i = 1; i<numThread;i++) {
+ ib[i].start();
+ }
+
+ /* End of Solve */
+
+ int numPathRouted = 0;
+ List_Iter it = new List_Iter();
+
+ it.reset(pathVectorListPtr);
+ while(it.hasNext(pathVectorListPtr)) {
+ Vector_t pathVectorPtr = (Vector_t)it.next(pathVectorListPtr);
+ numPathRouted += pathVectorPtr.getSize();
+ }
+
+ System.out.println("Paths routed = " + numPaathRouted);
+
+ boolean stats = mazePtr.checkPaths(pathVectorListPtr,global_doPrint);
+ if(!stats)
+ {
+ System.out.println("Verification not passed");
+
+ }
+ else
+ System.out.println("Verification passed.");
+ System.out.println("Finished");
+ }
+}
+
+/* =============================================================================
+ *
+ * End of labyrinth.c
+ *
+ * =============================================================================
+ */
--- /dev/null
+public class List_Iter {
+ List_Node itPtr;
+
+ /* =============================================================================
+ * list_iter_reset
+ * =============================================================================
+ void list_iter_reset (list_iter_t* itPtr, list_t* listPtr);
+ */
+ public List_Iter() {
+ itPtr = null;
+ }
+
+ public void reset(List_t listPtr)
+ {
+ itPtr = listPtr.head;
+ }
+
+ /* =============================================================================
+ * list_iter_hasNext
+ * =============================================================================
+ * bool_t list_iter_hasNext (list_iter_t* itPtr, list_t* listPtr);
+ */
+ public boolean hasNext(List_t listPtr) {
+ return (itPtr.nextPtr != null)? true : false;
+ }
+
+ /* =============================================================================
+ * list_iter_next
+ * =============================================================================
+ * void* list_iter_next (list_iter_t* itPtr, list_t* listPtr);
+ */
+ public Object next(List_t listPtr)
+ {
+ itPtr = itPtr.nextPtr;
+ }
+}
+
--- /dev/null
+
+public class List_Node {
+ Object dataPtr;
+ List_Node nextPtr;
+
+ public List_Node() {
+ dataPtr = null;
+ nextPtr = null;
+ }
+}
--- /dev/null
+/* =============================================================================
+ *
+ * List_t.java
+ * -- Sorted singly linked list
+ * -- Options: duplicate allowed
+ * (DLIST_NO_DUPLICATES) is no implemented yet (default: allow duplicates)
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * =============================================================================
+ *
+ * For the license of bayes/sort.h and bayes/sort.c, please see the header
+ * of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of kmeans, please see kmeans/LICENSE.kmeans
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of ssca2, please see ssca2/COPYRIGHT
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
+ * header of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/rbtree.h and lib/rbtree.c, please see
+ * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
+ *
+ * ------------------------------------------------------------------------
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * =============================================================================
+ */
+
+
+public class List_t {
+
+ public List_Node head;
+ boolean isCoordinate;
+ int size;
+
+ public List_t() {
+ head = new List_Node();
+ }
+
+
+ /* =======================================================================
+ * allocNode
+ * -- Returns null on failure
+ * =======================================================================
+ */
+ private List_Node allocNode(Object dataPtr)
+ {
+ List_Node nodePtr = new List_Node();
+
+ if(nodePtr == null) {
+ return null;
+ }
+
+ nodePtr.dataPtr = dataPtr;
+ nodePtr.nextPtr = null;
+
+ return nodePtr;
+ }
+
+
+/* =============================================================================
+ * list_alloc
+ * -- If NULL passed for 'compare' function, will compare data pointer addresses
+ * -- Returns NULL on failure
+ * =============================================================================
+ * list_t* list_alloc (long (*compare)(const void*, const void*));
+ */
+ public static List_t alloc(int flag)
+ {
+ List_t listPtr = new List_t();
+
+ if(listPtr == null) {
+ return null;
+ }
+
+ listPtr.head.dataPtr = null;
+ listPtr.nextPtr = null;
+ listPtr.size = 0;
+
+ isCoordinate = (flag==1)?true:false;
+
+ return listPtr;
+ }
+
+/* =============================================================================
+ * list_free
+ * -- If NULL passed for 'compare' function, will compare data pointer addresses
+ * -- Returns NULL on failure
+ * =============================================================================
+ * void list_free (list_t* listPtr);
+ */
+ public static void free(List listPtr)
+ {
+ listPtr = null;
+ }
+
+// privae freeList
+
+/* =============================================================================
+ * list_isEmpty
+ * -- Return TRUE if list is empty, else FALSE
+ * =============================================================================
+ * bool_t list_isEmpty (list_t* listPtr);
+ */
+ public boolean isEmpty()
+ {
+ return (head.nextPtr == null);
+ }
+
+/* =============================================================================
+ * list_getSize
+ * -- Returns size of list
+ * =============================================================================
+ * long list_getSize (list_t* listPtr);
+ */
+ public int getSize() {
+ return size;
+ }
+
+/* =============================================================================
+ * findPrevious
+ * =============================================================================
+ * void* list_find (list_t* listPtr, void* dataPtr);
+ */
+ private List_Node findPrevious(Object dataPtr)
+ {
+ List_Node prevPtr = head;
+ List_Node nodePtr = prevPtr.nextPtr;
+
+ for(; nodePtr != null; noePtr = nodePtr.nextPtr) {
+ if (compare(nodePtr.dataPtr,dataPtr) >= 0) {
+ return prevPtr;
+ }
+ prevPtr = nodePtr;
+ }
+
+ return prevPtr;
+ }
+
+ /* =============================================================================
+ * list_find
+ * -- Returns NULL if not found, else returns pointer to data
+ * =============================================================================
+ * void* list_find (list_t* listPtr, void* dataPtr);
+ */
+ public Object find(Object dataPtr) {
+ List_Node nodePtr;
+ List_Node prevPtr = findPrevious(dataPtr);
+
+ nodePtr = prevPtr.nextPtr;
+
+ if((nodePtr == null) ||
+ (compare(nodePtr.dataPtr,dataPtr) != 0)) {
+ return null;
+ }
+
+ return (nodePtr.dataPtr);
+ }
+
+ public int compare(Object obj1,Object obj2)
+ {
+ if(isCoordinate)
+ {
+ return Coordinate.comparePair(obj1,obj2);
+ }
+ else
+ return compareObject(obj1,obj2);
+ }
+
+/* =============================================================================
+ * list_insert
+ * -- Return TRUE on success, else FALSE
+ * =============================================================================
+ * bool_t list_insert (list_t* listPtr, void* dataPtr);
+ */
+ public boolean insert(Object dataPtr) {
+ List_Node prevPtr;
+ List_Node nodePtr;
+ List_Node currPtr;
+
+ prevPtr = findPrevious(dataPtr);
+ currPtr = prePtr.nextPtr;
+
+ nodePtr = allocNode(dataPtr);
+ if (nodePtr == null) {
+ return false;
+ }
+
+ nodePtr.nextPtr = currPtr;
+ prevPtr.nextPr = nodePtr;
+ size++;
+
+ return true;
+ }
+
+
+/* =============================================================================
+ * list_remove
+ * -- Returns TRUE if successful, else FALSE
+ * =============================================================================
+ * bool_t list_remove (list_t* listPtr, void* dataPtr);
+ */
+ public boolean remove(Object dataPtr)
+ {
+ List_Node prevPtr;
+ List_Node nodePtr;
+
+ prevPtr = findPrevious(dataPtr);
+
+ nodePtr = prevPtr.nextPtr;
+
+ if((nodePtr != null) &&
+ (compare(nodePtr.dataPtr,dataPtr) == 0))
+ {
+ prevPtr.nextPtr = nodePtr.nextPtr;
+ nodePtr.nextPtr = null;
+ nodePtr = null;
+ size--;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ int compareObject(Object obj1,Object obj2) {
+ return obj1 - obj2;
+ }
+
+
+/* =============================================================================
+ * list_clear
+ * -- Removes all elements
+ * =============================================================================
+ * void list_clear (list_t* listPtr);
+ */
+ public void clear() {
+ head = new List_Node();
+ size = 0;
+ }
+
+/* =============================================================================
+ *
+ * End of list.java
+ *
+ * =============================================================================
+ */
+
+ /* Test list */
+
+ public static void main(String[] argv) {
+ List_t listPtr;
+ int[] data1 = new int[5];
+ int[] data2 = new int [6];
+
+ int i;
+
+ System.out.println("Starting...");
+ }
+
+}
+
+
+
--- /dev/null
+/*=============================================================================
+ *
+ * Maze.java
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * =============================================================================
+ *
+ * For the license of bayes/sort.h and bayes/sort.c, please see the header
+ * of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of kmeans, please see kmeans/LICENSE.kmeans
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of ssca2, please see ssca2/COPYRIGHT
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
+ * header of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/rbtree.h and lib/rbtree.c, please see
+ * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
+ *
+ * ------------------------------------------------------------------------
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * =============================================================================
+ */
+
+
+public class Maze {
+ Grid gridPtr;
+ Queue workQueuePtr;
+ Vector_t wallvectorPtr; /* contains source/destination pairs to route */
+ Vector_t srcVectorPtr; /* obstacles */
+ Vector_t dstVectorPtr; /* destinations */
+
+
+/* =============================================================================
+ * maze_alloc
+ * =============================================================================
+ maze_t* maze_alloc ();
+ */
+ public static Maze alloc()
+ {
+ Maze mazePtr = new Maze();
+
+ if(mazePtr != null) {
+ mazePtr.gridPtr = null;
+ mazePtr.workQueuePtr = Queue.alloc(1024);
+ mazePtr.wallVectorPtr = Vector.alloc(1);
+ mazePtr.srcVectorPtr = Vector.alloc(1);
+ mazePtr.dstVectorPtr = Vector.alloc(1);
+
+ }
+
+ return mazePtr;
+ }
+
+/* =============================================================================
+ * maze_free
+ * =============================================================================
+ void maze_free (maze_t* mazePtr);
+ */
+ public static void free(Maze m)
+ {
+ m = null;
+ }
+
+/* =============================================================================
+ * addToGrid
+ * =============================================================================
+ */
+ private void addToGrid(Grid gridPtr,Vector_t vectorPtr,char[] type)
+ {
+ int i;
+ int n = vectorPtr.getSize();
+
+ for(i = 0; i < n; i++) {
+ Coordinate coordinatePtr = (Coordinate)vectorPtr.vector_at(i);
+ if(!gridPtr.isPointValid(coodinatePtr.x,coordinatePtr.y,coordinatePtr.z))
+ {
+ System.err.println("Error: " + type + " (" + coordinate.x +
+ ", " + coordinate.y +
+ ", " + coordinate.z);
+ System.exit(1);
+ }
+ }
+ gridPtr.addPath(vectorPtr);
+ }
+/* =============================================================================
+ * maze_read
+ * -- Return number of path to route
+ * =============================================================================
+ long maze_read (maze_t* mazePtr, char* inputFileName);
+ */
+ public int read(String inputFileName)
+ {
+ BufferedReader in = new BufferedReader(new FileReader(inputFileName));
+
+ /*
+ * Parse input file
+ */
+ int lineNumber = 0;
+ int height = -1;
+ int width = -1;
+ int depth = -1;
+ char[] line = new char[256];
+ boolean isParseError = false;
+ List_t workListPtr = List.alloc(1); // List.alloc(Coordinate.comparePair);
+ String line;
+
+ while(line = in.readLine()) {
+
+ char code;
+ int[] xy = new int[6]; // equivalent to x1,y1,z1,x2,y2,z2
+ int numToken = 0;
+
+ StringTokenizer tok = new StringTokenizer(line);
+
+ if(numToken = tok.countTokens() < 1 ) {
+ continue;
+ }
+
+ code = (char)tok.nextElement();
+
+ for(i=0;i<numToken-1;i++) {
+ xy[i] = Integer.ParserInt(tok.nextToken());
+ }
+
+ if(code == '#')
+ { /* comment */
+ /* ignore line */
+
+ }else if(code == 'd') {
+ /* dimensions (format: d x y z) */
+ if(numToken != 4) {
+ isParseError = true;
+ }
+ else {
+ width = xy[0];
+ height = xy[1];
+ depth = xy[2];
+ if(width < 1 || height < 1 || depth <1)
+ isParseError = true;
+ }
+ }else if(code == 'p') { /* paths (format: p x1 y1 z1 x2 y2 z2) */
+ if(numToken != 7) {
+ isParseError = true;
+ }
+ else {
+ Coordinate srcPtr = Coordinate.alloc(xy[0],xy[1],xy[2]);
+ Coordinate dstPtr = Coordinate.alloc(xy[3],xy[4],xy[5]);
+
+ if(Coordinate.isEqual(srcPtr,dstPtr)) {
+ isParseError = true;
+ }
+ else {
+ Pair coordinatePtr = Pair.alloc(srcPtr,dstPtr);
+ boolean status = workListPtr.insert((Object)coordinatePairPtr);
+ }
+ }
+ }else if(code == 'w') {
+ /* walls (format: w x y z) */
+ if(numToken != 4) {
+ isParseError = true;
+ } else {
+ Coordinate wallPtr = Coordinate.alloc(xy[0],xy[1],xy[2]);
+ wallVectorPtr.vector_pushBack(wallPtr);
+ }
+ }else { /* error */
+ isParseError = true;
+ }
+
+ if(isParseError) {/* Error */
+ System.err.println("Error: line " + lineNumber + " of " + inputfileName + "invalid");
+ System.exit(1);
+ }
+ }
+ /* iterate over lines in put file */
+
+ /*
+ * Initialize grid contents
+ */
+ if(width < 1 || height < 1 || depth < 1) {
+ System.err.println("Error : Invalid dimensions ( " + width + ", " + height + ", "+ depth + ")");
+ System.exit(1);
+ }
+
+ Grid gridPtr = Grid.alloc(width,height,depth);
+ mazePtr.gridPtr = gridPtr;
+ gridPtr.addToGrid(wallVectorPtr,"wall");
+ gridPtr.addToGrid(srcVectorPtr, "source");
+ grdPtr.addToGrid(dstVectorPtr, " destination");
+ System.out.println("Maze dimensions = " + width + " x " + height + " x " + depth);
+ System.out.println("Paths to route = " + workListPtr.getSize());
+
+ /*
+ * Initialize work queue
+ */
+ Queue workQueuePtr = mazePtr.workQueuePtr;
+ List_Iter it = new List_Iter();
+ it.reset(workListPtr);
+
+ while(it.hasNext(wrokListPtr)) {
+ Pair coordinatePtr = (Pair)it.next(workListPtr);
+ workQueuePtr.queue_push((Object)coordinatePairPtr);
+ }
+
+ workListPtr = free;
+
+ return srcVectorPtr.getSize();
+ }
+
+
+/* =============================================================================
+ * maze_checkPaths
+ * =============================================================================
+ bool_t maze_checkPaths (maze_t* mazePtr, list_t* pathListPtr, bool_t doPrintPaths);
+ */
+ public boolean checkPaths(List_t pathListPtr,boolean doPrintPaths)
+ {
+ int i;
+
+ /* Mark walls */
+ Grid testGridPtr = Grid.alloc(width,height,depth);
+ testGridPtr.addPath(wallVectorPtr);
+
+ /* Mark sources */
+ int numSrc = srcVectorPtr.getSize();
+ for(i = 0; i < numSrc; i++) {
+ Coordinate srcPtr = (Coordinate)srcVectorPtr.vector_at(i);
+ testGridPtr.setPoint(srcPtr.x,srcPtr.y,srcPtr.z,0);
+ }
+
+ /* Mark destinations */
+ int numDst = destVectorPtr.getSize();
+ for(i = 0; i < numdst; i++) {
+ Coordinate dstPtr = (Coordinate)dstVector.vector_at(i);
+ testGridPtr.setPoint(dstPtr.x,dstPtr.y,dstPtr.z,0);
+ }
+
+ /* Make sure path is contiguous and does not overlap */
+ int id = 0;
+ List_Iter it = new List_Iter();
+ it.reset(pathVectorListPtr);
+ while(it.hasNext(pathVectorListPtr)) {
+ Vector_t pathVectorPtr = it.next(pathVectorListPtr);
+ int numPath = pathVectorPtr.getSize();
+ int i;
+ for(i = 0; i < numPath; i++) {
+ id++;
+ Vector pointVectorPtr = pathVectorPtr.vector_at(i);
+ /* Check start */
+ int prevGridPointIndex = pointVectorPtr.vector_at(0);
+ int[] x = new int[1];
+ int[] y = new int[1];
+ int[] z = new int[1];
+ gridPtr.getPointIndices(prevGridPointIndex,x,y,z);
+ if(testGridPtr.getPoint(x[0],y[0],z[0]) != 0) {
+ Grid.free(testGridPtr);
+ return false;
+ }
+ Coordinate prevCoordinate = new Coordinate();
+ int[] x = new int[1];
+ int[] y = new int[1];
+ int[] z = new int[1];
+ gridPtr.getpointIndices(prevGridPointIndex,x,y,z);
+ prevCoordinate.x = x[0];
+ prevCoordinate.y = y[0];
+ prevCoordinate.z = z[0];
+
+ int numPoiont = pointVectorPtr.getSize();
+ int j;
+ for(j = 1; j< (numPoint - 1) ;j++) { /* no need to check endpoints */
+ int currGridPointIndex = pointVectorPtr.vector_at(j);
+ Coordinate currCoordinate = new Coordinate();
+ int[] x = new int[1];
+ int[] y = new int[1];
+ int[] z = new int[1];
+ gridPtr.getPointIndices(currGridPointIndex,x,y,z);
+ currGridPoint.x = x[0];
+ currGridPoint.y = y[0];
+ currGridPoint.z = z[0];
+
+ if(Coordinate.areAdjacent(currCoordinate,preCoordinate)) {
+ Grid.free(testGridPtr);
+ return false;
+ }
+
+ prevCoordinate = currCoordinate;
+ int x = currCoordinate.x;
+ int y = currCoordinate.y;
+ int z = currCoordinate.z;
+ if(testGridPtr.getPoint(x,y,z) != GRID_POINT_EMPTY) {
+ Grid.free(testGridPtr);
+ return false;
+ } else {
+ testGridPtr.setPoint(x,y,z,id);
+ }
+ }
+ /* Check end */
+ int lastGridPointIndex = pointVectorPtr.vector_at(j);
+ gridPtr.getPointindices(lastGridPointIndex,x,y,z);
+ if(testGridPtr.getPoint(x[0],y[0],z[0]) != 0) {
+ Grid.free(testGridPtr);
+ return false;
+ }
+ } /* iterate over pathVector */
+ } /* iterate over pathVectorList */
+
+ if(doPrintPaths) {
+ system.out.println("\nRouted Maze:");
+ testGridPtr.print();
+ }
+
+ Grid.free(testGridPtr);
+
+ return true;
+ }
+
+ }
+/* =============================================================================
+ *
+ * End of maze.h
+ *
+ * =============================================================================
+ */
--- /dev/null
+
+ public class Point {
+ int x;
+ int y;
+ int z;
+ int value;
+ int momentum;
+
+ public Point(int x,int y, int z,int value, int m) {
+ this.x = x;
+ this.y = y;
+ this.z = z;
+ this.value = value;
+ momentum = m;
+ }
+ }
+
--- /dev/null
+public class QuickSort {
+ public QuickSort() {
+
+ }
+
+ /**
+ * Sort an array of Objects into ascending order. The sort algorithm is an optimised
+ * quicksort, as described in Jon L. Bentley and M. Douglas McIlroy's
+ * "Engineering a Sort Function", Software-Practice and Experience, Vol.
+ * 23(11) P. 1249-1265 (November 1993). This algorithm gives nlog(n)
+ * performance on many arrays that would take quadratic time with a standard
+ * quicksort.
+ *
+ * @param a the array to sort
+ */
+ public void sort(Object[] a)
+ {
+ qsort(a, 0, a.length);
+ }
+
+ public void sort(Object[] a, int fromIndex, int toIndex)
+ {
+ qsort(a, fromIndex, toIndex);
+ }
+
+ private int med3(int a, int b, int c, Object[]d)
+ {
+ if(less(d[a], d[b])) {
+ if(less(d[b], d[c])) {
+ return b;
+ } else {
+ if(less(d[a], d[c]))
+ return c;
+ else
+ return a;
+ }
+ } else {
+ if(less(d[c], d[b])) {
+ return b;
+ } else {
+ if(less(d[c], d[a]))
+ return c;
+ else
+ return a;
+ }
+ }
+ }
+
+ private void swap(int i, int j, Object[] a)
+ {
+ Object c = a[i];
+ a[i] = a[j];
+ a[j] = c;
+ }
+
+ private void qsort(Object[] a, int start, int n)
+ {
+ // use an insertion sort on small arrays
+ if (n <= 7)
+ {
+ for (int i = start + 1; i < start + n; i++)
+ for (int j = i; j > 0 && less(a[j], a[j - 1]); j--)
+ swap(j, j - 1, a);
+ return;
+ }
+
+ int pm = n / 2; // small arrays, middle element
+ if (n > 7)
+ {
+ int pl = start;
+ int pn = start + n - 1;
+
+ if (n > 40)
+ { // big arrays, pseudomedian of 9
+ int s = n / 8;
+ pl = med3(pl, pl + s, pl + 2 * s, a);
+ pm = med3(pm - s, pm, pm + s, a);
+ pn = med3(pn - 2 * s, pn - s, pn, a);
+ }
+ pm = med3(pl, pm, pn, a); // mid-size, med of 3
+ }
+
+ int pa, pb, pc, pd, pv;
+ int r;
+
+ pv = start;
+ swap(pv, pm, a);
+ pa = pb = start;
+ pc = pd = start + n - 1;
+
+ while(true)
+ {
+ while (pb <= pc && (r = diff(a[pb],a[pv])) <= 0)
+ {
+ if (r == 0)
+ {
+ swap(pa, pb, a);
+ pa++;
+ }
+ pb++;
+ }
+ while (pc >= pb && (r = diff(a[pc],a[pv])) >= 0)
+ {
+ if (r == 0)
+ {
+ swap(pc, pd, a);
+ pd--;
+ }
+ pc--;
+ }
+ if (pb > pc)
+ break;
+ swap(pb, pc, a);
+ pb++;
+ pc--;
+ }
+ int pn = start + n;
+ int s;
+ s = Math.imin(pa - start, pb - pa);
+ vecswap(start, pb - s, s, a);
+ s = Math.imin(pd - pc, pn - pd - 1);
+ vecswap(pb, pn - s, s, a);
+ if ((s = pb - pa) > 1)
+ qsort(a, start, s);
+ if ((s = pd - pc) > 1)
+ qsort(a, pn - s, s);
+ }
+
+ private void vecswap(int i, int j, int n, Object[] a)
+ {
+ for (; n > 0; i++, j++, n--)
+ swap(i, j, a);
+ }
+
+ public boolean less(Object x, Object y) {
+ Query aQueryPtr = (Query) x;
+ Query bQueryPtr = (Query) y;
+ if(aQueryPtr.index < bQueryPtr.index)
+ return true;
+ return false;
+ }
+
+ public int diff(Object x, Object y) {
+ Query aQueryPtr = (Query) x;
+ Query bQueryPtr = (Query) y;
+ return (aQueryPtr.index - bQueryPtr.index);
+ }
+
+ /**
+ * main to test quick sort
+ **/
+ /*
+ public static void main(String[] args) {
+ QuickSort qs = new QuickSort();
+ Query[] queries = new Query[10];
+
+ Random r = new Random();
+ r.random_alloc();
+ r.random_seed(0);
+
+ for(int i = 0; i<10; i++) {
+ queries[i] = new Query();
+ queries[i].index = (int) (r.random_generate() % 100);
+ queries[i].value = -1;
+ }
+
+ System.out.println("Before sorting");
+ for(int i = 0; i<10; i++)
+ System.out.println("queries["+i+"]= "+ queries[i]);
+
+ qs.sort(queries);
+
+ System.out.println("After sorting");
+ for(int i = 0; i<10; i++)
+ System.out.println("queries["+i+"]= "+ queries[i]);
+ }
+ */
+}
--- /dev/null
+Introduction
+------------
+
+Given a maze, this benchmark finds the shortest-distance paths between pairs of
+starting and ending points. The routing algorithm used is Lee's algorithm [2].
+
+In this algorithm, the maze is represented as a grid, where each grid point can
+contain connections to adjacent, non-diagonal grid points. The algorithm
+searches for a shortest path between the start and end points of a connection by
+performing a breadth-first search and labeling each grid point with its distance
+from the start. This expansion phase will eventually reach the end point if a
+connection is possible. A second traceback phase then forms the connection by
+following any path with a decreasing distance. This algorithm is guaranteed to
+find the shortest path between a start and end point; however, when multiple
+paths are made, one path may block another.
+
+When creating the transactional version of this program, the techniques
+described in [3] were used. When using this benchmark, please cite [1].
+
+
+Compiling and Running
+---------------------
+
+To build the application, simply run:
+
+ make -f <makefile>
+
+in the source directory. For example, for the sequential flavor, run:
+
+ make -f Makefile.seq
+
+By default, this produces an executable named "labyrinth", which can then be
+run in the following manner:
+
+ ./labyrinth -i <input_file>
+
+The following input file is recommended for simulated runs:
+
+ ./labyrinth -i inputs/random-x32-y32-z3-n96.txt
+
+For non-simulator runs, a larger input can be used:
+
+ ./labyrinth -i inputs/random-x512-y512-z7-n512.txt
+
+
+Input Files
+-----------
+
+More input sets can be generated by using "inputs/generate.py". For example,
+
+ inputs/generate.py 128 128 3 64
+
+Will create a 128x128x3 maze grid and select 64 uniformly random start/end
+point pairs.
+
+
+References
+----------
+
+[1] C. Cao Minh, J. Chung, C. Kozyrakis, and K. Olukotun. STAMP: Stanford
+ Transactional Applications for Multi-processing. In IISWC '08: Proceedings
+ of The IEEE International Symposium on Workload Characterization,
+ September 2008.
+
+[2] C. Lee. An algorithm for path connections and its applications. IRE Trans.
+ On Electronic Computers, 1961.
+
+[3] I. Watson, C. Kirkham, and M. Lujan. A Study of a Transactional Parallel
+ Routing Algorithm. Proceedings of the 16th International Conference on
+ Parallel Architectures and Compilation Techniques, 2007.
+
--- /dev/null
+/* =============================================================================
+ *
+ * Router.java
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * Ported to Java
+ * Author: Jihoon Lee
+ * University of California, Irvine
+ *
+ * =============================================================================
+ *
+ * For the license of bayes/sort.h and bayes/sort.c, please see the header
+ * of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of kmeans, please see kmeans/LICENSE.kmeans
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of ssca2, please see ssca2/COPYRIGHT
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
+ * header of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/rbtree.h and lib/rbtree.c, please see
+ * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
+ *
+ * ------------------------------------------------------------------------
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * =============================================================================
+ */
+
+#define MOMENTUM_ZERO 0
+#define MOMENTUM_POSX 1
+#define MOMENTUM_POSY 2
+#define MOMENTUM_POSZ 3
+#define MOMENTUM_NEGX 4
+#define MOMENTUM_NEGY 5
+#define MOMENTUM_NEGZ 6
+
+
+public class Router {
+ int xCost;
+ int yCost;
+ int zCost;
+ int bendCost;
+
+
+/* =============================================================================
+ * router_alloc
+ * =============================================================================
+ * router_t* router_alloc (long xCost, long yCost, long zCost, long bendCost);
+ */
+ public static Router alloc(int xCost,int yCost,int zCost,int bendCost)
+ {
+ Router routerPtr = new Router();
+
+
+ Point MOVE_POSX = new Point(1,0,0,0,MOMENTUM_POSX);
+ Point MOVE_POSY = new Point(0,1,0,0,MOMENTUM_POSY);
+ Point MOVE_POSZ = new Point(0,0,1,0,MOMENTUM_POSZ);
+ Point MOVE_NEGX = new Point(-1,0,0,0,MOMENTUM_NEGX);
+ Point MOVE_NEGY = new Point(0,-1,0,0,MOMENTUM_NEGY);
+ Point MOVE_NEGZ = new Point(0,0,-1,0,MOMENTUM_NEGZ);
+
+ if(routerPtr != null) {
+ routerPtr.xCost = xCost;
+ routerPtr.yCost = yCost;
+ routerPtr.zCost = zCost;
+ routerPtr.bendCost = bendCost;
+ }
+
+ return routerPtr;
+ }
+
+
+
+
+/* =============================================================================
+ * router_free
+ * =============================================================================
+ * void router_free (router_t* routerPtr);
+ */
+ public static void free(Router routerPtr)
+ {
+ routerPtr = null;
+ }
+
+/* ============================================================================
+ * PexpandToneighbor
+ * ============================================================================
+ */
+ private void PexpandToNeighbor(Grid myGridPtr,
+ int x,int y,int z, int value,Queue queuePtr)
+ {
+ if (myGridPtr.isPointValid(x,y,z)) {
+ int neighborGridPointIndex = myGridPtr.getPointIndex(x,y,z);
+ int neighborValue = myGridPtr.getPoint(neighborGridPointIndex);
+ if (neighborValue == GRID_POINT_EMPTY) {
+ myGridPtr.setPoint(neighborGridPointIndex,value);
+ queuePtr.queue_push(neighborGridPointIndex);
+ } else if (neighborValue != GRID_POINT_FULL) {
+
+ if (value < neighborValue) {
+ myGridPtr.setPoint(neighborGridPointIndex,value);
+ queuePtr.queue_push(neighborGridPointIndex);
+ }
+ }
+ }
+ }
+
+
+/* ============================================================================
+ * PdoExpansion
+ * ============================================================================
+ */
+ private boolean PdoExpansion (Router routerPtr,Grid myGridPtr,Queue queuePtr,
+ Coordinate srcPtr,Coordinate dstPtr)
+ {
+ int xCost = routerPtr.xCost;
+ int yCost = routerPtr.yCost;
+ int zCost = routerPtr.zCost;
+
+ /*
+ * Potential Optimization: Make 'src' the one closet to edge.
+ * This will likely decrease the area of the emitted wave.
+ */
+
+ queuePtr.queue_clear();
+
+ int srcGridPointIndex = myGridPtr.getPointIndex(srcPtr.x,srcPtr.y,srcPtr.z);
+ queuePtr.queue_push(srcGridPointIndex);
+ myGridPtr.setPoint(srcPtr.x,srcPtr.y,srcPtr.z,0);
+ myGridPtr.setPoint(dstPtr.x,dstPtr.y,dstPtr.z,GRID_POINT_EMPTY);
+ int dstGridPointIndex = myGridPtr.getPointIndex(dstPtr.x,dstPtr.y,dstPtr.z);
+ boolean isPathFound = false;
+
+ while (queuePtr.queue_isEmpty()) {
+ int gridPointIndex = queuePtr.queue_pop();
+ if(gridPointIndex == dstGridPointIndex) {
+ isPathFound = true;
+ break;
+ }
+
+ int[] x = new int[1];
+ int[] y = new int[1];
+ int[] z = new int[1];
+
+ myGridPtr.getPointIndices(gridPointIndex,x,y,z);
+ int value = myGridPtr.getPoint(gridPointIndex);
+
+ /*
+ * Check 6 neighbors
+ *
+ * Potential Optimization: Only need to check 5 of these
+ */
+ PexpandToNeighbor(myGridPtr, x[0]+1, y[0], z[0], (value + xCost), queuePtr);
+ PexpandToNeighbor(myGridPtr, x[0]-1, y[0], z[0], (value + xCost), queuePtr);
+ PexpandToNeighbor(myGridPtr, x[0], y[0]+1, z[0], (value + xCost), queuePtr);
+ PexpandToNeighbor(myGridPtr, x[0], y[0]-1, z[0], (value + xCost), queuePtr);
+ PexpandToNeighbor(myGridPtr, x[0], y[0], z[0]+1, (value + xCost), queuePtr);
+ PexpandToNeighbor(myGridPtr, x[0], y[0], z[0]-1, (value + xCost), queuePtr);
+
+ } /* iterate over work queue */
+
+ return isPathfound;
+ }
+
+
+/* ============================================================================
+ * traceToNeighbor
+ * ============================================================================
+ */
+ private void traceToNeighbor(Grid myGridPtr,
+ Point currPtr,
+ Point movePtr,
+ boolean useMomentum,
+ int bendCost,
+ Point nextPtr)
+ {
+ int x = currPtr.x + movePtr.x;
+ int y = currPtr.y + movePtr.y;
+ int z = currPtr.z + movePtr.z;
+
+ if (myGridPtr.isPointValid(x,y,z) &&
+ !myGridPtr.isPointEmpty(x,y,z) &&
+ !myGridPtr.isPointFull(x,y,z))
+ {
+ int value = myGridPtr.getPoint(x,y,z);
+ int b = 0;
+
+ if (useMomentum && (currPtr.momentum != movePtr.momentum)) {
+ b = bendCost;
+ }
+ if ((value + b) <= nextPtr.value) { /* '=' favors neighbors over current */
+ nextPtr.x = x;
+ nextPtr.y = y;
+ nextPtr.z = z;
+ nextPtr.value = value;
+ nextPtr.momentum = movePtr.momentum;
+ }
+ }
+ }
+/* =============================================================================
+ * PdoTraceback
+ * =============================================================================
+ */
+
+ private Vector_t PdoTraceback(Grid gridPtr,Grid myGridPtr,
+ Coordinate dstPtr, int bendCost)
+ {
+ Vector_t pointVectorPtr = Vector.vector_alloc(1);
+
+ Point next = new Point();
+ next.x = dstPtr.x;
+ next.y = dstPtr.y;
+ next.z = dstPtr.z;
+ next.value = myGridPtr.getPoint(next.x,next.y,next.z);
+ next.momentum = MOMENTUM_ZERO;
+
+ while(true) {
+ int gridPointIndex = gridPtr.getPointIndex(next.x,next.y,next.z);
+ pointVectorPtr.vector_pushBack(gridPointIndex);
+ myGridPtr.setPoint(next.x,next.y,next.z,GRID_POINT_FULL);
+
+ /* Check if we are done */
+ if (next.value == 0) {
+ break;
+ }
+ Point curr = next;
+
+ /*
+ * Check 6 neibors
+ */
+
+ traceToNeighbor(myGridPtr,curr,MOVE_POSX,true, bendCost, next);
+ traceToNeighbor(myGridPtr,curr,MOVE_POSY,true, bendCost, next);
+ traceToNeighbor(myGridPtr,curr,MOVE_POSZ,true, bendCost, next);
+ traceToNeighbor(myGridPtr,curr,MOVE_NEGX,true, bendCost, next);
+ traceToNeighbor(myGridPtr,curr,MOVE_NEGY,true, bendCost, next);
+ traceToNeighbor(myGridPtr,curr,MOVE_NEGZ,true, bendCost, next);
+
+ /*
+ * Because of bend costs, none of the neighbors may appear to be closer.
+ * In this case, pick a neighbor while ignoring momentum.
+ */
+
+ if ((curr.x == next.y) &&
+ (curr.y == next.y) &&
+ (curr.z == next.z))
+ {
+ next.value = curr.value;
+ traceToNeighbor(myGridPtr,curr,MOVE_POSX,false, bendCost, next);
+ traceToNeighbor(myGridPtr,curr,MOVE_POSY,false, bendCost, next);
+ traceToNeighbor(myGridPtr,curr,MOVE_POSZ,false, bendCost, next);
+ traceToNeighbor(myGridPtr,curr,MOVE_NEGX,false, bendCost, next);
+ traceToNeighbor(myGridPtr,curr,MOVE_NEGY,false, bendCost, next);
+ traceToNeighbor(myGridPtr,curr,MOVE_NEGZ,false, bendCost, next);
+
+ if ((curr.x == next.x) &&
+ (curr.y == next.y) &&
+ (curr.z == next.z))
+ {
+ pointVectorPtr.vector_free();
+ return null;
+ }
+ }
+ }
+
+ return pointVectorPtr;
+ }
+
+/* =============================================================================
+ * router_solve
+ * =============================================================================
+ * void router_solve (void* argPtr);
+ */
+ public static void solve(Object argPtr)
+ {
+ // TM_THREAD_ENTER();
+ Solve_Arg routerArgPtr = (SolveArg) argPtr;
+ Router routerPtr = routerArgPtr.routerPtr;
+ Maze mazePtr = routerArgPtr.mazePtr;
+ Vector_t myPathVectorPtr = Vector.alloc(1);
+
+ Queue workQueuePtr = mazePtr.workqueuePtr;
+ Grid gridPtr = mazePtr.gridPtr;
+ Grid myGridPtr = Grid.alloc(gridPtr.width,gridPtr.height,gridPtr.depth);
+ int bendCost = routerPtr.bendCost;
+ Queue myExpansionQueuePtr = Queue.queue_alloc(-1);
+
+ /*
+ * Iterate over work list to route each path. This involves an
+ * 'expansion' and 'tracback' phase for each source/destination pair.
+ */
+ while(true) {
+ Pair coordinatePairPtr;
+
+ // TM_BEGIN();
+ atomic {
+ if(workQueuePtr.queue_isEmpty()) {
+ coordinatePairPtr = null;
+ } else {
+ coordinatePairPtr = (Pair)workQueuePtr.queue_pop();
+ }
+ }
+ // TM_END();
+ //
+
+ if (coordinatePairPtr = null) {
+ break;
+ }
+
+ Coordinate srcPtr = coordinatePairPtr.firstPtr;
+ Coordinate dstPtr = coordinatePairPtr.secondPtr;
+
+ boolean success = false;
+ Vector_t pointvectorPtr = null;
+
+ // TM_BEGIN();
+ atomic {
+ Grid.copy(myGridPtr, gridPtr); /* ok if not most up-to-date */
+ if (PdoExpansion(routerPtr, myGridPtr, myExpansionQueuePtr,
+ srcPtr, dstPtr)) {
+ pointVectorPtr = PdoTraceback(gridPtr,myGridPtr,dstPtr,bendCost);
+
+ if (pointVectorPtr) {
+ gridPtr.addPath(pointVectorPtr);
+ success = true;
+ }
+ }
+ }
+
+ if(success) {
+ boolean status = myPathVectorPtr.vector_pushBack(pointVectorPtr);
+ }
+ }
+
+ /*
+ * Add my paths to global list
+ */
+ List_t pathVectorListPtr = routerArgPtr.pathVectorListPtr;
+
+ atomic {
+ pathVectorListPtr.insert(myPathVectorPtr);
+ }
+
+ // TM_THREAD_EXIT();
+ }
+}
+/* =============================================================================
+ *
+ * End of router.java
+ *
+ * =============================================================================
+ */
--- /dev/null
+
+
+ public class Solve_Arg {
+ Router routerPtr;
+ Maze mazePtr;
+ List_t pathVectorListPtr;
+
+ public Solve_Arg(Router r,Maze m,List_t l)
+ {
+ routerPtr = r;
+ mazePtr = m;
+ pathVectorListPtr = l;
+ }
+ }
+
--- /dev/null
+public class Vector_t {
+ int size;
+ int capacity;
+ Object[] elements;
+ QuickSort qsort;
+
+ public Vector_t() {
+ qsort = new QuickSort();
+ }
+
+ /* =============================================================================
+ * Vector_alloc
+ * -- Returns null if failed
+ * =============================================================================
+ */
+ public static Vector_t vector_alloc (int initCapacity) {
+ int capacity = Math.imax(initCapacity, 1);
+ Vector_t vectorPtr = new Vector_t();
+ if(vectorPtr != null) {
+ vectorPtr.size = 0;
+ vectorPtr.capacity = capacity;
+ vectorPtr.elements = new Object[capacity];
+ if(vectorPtr.elements == null)
+ return null;
+ }
+ return vectorPtr;
+ }
+
+ /* =============================================================================
+ * Vector_free
+ * =============================================================================
+ */
+ public void
+ vector_free ()
+ {
+ elements = null;
+ }
+
+ /* =============================================================================
+ * Vector_at
+ * -- Returns null if failed
+ * =============================================================================
+ */
+ public Object vector_at (int i) {
+ if ((i < 0) || (i >= size)) {
+ System.out.println("Illegal Vector.element\n");
+ return null;
+ }
+ return (elements[i]);
+ }
+
+
+ /* =============================================================================
+ * Vector_pushBack
+ * -- Returns false if fail, else true
+ * =============================================================================
+ */
+ public boolean vector_pushBack (Object dataPtr) {
+ if (size == capacity) {
+ int newCapacity = capacity * 2;
+ Object[] newElements = new Object[newCapacity];
+
+ //void** newElements = (void**)malloc(newCapacity * sizeof(void*));
+ if (newElements == null) {
+ return false;
+ }
+ capacity = newCapacity;
+ for (int i = 0; i < size; i++) {
+ newElements[i] = elements[i];
+ }
+ elements = null;
+ elements = newElements;
+ }
+
+ elements[size++] = dataPtr;
+
+ return true;
+ }
+
+ /* =============================================================================
+ * Vector_popBack
+ * -- Returns null if fail, else returns last element
+ * =============================================================================
+ */
+ public Object
+ vector_popBack ()
+ {
+ if (size < 1) {
+ return null;
+ }
+
+ return (elements[--(size)]);
+ }
+
+ /* =============================================================================
+ * Vector_getSize
+ * =============================================================================
+ */
+ public int
+ vector_getSize ()
+ {
+ return (size);
+ }
+
+ /* =============================================================================
+ * Vector_clear
+ * =============================================================================
+ */
+ public void
+ vector_clear ()
+ {
+ size = 0;
+ }
+
+ /* =============================================================================
+ * Vector_sort
+ * =============================================================================
+ *
+ public void
+ vector_sort ()
+ {
+ //qsort.sort(elements, 0, (elements.length - 1));
+ qsort.sort(elements);
+ //qsort(elements, size, 4, compare);
+ }
+
+ * =============================================================================
+ * Vector_copy
+ * =============================================================================
+ */
+ public static boolean
+ vector_copy (Vector_t dstVectorPtr, Vector_t srcVectorPtr)
+ {
+ int dstCapacity = dstVectorPtr.capacity;
+ int srcSize = srcVectorPtr.size;
+ if (dstCapacity < srcSize) {
+ int srcCapacity = srcVectorPtr.capacity;
+ Object[] elements = new Object[srcCapacity];
+
+ if (elements == null) {
+ return false;
+ }
+ dstVectorPtr.elements = null;
+ dstVectorPtr.elements = elements;
+ dstVectorPtr.capacity = srcCapacity;
+ }
+
+ for(int i = 0; i< srcSize; i++) {
+ dstVectorPtr.elements[i] = srcVectorPtr.elements[i];
+ }
+
+ dstVectorPtr.size = srcSize;
+
+ return true;
+ }
+}
--- /dev/null
+/* =============================================================================
+ *
+ * Router.java
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * =============================================================================
+ *
+ * For the license of bayes/sort.h and bayes/sort.c, please see the header
+ * of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of kmeans, please see kmeans/LICENSE.kmeans
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of ssca2, please see ssca2/COPYRIGHT
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
+ * header of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/rbtree.h and lib/rbtree.c, please see
+ * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
+ *
+ * ------------------------------------------------------------------------
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * =============================================================================
+ */
+
+
+typedef struct router_solve_arg {
+ router_t* routerPtr;
+ maze_t* mazePtr;
+ list_t* pathVectorListPtr;
+} router_solve_arg_t;
+
+public class Router {
+ int xCost;
+ int yCost;
+ int zCost;
+ int bendCost;
+
+ public class Solve_Arg {
+ Router routerPtr;
+ Maze mazePtr;
+ List_t pathVectorListPtr;
+ }
+
+ enum {
+ MOMENTUM_ZERO = 0,
+ MOMENTUM_POSX = 1,
+ MOMENTUM_POSY = 2,
+ MOMENTUM_POSZ = 3,
+ MOMENTUM_NEGX = 4,
+ MOMENTUM_NEGY = 5,
+ MOMENTUM_NEGZ = 6
+ } Momentum_t;
+
+ private class Point {
+ int x;
+ int y;
+ int z;
+ int value;
+ Momentum_t momentum;
+
+ public Point(int x,int y, int z,int value, Momentum_t m) {
+ this.x = x;
+ this.y = y;
+ this.z = z;
+ this.value = value;
+ momentum = m;
+ }
+ }
+
+ Point MOVE_POSX = new Point(1,0,0,0,MOMENTUM_POSX);
+ Point MOVE_POSY = new Point(0,1,0,0,MOMENTUM_POSY);
+ Point MOVE_POSZ = new Point(0,0,1,0,MOMENTUM_POSZ);
+ Point MOVE_NEGX = new Point(-1,0,0,0,MOMENTUM_NEGX);
+ Point MOVE_NEGY = new Point(0,-1,0,0,MOMENTUM_NEGY);
+ Point MOVE_NEGZ = new Point(0,0,-1,0,MOMENTUM_NEGZ);
+
+
+/* =============================================================================
+ * router_alloc
+ * =============================================================================
+ * router_t* router_alloc (long xCost, long yCost, long zCost, long bendCost);
+ */
+ public static Router alloc(int xCost,int yCost,int zCost,int bendCost)
+ {
+ Router routerPtr = new Router();
+
+ if(routerPtr != null) {
+ routerPtr.xCost = xCost;
+ routerPtr.yCost = yCost;
+ routerPtr.zCost = zCost;
+ routerPtr.bendCost = bendCost;
+ }
+
+ return routerPtr;
+ }
+
+
+
+
+/* =============================================================================
+ * router_free
+ * =============================================================================
+ * void router_free (router_t* routerPtr);
+ */
+ public static void free(Router routerPtr)
+ {
+ routerPtr = null;
+ }
+
+
+
+/* ============================================================================
+ * traceToNeighbor
+ * ============================================================================
+ */
+ private void traceToNeighbor(Grid myGridPtr,
+ Point currPtr,
+ Point movePtr,
+ boolean useMomentum,
+ int bendCost,
+ Point nextPtr)
+ {
+ int x = currPtr.x + movePtr.x;
+ int y = currPtr.y + movePtr.y;
+ int z = currPtr.z + movePtr.z;
+
+ if (myGridPtr.isPointValid(x,y,z) &&
+ !myGridPtr.isPointEmpty(x,y,z) &&
+ !myGridPtr.isPointFull(x,y,z))
+ {
+ int value = myGridPtr.getPoint(x,y,z);
+ int b = 0;
+
+ if (useMomentum && (currPtr.momentum != movePtr.momentum)) {
+ b = bendCost;
+ }
+ if ((value + b) <= nextPtr.value) { /* '=' favors neighbors over current */
+ nextPtr.x = x;
+ nextPtr.y = y;
+ nextPtr.z = z;
+ nextPtr.value = value;
+ nextPtr.momentum = movePtr.momentum;
+ }
+ }
+ }
+
+
+/* =============================================================================
+ * router_solve
+ * =============================================================================
+ * void router_solve (void* argPtr);
+ */
+ public void solve(Object argPtr)
+ {
+ Barrior
+ }
+
+
+}
+
+/* =============================================================================
+ *
+ * End of router.h
+ *
+ * =============================================================================
+ */
--- /dev/null
+#!/bin/sh
+lines=$(grep -n "#" $1 | cut -d: -f1 | sed '1q')
+sed '/^#/d' $1 > ttt$1
--- /dev/null
+MAINCLASS=Labyrinth
+SRC=ttttmp${MAINCLASS}.java \
+ ../common/Pair.java \
+ ../common/Queue_t.java \
+ Vector_t.java \
+ ../common/List_t.java \
+ ../common/List_Node.java \
+ ../common/List_Iter.java \
+ Coordinate.java \
+ ttttmpGrid.java \
+ Maze.java \
+ ttttmpRouter.java \
+ Point.java \
+ Solve_arg.java \
+ ../../../ClassLibrary/JavaSTM/Barrier.java
+
+FLAGS=-mainclass ${MAINCLASS} -thread -nooptimize -debug
+
+default:
+ cpp ${MAINCLASS}.java > tmp${MAINCLASS}.java
+ cpp Grid.java > tmpGrid.java
+ cpp Router.java > tmpRouter.java
+ ./extractLines tmp${MAINCLASS}.java
+ ./extractLines tmpGrid.java
+ ./extractLines tmpRouter.java
+ ../../../buildscript ${FLAGS} -o ${MAINCLASS} ${SRC}
+ rm ttt*.java tmp*.java
+
+clean:
+ rm -rf tmpbuilddirectory
+ rm *.bin