package structures; import java.util.Arrays; import java.util.Random; import shared.KillSwitch; import shared.Tools; /** * Like IntHashMap, but uses a power-of-2 length to avoid modulo operations. * @author Brian Bushnell * @date June 8, 2017 * */ public final class IntHashMapBinary extends AbstractIntHashMap{ public static void main(String[] args){ IntHashMapBinary set=new IntHashMapBinary(32, 0.7f); test(set); } /*--------------------------------------------------------------*/ /*---------------- Initialization ----------------*/ /*--------------------------------------------------------------*/ public IntHashMapBinary(){ this(256); } public IntHashMapBinary(int initialSize){ this(initialSize, 0.7f); } public IntHashMapBinary(int initialSize, float loadFactor_){ if(Integer.bitCount(initialSize)>1){ int zeros=Integer.numberOfLeadingZeros(initialSize); if(zeros<2){zeros=2;} initialSize=1<<(32-zeros); } invalid=randy.nextInt()|MINMASK; assert(invalid<0); assert(initialSize>0); assert(loadFactor_>0 && loadFactor_<1); loadFactor=Tools.mid(0.25f, loadFactor_, 0.90f); resize(initialSize); } /*--------------------------------------------------------------*/ /*---------------- Public Methods ----------------*/ /*--------------------------------------------------------------*/ @Override public void clear(){ if(size<1){return;} Arrays.fill(keys, invalid); Arrays.fill(values, 0); size=0; } @Override public int get(int key){ int cell=findCell(key); return cell<0 ? -1 : values[cell]; } @Override public int put(int key, int value){return set(key, value);} @Override public int set(int key, int value){ if(key==invalid){resetInvalid();} final int cell=findCellOrEmpty(key); final int oldV=values[cell]; values[cell]=value; if(keys[cell]==invalid){ keys[cell]=key; size++; if(size>sizeLimit){resize();} } // assert(get(key)==value);//TODO: slow return oldV; } @Override public int increment(int key){ return increment(key, 1); } @Override public int increment(int key, int incr){ if(key==invalid){resetInvalid();} final int cell=findCellOrEmpty(key); final int oldV=values[cell]; final int value=oldV+incr; values[cell]=value; values[cell]=Tools.min(Integer.MAX_VALUE, value); if(keys[cell]==invalid){ keys[cell]=key; size++; if(size>sizeLimit){resize();} } // assert(get(key)==value);//TODO: slow return value; } @Override public boolean remove(int key){ if(key==invalid){return false;} final int cell=findCell(key); if(cell<0){return false;} assert(keys[cell]==key); keys[cell]=invalid; values[cell]=0; size--; rehashFrom(cell); return true; } /*--------------------------------------------------------------*/ /*---------------- Private Methods ----------------*/ /*--------------------------------------------------------------*/ private void rehashFrom(int initial){ if(size<1){return;} final int limit=keys.length; for(int cell=initial+1; cell0) : newModulus; assert(newModulus+extramodulus); // System.err.println("size="+size+", modulus="+modulus+" -> size2="+size2+", modulus2="+newModulus); modulus=(int)newModulus; final int size3=(int)(newModulus+extra); sizeLimit=(int)(newModulus*loadFactor); final int[] oldK=keys; final int[] oldV=values; keys=KillSwitch.allocInt1D(size3); values=KillSwitch.allocInt1D(size3); Arrays.fill(keys, invalid); // System.err.println("Resizing "+(old==null ? "null" : ""+old.length)+" to "+size3); if(size<1){return;} size=0; for(int i=0; i