/****************************************************************************** CHANGE LOG. 23 Aug 98: Added stringifyP3, stringifyRD3, etc. Added various methods for parsing on StreamTokenizers (expect(), etc.). [pike] 23 Nov 98: Added one or two more stringify methods. [pike] 25 Nov 98: Added parseNthInt. [pike] 18 Dec 98: Fixed stringifyBoxedListRD3() to handle a null argument correctly. [pike] ******************************************************************************/ import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.PrintStream; import java.io.IOException; import java.io.StreamTokenizer; // Temporary. import ti.domains.*; // Temporary end. class Util { static RectDomain<3> doubledDomain(RectDomain<3> r) { return [r.min() * 2 : r.max() * 2 + [1, 1, 1]]; } static int parseNthInt(String[] a, int n) throws IOException { if (a.length <= n) throw new IOException(); else return Integer.parseInt(a[n]); } static String local stringify(Point<3> p) { return stringifyP3(p); } static String local stringifyP3(Point<3> p) { return (String local) ("[" + p[1] + ", " + p[2] + ", " + p[3] + "]"); } static String local stringifyBoxedListRD3(BoxedList_RectDomain_3 l) { return (l == null) ? "{}" : stringifyListRD3(l.toList()); } static String local stringifyListRD3(List_RectDomain_3 l) { String local s; if (l == null) return "{}"; else s = (String local) ("{" + stringifyRD3(l.first())); while ((l = l.rest()) != null) s = (String local) (s + ", " + stringifyRD3(l.first())); return (String local) (s + "}"); } static String local stringify(RectDomain<3> R) { return stringifyRD3(R); } static String local stringifyRD3(RectDomain<3> R) { return (String local) ("[" + stringifyP3(R.min()) + " : " + stringifyP3(R.max()) + ((R.stride() != [1, 1, 1]) ? (" : " + stringifyP3(R.stride())) : "") + "]"); } static void printP3(Point<3> p) { System.out.print(stringifyP3(p)); } static void printRD3(RectDomain<3> R) { System.out.print(stringifyRD3(R)); } static void printlnRD3(RectDomain<3> R) { printRD3(R); System.out.println(""); } static void printlnD3(Domain<3> D) { System.out.println("_______"); RectDomain<3>[1d] l = D.RectDomainList(); foreach (i in l.domain()) { printlnRD3(l[i]); } System.out.println("~~~~~~~"); } static void expect(StreamTokenizer s, String q) throws IOException { if ((s.ttype != StreamTokenizer.TT_WORD || !s.sval.equals(q)) && (q.length() != 1 || q.charAt(0) != (char) s.ttype)) { System.err.println("Expecting `" + q + "' but got " + s); throw new IOException(); } s.nextToken(); } static void expectInt(StreamTokenizer s, int i) throws IOException { if (getInt(s) != i) throw new IOException(); } static void skipUpTo(StreamTokenizer s, String q) throws IOException { boolean q1 = (q.length() == 1); while ((s.ttype != StreamTokenizer.TT_WORD || !s.sval.equals(q)) && (!q1 || s.ttype != (char) q.charAt(0))) { // System.out.println("Skip up to " + q + ": got " + s); s.nextToken(); } // System.out.print("Skip up to " + q + ": got " + s + "; "); s.nextToken(); // System.out.println("next token is " + s); } static int getInt(StreamTokenizer s) throws IOException { if (s.ttype != StreamTokenizer.TT_NUMBER) throw new IOException(); int r = (int) s.nval; s.nextToken(); return r; } static double getDouble(StreamTokenizer s) throws IOException { if (s.ttype != StreamTokenizer.TT_NUMBER) throw new IOException(); double r = s.nval; s.nextToken(); if (s.ttype == StreamTokenizer.TT_WORD && s.sval.length() > 1 && (s.sval.charAt(0) == 'e' || s.sval.charAt(0) == 'E')) { try { r *= Math.pow(10, Integer.parseInt(s.sval.substring(1))); } catch (Throwable x) { throw new IOException(); } s.nextToken(); } return r; } /* Returns whether p lexicographically preceeds q. */ static boolean greater(Point<3> q, Point<3> p) { return less(p, q); } /* Returns whether p lexicographically preceeds q. */ static boolean less(Point<3> p, Point<3> q) { return p[1] < q[1] || p[1] == q[1] && (p[2] < q[2] || p[2] == q[2] && p[3] < q[3]); } }