public void print() { long[] t = _t; System.out.print("["+t[0]); for( int i=1; i<t.length; i++ ) System.out.print(","+t[i]); System.out.print("]"); if( _next != null ) _next.print(); } }
private final static boolean CAS( long[] A, int idx, long old, long nnn ) { return _unsafe.compareAndSwapLong( A, rawIndex(A,idx), old, nnn ); }
String toString( long mask ) { return Long.toString(sum(mask)); }
private long add_if_mask( long x, long mask ) { return _cat.add_if_mask(x,mask,hash(),this); }
/** Same as {@link #get}, included for completeness. */ public long longValue() { return _cat.sum(0); }
/** Same as {@link #get}, included for completeness. */ public int intValue() { return (int)_cat.sum(0); } /** Same as {@link #get}, included for completeness. */
/** * A more verbose print than {@link #toString}, showing internal structure. * Useful for debugging. */ public void print() { _cat.print(); }
public void all_set( long val ) { long[] t = _t; for( int i=0; i<t.length; i++ ) t[i] = val; if( _next != null ) _next.all_set(val); }
public long estimate_sum( long mask ) { // For short tables, just do the work if( _t.length <= 64 ) return sum(mask); // For bigger tables, periodically freshen a cached value long millis = System.currentTimeMillis(); if( _fuzzy_time != millis ) { // Time marches on? _fuzzy_sum_cache = sum(mask); // Get sum the hard way _fuzzy_time = millis; // Indicate freshness of cached value } return _fuzzy_sum_cache; // Return cached sum }
/** * Current value of the counter. Since other threads are updating furiously * the value is only approximate, but it includes all counts made by the * current thread. Requires a pass over the internally striped counters. */ public long get() { return _cat.sum(0); } /** Same as {@link #get}, included for completeness. */
/** * A cheaper {@link #get}. Updated only once/millisecond, but as fast as a * simple load instruction when not updating. */ public long estimate_get( ) { return _cat.estimate_sum(0); }
/** Atomically set the sum of the striped counters to specified value. * Rather more expensive than a simple store, in order to remain atomic. */ public void set( long x ) { CAT newcat = new CAT(null,4,x); // Spin until CAS works while( !CAS_cat(_cat,newcat) ); }
public long sum( long mask ) { long sum = _next == null ? 0 : _next.sum(mask); // Recursively get cached sum final long[] t = _t; for( int i=0; i<t.length; i++ ) sum += t[i]&(~mask); return sum; }