@@ -54,8 +54,11 @@ type Filter struct {
5454 count int64 // Estimate number of elements
5555}
5656
57- // MurmurHash3 function.
58- var murmur = new (digest )
57+ // MurmurHash3 functions.
58+ var (
59+ murmur = new (digest )
60+ murmurString = new (digestString )
61+ )
5962
6063// New creates an empty Bloom filter with room for n elements
6164// at a false-positives rate less than 1/p.
@@ -73,7 +76,15 @@ func New(n int, p int) *Filter {
7376
7477// AddByte adds b to the filter and tells if b was already a likely member.
7578func (f * Filter ) AddByte (b []byte ) bool {
76- h1 , h2 := murmur .hash (b )
79+ return f .add (murmur .hash (b ))
80+ }
81+
82+ // Add adds s to the filter and tells if s was already a likely member.
83+ func (f * Filter ) Add (s string ) bool {
84+ return f .add (murmurString .hash (s ))
85+ }
86+
87+ func (f * Filter ) add (h1 , h2 uint64 ) bool {
7788 trunc := uint64 (len (f .data ))<< shift - 1
7889 member := true
7990 for i := f .lookups ; i > 0 ; i -- {
@@ -91,16 +102,17 @@ func (f *Filter) AddByte(b []byte) bool {
91102 return member
92103}
93104
94- // Add adds s to the filter and tells if s was already a likely member.
95- func (f * Filter ) Add (s string ) bool {
96- b := make ([]byte , len (s ))
97- copy (b , s )
98- return f .AddByte (b )
99- }
100-
101105// TestByte tells if b is a likely member of the filter.
102106func (f * Filter ) TestByte (b []byte ) bool {
103- h1 , h2 := murmur .hash (b )
107+ return f .test (murmur .hash (b ))
108+ }
109+
110+ // Test tells if s is a likely member of the filter.
111+ func (f * Filter ) Test (s string ) bool {
112+ return f .test (murmurString .hash (s ))
113+ }
114+
115+ func (f * Filter ) test (h1 , h2 uint64 ) bool {
104116 trunc := uint64 (len (f .data ))<< shift - 1
105117 for i := f .lookups ; i > 0 ; i -- {
106118 h1 += h2
@@ -113,13 +125,6 @@ func (f *Filter) TestByte(b []byte) bool {
113125 return true
114126}
115127
116- // Test tells if s is a likely member of the filter.
117- func (f * Filter ) Test (s string ) bool {
118- b := make ([]byte , len (s ))
119- copy (b , s )
120- return f .TestByte (b )
121- }
122-
123128// Count returns an estimate of the number of elements in the filter.
124129func (f * Filter ) Count () int64 {
125130 return f .count
0 commit comments