-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
btree.go
170 lines (148 loc) · 2.97 KB
/
btree.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package btree
import (
"encoding/hex"
"errors"
)
// Btree is the binary tree implementation used by the Kademlia routing table.
// The Kademlia literature refers to "buckets" that can be "split" according to certain rules when they are full.
// This implementation does not use buckets exactly as described in the literature, but rather a simpler binary tree,
// however the end result is largely equivalent.
type Btree interface {
N() int
Put(NodeID) PutResult
Has(NodeID) bool
Drop(NodeID) bool
Closest(NodeID, int) []NodeID
Count() int
}
type NodeID []byte
type Bit bool
const (
Bit0 Bit = false
Bit1 Bit = true
)
type Bits []Bit
func (b Bits) LeadingZeros() int {
for i, bit := range b {
if bit {
return i
}
}
return len(b)
}
func (b Bits) Cmp(other Bits) int {
thisPos := b.LeadingZeros() + 1
otherPos := other.LeadingZeros() + 1
if thisPos < otherPos {
return 1
}
if thisPos > otherPos {
return -1
}
if thisPos == len(b) {
return 0
}
return b[thisPos:].Cmp(other[thisPos:])
}
func (b Bits) String() string {
str := ""
for _, bit := range b {
if bit {
str += "1"
} else {
str += "0"
}
}
return str
}
func ParseBinaryNodeID(str string) (NodeID, error) {
if len(str)%8 != 0 {
return nil, errors.New("length must be multiple of 8")
}
id := make(NodeID, len(str)/8)
for i := 0; i < len(str); i++ {
if str[i] == '1' {
id[i/8] |= 1 << (7 - uint(i%8))
} else if str[i] != '0' {
return nil, errors.New("invalid character")
}
}
return id, nil
}
func MustParseBinaryNodeID(str string) NodeID {
id, err := ParseBinaryNodeID(str)
if err != nil {
panic(err)
}
return id
}
func (id NodeID) GetBit(n int) Bit {
return id[n/8]>>(7-uint(n%8))&1 == 1
}
func (id NodeID) Xor(other NodeID) (NodeID, error) {
if len(id) != len(other) {
return nil, errors.New("length mismatch")
}
ret := make(NodeID, len(id))
for i := 0; i < len(id); i++ {
ret[i] = id[i] ^ other[i]
}
return ret, nil
}
func (id NodeID) MustXor(other NodeID) NodeID {
ret, err := id.Xor(other)
if err != nil {
panic(err)
}
return ret
}
func (id NodeID) String() string {
return hex.EncodeToString(id)
}
func (id NodeID) BinaryString() string {
return id.Bits().String()
}
func (id NodeID) Equals(other NodeID) bool {
if len(other) != len(id) {
return false
}
for i, b := range other {
if b != id[i] {
return false
}
}
return true
}
func (id NodeID) Bits() Bits {
path := make(Bits, len(id)*8)
for i := 0; i < len(id)*8; i++ {
if id.GetBit(i) {
path[i] = Bit1
}
}
return path
}
type PutResult int
const (
PutRejected PutResult = iota
PutAccepted
PutAlreadyExists
)
func (r PutResult) String() string {
switch r {
case PutRejected:
return "rejected"
case PutAccepted:
return "accepted"
case PutAlreadyExists:
return "already exists"
default:
return "unknown"
}
}
func appendToPath(path []Bit, bit Bit) []Bit {
newPath := make([]Bit, 0, len(path)+1)
newPath = append(newPath, path...)
newPath = append(newPath, bit)
return newPath
}