-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_set.go
More file actions
63 lines (53 loc) · 923 Bytes
/
node_set.go
File metadata and controls
63 lines (53 loc) · 923 Bytes
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
package maneuver
import (
"sync"
)
type NodeSet struct {
length uint64
lock sync.Mutex
set map[uint64]Node
}
func (n *NodeSet) Add(node Node) {
n.lock.Lock()
defer n.lock.Unlock()
if n.Contains(node) {
return
}
n.set[node.GetId()] = node
n.length++
}
func (n *NodeSet) Remove(node Node) {
n.lock.Lock()
defer n.lock.Unlock()
if !n.Contains(node) {
return
}
delete(n.set, node.GetId())
n.length--
}
func (n *NodeSet) Contains(node Node) bool {
_, ok := n.set[node.GetId()]
return ok
}
func (n *NodeSet) Empty() bool {
return n.length == 0
}
func (n *NodeSet) Get(id uint64) Node {
return n.set[id]
}
func (n *NodeSet) AllNodes() []Node {
nodes := make([]Node, len(n.set))
i := 0
for _, v := range n.set {
nodes[i], i = v, i+1
}
return nodes
}
func (n *NodeSet) Size() uint64 {
return n.length
}
func NewNodeSet() *NodeSet {
return &NodeSet{
set: make(map[uint64]Node),
}
}