X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FCartesian.java;h=1bac161cf1a70b2cd767cae20ebf4604647e79a2;hb=e84029a8b861075d6d0ed5040f919b2e4da4c98f;hp=676a4cf3f597def00bcaf4ac18260f8cc8faad4e;hpb=c8a17fdd2e149fe5feecd96c71b7f2cab286ab96;p=sbp.git diff --git a/src/edu/berkeley/sbp/misc/Cartesian.java b/src/edu/berkeley/sbp/misc/Cartesian.java index 676a4cf..1bac161 100644 --- a/src/edu/berkeley/sbp/misc/Cartesian.java +++ b/src/edu/berkeley/sbp/misc/Cartesian.java @@ -1,3 +1,5 @@ +// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license + package edu.berkeley.sbp.misc; import java.io.*; import java.util.*; @@ -11,25 +13,26 @@ public class Cartesian { public static abstract class Input implements edu.berkeley.sbp.Input { - public abstract Token next() throws IOException; + public abstract Token _next() throws IOException; public abstract boolean isCR(); long then = 0; - private Cartesian.Location location = new Cartesian.Location(0, 1); + private Cartesian.Location location = new Cartesian.Location(); public edu.berkeley.sbp.Input.Location getLocation() { return location; } - public Token next(int numstates, int resets, int waits) throws IOException { - int line = location.getRow(); - int col = location.getCol(); - Token t = next(); + public Token next() throws IOException { + int line = location.getRow(); + int col = location.getCol(); + int scalar = location.getScalar(); + Token t = _next(); if (t==null) return null; String s = " line "+line+", col " + col; while(s.length() < 20) s += " "; - s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]"; + //s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]"; long now = System.currentTimeMillis(); if (now-then > 10) { then = now; - System.out.print(s + " \r"); + //System.out.print(s + " \r"); } if (isCR()) { line++; @@ -37,19 +40,32 @@ public class Cartesian { } else { col++; } - location = new Cartesian.Location(col, line); + location.next = new Cartesian.Location(col, line, scalar+1); + location.next.prev = location; + location = location.next; return t; } + + public String showRegion(Input.Region region) { + return null; + } } /** an implementation of Location for a cartesian grid (row, col) */ public static class Location implements Input.Location, Comparable { protected final int row; protected final int col; + protected final int scalar; + Location next = null; + Location prev = null; + public Location next() { return next; } + public Location prev() { return prev; } public String toString() { return row+":"+col; } public int getCol() { return col; } public int getRow() { return row; } - public Location(int col, int row) { this.row = row; this.col = col; } + public int getScalar() { return scalar; } + public Location() { this(-1, 1, 0); } + public Location(int col, int row, int scalar) { this.row = row; this.col = col; this.scalar = scalar; } public int compareTo(Input.Location loc) throws ClassCastException { if (!(loc instanceof Cartesian.Location)) throw new ClassCastException(loc.getClass().getName()); Location c = (Location)loc; @@ -59,5 +75,27 @@ public class Cartesian { if (col > c.col) return 1; return 0; } + public Input.Region createRegion(Input.Location loc) { + return new Region(this, (Cartesian.Location)loc); } + } + + public static class Region implements Input.Region { + public final Location start; + public final Location end; + public Location getStart() { return start; } + public Location getEnd() { return end; } + public String toString() { + if (start.row==end.row && start.col==end.col) return start+""; + if (start.row==end.row) return start.row+":"+(start.col+"-"+end.col); + return start+"-"+end; + } + public Region(Location a, Location b) { + switch(a.compareTo(b)) { + case -1: + case 0: start=a; end=b; return; + case 1: start=b; end=a; return; + default: throw new Error("impossible"); + } + } } }