Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ func (p sortableSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

type set map[uint32]bool

func (s set) Add(i uint32) { s[i] = true }
func (s set) Add(i uint32) bool {
if s[i] {
return false
}
s[i] = true
return true
}

func alpha(m uint32) float64 {
if m == 16 {
Expand Down
5 changes: 4 additions & 1 deletion hyperloglog.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,18 @@ func (h *HyperLogLog) Clear() {
}

// Add adds a new item to HyperLogLog h.
func (h *HyperLogLog) Add(item Hash32) {
func (h *HyperLogLog) Add(item Hash32) bool {
x := item.Sum32()
i := eb32(x, 32, 32-h.p) // {x31,...,x32-p}
w := x<<h.p | 1<<(h.p-1) // {x32-p,...,x0}

changed := false
zeroBits := clz32(w) + 1
if zeroBits > h.reg[i] {
h.reg[i] = zeroBits
changed = true
}
return changed
}

// Merge takes another HyperLogLog and combines it with HyperLogLog h.
Expand Down
11 changes: 8 additions & 3 deletions hyperloglogplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,25 @@ func (h *HyperLogLogPlus) toNormal() {
}

// Add adds a new item to HyperLogLogPlus h.
func (h *HyperLogLogPlus) Add(item Hash64) {
func (h *HyperLogLogPlus) Add(item Hash64) bool {
x := item.Sum64()
changed := false
if h.sparse {
h.tmpSet.Add(h.encodeHash(x))
h.maybeMerge()
changed = h.tmpSet.Add(h.encodeHash(x))
if changed {
h.maybeMerge()
}
} else {
i := eb64(x, 64, 64-h.p) // {x63,...,x64-p}
w := x<<h.p | 1<<(h.p-1) // {x63-p,...,x0}

zeroBits := clz64(w) + 1
if zeroBits > h.reg[i] {
h.reg[i] = zeroBits
changed = true
}
}
return changed
}

// Merge takes another HyperLogLogPlus and combines it with HyperLogLogPlus h.
Expand Down