forked from tonkeeper/tongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbintree.go
65 lines (57 loc) · 1.14 KB
/
bintree.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
package tlb
import (
"fmt"
"github.com/tonkeeper/tongo/boc"
)
type BinTree[T any] struct {
Values []T
}
func decodeRecursiveBinTree(c *boc.Cell) ([]*boc.Cell, error) {
var cellAr []*boc.Cell
isBranch, err := c.ReadBit()
if err != nil {
return nil, err
}
if !isBranch {
return append(cellAr, c), nil
}
l, err := c.NextRef()
if err != nil {
return nil, err
}
rec, err := decodeRecursiveBinTree(l)
if err != nil {
return nil, err
}
cellAr = append(cellAr, rec...)
r, err := c.NextRef()
if err != nil {
return nil, err
}
rec, err = decodeRecursiveBinTree(r)
if err != nil {
return nil, err
}
cellAr = append(cellAr, rec...)
return cellAr, nil
}
func (b BinTree[T]) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
// TODO: implement
return fmt.Errorf("BinTree marshaling not implemented")
}
func (b *BinTree[T]) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
dec, err := decodeRecursiveBinTree(c)
if err != nil {
return err
}
b.Values = make([]T, 0, len(dec))
for _, i := range dec {
var t T
err := decoder.Unmarshal(i, &t)
if err != nil {
return err
}
b.Values = append(b.Values, t)
}
return nil
}