checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / Cartesian.java
1 package edu.berkeley.sbp.misc;
2 import java.io.*;
3 import java.util.*;
4 import java.lang.reflect.*;
5 import java.lang.ref.*;
6 import edu.berkeley.sbp.*;
7 import edu.berkeley.sbp.Input.Location;
8 import edu.berkeley.sbp.util.*;
9
10 public class Cartesian {
11
12     public static abstract class Input<Token> implements edu.berkeley.sbp.Input<Token> {
13
14         public abstract Token   next() throws IOException;
15         public abstract boolean isCR();
16
17         long then = 0;
18         private Cartesian.Location location = new Cartesian.Location(0, 1);
19         public  edu.berkeley.sbp.Input.Location    getLocation() { return location; }
20
21         public Token next(int numstates, int resets, int waits) throws IOException {
22             int line  = location.getRow();
23             int col   = location.getCol();
24             Token t = next();
25             if (t==null) return null;
26             String s = "  line "+line+", col " + col;
27             while(s.length() < 20) s += " ";
28             s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]";
29             long now = System.currentTimeMillis();
30             if (now-then > 10) {
31                 then = now;
32                 System.out.print(s + "                                \r");
33             }
34             if (isCR()) { 
35                 line++;
36                 col = 1;
37             } else {
38                 col++;
39             }
40             location = new Cartesian.Location(col, line);
41             return t;
42         }
43     }
44
45     /** an implementation of Location for a cartesian grid (row, col) */
46     public static class Location<Tok> implements Input.Location<Tok>, Comparable<Input.Location> {
47         protected final int row;
48         protected final int col;
49         public String toString() { return row+":"+col; }
50         public int getCol() { return col; }
51         public int getRow() { return row; }
52         public Location(int col, int row) { this.row = row; this.col = col; }
53         public int compareTo(Input.Location loc) throws ClassCastException {
54             if (!(loc instanceof Cartesian.Location)) throw new ClassCastException(loc.getClass().getName());
55             Location<Tok> c = (Location<Tok>)loc;
56             if (row < c.row) return -1;
57             if (row > c.row) return  1;
58             if (col < c.col) return -1;
59             if (col > c.col) return  1;
60             return 0;
61         }
62         public Input.Region<Tok> createRegion(Input.Location<Tok> loc) {
63             return new Region<Tok>(this, (Cartesian.Location<Tok>)loc); }
64     }
65
66     public static class Region<Tok> implements Input.Region<Tok> {
67         public final Location<Tok> start;
68         public final Location<Tok> end;
69         public String toString() {
70             if (start.row==end.row) return start.row+":"+(start.col+"-"+end.col);
71             return start+"-"+end;
72         }
73         public Region(Location<Tok> a, Location<Tok> b) {
74             switch(a.compareTo(b)) {
75                 case -1:
76                 case  0: start=a; end=b; return;
77                 case  1: start=b; end=a; return;
78                 default: throw new Error("impossible");
79             }
80         }
81     }
82 }