-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
65 lines (53 loc) · 1.26 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
65
// package set contains various different types of 'BlockSet's
package set
import (
"github.com/ipfs/go-ipfs/blocks/bloom"
key "github.com/ipfs/go-ipfs/blocks/key"
"github.com/ipfs/go-ipfs/util"
)
var log = util.Logger("blockset")
// BlockSet represents a mutable set of keyed blocks
type BlockSet interface {
AddBlock(key.Key)
RemoveBlock(key.Key)
HasKey(key.Key) bool
GetBloomFilter() bloom.Filter
GetKeys() []key.Key
}
func SimpleSetFromKeys(keys []key.Key) BlockSet {
sbs := &simpleBlockSet{blocks: make(map[key.Key]struct{})}
for _, k := range keys {
sbs.blocks[k] = struct{}{}
}
return sbs
}
func NewSimpleBlockSet() BlockSet {
return &simpleBlockSet{blocks: make(map[key.Key]struct{})}
}
type simpleBlockSet struct {
blocks map[key.Key]struct{}
}
func (b *simpleBlockSet) AddBlock(k key.Key) {
b.blocks[k] = struct{}{}
}
func (b *simpleBlockSet) RemoveBlock(k key.Key) {
delete(b.blocks, k)
}
func (b *simpleBlockSet) HasKey(k key.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() []key.Key {
var out []key.Key
for k := range b.blocks {
out = append(out, k)
}
return out
}