added more
authoradam <adam@megacz.com>
Sun, 8 Jan 2006 09:39:00 +0000 (04:39 -0500)
committeradam <adam@megacz.com>
Sun, 8 Jan 2006 09:39:00 +0000 (04:39 -0500)
darcs-hash:20060108093900-5007d-e9f9e1fc3ca983fb1390b2ce2a86853ba00d28bf.gz

TODO
src/edu/berkeley/sbp/util/ConcatenatedIterator.java [new file with mode: 0644]
src/edu/berkeley/sbp/util/IntPairMap.java [new file with mode: 0644]
src/edu/berkeley/sbp/util/IntegerMappable.java [new file with mode: 0644]

diff --git a/TODO b/TODO
index c7ad150..b383c90 100644 (file)
--- a/TODO
+++ b/TODO
@@ -2,10 +2,6 @@ _____________________________________________________________________________
 Immediately
 
   - Performance
 Immediately
 
   - Performance
-
-     - Forest: keep() and valid() -- can we do this with states
-       rather than subtrees?
-
      - hash Long->long: it's all bogus
 
   * pick back up cleaning up end of Parser.java (Reduction)
      - hash Long->long: it's all bogus
 
   * pick back up cleaning up end of Parser.java (Reduction)
diff --git a/src/edu/berkeley/sbp/util/ConcatenatedIterator.java b/src/edu/berkeley/sbp/util/ConcatenatedIterator.java
new file mode 100644 (file)
index 0000000..7f5f749
--- /dev/null
@@ -0,0 +1,26 @@
+package edu.berkeley.sbp.util;
+import java.util.*;
+
+public final class ConcatenatedIterator<T> implements Iterator<T>, Iterable<T> {
+
+    private final Iterator<Iterable<T>> ita;
+    private       Iterator<T> cur;
+
+    public ConcatenatedIterator(Iterator<Iterable<T>> ita) {
+        this.ita = ita;
+        cur = ita.hasNext() ? ita.next().iterator() : null;
+    }
+
+    public void    remove()       { throw new Error(); }
+    public Iterator<T> iterator() { return this; }
+    public boolean hasNext()      {
+        while (cur!=null && !cur.hasNext())
+            cur = ita.hasNext() ? ita.next().iterator() : null;
+        return cur!=null && cur.hasNext();
+    }
+    public T       next()         {
+        while (cur!=null && !cur.hasNext())
+            cur = ita.hasNext() ? ita.next().iterator() : null;
+        return cur==null ? null : cur.next();
+    }
+}
diff --git a/src/edu/berkeley/sbp/util/IntPairMap.java b/src/edu/berkeley/sbp/util/IntPairMap.java
new file mode 100644 (file)
index 0000000..c355488
--- /dev/null
@@ -0,0 +1,18 @@
+package edu.berkeley.sbp.util;
+import java.util.*;
+
+/** a mapping from keys of type <tt>K</tt> to <i>sets</i> of values of type <tt>T</tt> */
+public final class IntPairMap<V> {
+
+    private final HashMap<Long, V> hm = new HashMap<Long, V>();
+
+    private static long key(IntegerMappable k1, IntegerMappable k2) {
+        return (k1==null ? 0 : (((long)k1.toInt()) << 32)) | (k2==null ? 0 : k2.toInt());
+    }
+
+    public void put(IntegerMappable k1, IntegerMappable k2, V v) { hm.put(key(k1, k2), v); }
+    public V get(IntegerMappable k1, IntegerMappable k2) { return hm.get(key(k1, k2)); }
+    public Iterable<V> values() { return hm.values(); }
+    public int size() { return hm.size(); }
+    public void toArray(V[] v) { hm.values().toArray(v); }
+}
diff --git a/src/edu/berkeley/sbp/util/IntegerMappable.java b/src/edu/berkeley/sbp/util/IntegerMappable.java
new file mode 100644 (file)
index 0000000..34a96e7
--- /dev/null
@@ -0,0 +1,5 @@
+package edu.berkeley.sbp.util;
+
+public interface IntegerMappable {
+    public int toInt();
+}