optimizations to IntPairMap.java
[sbp.git] / src / edu / berkeley / sbp / util / FastSet.java
index c8302b5..289e5ec 100644 (file)
@@ -3,7 +3,7 @@
 package edu.berkeley.sbp.util;
 import java.util.*;
 
-public /*final*/ class FastSet<T> implements Iterator<T>, Iterable<T>, Visitable<T> {
+public final class FastSet<T> implements Iterator<T>, Iterable<T>, Visitable<T> {
 
     public static final int INITIAL_SIZE = 8;
 
@@ -22,6 +22,12 @@ public /*final*/ class FastSet<T> implements Iterator<T>, Iterable<T>, Visitable
         return (T)array[i];
     }
 
+    public T first() {
+        if (only != null) return only;
+        if (array != null) return (T)(array[0]);
+        return null;
+    }
+
     public FastSet() { }
     public FastSet(T t) { only = t; }
     public FastSet(Set<T> s) {
@@ -36,17 +42,22 @@ public /*final*/ class FastSet<T> implements Iterator<T>, Iterable<T>, Visitable
             return;
         }
         boolean found = false;
-        for(int j=0; j<size; j++) {
-            if (array[j]==t) found = true;
-            if (found && j<size-1) array[j] = array[j+1];
-        }
-        if (found) size--;
+        int j;
+        for(j=0; j<size; j++)
+            if (array[j]==t) {
+                found = true;
+                break;
+            }
+        if (!found) return;
+        array[j] = array[size-1];
+        array[size-1] = null;
+        size--;
     }
 
-    public <B> void visit(Invokable<T,B> ivbc, B b) {
-        if (only!=null) ivbc.invoke(only, b);
+    public <B,C> void visit(Invokable<T,B,C> ivbc, B b, C c) {
+        if (only!=null) ivbc.invoke(only, b, c);
         else for(int j=0; j<size; j++)
-            ivbc.invoke((T)array[j], b);
+                 ivbc.invoke((T)array[j], b, c);
     }
 
     public int size() { return only==null ? size : 1; }