forked from ErosZy/singoriensis
/
urlHeap.go
53 lines (45 loc) · 987 Bytes
/
urlHeap.go
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
package singoriensis
import (
"container/list"
"crypto/md5"
"encoding/hex"
"math/big"
"singoriensis/common"
)
type UrlHeap struct {
hash []*list.List
size int
}
func NewUrlHeap(heapSize int) *UrlHeap {
return &UrlHeap{
hash: make([]*list.List, heapSize+1),
size: heapSize,
}
}
func (self *UrlHeap) Contain(elem common.ElementItem) bool {
flag := false
crypto := md5.New()
crypto.Write([]byte(elem.UrlStr))
hashNum := common.NewDjb2Hash(hex.EncodeToString(crypto.Sum(nil)))
hashNum = hashNum.Mod(hashNum, big.NewInt(int64(self.size)))
i := hashNum.Int64()
hashList := self.hash[int(i)]
if hashList != nil {
nextItem := hashList.Front()
for ; nextItem != nil; nextItem = nextItem.Next() {
value := nextItem.Value.(common.ElementItem)
if value.UrlStr == elem.UrlStr {
flag = true
break
}
}
if !flag {
hashList.PushBack(elem)
}
} else {
hashList = list.New()
hashList.PushBack(elem)
self.hash[i] = hashList
}
return flag
}