-
Notifications
You must be signed in to change notification settings - Fork 671
/
ancestor_tree.go
94 lines (81 loc) · 2.27 KB
/
ancestor_tree.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package snowman
import (
"github.com/ava-labs/avalanchego/ids"
)
type AncestorTree interface {
Add(blkID ids.ID, parentID ids.ID)
Has(blkID ids.ID) bool
GetRoot(blkID ids.ID) ids.ID
Remove(blkID ids.ID)
RemoveSubtree(blkID ids.ID)
Len() int
}
type ancestorTree struct {
childToParent map[ids.ID]ids.ID
parentToChildren map[ids.ID]ids.Set
}
func NewAncestorTree() AncestorTree {
return &ancestorTree{
childToParent: make(map[ids.ID]ids.ID),
parentToChildren: make(map[ids.ID]ids.Set),
}
}
// Add maps given blkID to given parentID
func (p *ancestorTree) Add(blkID ids.ID, parentID ids.ID) {
p.childToParent[blkID] = parentID
children := p.parentToChildren[parentID]
children.Add(blkID)
p.parentToChildren[parentID] = children
}
// GetRoot returns the oldest parent of blkID, might return blkID if no parent is available.
func (p *ancestorTree) GetRoot(blkID ids.ID) ids.ID {
for {
parentID, ok := p.childToParent[blkID]
// this is the furthest parent available, break loop and return blkID
if !ok {
return blkID
}
// continue to loop with parentID
blkID = parentID
}
}
// Has returns if blkID is in the tree or not
func (p *ancestorTree) Has(blkID ids.ID) bool {
_, ok := p.childToParent[blkID]
return ok
}
// Remove removes blkID from the tree
func (p *ancestorTree) Remove(blkID ids.ID) {
parent, ok := p.childToParent[blkID]
if !ok {
return
}
delete(p.childToParent, blkID)
// remove blkID from children
children := p.parentToChildren[parent]
children.Remove(blkID)
// this parent has no more children, remove it from map
if children.Len() == 0 {
delete(p.parentToChildren, parent)
}
}
// Returns tree length
func (p *ancestorTree) Len() int {
return len(p.childToParent)
}
// RemoveSubtree removes whole subtree that blkID holds
func (p *ancestorTree) RemoveSubtree(blkID ids.ID) {
childrenList := []ids.ID{blkID}
for len(childrenList) > 0 {
newChildrenSize := len(childrenList) - 1
childID := childrenList[newChildrenSize]
childrenList = childrenList[:newChildrenSize]
p.Remove(childID)
// get children of child
for grandChildID := range p.parentToChildren[childID] {
childrenList = append(childrenList, grandChildID)
}
}
}