@Override public <T> T[] toArray(T[] a) { Object[] kvs = raw_array(); // Estimate size of array; be prepared to see more or fewer elements int sz = size(); T[] r = a.length >= sz ? a : (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), sz); // Fast efficient element walk. int j=0; for( int i=0; i<len(kvs); i++ ) { Object K = key(kvs,i); Object V = Prime.unbox(val(kvs,i)); if( K != null && K != TOMBSTONE && V != null && V != TOMBSTONE ) { if( j >= r.length ) { int sz2 = (int)Math.min(Integer.MAX_VALUE-8,((long)j)<<1); if( sz2<=r.length ) throw new OutOfMemoryError("Required array size too large"); r = Arrays.copyOf(r,sz2); } r[j++] = (T)K; } } if( j <= a.length ) { // Fit in the original array? if( a!=r ) System.arraycopy(r,0,a,0,j); if( j<a.length ) r[j++]=null; // One final null not in the spec but in the default impl return a; // Return the original } return Arrays.copyOf(r,j); } };
final Prime box = (oldval == null || oldval == TOMBSTONE) ? TOMBPRIME : new Prime(oldval); if( CAS_val(oldkvs,idx,oldval,box) ) { // CAS down a box'd version of oldval
@Override public <T> T[] toArray(T[] a) { Object[] kvs = raw_array(); // Estimate size of array; be prepared to see more or fewer elements int sz = size(); T[] r = a.length >= sz ? a : (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), sz); // Fast efficient element walk. int j=0; for( int i=0; i<len(kvs); i++ ) { Object K = key(kvs,i); Object V = Prime.unbox(val(kvs,i)); if( K != null && K != TOMBSTONE && V != null && V != TOMBSTONE ) { if( j >= r.length ) { int sz2 = (int)Math.min(Integer.MAX_VALUE-8,((long)j)<<1); if( sz2<=r.length ) throw new OutOfMemoryError("Required array size too large"); r = Arrays.copyOf(r,sz2); } r[j++] = (T)K; } } if( j <= a.length ) { // Fit in the original array? if( a!=r ) System.arraycopy(r,0,a,0,j); if( j<a.length ) r[j++]=null; // One final null not in the spec but in the default impl return a; // Return the original } return Arrays.copyOf(r,j); } };
private final void print( Object[] kvs ) { for( int i=0; i<len(kvs); i++ ) { Object K = key(kvs,i); if( K != null ) { String KS = (K == TOMBSTONE) ? "XXX" : K.toString(); Object V = val(kvs,i); Object U = Prime.unbox(V); String p = (V==U) ? "" : "prime_"; String US = (U == TOMBSTONE) ? "tombstone" : U.toString(); System.out.println(""+i+" ("+KS+","+p+US+")"); } } Object[] newkvs = chm(kvs)._newkvs; // New table, if any if( newkvs != null ) { System.out.println("----"); print(newkvs); } } // print only the live values, broken down by the table they are in
private final void print2( Object[] kvs) { for( int i=0; i<len(kvs); i++ ) { Object key = key(kvs,i); Object val = val(kvs,i); Object U = Prime.unbox(val); if( key != null && key != TOMBSTONE && // key is sane val != null && U != TOMBSTONE ) { // val is sane String p = (val==U) ? "" : "prime_"; System.out.println(""+i+" ("+key+","+p+val+")"); } } Object[] newkvs = chm(kvs)._newkvs; // New table, if any if( newkvs != null ) { System.out.println("----"); print2(newkvs); } }
final Prime box = (oldval == null || oldval == TOMBSTONE) ? TOMBPRIME : new Prime(oldval); if( CAS_val(oldkvs,idx,oldval,box) ) { // CAS down a box'd version of oldval
private final void print2( Object[] kvs) { for( int i=0; i<len(kvs); i++ ) { Object key = key(kvs,i); Object val = val(kvs,i); Object U = Prime.unbox(val); if( key != null && key != TOMBSTONE && // key is sane val != null && U != TOMBSTONE ) { // val is sane String p = (val==U) ? "" : "prime_"; System.out.println(""+i+" ("+key+","+p+val+")"); } } Object[] newkvs = chm(kvs)._newkvs; // New table, if any if( newkvs != null ) { System.out.println("----"); print2(newkvs); } }
private final void print( Object[] kvs ) { for( int i=0; i<len(kvs); i++ ) { Object K = key(kvs,i); if( K != null ) { String KS = (K == TOMBSTONE) ? "XXX" : K.toString(); Object V = val(kvs,i); Object U = Prime.unbox(V); String p = (V==U) ? "" : "prime_"; String US = (U == TOMBSTONE) ? "tombstone" : U.toString(); System.out.println(""+i+" ("+KS+","+p+US+")"); } } Object[] newkvs = chm(kvs)._newkvs; // New table, if any if( newkvs != null ) { System.out.println("----"); print(newkvs); } } // print only the live values, broken down by the table they are in