| 1 |
package cardinality; |
| 2 |
|
| 3 |
import shared.Parser; |
| 4 |
import shared.Tools; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
public final class BBLog_simple extends CardinalityTracker { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
BBLog_simple(){ |
| 19 |
this(2048, 31, -1, 0f); |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
BBLog_simple(Parser p){ |
| 24 |
super(p); |
| 25 |
maxArray=new long[buckets]; |
| 26 |
counts=(trackCounts ? new int[buckets] : null); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
BBLog_simple(int buckets_, int k_, long seed, float minProb_){ |
| 37 |
super(buckets_, k_, seed, minProb_); |
| 38 |
maxArray=new long[buckets]; |
| 39 |
counts=(trackCounts ? new int[buckets] : null); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
@Override |
| 47 |
public final long cardinality(){ |
| 48 |
double difSum=0; |
| 49 |
int count=0; |
| 50 |
|
| 51 |
for(int i=0; i<maxArray.length; i++){ |
| 52 |
long val=maxArray[i]; |
| 53 |
if(val>0){ |
| 54 |
long dif=Long.MAX_VALUE-val; |
| 55 |
difSum+=dif; |
| 56 |
count++; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
final double mean=difSum/Tools.max(count, 1); |
| 61 |
final double estimatePerSet=2*(Long.MAX_VALUE/mean); |
| 62 |
final double total=estimatePerSet*count*((count+buckets)/(float)(buckets+buckets)); |
| 63 |
|
| 64 |
long cardinality=(long)(total); |
| 65 |
lastCardinality=cardinality; |
| 66 |
return cardinality; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public int[] getCounts(){ |
| 71 |
return counts; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public final void add(CardinalityTracker log){ |
| 76 |
assert(log.getClass()==this.getClass()); |
| 77 |
add((BBLog_simple)log); |
| 78 |
} |
| 79 |
|
| 80 |
public void add(BBLog_simple log){ |
| 81 |
if(maxArray!=log.maxArray){ |
| 82 |
for(int i=0; i<buckets; i++){ |
| 83 |
maxArray[i]=Tools.max(maxArray[i], log.maxArray[i]); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void hashAndStore(final long number){ |
| 90 |
|
| 91 |
|
| 92 |
final long key=Tools.hash64shift(number); |
| 93 |
|
| 94 |
|
| 95 |
final int bucket=(int)(key&bucketMask); |
| 96 |
|
| 97 |
{ |
| 98 |
maxArray[bucket]=Tools.max(key, maxArray[bucket]); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public final float[] compensationFactorLogBucketsArray(){ |
| 104 |
return null; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private final long[] maxArray; |
| 113 |
|
| 114 |
private final int[] counts; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
} |