X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FCartesian.java;fp=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FCartesian.java;h=6627fbf1c5b8ce6c77240db2810347fc12152034;hb=a96edcbb9051f33f65256dcd5adcbae6925956eb;hp=0000000000000000000000000000000000000000;hpb=225993309e6183afa9a88fc13d39df56be54b992;p=sbp.git diff --git a/src/edu/berkeley/sbp/misc/Cartesian.java b/src/edu/berkeley/sbp/misc/Cartesian.java new file mode 100644 index 0000000..6627fbf --- /dev/null +++ b/src/edu/berkeley/sbp/misc/Cartesian.java @@ -0,0 +1,63 @@ +package edu.berkeley.sbp.misc; +import java.io.*; +import java.util.*; +import java.lang.reflect.*; +import java.lang.ref.*; +import edu.berkeley.sbp.*; +import edu.berkeley.sbp.Input.Location; +import edu.berkeley.sbp.util.*; + +public class Cartesian { + + public static abstract class Input implements edu.berkeley.sbp.Input { + + public abstract Token next() throws IOException; + public abstract boolean isCR(); + + long then = 0; + private Cartesian.Location location = new Cartesian.Location(0, 1); + 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(); + 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 + "]"; + long now = System.currentTimeMillis(); + if (now-then > 10) { + then = now; + System.out.print(s + " \r"); + } + if (isCR()) { + line++; + col = 1; + } else { + col++; + } + location = new Cartesian.Location(col, line); + return t; + } + } + + /** 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; + 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 compareTo(Input.Location loc) throws ClassCastException { + if (!(loc instanceof Cartesian)) throw new ClassCastException(); + Location c = (Location)loc; + if (row < c.row) return -1; + if (row > c.row) return 1; + if (col < c.col) return -1; + if (col > c.col) return 1; + return 0; + } + } +}