Skip to content

Commit 5871f83

Browse files
committed
Minor style updates
1 parent 514d45e commit 5871f83

2 files changed

Lines changed: 29 additions & 26 deletions

File tree

src/cuckoo/bucket.jl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
# fingerprints. Could be basically any UInt
33
const FINGERPRINT_SALT = 0x7afb47f99881a598 % UInt
44

5+
# Sorted array of all UInt16 where each block of 4 bits are themselves sorted
6+
# "Encoding" more bits than 4 like this will save no more than 4 bits total,
7+
# and will just make encoding/decoding operations slower.
8+
const PREFIXES = let
9+
x = Set{UInt16}()
10+
for a in 0:15, b in 0:15, c in 0:15, d in 0:15
11+
k, m, n, p = sort!([a, b, c, d])
12+
y = UInt16(k) << 12 | UInt16(m) << 8 | UInt16(n) << 4 | UInt16(p)
13+
push!(x, y)
14+
end
15+
sort!(collect(x))
16+
end
17+
518
abstract type AbstractBucket{F} end
619

720
struct Bucket64{F} <: AbstractBucket{F}
@@ -45,18 +58,7 @@ end
4558

4659
Base.:(==)(x::AbstractBucket, y::AbstractBucket) = false
4760

48-
# Sorted array of all UInt16 where each block of 4 bits are themselves sorted
49-
# "Encoding" more bits than 4 like this will save no more than 4 bits total,
50-
# and will just make encoding/decoding operations slower.
51-
let x = Set{UInt16}()
52-
for a in 0:15, b in 0:15, c in 0:15, d in 0:15
53-
k, m, n, p = sort!([a, b, c, d])
54-
y = UInt16(k) << 12 | UInt16(m) << 8 | UInt16(n) << 4 | UInt16(p)
55-
push!(x, y)
56-
end
57-
const global PREFIXES = collect(x)
58-
sort!(PREFIXES)
59-
end
61+
6062

6163
# Sorts the four fingerprint in the bucket
6264
@inline function sort_bucket(x::AbstractBucket{F}) where {F}

src/hyperloglog/hyperloglog.jl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747

4848
HyperLogLog() = 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.
5353
Base.sizeof(::Type{HyperLogLog{P}}) where {P} = 1 << P
@@ -129,11 +129,11 @@ julia> a = HyperLogLog{14}(); (length(a), isempty(a))
129129
Base.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
139139
end
@@ -164,16 +164,17 @@ function Base.push!(hll::HyperLogLog, values...)
164164
end
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
202203
end
203204

@@ -217,6 +218,6 @@ julia> a = HyperLogLog{14}(); push!(a, 1,2,3,4,5,6,7,8); length(a)
217218
function 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))
222223
end

0 commit comments

Comments
 (0)