Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
e62b8222
提交
e62b8222
authored
9月 17, 2014
作者:
Juan Batiz-Benet
提交者:
Brian Tiger Chow
9月 22, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactored keyspace Adjusted -> Bytes
上级
6c00938e
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
18 行增加
和
18 行删除
+18
-18
util.go
routing/kbucket/util.go
+2
-2
keyspace.go
routing/keyspace/keyspace.go
+3
-3
xor.go
routing/keyspace/xor.go
+5
-5
xor_test.go
routing/keyspace/xor_test.go
+8
-8
没有找到文件。
routing/kbucket/util.go
浏览文件 @
e62b8222
...
...
@@ -25,8 +25,8 @@ func (id ID) equal(other ID) bool {
}
func
(
id
ID
)
less
(
other
ID
)
bool
{
a
:=
ks
.
Key
{
Space
:
ks
.
XORKeySpace
,
Adjusted
:
id
}
b
:=
ks
.
Key
{
Space
:
ks
.
XORKeySpace
,
Adjusted
:
other
}
a
:=
ks
.
Key
{
Space
:
ks
.
XORKeySpace
,
Bytes
:
id
}
b
:=
ks
.
Key
{
Space
:
ks
.
XORKeySpace
,
Bytes
:
other
}
return
a
.
Less
(
b
)
}
...
...
routing/keyspace/keyspace.go
浏览文件 @
e62b8222
...
...
@@ -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
Adjusted
one.
// as well as the new, KeySpace
Bytes
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
//
Adjusted
is the new value of the identifier, in the KeySpace.
Adjusted
[]
byte
//
Bytes
is the new value of the identifier, in the KeySpace.
Bytes
[]
byte
}
// Equal returns whether this key is equal to another.
...
...
routing/keyspace/xor.go
浏览文件 @
e62b8222
...
...
@@ -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
.
Adjusted
,
k2
.
Adjusted
)
return
bytes
.
Equal
(
k1
.
Bytes
,
k2
.
Bytes
)
}
// Distance returns the distance metric in this key space
func
(
s
*
xorKeySpace
)
Distance
(
k1
,
k2
Key
)
*
big
.
Int
{
// XOR the keys
k3
:=
XOR
(
k1
.
Adjusted
,
k2
.
Adjusted
)
k3
:=
XOR
(
k1
.
Bytes
,
k2
.
Bytes
)
// 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
.
Adjusted
b
:=
k2
.
Adjusted
a
:=
k1
.
Bytes
b
:=
k2
.
Bytes
for
i
:=
0
;
i
<
len
(
a
);
i
++
{
if
a
[
i
]
!=
b
[
i
]
{
return
a
[
i
]
<
b
[
i
]
...
...
routing/keyspace/xor_test.go
浏览文件 @
e62b8222
...
...
@@ -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
]
.
Adjusted
,
set
[
1
]
.
Adjusted
)
{
t
.
Errorf
(
"Key gen failed. %v != %v"
,
set
[
0
]
.
Adjusted
,
set
[
1
]
.
Adjusted
)
if
!
bytes
.
Equal
(
set
[
0
]
.
Bytes
,
set
[
1
]
.
Bytes
)
{
t
.
Errorf
(
"Key gen failed. %v != %v"
,
set
[
0
]
.
Bytes
,
set
[
1
]
.
Bytes
)
}
if
!
bytes
.
Equal
(
set
[
0
]
.
Original
,
ids
[
i
])
{
t
.
Errorf
(
"ptrs to original. %v != %v"
,
set
[
0
]
.
Original
,
ids
[
i
])
}
if
len
(
set
[
0
]
.
Adjusted
)
!=
32
{
t
.
Errorf
(
"key length incorrect. 32 != %d"
,
len
(
set
[
0
]
.
Adjusted
))
if
len
(
set
[
0
]
.
Bytes
)
!=
32
{
t
.
Errorf
(
"key length incorrect. 32 != %d"
,
len
(
set
[
0
]
.
Bytes
))
}
}
...
...
@@ -110,7 +110,7 @@ func TestDistancesAndCenterSorting(t *testing.T) {
keys
:=
make
([]
Key
,
len
(
adjs
))
for
i
,
a
:=
range
adjs
{
keys
[
i
]
=
Key
{
Space
:
XORKeySpace
,
Adjusted
:
a
}
keys
[
i
]
=
Key
{
Space
:
XORKeySpace
,
Bytes
:
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
]
.
Adjusted
,
keys
[
5
]
.
Adjusted
)
d2
=
d2
[
len
(
keys
[
2
]
.
Adjusted
)
-
len
(
d1
.
Bytes
())
:
]
// skip empty space for big
d2
:=
XOR
(
keys
[
2
]
.
Bytes
,
keys
[
5
]
.
Bytes
)
d2
=
d2
[
len
(
keys
[
2
]
.
Bytes
)
-
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
]
.
Adjusted
,
keys2
[
i
]
.
Adjusted
)
{
if
!
bytes
.
Equal
(
keys
[
o
]
.
Bytes
,
keys2
[
i
]
.
Bytes
)
{
t
.
Errorf
(
"order is wrong. %d?? %v == %v"
,
o
,
keys
[
o
],
keys2
[
i
])
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论