X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Futil%2FFastSet.java;h=289e5ec22c1546ae98569aff99368a5aa64164d4;hp=086f636a3a712466a69c6c10b82481c5729cc88a;hb=f069d11a0bc59d63b078df8a4aa488498c4e9cc2;hpb=f9536341fe8a0fc979f307d9ad5a189c96ec5850 diff --git a/src/edu/berkeley/sbp/util/FastSet.java b/src/edu/berkeley/sbp/util/FastSet.java index 086f636..289e5ec 100644 --- a/src/edu/berkeley/sbp/util/FastSet.java +++ b/src/edu/berkeley/sbp/util/FastSet.java @@ -1,13 +1,15 @@ +// Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license + package edu.berkeley.sbp.util; import java.util.*; -public /*final*/ class FastSet implements Iterator, Iterable { +public final class FastSet implements Iterator, Iterable, Visitable { - public static final int INITIAL_SIZE = 128; + public static final int INITIAL_SIZE = 8; - private Object[] array; + private Object[] array = null; private T only = null; - private int i = 0; + private int i = -1; private int size = 0; public Iterator iterator() { i=0; return this; } @@ -20,6 +22,12 @@ public /*final*/ class FastSet implements Iterator, Iterable { 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 s) { @@ -28,6 +36,30 @@ public /*final*/ class FastSet implements Iterator, Iterable { for(T t : s) array[size++] = t; } + public void remove(T t) { + if (only != null) { + if (only==t) only=null; + return; + } + boolean found = false; + int j; + for(j=0; j void visit(Invokable ivbc, B b, C c) { + if (only!=null) ivbc.invoke(only, b, c); + else for(int j=0; j implements Iterator, Iterable { } } public void add(T t, boolean check) { - if (check) for(Object o : this) if (o.equals(t)) return; + //if (check) for(Object o : this) if (o.equals(t)) return; + if (check) { + if (only==t) return; + if (array != null) + for(int i=0; i