checkpoint
[sbp.git] / src / edu / berkeley / sbp / Input.java
index eeb3cc1..be41cbe 100644 (file)
@@ -10,14 +10,46 @@ import edu.berkeley.sbp.util.*;
 public interface Input<Token> {
 
     /** returns the token just beyond the current location and advances beyond it */
-    public Token    next() throws IOException;
+    public Token           next() throws IOException;
 
     /** returns the location the input stream is currently at */
-    public Location getLocation();
+    public Location<Token> getLocation();
 
-    /** a location *between tokens* in the input stream */
-    public static interface Location {
+    /** a location <i>between tokens<i> in the input stream */
+    public static interface Location<Tok> extends Comparable<Location> {
         public String toString();
+
+        /** an implementation of Location for a cartesian grid (row, col) */
+        public static class Cartesian<Tok> implements Location<Tok>, Comparable<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 Cartesian(int col, int row) { this.row = row; this.col = col; }
+            public int compareTo(Location loc) throws ClassCastException {
+                if (!(loc instanceof Cartesian)) throw new ClassCastException();
+                Cartesian<Tok> c = (Cartesian<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;
+            }
+        }
+    }
+
+    public static class Region<Loc extends Location> /* implements Topology<Location<Tok>> */ {
+        public final Loc start;
+        public final Loc end;
+        public Region(Loc a, Loc 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");
+            }
+        }
     }
 }