| 1 |
package cardinality; |
| 2 |
|
| 3 |
import java.util.concurrent.atomic.AtomicIntegerArray; |
| 4 |
|
| 5 |
import shared.Parser; |
| 6 |
import shared.Tools; |
| 7 |
import structures.LongList; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
public final class LogLog extends CardinalityTracker { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
LogLog(){ |
| 22 |
this(2048, 31, -1, 0); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
LogLog(Parser p){ |
| 27 |
super(p); |
| 28 |
|
| 29 |
maxArrayA=(atomic ? new AtomicIntegerArray(buckets) : null); |
| 30 |
maxArray=(atomic ? null : new int[buckets]); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
LogLog(int buckets_, int k_, long seed, float minProb_){ |
| 41 |
super(buckets_, k_, seed, minProb_); |
| 42 |
|
| 43 |
maxArrayA=(atomic ? new AtomicIntegerArray(buckets) : null); |
| 44 |
maxArray=(atomic ? null : new int[buckets]); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
private long restore(int score){ |
| 53 |
int leading=score; |
| 54 |
long mantissa=1; |
| 55 |
int shift=64-leading-1; |
| 56 |
long original=mantissa<<shift; |
| 57 |
return original; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public final long cardinality(){ |
| 62 |
double difSum=0; |
| 63 |
double estLogSum=0; |
| 64 |
int count=0; |
| 65 |
LongList list=new LongList(buckets); |
| 66 |
|
| 67 |
if(atomic){ |
| 68 |
for(int i=0; i<maxArrayA.length(); i++){ |
| 69 |
int max=maxArrayA.get(i); |
| 70 |
long val=restore(max); |
| 71 |
if(max>0 && val>0){ |
| 72 |
|
| 73 |
|
| 74 |
final long dif=val; |
| 75 |
difSum+=dif; |
| 76 |
count++; |
| 77 |
double est=2*(Long.MAX_VALUE/(double)dif)*SKIPMOD; |
| 78 |
estLogSum+=Math.log(est); |
| 79 |
list.add(dif); |
| 80 |
} |
| 81 |
} |
| 82 |
}else{ |
| 83 |
for(int i=0; i<maxArray.length; i++){ |
| 84 |
int max=maxArray[i]; |
| 85 |
long val=restore(max); |
| 86 |
if(max>0 && val>0){ |
| 87 |
|
| 88 |
final long dif=val; |
| 89 |
difSum+=dif; |
| 90 |
count++; |
| 91 |
double est=2*(Long.MAX_VALUE/(double)dif)*SKIPMOD; |
| 92 |
estLogSum+=Math.log(est); |
| 93 |
list.add(dif); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
final int div=count; |
| 98 |
final double mean=difSum/Tools.max(div, 1); |
| 99 |
list.sort(); |
| 100 |
final long median=list.median(); |
| 101 |
final double mwa=list.medianWeightedAverage(); |
| 102 |
|
| 103 |
|
| 104 |
final double proxy=mean; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
final double estimatePerSet=2*(Long.MAX_VALUE/proxy)*SKIPMOD; |
| 109 |
final double conversionFactor=0.7213428177; |
| 110 |
final double total=conversionFactor*estimatePerSet*div*((count+buckets)/(float)(buckets+buckets)); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
long cardinality=(long)(total); |
| 123 |
lastCardinality=cardinality; |
| 124 |
return cardinality; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public final long cardinalityH(){ |
| 147 |
double sum=0; |
| 148 |
for(int i=0; i<maxArrayA.length(); i++){ |
| 149 |
int x=Tools.max(1, maxArrayA.get(i)); |
| 150 |
sum+=1.0/x; |
| 151 |
} |
| 152 |
double mean=buckets/sum; |
| 153 |
return (long)((Math.pow(2, mean)*buckets*SKIPMOD)); |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public final void add(CardinalityTracker log){ |
| 158 |
assert(log.getClass()==this.getClass()); |
| 159 |
add((LogLog)log); |
| 160 |
} |
| 161 |
|
| 162 |
public void add(LogLog log){ |
| 163 |
if(atomic && maxArrayA!=log.maxArrayA){ |
| 164 |
for(int i=0; i<buckets; i++){ |
| 165 |
maxArrayA.set(i, Tools.max(maxArrayA.get(i), log.maxArrayA.get(i))); |
| 166 |
} |
| 167 |
}else if(maxArray!=log.maxArray){ |
| 168 |
for(int i=0; i<buckets; i++){ |
| 169 |
maxArray[i]=Tools.max(maxArray[i], log.maxArray[i]); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public void hashAndStore(final long number){ |
| 176 |
|
| 177 |
long key=number; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
key=Tools.hash64shift(key); |
| 182 |
|
| 183 |
int leading=Long.numberOfLeadingZeros(key); |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
final int bucket=(int)(key&bucketMask); |
| 190 |
|
| 191 |
if(atomic){ |
| 192 |
int x=maxArrayA.get(bucket); |
| 193 |
while(leading>x){ |
| 194 |
boolean b=maxArrayA.compareAndSet(bucket, x, leading); |
| 195 |
if(b){x=leading;} |
| 196 |
else{x=maxArrayA.get(bucket);} |
| 197 |
} |
| 198 |
}else{ |
| 199 |
maxArray[bucket]=Tools.max(leading, maxArray[bucket]); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public final float[] compensationFactorLogBucketsArray(){ |
| 205 |
return compensationFactorLogBucketsArray; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
private final int[] maxArray; |
| 214 |
|
| 215 |
private final AtomicIntegerArray maxArrayA; |
| 216 |
|
| 217 |
private static final float[] compensationFactorLogBucketsArray={ |
| 218 |
0.053699781f, 0.49556874f, 0.742263622f, 0.861204899f, 0.926038294f, |
| 219 |
0.967001269f, 0.982949748f, 0.992495155f, 0.996185775f, 0.998077246f |
| 220 |
}; |
| 221 |
|
| 222 |
} |