Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
4300469b
提交
4300469b
authored
8月 17, 2014
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Documentation
上级
b7a0f583
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
53 行增加
和
1 行删除
+53
-1
PerfectHash.java
h2/src/tools/org/h2/dev/hash/PerfectHash.java
+53
-1
没有找到文件。
h2/src/tools/org/h2/dev/hash/PerfectHash.java
浏览文件 @
4300469b
...
@@ -36,15 +36,37 @@ import java.util.zip.Inflater;
...
@@ -36,15 +36,37 @@ import java.util.zip.Inflater;
*/
*/
public
class
PerfectHash
{
public
class
PerfectHash
{
/**
* The maximum size of a bucket.
*/
private
static
final
int
MAX_SIZE
=
16
;
private
static
final
int
MAX_SIZE
=
16
;
/**
* The maximum number of hash functions to test.
*/
private
static
final
int
OFFSETS
=
16
;
private
static
final
int
OFFSETS
=
16
;
/**
* The maximum number of buckets to split the set into.
*/
private
static
final
int
MAX_SPLIT
=
32
;
private
static
final
int
MAX_SPLIT
=
32
;
/**
/**
* The description of the hash function.
* The description of the hash function. Used for calculating the hash of a
* key.
*/
*/
private
final
byte
[]
data
;
private
final
byte
[]
data
;
/**
* The offset of the result of the hash function at the given offset within
* the data array. Used for calculating the hash of a key.
*/
private
final
int
[]
plus
;
private
final
int
[]
plus
;
/**
* The position of the next bucket in the data array (in case this bucket
* needs to be skipped). Used for calculating the hash of a key.
*/
private
final
int
[]
next
;
private
final
int
[]
next
;
/**
/**
...
@@ -127,6 +149,11 @@ public class PerfectHash {
...
@@ -127,6 +149,11 @@ public class PerfectHash {
if
(
size
<
MAX_SIZE
)
{
if
(
size
<
MAX_SIZE
)
{
int
max
=
minimal
?
size
:
Math
.
min
(
MAX_SIZE
-
1
,
size
*
2
);
int
max
=
minimal
?
size
:
Math
.
min
(
MAX_SIZE
-
1
,
size
*
2
);
for
(
int
s
=
size
;
s
<=
max
;
s
++)
{
for
(
int
s
=
size
;
s
<=
max
;
s
++)
{
// Try a few hash functions ("offset" is basically the hash
// function index). We could try less hash functions, and
// instead use a larger size and remember the position of the
// hole (specially for the minimal perfect case), but that's
// more complicated.
nextOffset:
nextOffset:
for
(
int
offset
=
0
;
offset
<
OFFSETS
;
offset
++)
{
for
(
int
offset
=
0
;
offset
<
OFFSETS
;
offset
++)
{
int
bits
=
0
;
int
bits
=
0
;
...
@@ -142,6 +169,9 @@ public class PerfectHash {
...
@@ -142,6 +169,9 @@ public class PerfectHash {
}
}
}
}
}
}
// Split the set into multiple smaller sets. We could try to split more
// evenly by trying out multiple hash functions, but that's more
// complicated.
int
split
;
int
split
;
if
(
minimal
)
{
if
(
minimal
)
{
split
=
size
>
150
?
size
/
83
:
(
size
+
3
)
/
4
;
split
=
size
>
150
?
size
/
83
:
(
size
+
3
)
/
4
;
...
@@ -162,6 +192,16 @@ public class PerfectHash {
...
@@ -162,6 +192,16 @@ public class PerfectHash {
}
}
}
}
/**
* Calculate the hash of a key. The result depends on the key, the recursion
* level, and the offset.
*
* @param x the key
* @param level the recursion level
* @param offset the index of the hash function
* @param size the size of the bucket
* @return the hash (a value between 0, including, and the size, excluding)
*/
private
static
int
hash
(
int
x
,
int
level
,
int
offset
,
int
size
)
{
private
static
int
hash
(
int
x
,
int
level
,
int
offset
,
int
size
)
{
x
+=
level
*
OFFSETS
+
offset
;
x
+=
level
*
OFFSETS
+
offset
;
x
=
((
x
>>>
16
)
^
x
)
*
0x45d9f3b
;
x
=
((
x
>>>
16
)
^
x
)
*
0x45d9f3b
;
...
@@ -170,6 +210,12 @@ public class PerfectHash {
...
@@ -170,6 +210,12 @@ public class PerfectHash {
return
Math
.
abs
(
x
%
size
);
return
Math
.
abs
(
x
%
size
);
}
}
/**
* Compress the hash description using a Huffman coding.
*
* @param d the data
* @return the compressed data
*/
private
static
byte
[]
compress
(
byte
[]
d
)
{
private
static
byte
[]
compress
(
byte
[]
d
)
{
Deflater
deflater
=
new
Deflater
();
Deflater
deflater
=
new
Deflater
();
deflater
.
setStrategy
(
Deflater
.
HUFFMAN_ONLY
);
deflater
.
setStrategy
(
Deflater
.
HUFFMAN_ONLY
);
...
@@ -185,6 +231,12 @@ public class PerfectHash {
...
@@ -185,6 +231,12 @@ public class PerfectHash {
return
out2
.
toByteArray
();
return
out2
.
toByteArray
();
}
}
/**
* Decompress the hash description using a Huffman coding.
*
* @param d the data
* @return the decompressed data
*/
private
static
byte
[]
expand
(
byte
[]
d
)
{
private
static
byte
[]
expand
(
byte
[]
d
)
{
Inflater
inflater
=
new
Inflater
();
Inflater
inflater
=
new
Inflater
();
inflater
.
setInput
(
d
);
inflater
.
setInput
(
d
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论