checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / Cartesian.java
diff --git a/src/edu/berkeley/sbp/misc/Cartesian.java b/src/edu/berkeley/sbp/misc/Cartesian.java
new file mode 100644 (file)
index 0000000..6627fbf
--- /dev/null
@@ -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<Token> implements edu.berkeley.sbp.Input<Token> {
+
+        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<Tok> implements Input.Location<Tok>, Comparable<Input.Location> {
+        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<Tok> c = (Location<Tok>)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;
+        }
+    }
+}