Skip to content

Commit

Permalink
Merge pull request #8 from yourbasic/tip
Browse files Browse the repository at this point in the history
Tip
  • Loading branch information
korthaj authored Jun 2, 2017
2 parents cfe02cd + 641687e commit 04a87e7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
25 changes: 0 additions & 25 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bloom_test
import (
"fmt"
"github.com/yourbasic/bloom"
"math/rand"
"strconv"
)

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

// Estimate the number of false positives.
func Example_falsePositives() {
// Create a Bloom filter with room for n elements
// at a false-positives rate less than 1/p.
n, p := 10000, 100
filter := bloom.New(n, p)

// Add n random strings.
for i := 0; i < n; i++ {
filter.Add(strconv.Itoa(rand.Int()))
}

// Do n random lookups and count the (mostly accidental) hits.
// It shouldn't be much more than n/p, and hopefully less.
count := 0
for i := 0; i < n; i++ {
if filter.Test(strconv.Itoa(rand.Int())) {
count++
}
}
fmt.Println(count, "mistakes were made.")
// Output: 26 mistakes were made.
}

// Compute the union of two filters.
func ExampleFilter_Union() {
// Create two Bloom filters, each with room for 1000 elements
Expand Down
23 changes: 21 additions & 2 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@
// a member”. Only false positives can occur: an element that has been added
// to the filter will always be identified as ”likely member”.
//
// The probabilities of different outcomes of a membership test at
// a false-positives rate of 1/100 are:
//
// Test(s) true false
// --------------------------------------
// s has been added 1 0
// s has not been added 0.01 0.99
//
// Elements can be added, but not removed. With more elements in the filter,
// the probability of false positives increases.
//
// Implementation
// Performance
//
// A full filter with a false-positives rate of 1/p uses roughly
// 0.26ln(p) bytes per element and performs ⌈1.4ln(p)⌉ bit array lookups
Expand All @@ -30,11 +38,22 @@
// 512 1.6 9
// 1024 1.8 10
//
// This implementation is not intended for cryptographic use.
// Each membership test makes a single call to a 128-bit hash function.
// This improves speed without increasing the false-positives rate
// as shown by Kirsch and Mitzenmacher.
//
// Limitations
//
// This implementation is not intended for cryptographic use.
//
// The internal data representation is different for big-endian
// and little-endian machines.
//
// Typical use case
//
// The Basics example contains a typcial use case:
// a blacklist of shady websites.
//
package bloom

import (
Expand Down

0 comments on commit 04a87e7

Please sign in to comment.