projects
/
sbp.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
copyright notices/updates
[sbp.git]
/
src
/
edu
/
berkeley
/
sbp
/
util
/
FastSet.java
diff --git
a/src/edu/berkeley/sbp/util/FastSet.java
b/src/edu/berkeley/sbp/util/FastSet.java
index
a433b7c
..
26c5f33
100644
(file)
--- a/
src/edu/berkeley/sbp/util/FastSet.java
+++ b/
src/edu/berkeley/sbp/util/FastSet.java
@@
-1,19
+1,26
@@
+// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
+
package edu.berkeley.sbp.util;
import java.util.*;
package edu.berkeley.sbp.util;
import java.util.*;
-public final class FastSet<T> implements Iterator<T>, Iterable<T> {
+public /*final*/ class FastSet<T> implements Iterator<T>, Iterable<T>, Visitable<T> {
- 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 T only = null;
- private int i = 0;
+ private int i = -1;
private int size = 0;
public Iterator<T> iterator() { i=0; return this; }
public void remove() { throw new Error(); }
public boolean hasNext() { return only==null ? i<size : i<1; }
public T next() { return only==null ? (T)array[i++] : (i++)==0 ? only : null; }
private int size = 0;
public Iterator<T> iterator() { i=0; return this; }
public void remove() { throw new Error(); }
public boolean hasNext() { return only==null ? i<size : i<1; }
public T next() { return only==null ? (T)array[i++] : (i++)==0 ? only : null; }
+ public T get(int i) {
+ if (i==0 && only!=null) return only;
+ if (array==null) return null;
+ return (T)array[i];
+ }
public FastSet() { }
public FastSet(T t) { only = t; }
public FastSet() { }
public FastSet(T t) { only = t; }
@@
-23,6
+30,12
@@
public final class FastSet<T> implements Iterator<T>, Iterable<T> {
for(T t : s) array[size++] = t;
}
for(T t : s) array[size++] = t;
}
+ 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);
+ }
+
public int size() { return only==null ? size : 1; }
private void grow() {
Object[] array2 = array==null ? new Object[INITIAL_SIZE] : new Object[array.length * 2];
public int size() { return only==null ? size : 1; }
private void grow() {
Object[] array2 = array==null ? new Object[INITIAL_SIZE] : new Object[array.length * 2];
@@
-34,7
+47,13
@@
public final class FastSet<T> implements Iterator<T>, Iterable<T> {
}
}
public void add(T t, boolean check) {
}
}
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<size; i++)
+ if (array[i]==t) return;
+ }
add(t);
}
public void add(T t) {
add(t);
}
public void add(T t) {