Skip to content

Commit 0d2fa99

Browse files
committed
Remove intersection
1 parent e01afd6 commit 0d2fa99

3 files changed

Lines changed: 19 additions & 66 deletions

File tree

example_test.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,25 @@ func Example_falsePositives() {
5151
// Output: 26 mistakes were made.
5252
}
5353

54-
// Compute the intersection and union of two filters.
55-
func ExampleFilter_And() {
54+
// Compute the union of two filters.
55+
func ExampleFilter_Or() {
5656
// Create two Bloom filter with room for 1000 elements
5757
// at a false-positives rate less than 1/100.
58-
n := 1000
59-
p := 100
60-
f1, f2 := bloom.New(n, p), bloom.New(n, p)
58+
n, p := 1000, 100
59+
f1 := bloom.New(n, p)
60+
f2 := bloom.New(n, p)
6161

62-
// Add "0", "1", …, "499" to f1
63-
for i := 0; i < n/2; i++ {
62+
// Add "0", "2", …, "498" to f1
63+
for i := 0; i < n/2; i += 2 {
6464
f1.Add(strconv.Itoa(i))
6565
}
6666

67-
// Add "250", "251", …, "749" to f2
68-
for i := n / 4; i < 3*n/4; i++ {
67+
// Add "1", "3", …, "499" to f2
68+
for i := 1; i < n/2; i += 2 {
6969
f2.Add(strconv.Itoa(i))
7070
}
7171

72-
// Compute the approximate size of f1 ∩ f2 and f1 ∪ f2.
73-
fmt.Println("f1 ∩ f2:", f1.And(f2).Count())
72+
// Compute the approximate size of f1 ∪ f2.
7473
fmt.Println("f1 ∪ f2:", f1.Or(f2).Count())
75-
// Output:
76-
// f1 ∩ f2: 276
77-
// f1 ∪ f2: 758
78-
74+
// Output: f1 ∪ f2: 505
7975
}

filter.go

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -122,43 +122,12 @@ func (f *Filter) Count() int64 {
122122
return f.count
123123
}
124124

125-
// And returns a new Bloom filter that consists of all elements
126-
// that belong to both f1 and f2. The two filters must be of
127-
// the same size and have the same false-positives rate.
128-
//
129-
// The false-positive rate of the resulting filter is bounded by
130-
// the maximum false-positive rate of f1 and f2, but it may be larger
131-
// than the rate of the filter created from scratch using
132-
// the intersection of the two sets.
133-
func (f1 *Filter) And(f2 *Filter) *Filter {
134-
if len(f1.data) != len(f2.data) || f1.lookups != f2.lookups {
135-
panic("operation requires filters of the same type")
136-
}
137-
len := len(f1.data)
138-
res := &Filter{
139-
data: make([]uint64, len),
140-
lookups: f1.lookups,
141-
}
142-
bitCount := 0
143-
for i := 0; i < len; i++ {
144-
w := f1.data[i] & f2.data[i]
145-
res.data[i] = w
146-
bitCount += count(w)
147-
}
148-
// Estimate the number of elements from the bitCount.
149-
m := 64 * float64(len)
150-
bits := float64(bitCount)
151-
n := m / float64(f1.lookups) * math.Log(m/(m-bits))
152-
res.count = int64(n)
153-
return res
154-
}
155-
156125
// Or returns a new Bloom filter that consists of all elements
157126
// that belong to either f1 or f2. The two filters must be of
158-
// the same size and have the same false-positives rate.
127+
// the same size n and have the same false-positives rate p.
159128
//
160-
// The resulting filter is the same as the filter created from scratch
161-
// using the union of the two sets.
129+
// The resulting filter is the same as the filter created
130+
// from scratch using the union of the two sets.
162131
func (f1 *Filter) Or(f2 *Filter) *Filter {
163132
if len(f1.data) != len(f2.data) || f1.lookups != f2.lookups {
164133
panic("operation requires filters of the same type")

filter_test.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestFilter(t *testing.T) {
5858
}
5959
}
6060

61-
func TestAndOr(t *testing.T) {
61+
func TestOr(t *testing.T) {
6262
s1 := "asöldkgjaösldkgaösldkasldgjkaösldkgjöasgkdjg"
6363
s2 := "elasödlnkgaölsdkfgaölsdkjfaölsdkgaölskgnaösl"
6464
s3 := "aölsdgkaösldkgaösldkgjaölsdkjgaölsdkgjaösldk"
@@ -69,20 +69,8 @@ func TestAndOr(t *testing.T) {
6969
f1.Add(s2)
7070
f2.Add(s2)
7171
f2.Add(s3)
72-
and, or := f1.And(f2), f1.Or(f2)
73-
member := and.Test(s1)
74-
if member {
75-
t.Errorf("and.Test(s1) = %v; want false\n", member)
76-
}
77-
member = and.Test(s2)
78-
if !member {
79-
t.Errorf("and.Test(s2) = %v; want true\n", member)
80-
}
81-
member = and.Test(s3)
82-
if member {
83-
t.Errorf("and.Test(s3) = %v; want false\n", member)
84-
}
85-
member = or.Test(s1)
72+
or := f1.Or(f2)
73+
member := or.Test(s1)
8674
if !member {
8775
t.Errorf("or.Test(s1) = %v; want true\n", member)
8876
}
@@ -138,13 +126,13 @@ func BenchmarkTestByte(b *testing.B) {
138126
}
139127
}
140128

141-
func BenchmarkTestAnd(b *testing.B) {
129+
func BenchmarkTestOr(b *testing.B) {
142130
n := 1000
143131
b.StopTimer()
144132
f1 := New(n, 200)
145133
f2 := New(n, 200)
146134
b.StartTimer()
147135
for i := 0; i < b.N; i++ {
148-
_ = f1.And(f2)
136+
_ = f1.Or(f2)
149137
}
150138
}

0 commit comments

Comments
 (0)