-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearcounting.go
More file actions
182 lines (166 loc) · 5.49 KB
/
Copy pathlinearcounting.go
File metadata and controls
182 lines (166 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package streamstats
import (
"fmt"
"hash"
"math"
)
const (
minLinearCountingP = 6
maxLinearCountingP = 24
)
// LinearCounting is a space efficient data structure for count distinct with hard upper bound
type LinearCounting struct {
hash hash.Hash64 // a 64-bit hash function to map inputs to uniform buckets
bits BitVector // bitvector to hold the occupied buckets
p byte // the number of buckets m = 2^p
}
// NewLinearCounting initializes a LinearCounting structure with size m=2^p and the given hash function
func NewLinearCounting(p byte, hash hash.Hash64) *LinearCounting {
// p is bounded by 8 and 24 for practical implementations
if p < minLinearCountingP {
p = minLinearCountingP
} else if p > maxLinearCountingP {
p = maxLinearCountingP
}
m := uint64(1 << p)
bits := NewBitVector(m)
return &LinearCounting{p: p, hash: hash, bits: bits}
}
// Add adds an item to the multiset represented by the LinearCounting structure
func (lc *LinearCounting) Add(item []byte) {
lc.hash.Reset()
lc.hash.Write(item)
hash := lc.hash.Sum64()
bucket := hash >> (64 - lc.p) // top p bits are the bucket
lc.bits.Set(bucket)
}
// Distinct returns the estimate of the number of distinct elements seen
// if the backing BitVector is full it returns m, the size of the BitVector
func (lc LinearCounting) Distinct() uint64 {
m := uint64(1 << lc.p)
zeroCount := m - lc.bits.PopCount()
if zeroCount > 0 {
return uint64(float64(m) * math.Log(float64(m)/float64(zeroCount)))
}
return (1 << lc.p)
}
// Compress produces a new LinearCouting with reduced size by 2^factor with reduced precision
// if new p < minLinearCountingP, p=minLinearCountingP , if factor=0 it just produces a copy
func (lc *LinearCounting) Compress(factor byte) *LinearCounting {
var p byte
if lc.p > factor {
p = lc.p - factor
}
if p < minLinearCountingP {
p = minLinearCountingP
}
newLC := NewLinearCounting(p, lc.hash)
// copy the old BitVector to a new temporary one that can be folded
bitsToFold := NewBitVector(uint64(1 << lc.p))
for i := range lc.bits {
bitsToFold[i] = lc.bits[i]
}
// "fold" the bit vector
for i := lc.p; i > p; i-- {
mFold := 1 << (i - 7) // half the current length in units of 64 bits
for j := 0; j < mFold; j++ {
bitsToFold[j] |= bitsToFold[j+mFold]
}
}
// populate the folded vector into the new LinearCounting
for i := range newLC.bits {
newLC.bits[i] = bitsToFold[i]
}
return newLC
}
// Union the estimate of two LinearCounting reducing the precision to the minimum of the two sets
// the function will return nil and an error if the hash functions mismatch
func (lc *LinearCounting) Union(lcB *LinearCounting) (*LinearCounting, error) {
// check that both hash functions get the same result for "LinearCounting"
lc.hash.Reset()
lc.hash.Write([]byte("LinearCounting"))
hash := lc.hash.Sum64()
lcB.hash.Reset()
lcB.hash.Write([]byte("LinearCounting"))
hashB := lcB.hash.Sum64()
if hash != hashB {
return nil, fmt.Errorf("Hash functions are not identical, return %0x != %0x for \"LinearCounting\"", hash, hashB)
}
// determine if either precision needs to be reduced
var combinedP byte
var lc1, lc2, combinedLC *LinearCounting
if lc.p < lcB.p {
combinedP = lc.p
factor := lcB.p - combinedP
lc1 = lc
lc2 = lcB.Compress(factor)
} else if lcB.p < lc.p {
combinedP = lcB.p
factor := lc.p - combinedP
lc1 = lc.Compress(factor)
lc2 = lcB
} else {
combinedP = lc.p
lc1 = lc
lc2 = lcB
}
// for each bucket take the OR of the two LinearCounting
combinedLC = NewLinearCounting(combinedP, lc.hash)
for i := range combinedLC.bits {
combinedLC.bits[i] = lc1.bits[i] | lc2.bits[i]
}
return combinedLC, nil
}
// Intersect the estimate of two LinearCounting reducing the precision to the minimum of the two sets
// the function will return nil and an error if the hash functions mismatch
func (lc *LinearCounting) Intersect(lcB *LinearCounting) (*LinearCounting, error) {
// check that both hash functions get the same result for "LinearCounting"
lc.hash.Reset()
lc.hash.Write([]byte("LinearCounting"))
hash := lc.hash.Sum64()
lcB.hash.Reset()
lcB.hash.Write([]byte("LinearCounting"))
hashB := lcB.hash.Sum64()
if hash != hashB {
return nil, fmt.Errorf("Hash functions are not identical, return %0x != %0x for \"LinearCounting\"", hash, hashB)
}
// determine if either precision needs to be reduced
var combinedP byte
var lc1, lc2, combinedLC *LinearCounting
if lc.p < lcB.p {
combinedP = lc.p
factor := lcB.p - combinedP
lc1 = lc
lc2 = lcB.Compress(factor)
} else if lcB.p < lc.p {
combinedP = lcB.p
factor := lc.p - combinedP
lc1 = lc.Compress(factor)
lc2 = lcB
} else {
combinedP = lc.p
lc1 = lc
lc2 = lcB
}
// for each bucket take the AND of the two LinearCounting
combinedLC = NewLinearCounting(combinedP, lc.hash)
for i := range combinedLC.bits {
combinedLC.bits[i] = lc1.bits[i] & lc2.bits[i]
}
return combinedLC, nil
}
// Occupancy returns the ratio of filled buckets in the LinearCounting bitvector
func (lc LinearCounting) Occupancy() float64 {
return float64(lc.bits.PopCount()) / float64(uint64(1<<lc.p))
}
// ExpectedError returns the expected error at the current filling in the LinearCounting
func (lc LinearCounting) ExpectedError() float64 {
m := float64(uint64(1 << lc.p))
loadFactor := lc.Occupancy()
return 2 * math.Sqrt((math.Exp(loadFactor)-loadFactor-1)/m) / loadFactor
}
func (lc LinearCounting) String() string {
N := lc.Distinct()
delta := uint64(float64(N) * lc.ExpectedError())
return fmt.Sprintf("LinearCounting N: %d +/- %d", N, delta)
}