-
Notifications
You must be signed in to change notification settings - Fork 13
/
set.go
77 lines (60 loc) · 1.32 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
72
73
74
75
76
77
package hamt
// Set represents a set.
type Set[T Entry[T]] struct {
size int
hamt hamt[T]
}
// NewSet creates a new set.
func NewSet[T Entry[T]]() Set[T] {
return Set[T]{0, newHamt[T](0)}
}
// Insert inserts a value into a set.
func (s Set[T]) Insert(e T) Set[T] {
size := s.size
if s.find(e) == nil {
size++
}
return Set[T]{size, s.hamt.Insert(e).(hamt[T])}
}
// Delete deletes a value from a set.
func (s Set[T]) Delete(e T) Set[T] {
n, b := s.hamt.Delete(e)
size := s.size
if b {
size--
}
return Set[T]{size, n.(hamt[T])}
}
func (s Set[T]) find(e T) *T {
return s.hamt.Find(e)
}
// Include returns true if a given entry is included in a set, or false otherwise.
func (s Set[T]) Include(e T) bool {
return s.find(e) != nil
}
// FirstRest returns a pointer to a value in a set and a rest of the set.
// This method is useful for iteration.
func (s Set[T]) FirstRest() (*T, Set[T]) {
e, n := s.hamt.FirstRest()
size := s.size
if e != nil {
size--
}
return e, Set[T]{size, n.(hamt[T])}
}
func (s Set[T]) ForEach(cb func(T) error) error {
return s.hamt.ForEach(cb)
}
// Merge merges 2 sets into one.
func (s Set[T]) Merge(t Set[T]) Set[T] {
for t.Size() != 0 {
var e *T
e, t = t.FirstRest()
s = s.Insert(*e)
}
return s
}
// Size returns a size of a set.
func (s Set[T]) Size() int {
return s.size
}