581886ec0163f3508ad16bbc3acf3de8648291a9
[sbp.git] / src / edu / berkeley / sbp / util / FastSet.java
1 // Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp.util;
4 import java.util.*;
5
6 public /*final*/ class FastSet<T> implements Iterator<T>, Iterable<T>, Visitable<T> {
7
8     public static final int INITIAL_SIZE = 8;
9
10     private       Object[] array = null;
11     private       T        only  = null;
12     private       int      i     = -1;
13     private       int      size  = 0;
14
15     public Iterator<T> iterator() { i=0; return this; }
16     public void        remove()   { throw new Error(); }
17     public boolean     hasNext()  { return only==null ? i<size : i<1; }
18     public T           next()     { return only==null ? (T)array[i++] : (i++)==0 ? only : null; }
19     public T get(int i) {
20         if (i==0 && only!=null) return only;
21         if (array==null) return null;
22         return (T)array[i];
23     }
24
25     public T first() {
26         if (only != null) return only;
27         if (array != null) return (T)(array[0]);
28         return null;
29     }
30
31     public FastSet() { }
32     public FastSet(T t) { only = t; }
33     public FastSet(Set<T> s) {
34         if (s.size()==1) { only = s.iterator().next(); return; }
35         array = new Object[s.size()];
36         for(T t : s) array[size++] = t;
37     }
38
39     public void remove(T t) {
40         if (only != null) {
41             if (only==t) only=null;
42             return;
43         }
44         boolean found = false;
45         for(int j=0; j<size; j++) {
46             if (array[j]==t) found = true;
47             if (found && j<size-1) array[j] = array[j+1];
48         }
49         if (found) size--;
50     }
51
52     public <B,C> void visit(Invokable<T,B,C> ivbc, B b, C c) {
53         if (only!=null) ivbc.invoke(only, b, c);
54         else for(int j=0; j<size; j++)
55                  ivbc.invoke((T)array[j], b, c);
56     }
57
58     public int size() { return only==null ? size : 1; }
59     private void grow() {
60         Object[] array2 = array==null ? new Object[INITIAL_SIZE] : new Object[array.length * 2];
61         if (array != null) System.arraycopy(array, 0, array2, 0, array.length);
62         array = array2;
63         if (only!=null) {
64             array[size++] = only;
65             only = null;
66         }
67     }
68     public void add(T t, boolean check) {
69         //if (check) for(Object o : this) if (o.equals(t)) return;
70         if (check) {
71             if (only==t) return;
72             if (array != null)
73                 for(int i=0; i<size; i++)
74                     if (array[i]==t) return;
75         }
76         add(t);
77     }
78     public void add(T t) {
79         if (array==null) {
80             if (only!=null) { only = t; return; }
81             grow();
82         } else if (size>=array.length-1) {
83             grow();
84         }
85         array[size++] = t;
86     }
87
88     public boolean contains(T t) {
89         if (t==only) return true;
90         if (array==null) return false;
91         for(Object o : array) if (o==t) return true;
92         return false;
93     }
94 }