13
13
//
14
14
// You should have received a copy of the GNU Lesser General Public License
15
15
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ // Package pot see doc.go
16
18
package pot
17
19
18
20
import (
@@ -26,35 +28,40 @@ import (
26
28
)
27
29
28
30
var (
29
- zeroAddr = & common.Hash {}
30
- zerosHex = zeroAddr .Hex ()[2 :]
31
31
zerosBin = Address {}.Bin ()
32
32
)
33
33
34
- var (
35
- addrlen = keylen
36
- )
37
-
34
+ // Address is an alias for common.Hash
38
35
type Address common.Hash
39
36
37
+ // NewAddressFromBytes constructs an Address from a byte slice
38
+ func NewAddressFromBytes (b []byte ) Address {
39
+ h := common.Hash {}
40
+ copy (h [:], b )
41
+ return Address (h )
42
+ }
43
+
40
44
func (a Address ) String () string {
41
45
return fmt .Sprintf ("%x" , a [:])
42
46
}
43
47
48
+ // MarshalJSON Address serialisation
44
49
func (a * Address ) MarshalJSON () (out []byte , err error ) {
45
50
return []byte (`"` + a .String () + `"` ), nil
46
51
}
47
52
53
+ // UnmarshalJSON Address deserialisation
48
54
func (a * Address ) UnmarshalJSON (value []byte ) error {
49
55
* a = Address (common .HexToHash (string (value [1 : len (value )- 1 ])))
50
56
return nil
51
57
}
52
58
53
- // the string form of the binary representation of an address (only first 8 bits)
59
+ // Bin returns the string form of the binary representation of an address (only first 8 bits)
54
60
func (a Address ) Bin () string {
55
61
return ToBin (a [:])
56
62
}
57
63
64
+ // ToBin converts a byteslice to the string binary representation
58
65
func ToBin (a []byte ) string {
59
66
var bs []string
60
67
for _ , b := range a {
@@ -63,6 +70,7 @@ func ToBin(a []byte) string {
63
70
return strings .Join (bs , "" )
64
71
}
65
72
73
+ // Bytes returns the Address as a byte slice
66
74
func (a Address ) Bytes () []byte {
67
75
return a [:]
68
76
}
@@ -107,23 +115,23 @@ func posProximity(one, other Address, pos int) (ret int, eq bool) {
107
115
return len (one ) * 8 , true
108
116
}
109
117
110
- // Address. ProxCmp compares the distances a->target and b->target.
118
+ // ProxCmp compares the distances a->target and b->target.
111
119
// Returns -1 if a is closer to target, 1 if b is closer to target
112
120
// and 0 if they are equal.
113
- func (target Address ) ProxCmp (a , b Address ) int {
114
- for i := range target {
115
- da := a [i ] ^ target [i ]
116
- db := b [i ] ^ target [i ]
117
- if da > db {
121
+ func (a Address ) ProxCmp (x , y Address ) int {
122
+ for i := range a {
123
+ dx := x [i ] ^ a [i ]
124
+ dy := y [i ] ^ a [i ]
125
+ if dx > dy {
118
126
return 1
119
- } else if da < db {
127
+ } else if dx < dy {
120
128
return - 1
121
129
}
122
130
}
123
131
return 0
124
132
}
125
133
126
- // randomAddressAt (address, prox) generates a random address
134
+ // RandomAddressAt (address, prox) generates a random address
127
135
// at proximity order prox relative to address
128
136
// if prox is negative a random address is generated
129
137
func RandomAddressAt (self Address , prox int ) (addr Address ) {
@@ -148,71 +156,12 @@ func RandomAddressAt(self Address, prox int) (addr Address) {
148
156
return
149
157
}
150
158
151
- // KeyRange(a0, a1, proxLimit) returns the address inclusive address
152
- // range that contain addresses closer to one than other
153
- // func KeyRange(one, other Address, proxLimit int) (start, stop Address) {
154
- // prox := proximity(one, other)
155
- // if prox >= proxLimit {
156
- // prox = proxLimit
157
- // }
158
- // start = CommonBitsAddrByte(one, other, byte(0x00), prox)
159
- // stop = CommonBitsAddrByte(one, other, byte(0xff), prox)
160
- // return
161
- // }
162
-
163
- func CommonBitsAddrF (self , other Address , f func () byte , p int ) (addr Address ) {
164
- prox , _ := proximity (self , other )
165
- var pos int
166
- if p <= prox {
167
- prox = p
168
- }
169
- pos = prox / 8
170
- addr = self
171
- trans := byte (prox % 8 )
172
- var transbytea byte
173
- if p > prox {
174
- transbytea = byte (0x7f )
175
- } else {
176
- transbytea = byte (0xff )
177
- }
178
- transbytea >>= trans
179
- transbyteb := transbytea ^ byte (0xff )
180
- addrpos := addr [pos ]
181
- addrpos &= transbyteb
182
- if p > prox {
183
- addrpos ^= byte (0x80 >> trans )
184
- }
185
- addrpos |= transbytea & f ()
186
- addr [pos ] = addrpos
187
- for i := pos + 1 ; i < len (addr ); i ++ {
188
- addr [i ] = f ()
189
- }
190
-
191
- return
192
- }
193
-
194
- func CommonBitsAddr (self , other Address , prox int ) (addr Address ) {
195
- return CommonBitsAddrF (self , other , func () byte { return byte (rand .Intn (255 )) }, prox )
196
- }
197
-
198
- func CommonBitsAddrByte (self , other Address , b byte , prox int ) (addr Address ) {
199
- return CommonBitsAddrF (self , other , func () byte { return b }, prox )
200
- }
201
-
202
- // randomAddressAt() generates a random address
159
+ // RandomAddress generates a random address
203
160
func RandomAddress () Address {
204
161
return RandomAddressAt (Address {}, - 1 )
205
162
}
206
163
207
- // wraps an Address to implement the PotVal interface
208
- type HashAddress struct {
209
- Address
210
- }
211
-
212
- func (a * HashAddress ) String () string {
213
- return a .Address .Bin ()
214
- }
215
-
164
+ // NewAddressFromString creates a byte slice from a string in binary representation
216
165
func NewAddressFromString (s string ) []byte {
217
166
ha := [32 ]byte {}
218
167
@@ -227,85 +176,16 @@ func NewAddressFromString(s string) []byte {
227
176
return ha [:]
228
177
}
229
178
230
- func NewHashAddress (s string ) * HashAddress {
231
- ha := NewAddressFromString (s )
232
- h := common.Hash {}
233
- copy (h [:], ha )
234
- return & HashAddress {Address (h )}
235
- }
236
-
237
- func NewHashAddressFromBytes (b []byte ) * HashAddress {
238
- h := common.Hash {}
239
- copy (h [:], b )
240
- return & HashAddress {Address (h )}
241
- }
242
-
243
- // PO(addr, pos) return the proximity order of addr wrt to
244
- // the pinned address of the tree
245
- // assuming it is greater than or equal to pos
246
- func (self * HashAddress ) PO (val PotVal , pos int ) (po int , eq bool ) {
247
- return posProximity (self .Address , val .(* HashAddress ).Address , pos )
248
- }
249
-
250
- type BoolAddress struct {
251
- addr []bool
252
- }
253
-
254
- func NewBoolAddress (s string ) * BoolAddress {
255
- return NewBoolAddressXOR (s , zerosBin [:len (s )])
256
- }
257
-
258
- func NewBoolAddressXOR (s , t string ) * BoolAddress {
259
- if len (s ) != len (t ) {
260
- panic ("lengths do not match" )
261
- }
262
- addr := make ([]bool , len (s ))
263
- for i , _ := range addr {
264
- addr [i ] = s [i ] != t [i ]
265
- }
266
- return & BoolAddress {addr }
267
- }
268
-
269
- func (self * BoolAddress ) String () string {
270
- a := self .addr
271
- s := []byte (zerosBin )[:len (a )]
272
- for i , one := range a {
273
- if one {
274
- s [i ] = byte ('1' )
275
- }
276
- }
277
- return string (s )
278
- }
279
-
280
- func (self * BoolAddress ) PO (val PotVal , pos int ) (po int , eq bool ) {
281
- a := self .addr
282
- b := val .(* BoolAddress ).addr
283
- for po = pos ; po < len (b ); po ++ {
284
- if a [po ] != b [po ] {
285
- return po , false
286
- }
287
- }
288
- return po , true
289
- }
290
-
179
+ // BytesAddress is an interface for elements addressable by a byte slice
291
180
type BytesAddress interface {
292
181
Address () []byte
293
182
}
294
183
295
- type bytesAddress struct {
296
- bytes []byte
297
- toBytes func (v AnyVal ) []byte
298
- }
299
-
300
- func NewBytesVal (v AnyVal , f func (v AnyVal ) []byte ) * bytesAddress {
301
- if f == nil {
302
- f = ToBytes
184
+ // ToBytes turns the Val into bytes
185
+ func ToBytes (v Val ) []byte {
186
+ if v == nil {
187
+ return nil
303
188
}
304
- b := f (v )
305
- return & bytesAddress {b , f }
306
- }
307
-
308
- func ToBytes (v AnyVal ) []byte {
309
189
b , ok := v .([]byte )
310
190
if ! ok {
311
191
ba , ok := v .(BytesAddress )
@@ -317,15 +197,17 @@ func ToBytes(v AnyVal) []byte {
317
197
return b
318
198
}
319
199
320
- func (a * bytesAddress ) String () string {
321
- return fmt .Sprintf ("%08b" , a .bytes )
322
- }
323
- func (a * bytesAddress ) Address () []byte {
324
- return a .bytes
325
- }
326
-
327
- func (a * bytesAddress ) PO (val PotVal , i int ) (int , bool ) {
328
- return proximityOrder (a .bytes , a .toBytes (val ), i )
200
+ // DefaultPof returns a proximity order comparison operator function
201
+ // where all
202
+ func DefaultPof (max int ) func (one , other Val , pos int ) (int , bool ) {
203
+ return func (one , other Val , pos int ) (int , bool ) {
204
+ po , eq := proximityOrder (ToBytes (one ), ToBytes (other ), pos )
205
+ if po >= max {
206
+ eq = true
207
+ po = max
208
+ }
209
+ return po , eq
210
+ }
329
211
}
330
212
331
213
func proximityOrder (one , other []byte , pos int ) (int , bool ) {
@@ -346,3 +228,17 @@ func proximityOrder(one, other []byte, pos int) (int, bool) {
346
228
}
347
229
return len (one ) * 8 , true
348
230
}
231
+
232
+ // Label displays the node's key in binary format
233
+ func Label (v Val ) string {
234
+ if v == nil {
235
+ return "<nil>"
236
+ }
237
+ if s , ok := v .(fmt.Stringer ); ok {
238
+ return s .String ()
239
+ }
240
+ if b , ok := v .([]byte ); ok {
241
+ return ToBin (b )
242
+ }
243
+ panic (fmt .Sprintf ("unsupported value type %T" , v ))
244
+ }
0 commit comments