Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
f986f6ae
提交
f986f6ae
authored
11月 25, 2014
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Minimal perfect hash tool
上级
2d649ea2
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
528 行增加
和
0 行删除
+528
-0
TestIntPerfectHash.java
h2/src/test/org/h2/test/unit/TestIntPerfectHash.java
+109
-0
IntPerfectHash.java
h2/src/tools/org/h2/dev/hash/IntPerfectHash.java
+413
-0
MinimalPerfectHash.java
h2/src/tools/org/h2/dev/hash/MinimalPerfectHash.java
+6
-0
没有找到文件。
h2/src/test/org/h2/test/unit/TestIntPerfectHash.java
0 → 100644
浏览文件 @
f986f6ae
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
test
.
unit
;
import
java.util.ArrayList
;
import
java.util.BitSet
;
import
java.util.HashSet
;
import
java.util.Random
;
import
java.util.Set
;
import
org.h2.dev.hash.IntPerfectHash
;
import
org.h2.dev.hash.IntPerfectHash.BitArray
;
import
org.h2.test.TestBase
;
/**
* Tests the perfect hash tool.
*/
public
class
TestIntPerfectHash
extends
TestBase
{
/**
* Run just this test.
*
* @param a ignored
*/
public
static
void
main
(
String
...
a
)
throws
Exception
{
TestIntPerfectHash
test
=
(
TestIntPerfectHash
)
TestBase
.
createCaller
().
init
();
test
.
measure
();
test
.
test
();
test
.
measure
();
}
/**
* Measure the hash functions.
*/
public
void
measure
()
{
int
size
=
10000
;
test
(
size
/
10
);
int
s
;
long
time
=
System
.
currentTimeMillis
();
s
=
test
(
size
);
time
=
System
.
currentTimeMillis
()
-
time
;
System
.
out
.
println
((
double
)
s
/
size
+
" bits/key in "
+
time
+
" ms"
);
}
@Override
public
void
test
()
{
testBitArray
();
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
test
(
i
);
}
for
(
int
i
=
100
;
i
<=
10000
;
i
*=
10
)
{
test
(
i
);
}
}
private
void
testBitArray
()
{
byte
[]
data
=
new
byte
[
0
];
BitSet
set
=
new
BitSet
();
for
(
int
i
=
100
;
i
>=
0
;
i
--)
{
data
=
BitArray
.
setBit
(
data
,
i
,
true
);
set
.
set
(
i
);
}
Random
r
=
new
Random
(
1
);
for
(
int
i
=
0
;
i
<
10000
;
i
++)
{
int
pos
=
r
.
nextInt
(
100
);
boolean
s
=
r
.
nextBoolean
();
data
=
BitArray
.
setBit
(
data
,
pos
,
s
);
set
.
set
(
pos
,
s
);
pos
=
r
.
nextInt
(
100
);
assertTrue
(
BitArray
.
getBit
(
data
,
pos
)
==
set
.
get
(
pos
));
}
assertTrue
(
BitArray
.
countBits
(
data
)
==
set
.
cardinality
());
}
private
int
test
(
int
size
)
{
Random
r
=
new
Random
(
size
);
HashSet
<
Integer
>
set
=
new
HashSet
<
Integer
>();
while
(
set
.
size
()
<
size
)
{
set
.
add
(
r
.
nextInt
());
}
ArrayList
<
Integer
>
list
=
new
ArrayList
<
Integer
>();
list
.
addAll
(
set
);
byte
[]
desc
=
IntPerfectHash
.
generate
(
list
);
int
max
=
test
(
desc
,
set
);
assertEquals
(
size
-
1
,
max
);
return
desc
.
length
*
8
;
}
private
int
test
(
byte
[]
desc
,
Set
<
Integer
>
set
)
{
int
max
=
-
1
;
HashSet
<
Integer
>
test
=
new
HashSet
<
Integer
>();
IntPerfectHash
hash
=
new
IntPerfectHash
(
desc
);
for
(
int
x
:
set
)
{
int
h
=
hash
.
get
(
x
);
assertTrue
(
h
>=
0
);
assertTrue
(
h
<=
set
.
size
()
*
3
);
max
=
Math
.
max
(
max
,
h
);
assertFalse
(
test
.
contains
(
h
));
test
.
add
(
h
);
}
return
max
;
}
}
h2/src/tools/org/h2/dev/hash/IntPerfectHash.java
0 → 100644
浏览文件 @
f986f6ae
差异被折叠。
点击展开。
h2/src/tools/org/h2/dev/hash/MinimalPerfectHash.java
浏览文件 @
f986f6ae
...
...
@@ -76,6 +76,12 @@ public class MinimalPerfectHash<K> {
*/
private
static
final
int
DIVIDE
=
6
;
/**
* For sets larger than this, instead of trying to map then uniquely to a
* set of the same size, the size of the set is incremented by one. This
* reduces the time to find a mapping, but the index of the hole also needs
* to be stored, which increases the space usage.
*/
private
static
final
int
SPEEDUP
=
11
;
/**
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论