This repository was archived by the owner on Jul 7, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +37
-3
lines changed
main/java/com/clearspring/analytics/stream/cardinality
test/java/com/clearspring/analytics/stream/cardinality Expand file tree Collapse file tree 2 files changed +37
-3
lines changed Original file line number Diff line number Diff line change 54
54
*/
55
55
public class HyperLogLog implements ICardinality
56
56
{
57
- private final static int POW_2_32 = ( int ) Math .pow (2 , 32 );
58
- private final static int NEGATIVE_POW_2_32 = ( int ) Math . pow (- 2 , 32 ) ;
57
+ private final static double POW_2_32 = Math .pow (2 , 32 );
58
+ private final static double NEGATIVE_POW_2_32 = - - 4294967296.0 ;
59
59
60
60
private final RegisterSet registerSet ;
61
61
private final int log2m ;
@@ -180,6 +180,11 @@ public boolean offer(Object o)
180
180
181
181
@ Override
182
182
public long cardinality ()
183
+ {
184
+ return cardinality (true );
185
+ }
186
+
187
+ public long cardinality (boolean enableLongRangeCorrection )
183
188
{
184
189
double registerSum = 0 ;
185
190
int count = registerSet .count ;
@@ -211,7 +216,14 @@ else if (estimate <= (1.0 / 30.0) * POW_2_32)
211
216
else if (estimate > (1.0 / 30.0 ) * POW_2_32 )
212
217
{
213
218
// Large Range Estimate
214
- return Math .round ((NEGATIVE_POW_2_32 * Math .log (1 - (estimate / POW_2_32 ))));
219
+ if (enableLongRangeCorrection )
220
+ {
221
+ return Math .round ((NEGATIVE_POW_2_32 * Math .log (1.0 - (estimate / POW_2_32 ))));
222
+ }
223
+ else
224
+ {
225
+ return Math .round (estimate );
226
+ }
215
227
}
216
228
return 0 ;
217
229
}
Original file line number Diff line number Diff line change @@ -122,4 +122,26 @@ public void testMerge() throws CardinalityMergeException
122
122
assertTrue (mergedEstimate >= expectedCardinality - (3 * se ));
123
123
assertTrue (mergedEstimate <= expectedCardinality + (3 * se ));
124
124
}
125
+
126
+ @ Test
127
+ public void testPrecise_disableLongRangeCorrection () throws CardinalityMergeException
128
+ {
129
+ int cardinality = 150000000 ;
130
+
131
+ HyperLogLog baseline = new HyperLogLog (20 );
132
+ for (int j = 0 ; j < cardinality ; j ++)
133
+ {
134
+ double val = Math .random ();
135
+ baseline .offer (val );
136
+ }
137
+
138
+
139
+ long mergedEstimate = baseline .cardinality (false );
140
+ double se = cardinality * (1.04 / Math .sqrt (Math .pow (2 , 20 )));
141
+
142
+ System .out .println ("Expect estimate: " + mergedEstimate + " is between " + (cardinality - (3 * se )) + " and " + (cardinality + (3 * se )));
143
+
144
+ assertTrue (mergedEstimate >= cardinality - (3 * se ));
145
+ assertTrue (mergedEstimate <= cardinality + (3 * se ));
146
+ }
125
147
}
You can’t perform that action at this time.
0 commit comments