Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-kbucket
Commits
a35399ed
Commit
a35399ed
authored
Sep 17, 2014
by
Juan Batiz-Benet
Committed by
Brian Tiger Chow
Sep 22, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored keyspace Adjusted -> Bytes
parent
7550fd11
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
16 deletions
+16
-16
keyspace.go
keyspace.go
+3
-3
xor.go
xor.go
+5
-5
xor_test.go
xor_test.go
+8
-8
No files found.
keyspace.go
View file @
a35399ed
...
...
@@ -8,7 +8,7 @@ import (
// Key represents an identifier in a KeySpace. It holds a reference to the
// associated KeySpace, as well references to both the Original identifier,
// as well as the new, KeySpace
Adjus
te
d
one.
// as well as the new, KeySpace
By
te
s
one.
type
Key
struct
{
// Space is the KeySpace this Key is related to.
...
...
@@ -17,8 +17,8 @@ type Key struct {
// Original is the original value of the identifier
Original
[]
byte
//
Adjus
te
d
is the new value of the identifier, in the KeySpace.
Adjus
te
d
[]
byte
//
By
te
s
is the new value of the identifier, in the KeySpace.
By
te
s
[]
byte
}
// Equal returns whether this key is equal to another.
...
...
xor.go
View file @
a35399ed
...
...
@@ -21,19 +21,19 @@ func (s *xorKeySpace) Key(id []byte) Key {
return
Key
{
Space
:
s
,
Original
:
id
,
Adjusted
:
key
,
Bytes
:
key
,
}
}
// Equal returns whether keys are equal in this key space
func
(
s
*
xorKeySpace
)
Equal
(
k1
,
k2
Key
)
bool
{
return
bytes
.
Equal
(
k1
.
Adjus
te
d
,
k2
.
Adjus
te
d
)
return
bytes
.
Equal
(
k1
.
By
te
s
,
k2
.
By
te
s
)
}
// Distance returns the distance metric in this key space
func
(
s
*
xorKeySpace
)
Distance
(
k1
,
k2
Key
)
*
big
.
Int
{
// XOR the keys
k3
:=
XOR
(
k1
.
Adjus
te
d
,
k2
.
Adjus
te
d
)
k3
:=
XOR
(
k1
.
By
te
s
,
k2
.
By
te
s
)
// interpret it as an integer
dist
:=
big
.
NewInt
(
0
)
.
SetBytes
(
k3
)
...
...
@@ -42,8 +42,8 @@ func (s *xorKeySpace) Distance(k1, k2 Key) *big.Int {
// Less returns whether the first key is smaller than the second.
func
(
s
*
xorKeySpace
)
Less
(
k1
,
k2
Key
)
bool
{
a
:=
k1
.
Adjus
te
d
b
:=
k2
.
Adjus
te
d
a
:=
k1
.
By
te
s
b
:=
k2
.
By
te
s
for
i
:=
0
;
i
<
len
(
a
);
i
++
{
if
a
[
i
]
!=
b
[
i
]
{
return
a
[
i
]
<
b
[
i
]
...
...
xor_test.go
View file @
a35399ed
...
...
@@ -69,16 +69,16 @@ func TestXorKeySpace(t *testing.T) {
t
.
Errorf
(
"Key not eq. %v != %v"
,
set
[
0
],
set
[
1
])
}
if
!
bytes
.
Equal
(
set
[
0
]
.
Adjus
te
d
,
set
[
1
]
.
Adjus
te
d
)
{
t
.
Errorf
(
"Key gen failed. %v != %v"
,
set
[
0
]
.
Adjus
te
d
,
set
[
1
]
.
Adjus
te
d
)
if
!
bytes
.
Equal
(
set
[
0
]
.
By
te
s
,
set
[
1
]
.
By
te
s
)
{
t
.
Errorf
(
"Key gen failed. %v != %v"
,
set
[
0
]
.
By
te
s
,
set
[
1
]
.
By
te
s
)
}
if
!
bytes
.
Equal
(
set
[
0
]
.
Original
,
ids
[
i
])
{
t
.
Errorf
(
"ptrs to original. %v != %v"
,
set
[
0
]
.
Original
,
ids
[
i
])
}
if
len
(
set
[
0
]
.
Adjus
te
d
)
!=
32
{
t
.
Errorf
(
"key length incorrect. 32 != %d"
,
len
(
set
[
0
]
.
Adjus
te
d
))
if
len
(
set
[
0
]
.
By
te
s
)
!=
32
{
t
.
Errorf
(
"key length incorrect. 32 != %d"
,
len
(
set
[
0
]
.
By
te
s
))
}
}
...
...
@@ -110,7 +110,7 @@ func TestDistancesAndCenterSorting(t *testing.T) {
keys
:=
make
([]
Key
,
len
(
adjs
))
for
i
,
a
:=
range
adjs
{
keys
[
i
]
=
Key
{
Space
:
XORKeySpace
,
Adjus
te
d
:
a
}
keys
[
i
]
=
Key
{
Space
:
XORKeySpace
,
By
te
s
:
a
}
}
cmp
:=
func
(
a
int
,
b
*
big
.
Int
)
int
{
...
...
@@ -126,8 +126,8 @@ func TestDistancesAndCenterSorting(t *testing.T) {
}
d1
:=
keys
[
2
]
.
Distance
(
keys
[
5
])
d2
:=
XOR
(
keys
[
2
]
.
Adjus
te
d
,
keys
[
5
]
.
Adjus
te
d
)
d2
=
d2
[
len
(
keys
[
2
]
.
Adjus
te
d
)
-
len
(
d1
.
Bytes
())
:
]
// skip empty space for big
d2
:=
XOR
(
keys
[
2
]
.
By
te
s
,
keys
[
5
]
.
By
te
s
)
d2
=
d2
[
len
(
keys
[
2
]
.
By
te
s
)
-
len
(
d1
.
Bytes
())
:
]
// skip empty space for big
if
!
bytes
.
Equal
(
d1
.
Bytes
(),
d2
)
{
t
.
Errorf
(
"bytes should be the same. %v == %v"
,
d1
.
Bytes
(),
d2
)
}
...
...
@@ -139,7 +139,7 @@ func TestDistancesAndCenterSorting(t *testing.T) {
keys2
:=
SortByDistance
(
XORKeySpace
,
keys
[
2
],
keys
)
order
:=
[]
int
{
2
,
3
,
4
,
5
,
1
,
0
}
for
i
,
o
:=
range
order
{
if
!
bytes
.
Equal
(
keys
[
o
]
.
Adjus
te
d
,
keys2
[
i
]
.
Adjus
te
d
)
{
if
!
bytes
.
Equal
(
keys
[
o
]
.
By
te
s
,
keys2
[
i
]
.
By
te
s
)
{
t
.
Errorf
(
"order is wrong. %d?? %v == %v"
,
o
,
keys
[
o
],
keys2
[
i
])
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment