package structures; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Random; import shared.KillSwitch; import shared.Primes; import shared.Shared; import shared.Timer; import shared.Tools; /** * @author Brian Bushnell * @date July 6, 2016 * */ public final class LongHashSet{ public static void main(String[] args){ Random randy2=Shared.threadLocalRandom(); LongHashSet set=new LongHashSet(20, 0.7f); HashSet set2=new HashSet(20, 0.7f); ArrayList list=new ArrayList(); ArrayList list2=new ArrayList(); for(long i=0; i<1000; i++){ assert(!set.contains(i)); assert(!set2.contains(i)); list.add(Long.valueOf(i)); } for(int i=0; i<1000; i++){ long r=randy2.nextLong(); list2.add(r); } for(long x : list){ set.add(x); set2.add(x); assert(set.contains(x)); assert(set2.contains(x)); } for(long x : list){ assert(set.contains(x)); assert(set2.contains(x)); set.remove(x); set2.remove(x); assert(!set.contains(x)); assert(!set2.contains(x)); } assert(set.isEmpty()); assert(set2.isEmpty()); for(long x : list2){ set.add(x); set2.add(x); assert(set.contains(x)); assert(set2.contains(x)); } for(long x : list2){ assert(set.contains(x)); assert(set2.contains(x)); set.remove(x); set2.remove(x); assert(!set.contains(x)); assert(!set2.contains(x)); } assert(set.isEmpty()); assert(set2.isEmpty()); int count=4000000; int runs=32; LongList ll=new LongList(count); for(int i=0; i0) : "Attempting to initialize a "+getClass().getSimpleName()+" of size<1."; assert(loadFactor_>0 && loadFactor_<1) : "Attempting to initialize a "+getClass().getSimpleName()+" with invalid load factor: "+loadFactor_; loadFactor=Tools.mid(0.25f, loadFactor_, 0.90f); resize(initialSize); } /*--------------------------------------------------------------*/ /*---------------- Public Methods ----------------*/ /*--------------------------------------------------------------*/ public void clear(){ if(size<1){return;} Arrays.fill(array, invalid); size=0; } public boolean contains(long value){ return value==invalid ? false : findCell(value)>=0; } /** * Add this value to the set. * @param value * @return true if the value was added, false if it was already contained. */ public boolean add(long value){ if(value==invalid){resetInvalid();} int cell=findCellOrEmpty(value); if(array[cell]==invalid){ array[cell]=value; size++; if(size>sizeLimit){resize();} return true; } assert(array[cell]==value); return false; } /** * Remove this value from the set. * @param value * @return true if the value was removed, false if it was not present. */ public boolean remove(long value){ if(value==invalid){return false;} final int cell=findCell(value); if(cell<0){return false;} assert(array[cell]==value); array[cell]=invalid; size--; rehashFrom(cell); return true; } public int size(){return size;} public boolean isEmpty(){return size==0;} /*--------------------------------------------------------------*/ /*---------------- String Methods ----------------*/ /*--------------------------------------------------------------*/ @Override public String toString(){ return toStringListView(); } public String toStringSetView(){ StringBuilder sb=new StringBuilder(); sb.append('['); String comma=""; for(int i=0; iInteger.MAX_VALUE){ newPrime=Primes.primeAtMost(Integer.MAX_VALUE-extra); } assert(newPrime>modulus) : "Overflow: "+size+", "+size2+", "+modulus+", "+newPrime; modulus=(int)newPrime; final int size3=(int)(newPrime+extra); sizeLimit=(int)(modulus*loadFactor); final long[] old=array; array=KillSwitch.allocLong1D(size3); Arrays.fill(array, invalid); // System.err.println("Resizing "+(old==null ? "null" : ""+old.length)+" to "+size3); if(size<1){return;} size=0; for(long value : old){ if(value!=invalid){ add(value); } } } public final void resizeDestructive(int newSize){ size=0; sizeLimit=0; array=null; resize(newSize); } /** Number of values that can be held without resizing */ public int capacity(){ return sizeLimit; } /*--------------------------------------------------------------*/ /*---------------- Fields ----------------*/ /*--------------------------------------------------------------*/ private long[] array; private int size=0; /** Value for empty cells */ private long invalid; private int modulus; private int sizeLimit; private final float loadFactor; private static final Random randy=new Random(1); private static final long MASK=Long.MAX_VALUE; private static final long MINMASK=Long.MIN_VALUE; private static final int extra=10; }