package structures; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Random; import shared.KillSwitch; import shared.Primes; import shared.Shared; import shared.Timer; import shared.Tools; /** * This class should allow mapping a long to one or more values. * For a single value, it will act as a LongLongHashMap. * For multiple values, it will act more like a LongLongListHashMap. * The primary value stored when there are multiple values will be the * (-index-OFFSET) of the list's index. * As such, this does NOT support negative values, though it could be * modified to support most negative values, by making OFFSET large. * However, that makes the logic of determining whether a key is present * from the return value more confusing. * @author Brian Bushnell * @date November 13, 2024 * */ public final class LongLongHashMapHybrid{ public static void main(String[] args){ Random randy2=Shared.threadLocalRandom(); LongLongHashMapHybrid map=new LongLongHashMapHybrid(20, 0.7f); HashMap map2=new HashMap(20, 0.7f); ArrayList list=new ArrayList(); ArrayList list2=new ArrayList(); // ArrayList vals=new ArrayList(); for(long i=0; i<1000; i++){ assert(!map.contains(i)); assert(!map2.containsKey(i)); list.add(Long.valueOf(i)); } for(int i=0; i<1000; i++){ long r=randy2.nextLong(Long.MAX_VALUE/4); list2.add(r); } for(long x : list){ map.put(x, (2*x)); map2.put(x, (2*x)); assert(map.get(x)==((2*x))); assert(map2.get(x)==((2*x))); } for(long x : list){ assert(map.get(x)==((2*x))); assert(map2.get(x)==((2*x))); map.remove(x); map2.remove(x); assert(!map.contains(x)); assert(!map2.containsKey(x)); } assert(map.isEmpty()); assert(map2.isEmpty()); for(long x : list2){ map.put(x, (2*x)); map2.put(x, (2*x)); assert(map.get(x)==((2*x))); assert(map2.get(x)==((2*x))); } for(long x : list2){ assert(map.get(x)==((2*x))); assert(map2.get(x)==((2*x))); map.remove(x); map2.remove(x); assert(!map.contains(x)); assert(!map2.containsKey(x)); } assert(map.isEmpty()); assert(map2.isEmpty()); int count=4000000; int runs=32; LongList ll=new LongList(count); for(int i=0; i0); assert(loadFactor_>0 && loadFactor_<1); loadFactor=Tools.mid(0.25f, loadFactor_, 0.90f); resize(initialSize); } /*--------------------------------------------------------------*/ /*---------------- Public Methods ----------------*/ /*--------------------------------------------------------------*/ public void clear(){ if(size<1){return;} Arrays.fill(keys, invalid); Arrays.fill(values, 0); size=0; // assert(verify()); //123 } public boolean contains(long key){ // assert(verify()); //123 return key==invalid ? false : findCell(key)>=0; } public boolean containsKey(long key){ return contains(key); } public long get(long key){ // assert(verify()); //123 long value=NOTPRESENT; if(key!=invalid){ int cell=findCell(key); if(cell>=0){value=values[cell];} } return value; } public LongList getListFromCode(long code){ assert(code<=-OFFSET) : code; return multivalues.get((int)(-code-OFFSET)); } public LongList getOrFill(long key, LongList buffer){ // assert(buffer.isEmpty()); buffer.clear(); long code=get(key); if(code==NOTPRESENT) {return buffer;} if(code>=0) {buffer.add(code); return buffer;} return multivalues.get((int)(-code-OFFSET)); } public long fill(long key, LongList buffer){ // assert(buffer.isEmpty()); buffer.clear(); long code=get(key); if(code==NOTPRESENT) {return code;} if(code>=0) {buffer.add(code);} else {buffer.addAll(multivalues.get((int)(-code-OFFSET)));} return code; } /** * Map this key to value. * @param key * @param value * @return true if the value was added, false if it was already contained. */ public boolean put(long key, long value){ assert(value>=0) : "Unsupported negative value "+value; return putInner(key, value); } /** * Map this key to value. * @param key * @param value * @return true if the value was added, false if it was already contained. */ public boolean putInner(long key, long value){ // assert(verify()); //123 if(key==invalid){resetInvalid();} int cell=findCellOrEmpty(key); if(keys[cell]==invalid){ keys[cell]=key; values[cell]=value; size++; if(size>sizeLimit){resize();} // assert(verify()); //123 return true; } assert(keys[cell]==key); final long vCell=values[cell]; if(vCell==value) {return false;} if(vCell>=0) { int listIndex=multivalues.size(); LongList list=new LongList(4); multivalues.add(list); list.add(values[cell]); list.add(value); values[cell]=-listIndex-OFFSET; return true; } int listIndex=(int)(-vCell-OFFSET); LongList list=multivalues.get(listIndex); // if(list.contains(value)) {return false;}//Slow and not really needed for indexing. list.add(value); // assert(verify()); //123 return true; } /** * Remove this key from the map. * @param key * @return Old value. */ public long remove(long key){ //This operation is difficult when the key has multiple values //It can be done, but will leave a hole (null list) //No reason to do it when used for indexing anyway // throw new RuntimeException("Unimplemented"); // assert(verify()); if(key==invalid){return NOTPRESENT;} final int cell=findCell(key); if(cell<0){return NOTPRESENT;} assert(keys[cell]==key || keys[cell]=thresh){ assert(keys[i]>=0) : "\nNegative key ("+keys[i]+", "+values[i]+", "+i+") for thresh "+thresh+":\n"+toStringSetView(); len++; } } long[] x=KillSwitch.allocLong1D(len); for(int i=0, j=0; j=thresh){ x[j]=keys[i]; assert(keys[i]>=0) : "\nNegative key ("+keys[i]+", "+values[i]+", "+i+") for thresh "+thresh+":\n"+toStringSetView(); j++; } } return x; } /*--------------------------------------------------------------*/ /*---------------- Private Methods ----------------*/ /*--------------------------------------------------------------*/ public boolean verify(){ if(keys==null){return true;} int numValues=0; int numFound=0; for(int i=0; i=0) ? keys[cell]+", "+values[cell]+"\n" : ""); return false; } } } boolean pass=(numValues==numFound && numValues==size); assert(pass) : numValues+", "+numFound+", "+size; return pass; } private void rehashFrom(int initial){ if(size<1){return;} final int limit=keys.length; for(int cell=initial+1; cellInteger.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[] oldKeys=keys; final long[] oldValues=values; keys=KillSwitch.allocLong1D(size3); values=KillSwitch.allocLong1D(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 multivalues=new ArrayList(4); private long[] keys; private long[] values; 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; public static final long NOTPRESENT=-1; private static final long OFFSET=2; private static final int extra=10; }