add FastSet.first() for quick access to the first element of a FastSet
[sbp.git] / src / edu / berkeley / sbp / util / FastSet.java
index 711c984..581886e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
+// Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
 
 package edu.berkeley.sbp.util;
 import java.util.*;
@@ -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) {
@@ -46,7 +52,7 @@ public /*final*/ class FastSet<T> implements Iterator<T>, Iterable<T>, Visitable
     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, c);
+                 ivbc.invoke((T)array[j], b, c);
     }
 
     public int size() { return only==null ? size : 1; }