Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
jobily
Questdb
提交
7ea4acb5
Q
Questdb
项目概览
jobily
/
Questdb
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Q
Questdb
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7ea4acb5
编写于
3月 28, 2019
作者:
V
Vlad Ilyushchenko
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#64: IntLong- and LongLong hash maps.
上级
a245fb52
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
422 addition
and
0 deletion
+422
-0
core/src/main/java/com/questdb/std/IntLongHashMap.java
core/src/main/java/com/questdb/std/IntLongHashMap.java
+105
-0
core/src/main/java/com/questdb/std/LongLongHashMap.java
core/src/main/java/com/questdb/std/LongLongHashMap.java
+105
-0
core/src/test/java/com/questdb/std/IntLongHashMapTest.java
core/src/test/java/com/questdb/std/IntLongHashMapTest.java
+106
-0
core/src/test/java/com/questdb/std/LongLongHashMapTest.java
core/src/test/java/com/questdb/std/LongLongHashMapTest.java
+106
-0
未找到文件。
core/src/main/java/com/questdb/std/IntLongHashMap.java
0 → 100644
浏览文件 @
7ea4acb5
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (C) 2014-2019 Appsicle
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
package
com.questdb.std
;
import
java.util.Arrays
;
public
class
IntLongHashMap
extends
AbstractIntHashSet
{
private
static
final
int
noEntryValue
=
-
1
;
private
long
[]
values
;
public
IntLongHashMap
()
{
this
(
8
);
}
public
IntLongHashMap
(
int
initialCapacity
)
{
this
(
initialCapacity
,
0.5f
);
}
private
IntLongHashMap
(
int
initialCapacity
,
double
loadFactor
)
{
super
(
initialCapacity
,
loadFactor
);
values
=
new
long
[
keys
.
length
];
clear
();
}
public
long
get
(
int
key
)
{
return
valueAt
(
keyIndex
(
key
));
}
public
void
put
(
int
key
,
long
value
)
{
putAt
(
keyIndex
(
key
),
key
,
value
);
}
public
void
putAt
(
int
index
,
int
key
,
long
value
)
{
if
(
index
<
0
)
{
Unsafe
.
arrayPut
(
values
,
-
index
-
1
,
value
);
}
else
{
Unsafe
.
arrayPut
(
keys
,
index
,
key
);
Unsafe
.
arrayPut
(
values
,
index
,
value
);
if
(--
free
==
0
)
{
rehash
();
}
}
}
public
long
valueAt
(
int
index
)
{
return
index
<
0
?
Unsafe
.
arrayGet
(
values
,
-
index
-
1
)
:
noEntryValue
;
}
@Override
protected
void
erase
(
int
index
)
{
Unsafe
.
arrayPut
(
keys
,
index
,
this
.
noEntryKeyValue
);
}
@Override
protected
void
move
(
int
from
,
int
to
)
{
Unsafe
.
arrayPut
(
keys
,
to
,
Unsafe
.
arrayGet
(
keys
,
from
));
Unsafe
.
arrayPut
(
values
,
to
,
Unsafe
.
arrayGet
(
values
,
from
));
erase
(
from
);
}
private
void
rehash
()
{
int
size
=
size
();
int
newCapacity
=
capacity
*
2
;
mask
=
newCapacity
-
1
;
free
=
capacity
=
newCapacity
;
int
arrayCapacity
=
(
int
)
(
newCapacity
/
loadFactor
);
long
[]
oldValues
=
values
;
int
[]
oldKeys
=
keys
;
this
.
keys
=
new
int
[
arrayCapacity
];
this
.
values
=
new
long
[
arrayCapacity
];
Arrays
.
fill
(
keys
,
noEntryKeyValue
);
free
-=
size
;
for
(
int
i
=
oldKeys
.
length
;
i
--
>
0
;
)
{
int
key
=
Unsafe
.
arrayGet
(
oldKeys
,
i
);
if
(
key
!=
noEntryKeyValue
)
{
final
int
index
=
keyIndex
(
key
);
Unsafe
.
arrayPut
(
keys
,
index
,
key
);
Unsafe
.
arrayPut
(
values
,
index
,
Unsafe
.
arrayGet
(
oldValues
,
i
));
}
}
}
}
core/src/main/java/com/questdb/std/LongLongHashMap.java
0 → 100644
浏览文件 @
7ea4acb5
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (C) 2014-2019 Appsicle
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
package
com.questdb.std
;
import
java.util.Arrays
;
public
class
LongLongHashMap
extends
AbstractLongHashSet
{
private
static
final
int
noEntryValue
=
-
1
;
private
long
[]
values
;
public
LongLongHashMap
()
{
this
(
8
);
}
public
LongLongHashMap
(
int
initialCapacity
)
{
this
(
initialCapacity
,
0.5f
);
}
private
LongLongHashMap
(
int
initialCapacity
,
double
loadFactor
)
{
super
(
initialCapacity
,
loadFactor
);
values
=
new
long
[
keys
.
length
];
clear
();
}
public
long
get
(
long
key
)
{
return
valueAt
(
keyIndex
(
key
));
}
public
void
put
(
long
key
,
long
value
)
{
putAt
(
keyIndex
(
key
),
key
,
value
);
}
public
void
putAt
(
int
index
,
long
key
,
long
value
)
{
if
(
index
<
0
)
{
Unsafe
.
arrayPut
(
values
,
-
index
-
1
,
value
);
}
else
{
Unsafe
.
arrayPut
(
keys
,
index
,
key
);
Unsafe
.
arrayPut
(
values
,
index
,
value
);
if
(--
free
==
0
)
{
rehash
();
}
}
}
public
long
valueAt
(
int
index
)
{
return
index
<
0
?
Unsafe
.
arrayGet
(
values
,
-
index
-
1
)
:
noEntryValue
;
}
@Override
protected
void
erase
(
int
index
)
{
Unsafe
.
arrayPut
(
keys
,
index
,
this
.
noEntryKeyValue
);
}
@Override
protected
void
move
(
int
from
,
int
to
)
{
Unsafe
.
arrayPut
(
keys
,
to
,
Unsafe
.
arrayGet
(
keys
,
from
));
Unsafe
.
arrayPut
(
values
,
to
,
Unsafe
.
arrayGet
(
values
,
from
));
erase
(
from
);
}
private
void
rehash
()
{
int
size
=
size
();
int
newCapacity
=
capacity
*
2
;
mask
=
newCapacity
-
1
;
free
=
capacity
=
newCapacity
;
int
arrayCapacity
=
(
int
)
(
newCapacity
/
loadFactor
);
long
[]
oldValues
=
values
;
long
[]
oldKeys
=
keys
;
this
.
keys
=
new
long
[
arrayCapacity
];
this
.
values
=
new
long
[
arrayCapacity
];
Arrays
.
fill
(
keys
,
noEntryKeyValue
);
free
-=
size
;
for
(
int
i
=
oldKeys
.
length
;
i
--
>
0
;
)
{
long
key
=
Unsafe
.
arrayGet
(
oldKeys
,
i
);
if
(
key
!=
noEntryKeyValue
)
{
final
int
index
=
keyIndex
(
key
);
Unsafe
.
arrayPut
(
keys
,
index
,
key
);
Unsafe
.
arrayPut
(
values
,
index
,
Unsafe
.
arrayGet
(
oldValues
,
i
));
}
}
}
}
\ No newline at end of file
core/src/test/java/com/questdb/std/IntLongHashMapTest.java
0 → 100644
浏览文件 @
7ea4acb5
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (C) 2014-2019 Appsicle
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
package
com.questdb.std
;
import
org.junit.Assert
;
import
org.junit.Test
;
public
class
IntLongHashMapTest
{
@Test
public
void
testAll
()
{
Rnd
rnd
=
new
Rnd
();
// populate map
IntLongHashMap
map
=
new
IntLongHashMap
();
final
int
N
=
1000
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
long
value
=
i
+
1
;
map
.
put
(
i
,
value
);
}
Assert
.
assertEquals
(
N
,
map
.
size
());
rnd
.
reset
();
// assert that map contains the values we just added
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
Assert
.
assertFalse
(
map
.
excludes
(
i
));
Assert
.
assertEquals
(
i
+
1
,
map
.
get
(
i
));
}
Rnd
rnd2
=
new
Rnd
();
rnd
.
reset
();
// remove some keys and assert that the size() complies
int
removed
=
0
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
if
(
rnd2
.
nextPositiveInt
()
%
16
==
0
)
{
Assert
.
assertTrue
(
map
.
remove
(
i
)
>
-
1
);
removed
++;
Assert
.
assertEquals
(
N
-
removed
,
map
.
size
());
}
}
// if we didn't remove anything test has no value
Assert
.
assertTrue
(
removed
>
0
);
rnd2
.
reset
();
rnd
.
reset
();
Rnd
rnd3
=
new
Rnd
();
// assert that keys we didn't remove are still there and
// keys we removed are not
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
int
value
=
rnd
.
nextInt
();
if
(
rnd2
.
nextPositiveInt
()
%
16
==
0
)
{
Assert
.
assertTrue
(
map
.
excludes
(
i
));
}
else
{
Assert
.
assertFalse
(
map
.
excludes
(
i
));
int
index
=
map
.
keyIndex
(
i
);
Assert
.
assertEquals
(
i
+
1
,
map
.
valueAt
(
index
));
// update value
map
.
putAt
(
index
,
value
,
rnd3
.
nextLong
());
}
}
// assert that update is visible correctly
rnd3
.
reset
();
rnd2
.
reset
();
rnd
.
reset
();
// assert that keys we didn't remove are still there and
// keys we removed are not
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
if
(
rnd2
.
nextPositiveInt
()
%
16
==
0
)
{
Assert
.
assertTrue
(
map
.
excludes
(
i
));
}
else
{
Assert
.
assertFalse
(
map
.
excludes
(
i
));
Assert
.
assertEquals
(
rnd3
.
nextLong
(),
map
.
get
(
i
));
}
}
}
}
core/src/test/java/com/questdb/std/LongLongHashMapTest.java
0 → 100644
浏览文件 @
7ea4acb5
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (C) 2014-2019 Appsicle
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
package
com.questdb.std
;
import
org.junit.Assert
;
import
org.junit.Test
;
public
class
LongLongHashMapTest
{
@Test
public
void
testAll
()
{
Rnd
rnd
=
new
Rnd
();
// populate map
LongLongHashMap
map
=
new
LongLongHashMap
();
final
int
N
=
1000
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
long
value
=
i
+
1
;
map
.
put
(
i
,
value
);
}
Assert
.
assertEquals
(
N
,
map
.
size
());
rnd
.
reset
();
// assert that map contains the values we just added
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
Assert
.
assertFalse
(
map
.
excludes
(
i
));
Assert
.
assertEquals
(
i
+
1
,
map
.
get
(
i
));
}
Rnd
rnd2
=
new
Rnd
();
rnd
.
reset
();
// remove some keys and assert that the size() complies
int
removed
=
0
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
if
(
rnd2
.
nextPositiveInt
()
%
16
==
0
)
{
Assert
.
assertTrue
(
map
.
remove
(
i
)
>
-
1
);
removed
++;
Assert
.
assertEquals
(
N
-
removed
,
map
.
size
());
}
}
// if we didn't remove anything test has no value
Assert
.
assertTrue
(
removed
>
0
);
rnd2
.
reset
();
rnd
.
reset
();
Rnd
rnd3
=
new
Rnd
();
// assert that keys we didn't remove are still there and
// keys we removed are not
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
int
value
=
rnd
.
nextInt
();
if
(
rnd2
.
nextPositiveInt
()
%
16
==
0
)
{
Assert
.
assertTrue
(
map
.
excludes
(
i
));
}
else
{
Assert
.
assertFalse
(
map
.
excludes
(
i
));
int
index
=
map
.
keyIndex
(
i
);
Assert
.
assertEquals
(
i
+
1
,
map
.
valueAt
(
index
));
// update value
map
.
putAt
(
index
,
value
,
rnd3
.
nextLong
());
}
}
// assert that update is visible correctly
rnd3
.
reset
();
rnd2
.
reset
();
rnd
.
reset
();
// assert that keys we didn't remove are still there and
// keys we removed are not
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
if
(
rnd2
.
nextPositiveInt
()
%
16
==
0
)
{
Assert
.
assertTrue
(
map
.
excludes
(
i
));
}
else
{
Assert
.
assertFalse
(
map
.
excludes
(
i
));
Assert
.
assertEquals
(
rnd3
.
nextLong
(),
map
.
get
(
i
));
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录