@Override public boolean removeAll(Collection<?> c) { boolean changed = false; for (Object o : c) { changed |= removeFast(asElementType(o)); } return changed; }
@Override public boolean equals(Object o) { if (o == this) return true; if ( !(o instanceof Array2DHashSet) ) return false; Array2DHashSet<?> other = (Array2DHashSet<?>)o; if ( other.size() != size() ) return false; boolean same = this.containsAll(other); return same; }
@Override public final boolean contains(Object o) { return containsFast(asElementType(o)); }
/** * Add {@code o} to set if not there; return existing value if already * there. This method performs the same operation as {@link #add} aside from * the return value. */ public final T getOrAdd(T o) { if ( n > threshold ) expand(); return getOrAddImpl(o); }
currentPrime += 4; int newCapacity = buckets.length * 2; T[][] newTable = createBuckets(newCapacity); int[] newBucketLengths = new int[newTable.length]; buckets = newTable; int oldSize = size(); for (T[] bucket : old) { if ( bucket==null ) { int b = getBucket(o); int bucketLength = newBucketLengths[b]; T[] newBucket; if (bucketLength == 0) { newBucket = createBucket(initialBucketCapacity); newTable[b] = newBucket;
@Override public T[] toArray() { T[] a = createBucket(size()); int i = 0; for (T[] bucket : buckets) { if ( bucket==null ) { continue; } for (T o : bucket) { if ( o==null ) { break; } a[i++] = o; } } return a; }
protected T getOrAddImpl(T o) { int b = getBucket(o); T[] bucket = buckets[b]; // NEW BUCKET if ( bucket==null ) { bucket = createBucket(initialBucketCapacity); bucket[0] = o; buckets[b] = bucket; n++; return o; } // LOOK FOR IT IN BUCKET for (int i=0; i<bucket.length; i++) { T existing = bucket[i]; if ( existing==null ) { // empty slot; not there, add. bucket[i] = o; n++; return o; } if ( comparator.equals(existing, o) ) return existing; // found existing, quit } // FULL BUCKET, expand and add to end int oldLength = bucket.length; bucket = Arrays.copyOf(bucket, bucket.length * 2); buckets[b] = bucket; bucket[oldLength] = o; // add to end n++; return o; }
public Array2DHashSet(AbstractEqualityComparator<? super T> comparator, int initialCapacity, int initialBucketCapacity) { if (comparator == null) { comparator = ObjectEqualityComparator.INSTANCE; } this.comparator = comparator; this.buckets = createBuckets(initialCapacity); this.initialBucketCapacity = initialBucketCapacity; }
public T get(T o) { if ( o==null ) return o; int b = getBucket(o); T[] bucket = buckets[b]; if ( bucket==null ) return null; // no bucket for (T e : bucket) { if ( e==null ) return null; // empty slot; not there if ( comparator.equals(e, o) ) return e; } return null; }
@Override public boolean addAll(Collection<? extends T> c) { boolean changed = false; for (T o : c) { T existing = getOrAdd(o); if ( existing!=o ) changed=true; } return changed; }
public boolean containsFast(T obj) { if (obj == null) { return false; } return get(obj) != null; }
currentPrime += 4; int newCapacity = buckets.length * 2; T[][] newTable = createBuckets(newCapacity); int[] newBucketLengths = new int[newTable.length]; buckets = newTable; int oldSize = size(); for (T[] bucket : old) { if ( bucket==null ) { int b = getBucket(o); int bucketLength = newBucketLengths[b]; T[] newBucket; if (bucketLength == 0) { newBucket = createBucket(initialBucketCapacity); newTable[b] = newBucket;
/** * Add {@code o} to set if not there; return existing value if already * there. This method performs the same operation as {@link #add} aside from * the return value. */ public final T getOrAdd(T o) { if ( n > threshold ) expand(); return getOrAddImpl(o); }
@Override public T[] toArray() { T[] a = createBucket(size()); int i = 0; for (T[] bucket : buckets) { if ( bucket==null ) { continue; } for (T o : bucket) { if ( o==null ) { break; } a[i++] = o; } } return a; }
protected T getOrAddImpl(T o) { int b = getBucket(o); T[] bucket = buckets[b]; // NEW BUCKET if ( bucket==null ) { bucket = createBucket(initialBucketCapacity); bucket[0] = o; buckets[b] = bucket; n++; return o; } // LOOK FOR IT IN BUCKET for (int i=0; i<bucket.length; i++) { T existing = bucket[i]; if ( existing==null ) { // empty slot; not there, add. bucket[i] = o; n++; return o; } if ( comparator.equals(existing, o) ) return existing; // found existing, quit } // FULL BUCKET, expand and add to end int oldLength = bucket.length; bucket = Arrays.copyOf(bucket, bucket.length * 2); buckets[b] = bucket; bucket[oldLength] = o; // add to end n++; return o; }
@Override public void clear() { buckets = createBuckets(INITAL_CAPACITY); n = 0; threshold = (int)Math.floor(INITAL_CAPACITY * LOAD_FACTOR); }
public boolean removeFast(T obj) { if (obj == null) { return false; } int b = getBucket(obj); T[] bucket = buckets[b]; if ( bucket==null ) { // no bucket return false; } for (int i=0; i<bucket.length; i++) { T e = bucket[i]; if ( e==null ) { // empty slot; not there return false; } if ( comparator.equals(e, obj) ) { // found it // shift all elements to the right down one System.arraycopy(bucket, i+1, bucket, i, bucket.length-i-1); bucket[bucket.length - 1] = null; n--; return true; } } return false; }
@Override public final boolean add(T t) { T existing = getOrAdd(t); return existing==t; }
public boolean containsFast(T obj) { if (obj == null) { return false; } return get(obj) != null; }
currentPrime += 4; int newCapacity = buckets.length * 2; T[][] newTable = createBuckets(newCapacity); int[] newBucketLengths = new int[newTable.length]; buckets = newTable; int oldSize = size(); for (T[] bucket : old) { if ( bucket==null ) { int b = getBucket(o); int bucketLength = newBucketLengths[b]; T[] newBucket; if (bucketLength == 0) { newBucket = createBucket(initialBucketCapacity); newTable[b] = newBucket;