-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
71 lines (58 loc) · 1.36 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
66
67
68
69
70
71
package deks
import (
"math/big"
"github.com/simia-tech/conflux"
"github.com/simia-tech/conflux/recon"
"github.com/simia-tech/errx"
)
var p *big.Int
func init() {
p = big.NewInt(0)
p.SetString("530512889551602322505127520352579437339", 10)
}
// Set defines a set of items.
type Set struct {
pf *recon.MemPrefixTree
}
// NewSet returns a new empty set.
func NewSet() *Set {
pf := &recon.MemPrefixTree{}
pf.Init()
return &Set{pf: pf}
}
// Insert adds the provided item to the set.
func (s *Set) Insert(item Item) error {
if err := s.pf.Insert(conflux.Zb(p, item[:])); err != nil {
return errx.Annotatef(err, "insert")
}
return nil
}
// Remove removes the provided item from the set.
func (s *Set) Remove(item Item) error {
if err := s.pf.Remove(conflux.Zb(p, item[:])); err != nil {
return errx.Annotatef(err, "remove")
}
return nil
}
// Items returns a slice of all items in the set.
func (s *Set) Items() []Item {
items := s.pf.Items()
results := make([]Item, len(items))
for index, item := range items {
i := Item{}
copy(i[:], item.Bytes())
results[index] = i
}
return results
}
// Len returns the length of the set.
func (s *Set) Len() int {
return s.pf.Len()
}
func (s *Set) prefixTree() recon.PrefixTree {
return s.pf
}
// ItemSize specifies the item size.
const ItemSize = keyHashSize + 8
// Item defines the set item.
type Item [ItemSize]byte