Skip to content

Commit 04a87e7

Browse files
authored
Merge pull request #8 from yourbasic/tip
Tip
2 parents cfe02cd + 641687e commit 04a87e7

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

example_test.go

-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package bloom_test
33
import (
44
"fmt"
55
"github.com/yourbasic/bloom"
6-
"math/rand"
76
"strconv"
87
)
98

@@ -26,30 +25,6 @@ func Example_basics() {
2625
// Output: https://rascal.com seems to be shady.
2726
}
2827

29-
// Estimate the number of false positives.
30-
func Example_falsePositives() {
31-
// Create a Bloom filter with room for n elements
32-
// at a false-positives rate less than 1/p.
33-
n, p := 10000, 100
34-
filter := bloom.New(n, p)
35-
36-
// Add n random strings.
37-
for i := 0; i < n; i++ {
38-
filter.Add(strconv.Itoa(rand.Int()))
39-
}
40-
41-
// Do n random lookups and count the (mostly accidental) hits.
42-
// It shouldn't be much more than n/p, and hopefully less.
43-
count := 0
44-
for i := 0; i < n; i++ {
45-
if filter.Test(strconv.Itoa(rand.Int())) {
46-
count++
47-
}
48-
}
49-
fmt.Println(count, "mistakes were made.")
50-
// Output: 26 mistakes were made.
51-
}
52-
5328
// Compute the union of two filters.
5429
func ExampleFilter_Union() {
5530
// Create two Bloom filters, each with room for 1000 elements

filter.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@
99
// a member”. Only false positives can occur: an element that has been added
1010
// to the filter will always be identified as ”likely member”.
1111
//
12+
// The probabilities of different outcomes of a membership test at
13+
// a false-positives rate of 1/100 are:
14+
//
15+
// Test(s) true false
16+
// --------------------------------------
17+
// s has been added 1 0
18+
// s has not been added 0.01 0.99
19+
//
1220
// Elements can be added, but not removed. With more elements in the filter,
1321
// the probability of false positives increases.
1422
//
15-
// Implementation
23+
// Performance
1624
//
1725
// A full filter with a false-positives rate of 1/p uses roughly
1826
// 0.26ln(p) bytes per element and performs ⌈1.4ln(p)⌉ bit array lookups
@@ -30,11 +38,22 @@
3038
// 512 1.6 9
3139
// 1024 1.8 10
3240
//
33-
// This implementation is not intended for cryptographic use.
3441
// Each membership test makes a single call to a 128-bit hash function.
3542
// This improves speed without increasing the false-positives rate
3643
// as shown by Kirsch and Mitzenmacher.
3744
//
45+
// Limitations
46+
//
47+
// This implementation is not intended for cryptographic use.
48+
//
49+
// The internal data representation is different for big-endian
50+
// and little-endian machines.
51+
//
52+
// Typical use case
53+
//
54+
// The Basics example contains a typcial use case:
55+
// a blacklist of shady websites.
56+
//
3857
package bloom
3958

4059
import (

0 commit comments

Comments
 (0)