-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
64 lines (52 loc) · 1.24 KB
/
set.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
54
55
56
57
58
59
60
61
62
63
64
// package set contains various different types of 'BlockSet's
package set
import (
"github.com/ipfs/go-ipfs/blocks/bloom"
"github.com/ipfs/go-ipfs/util"
)
var log = util.Logger("blockset")
// BlockSet represents a mutable set of keyed blocks
type BlockSet interface {
AddBlock(util.Key)
RemoveBlock(util.Key)
HasKey(util.Key) bool
GetBloomFilter() bloom.Filter
GetKeys() []util.Key
}
func SimpleSetFromKeys(keys []util.Key) BlockSet {
sbs := &simpleBlockSet{blocks: make(map[util.Key]struct{})}
for _, k := range keys {
sbs.blocks[k] = struct{}{}
}
return sbs
}
func NewSimpleBlockSet() BlockSet {
return &simpleBlockSet{blocks: make(map[util.Key]struct{})}
}
type simpleBlockSet struct {
blocks map[util.Key]struct{}
}
func (b *simpleBlockSet) AddBlock(k util.Key) {
b.blocks[k] = struct{}{}
}
func (b *simpleBlockSet) RemoveBlock(k util.Key) {
delete(b.blocks, k)
}
func (b *simpleBlockSet) HasKey(k util.Key) bool {
_, has := b.blocks[k]
return has
}
func (b *simpleBlockSet) GetBloomFilter() bloom.Filter {
f := bloom.BasicFilter()
for k := range b.blocks {
f.Add([]byte(k))
}
return f
}
func (b *simpleBlockSet) GetKeys() []util.Key {
var out []util.Key
for k := range b.blocks {
out = append(out, k)
}
return out
}