-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbloomFilter.go
More file actions
157 lines (136 loc) · 2.71 KB
/
bloomFilter.go
File metadata and controls
157 lines (136 loc) · 2.71 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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
)
const SYSTEM_BIT int64 = 32 << (^uint(0) >> 63)
type IntSet struct {
words []uint64
}
func (s *IntSet) Has(x int64) bool {
word, bit := x/SYSTEM_BIT, uint(x%SYSTEM_BIT)
return int(word) < len(s.words) && s.words[word]&(1<<bit) != 0
}
func (s *IntSet) Add(x int64) {
word, bit := x/SYSTEM_BIT, uint(x%SYSTEM_BIT)
for int(word) >= len(s.words) {
s.words = append(s.words, 0)
}
s.words[word] |= 1 << bit
}
func (s *IntSet) Remove(x int64) {
word, bit := x/SYSTEM_BIT, uint(x%SYSTEM_BIT)
s.words[word] &^= 1 << bit
}
func (s *IntSet) Clear() {
for i := range s.words {
s.words[i] = 0
}
}
func (s *IntSet) Copy() *IntSet {
ss := &IntSet{}
ss.words = make([]uint64, len(s.words))
copy(ss.words, s.words)
return ss
}
func (s *IntSet) UnionWith(t *IntSet) {
for i, tword := range t.words {
if i < len(s.words) {
s.words[i] |= tword
} else {
s.words = append(s.words, tword)
}
}
}
func (s *IntSet) Len() int {
count := 0
for _, word := range s.words {
for word != 0 {
count++
word &= word - 1
}
}
return count
}
//字符串表现形式
func (s *IntSet) String() string {
var buf bytes.Buffer
buf.WriteByte('{')
for i, word := range s.words {
if word == 0 {
continue
}
for j := 0; j < 64; j++ {
if word&(1<<uint(j)) != 0 {
if buf.Len() > len("{") {
buf.WriteByte(' ')
}
fmt.Fprintf(&buf, "%d", 64*i+j)
}
}
}
buf.WriteByte('}')
return buf.String()
}
//以上是实现位向量bitvector
type BloomFilter struct {
BitVector *IntSet
Seeds []int64
}
func (bf *BloomFilter) Hash(value string, seed int64) int64 {
var ret int64
for _, ch := range value {
ret += seed*ret + int64(ch)
}
return ret & ((1 << 31) - 1)
}
func (bf *BloomFilter) Insert(value string) {
for _, seed := range bf.Seeds {
ret := bf.Hash(value, seed)
bf.BitVector.Add(ret)
}
}
func (bf *BloomFilter) IsContain(value string) bool {
if value == "" {
return false
}
result := true
for _, seed := range bf.Seeds {
ret := bf.Hash(value, seed)
result = bf.BitVector.Has(ret)
}
return result
}
func main() {
bf := BloomFilter{}
bf.BitVector = new(IntSet)
bf.Seeds = []int64{5, 7, 11, 13, 19, 31, 37, 61}
bf.BitVector.Add(1 << 32)
f, err := os.Open("urls.txt")
if err != nil {
fmt.Println(err.Error())
}
buf := bufio.NewReader(f)
for {
b, errR := buf.ReadBytes('\n')
if errR != nil {
if errR == io.EOF {
break
}
fmt.Println(errR.Error())
}
s := strings.TrimRight(string(b), "\r\n")
if strings.Compare(s, "exit") == 0 {
fmt.Println("complete and exit now")
break
} else if bf.IsContain(s) == false {
bf.Insert(s)
} else {
fmt.Printf("url: %s is exist\n", s)
}
}
}