Commits

zetro committed 69cc055

Removed constructor parameter 'level' in HashNode.

  • Participants
  • Parent commits 45243f2

Comments (0)

Files changed (5)

File src/org/bitbucket/zetro/squaredino/backend/experimental/HashAutomaton.java

  * HashAutomaton class. Only implements the standard 3/23 rule.
  */
 public class HashAutomaton extends AbstractAutomaton {
-	public static boolean useLinear = true;
-	public static int stepSize = 2;
+	public static boolean useLinear = false;
+	public static int stepSize = 0;
 	private HashQuadTree<Cell> hashTree;
 	private HashNodeCache cache;
 	private boolean linear;
 		expanded = expand(expanded);			// level == node.level + 2
 
 		HashNode n;
-		if (useLinear) {
+		if (useLinear || stepSize == 0) {
 			n = nextGeneration(expanded, 0);	// level == node.level + 1
 		} else {
-			while (expanded.level - 3 < stepSize) {
+			while (expanded.level < stepSize) {
 				expanded = expand(expanded);
 			}
 			
-			System.out.print("Step size: " + (1 << Math.min(stepSize, expanded.level - 3)) + ", population: " + expanded.size());
+			System.out.print("Step size: " + (1 << stepSize) + ", population: " + expanded.size());
 			n = nextGeneration(expanded, stepSize);		// level == node.level + 1
 			System.out.println(", new population: " + n.size());
 		}
 			HashNode sw = checkNeighbourCount(node.sw.ne, sum(node.nw, 2, 3) + sum(node.ne, 3) + sum(node.sw, 0, 2, 3) + sum(node.se, 0, 3));
 			HashNode se = checkNeighbourCount(node.se.nw, sum(node.nw, 2) + sum(node.ne, 2, 3) + sum(node.sw, 1, 2) + sum(node.se, 1, 2, 3));
 
-			nextNode = HashNode.create(cache, 1, nw, ne, sw, se);
+			nextNode = HashNode.create(cache, nw, ne, sw, se);
 		} else {
 			HashNode nwc, top, nec, lef, cen, rig, swc, dwn, sec;
-			if (node.level == 3 || node.level - 3 > linearLevel) { // Linear
+			if (/*node.level == 3 ||*/node.level - 2 > linearLevel) { // Linear
 				nwc = centeredSubnode(node.nw);				// nw center
 				top = centeredHorizontal(node.nw, node.ne);	// top
 				nec = centeredSubnode(node.ne);				// ne center
 				sec = nextGeneration(node.se, linearLevel);				// se center
 			}
 
-			nextNode = HashNode.create(cache, node.level - 1,
-					nextGeneration(HashNode.create(cache, node.level - 1, nwc, top, lef, cen), linearLevel),
-					nextGeneration(HashNode.create(cache, node.level - 1, top, nec, cen, rig), linearLevel),
-					nextGeneration(HashNode.create(cache, node.level - 1, lef, cen, swc, dwn), linearLevel),
-					nextGeneration(HashNode.create(cache, node.level - 1, cen, rig, dwn, sec), linearLevel));
+			nextNode = HashNode.create(cache, 
+					nextGeneration(HashNode.create(cache, nwc, top, lef, cen), linearLevel),
+					nextGeneration(HashNode.create(cache, top, nec, cen, rig), linearLevel),
+					nextGeneration(HashNode.create(cache, lef, cen, swc, dwn), linearLevel),
+					nextGeneration(HashNode.create(cache, cen, rig, dwn, sec), linearLevel));
 		}
 
 		node.next = nextNode;
 	}
 
 	private HashNode horizontalForward(HashNode w, HashNode e, int linearLevel) {
-		return nextGeneration(HashNode.create(cache, w.level, w.ne, e.nw, w.se, e.sw), linearLevel);
+		return nextGeneration(HashNode.create(cache, w.ne, e.nw, w.se, e.sw), linearLevel);
 	}
 
 	private HashNode verticalForward(HashNode n, HashNode s, int linearLevel) {
-		return nextGeneration(HashNode.create(cache, n.level, n.sw, n.se, s.nw, s.ne), linearLevel);
+		return nextGeneration(HashNode.create(cache, n.sw, n.se, s.nw, s.ne), linearLevel);
 	}
 
 	private HashNode centeredForward(HashNode c, int linearLevel) {
-		return nextGeneration(HashNode.create(cache, c.level - 1, c.nw.se, c.ne.sw, c.sw.ne, c.se.nw), linearLevel);
+		return nextGeneration(HashNode.create(cache, c.nw.se, c.ne.sw, c.sw.ne, c.se.nw), linearLevel);
 	}
 
 	private HashNode centeredSubnode(HashNode c) {
-		return HashNode.create(cache, c.level - 1, c.nw.se, c.ne.sw, c.sw.ne, c.se.nw);
+		return HashNode.create(cache, c.nw.se, c.ne.sw, c.sw.ne, c.se.nw);
 	}
 
 	private HashNode centeredHorizontal(HashNode w, HashNode e) {
-		return HashNode.create(cache, w.level - 1, w.ne.se, e.nw.sw, w.se.ne, e.sw.nw);
+		return HashNode.create(cache, w.ne.se, e.nw.sw, w.se.ne, e.sw.nw);
 	}
 
 	private HashNode centeredVertical(HashNode n, HashNode s) {
-		return HashNode.create(cache, n.level - 1, n.sw.se, n.se.sw, s.nw.ne, s.ne.nw);
+		return HashNode.create(cache, n.sw.se, n.se.sw, s.nw.ne, s.ne.nw);
 	}
 
 	private HashNode centeredSubSubnode(HashNode c) {
-		return HashNode.create(cache, c.level - 2, c.nw.se.se, c.ne.sw.sw, c.sw.ne.ne,
+		return HashNode.create(cache, c.nw.se.se, c.ne.sw.sw, c.sw.ne.ne,
 				c.se.nw.nw);
 	}
 
 				&& cur.ne.nw == space && cur.ne.ne == space && cur.ne.se == space
 				&& cur.sw.nw == space && cur.sw.sw == space && cur.sw.se == space
 				&& cur.se.ne == space && cur.se.sw == space && cur.se.se == space) {
-			cur = HashNode.create(cache, cur.level - 1,
+			cur = HashNode.create(cache, 
 					cur.nw.se, cur.ne.sw, cur.sw.ne, cur.se.nw);
 			if (cur.level >= 2) {
 				space = hashTree.getSpaceNode(cur.level - 2);
 
 	private HashNode expand(HashNode node) {
 		HashNode space = hashTree.getSpaceNode(node.level - 1);
-		HashNode nw = HashNode.create(cache, node.level, space, space, space, node.nw);
-		HashNode ne = HashNode.create(cache, node.level, space, space, node.ne, space);
-		HashNode sw = HashNode.create(cache, node.level, space, node.sw, space, space);
-		HashNode se = HashNode.create(cache, node.level, node.se, space, space, space);
-		return HashNode.create(cache, node.level + 1, nw, ne, sw, se);
+		HashNode nw = HashNode.create(cache, space, space, space, node.nw);
+		HashNode ne = HashNode.create(cache, space, space, node.ne, space);
+		HashNode sw = HashNode.create(cache, space, node.sw, space, space);
+		HashNode se = HashNode.create(cache, node.se, space, space, space);
+		return HashNode.create(cache, nw, ne, sw, se);
 	}
 }

File src/org/bitbucket/zetro/squaredino/backend/experimental/HashContentNode.java

 	 * @param content stored value
 	 */
 	private HashContentNode(T content) {
-		super(0, null, null, null, null);
+		super(null, null, null, null);
 
 		this.content = content;
 	}

File src/org/bitbucket/zetro/squaredino/backend/experimental/HashNode.java

 	/**
 	 * HashNode constructor.
 	 *
-	 * @param level node level
 	 * @param nw north-west node
 	 * @param ne north-east node
 	 * @param sw south-west node
 	 * @param se south-east node
 	 */
-	HashNode(int level, HashNode nw, HashNode ne, HashNode sw, HashNode se) {
-		this.level = level;
+	HashNode(HashNode nw, HashNode ne, HashNode sw, HashNode se) {
+		this.level = nw != null ? nw.level+1 : 0;
 		this.nw = nw;
 		this.ne = ne;
 		this.sw = sw;
 	 * Creates a new HashNode.
 	 *
 	 * @param cache node cache
-	 * @param level node level
 	 * @param nw north-west node
 	 * @param ne north-east node
 	 * @param sw south-west node
 	 * @param se south-east node
 	 * @return the new HashNode
 	 */
-	public static HashNode create(HashNodeCache cache, int level, HashNode nw, HashNode ne, HashNode sw, HashNode se) {
-		return cache.findOrCacheNode(new HashNode(level, nw, ne, sw, se));
+	public static HashNode create(HashNodeCache cache,  HashNode nw, HashNode ne, HashNode sw, HashNode se) {
+		return cache.findOrCacheNode(new HashNode(nw, ne, sw, se));
 	}
 
 	private int calculateHash() {

File src/org/bitbucket/zetro/squaredino/backend/experimental/HashQuadTree.java

 	}
 
 	private static <T> HashNode buildNode(HashQuadTree<T> hashTree, QuadTree<T>.Node node, int level) {
-		//System.out.println("Building node " + node);
-
 		if (node == null) {
 			return hashTree.getSpaceNode(level);
 		} else if (!node.isLeaf()) {
 			HashNode sw = buildNode(hashTree, node.sw, level - 1);
 			HashNode se = buildNode(hashTree, node.se, level - 1);
 
-			return HashNode.create(hashTree.cache, level, nw, ne, sw, se);
+			return HashNode.create(hashTree.cache, nw, ne, sw, se);
 		} else if (node instanceof QuadTree.ContentNode) {
 			assert level == 0;
 			@SuppressWarnings("unchecked")
 		}
 
 		HashNode part = getSpaceNode(level - 1);
-		HashNode space = HashNode.create(cache, level, part, part, part, part);
+		HashNode space = HashNode.create(cache, part, part, part, part);
 		spaceCache.put(level, space);
 		return space;
 	}

File src/org/bitbucket/zetro/squaredino/backend/experimental/QuadTree.java

 		}
 		while (macro >= 2) {
 			if (p.x < x || p.y < y || p.x >= x + macro || p.y >= y + macro) {
-				System.out.printf("macro=%d, x=%d, y=%d, px=%d, py=%d\n", macro, x, y, p.x, p.y);
+				System.err.printf("macro=%d, x=%d, y=%d, px=%d, py=%d\n", macro, x, y, p.x, p.y);
 				throw new AssertionError();
 			}
 
 		}
 		while (macro >= 2) {
 			if (p.x < x || p.y < y || p.x >= x + macro || p.y >= y + macro) {
-				System.out.printf("macro=%d, x=%d, y=%d, px=%d, py=%d\n", macro, x, y, p.x, p.y);
+				System.err.printf("macro=%d, x=%d, y=%d, px=%d, py=%d\n", macro, x, y, p.x, p.y);
 				throw new AssertionError();
 			}