4747
4848HyperLogLog () = HyperLogLog {14} () # This is a good value for most practical applications
4949
50- Base. show (io:: IO , x :: HyperLogLog{P} ) where {P} = print (io, " HyperLogLog{$(P) }()" )
50+ Base. show (io:: IO , :: HyperLogLog{P} ) where {P} = print (io, " HyperLogLog{$(P) }()" )
5151
5252# Strangely, 2^P compiles less effectively that 1<<P here.
5353Base. sizeof (:: Type{HyperLogLog{P}} ) where {P} = 1 << P
@@ -129,11 +129,11 @@ julia> a = HyperLogLog{14}(); (length(a), isempty(a))
129129Base. isempty (x:: HyperLogLog ) = all (i == 0x00 for i in x. counts)
130130
131131# An UInt-sized hash is split, the first P bits is the bin index, the other bits the observation
132- getbin (hll :: HyperLogLog{P} , x:: UInt ) where {P} = x >>> (8 * sizeof (UInt) - P) + 1
132+ getbin (:: HyperLogLog{P} , x:: UInt ) where {P} = x >>> (8 * sizeof (UInt) - P) + 1
133133
134134# Get number of trailing zeros + 1. We use the mask to prevent number of zeros
135135# from being overestimated due to any zeros in the bin part of the UInt
136- function getzeros (hll :: HyperLogLog{P} , x:: UInt ) where {P}
136+ function getzeros (:: HyperLogLog{P} , x:: UInt ) where {P}
137137 or_mask = ((UInt (1 ) << P) - 1 ) << (8 * sizeof (UInt) - P)
138138 return trailing_zeros (x | or_mask) + 1
139139end
@@ -164,16 +164,17 @@ function Base.push!(hll::HyperLogLog, values...)
164164end
165165
166166# This corrects for systematic bias in the harmonic mean, see original paper.
167- α (x:: HyperLogLog{P} ) where {P} =
168- if P == 4
169- return 0.673
167+ function alpha (x:: HyperLogLog{P} ) where {P}
168+ return if P == 4
169+ 0.673
170170 elseif P == 5
171- return 0.697
171+ 0.697
172172 elseif P == 6
173- return 0.709
173+ 0.709
174174 else
175- return 0.7213 / (1 + 1.079 / sizeof (x))
175+ 0.7213 / (1 + 1.079 / sizeof (x))
176176 end
177+ end
177178
178179# This accounts for systematic bias for low raw estimates.
179180# From https://ai.google/research/pubs/pub40671
@@ -187,17 +188,17 @@ function bias(::HyperLogLog{P}, biased_estimate) where {P}
187188 biasarray = @inbounds BIAS_ARRAYS[P - 3 ]
188189 firstindex = searchsortedfirst (rawarray, biased_estimate)
189190 # Raw count large, no need for bias correction
190- if firstindex == length (rawarray) + 1
191- return 0.0
191+ return if firstindex == length (rawarray) + 1
192+ 0.0
192193 # Raw count too small, cannot be corrected. Maybe raise error?
193194 elseif firstindex == 1
194- return @inbounds biasarray[1 ]
195+ @inbounds biasarray[1 ]
195196 # Else linearly approximate the right value for bias
196197 else
197198 x1, x2 = @inbounds rawarray[firstindex - 1 ], @inbounds rawarray[firstindex]
198199 y1, y2 = @inbounds biasarray[firstindex - 1 ], @inbounds biasarray[firstindex]
199200 delta = @fastmath (biased_estimate - x1) / (x2 - x1) # relative distance of raw from x1
200- return y1 + delta * (y2 - y1)
201+ y1 + delta * (y2 - y1)
201202 end
202203end
203204
@@ -217,6 +218,6 @@ julia> a = HyperLogLog{14}(); push!(a, 1,2,3,4,5,6,7,8); length(a)
217218function Base. length (x:: HyperLogLog{P} ) where {P}
218219 # Harmonic mean estimates cardinality per bin. There are 2^P bins
219220 harmonic_mean = sizeof (x) / sum (1 / 1 << i for i in x. counts)
220- biased_estimate = α (x) * sizeof (x) * harmonic_mean
221+ biased_estimate = alpha (x) * sizeof (x) * harmonic_mean
221222 return round (Int, biased_estimate - bias (x, biased_estimate))
222223end
0 commit comments