-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch_node.go
74 lines (62 loc) · 1.2 KB
/
branch_node.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
package PromethoniXTrie
import (
"io"
)
const branchHashSize = 16
type BranchNode struct {
NodeDetails
Hashes [branchHashSize]Hash
}
func (node *BranchNode) Type() NodeType {
return Branch
}
func (node *BranchNode) Encode(writer io.Writer) error {
var err error = nil
for i := 0; i < branchHashSize; i++ {
l := len(node.Hashes[i])
err = writeInt32(writer, int32(l))
if err != nil {
return err
}
if l > 0 {
_, err = writer.Write(node.Hashes[i])
}
if err != nil {
return err
}
}
return err
}
func (node *BranchNode) Decode(reader io.Reader) error {
for i := 0; i < branchHashSize; i++ {
length, err := readInt32(reader)
if err != nil {
return err
}
node.Hashes[i], err = readBytes(reader, length)
if err != nil {
return err
}
}
return nil
}
func (node *BranchNode) NextRoute(route Route) (Hash, Route, error) {
return node.Hashes[route[0]], route[1:], nil
}
func (node *BranchNode) Length() int {
l := 0
for idx := range node.Hashes {
if len(node.Hashes[idx]) != 0 {
l++
}
}
return l
}
func AsBranch(node Node) *BranchNode {
return node.(*BranchNode)
}
func NewBranchNode() *BranchNode {
node := new(BranchNode)
node._node = node
return node
}