-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdense_copy.go
More file actions
41 lines (37 loc) · 734 Bytes
/
dense_copy.go
File metadata and controls
41 lines (37 loc) · 734 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
package trie
type copyTxn struct {
nodes []Node
edges []*Node
}
func (root *Node) DenseCopy() *Node {
var n int
root.Walk(func(node *Node) bool {
n += 1
return true
})
t := ©Txn{
nodes: make([]Node, n),
edges: make([]*Node, n-1),
}
return t.copyNode(root)
}
func (t *copyTxn) copyNode(n *Node) (cp *Node) {
if i := len(t.nodes) - 1; i >= 0 {
t.nodes, cp = t.nodes[:i:i], &t.nodes[i]
cp.key = n.key
cp.value = n.value
cp.edges = t.copyEdges(n.edges)
return
}
panic("node not preallocated")
}
func (t *copyTxn) copyEdges(es edges) (cp edges) {
n := len(es)
i := len(t.edges) - n
if i >= 0 {
t.edges, cp = t.edges[:i:i], t.edges[i:]
copy(cp, es)
return
}
panic("edges not preallocated")
}