Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
63857d77
提交
63857d77
authored
1月 18, 2013
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Partial sort
上级
9a2b7e25
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
48 行增加
和
52 行删除
+48
-52
SortOrder.java
h2/src/main/org/h2/result/SortOrder.java
+3
-4
Utils.java
h2/src/main/org/h2/util/Utils.java
+39
-32
TestUtils.java
h2/src/test/org/h2/test/unit/TestUtils.java
+6
-16
没有找到文件。
h2/src/main/org/h2/result/SortOrder.java
浏览文件 @
63857d77
...
...
@@ -163,16 +163,15 @@ public class SortOrder implements Comparator<Value[]> {
* @param limit the limit
*/
public
void
sort
(
ArrayList
<
Value
[]>
rows
,
int
offset
,
int
limit
)
{
if
(
rows
.
isEmpty
())
if
(
rows
.
isEmpty
())
{
return
;
}
if
(
limit
==
1
&&
offset
==
0
)
{
rows
.
set
(
0
,
Collections
.
min
(
rows
,
this
));
return
;
}
Value
[][]
arr
=
rows
.
toArray
(
new
Value
[
rows
.
size
()][]);
Utils
.
topNSorted
(
arr
,
offset
,
limit
,
this
);
Utils
.
sortTopN
(
arr
,
offset
,
limit
,
this
);
for
(
int
i
=
0
,
end
=
Math
.
min
(
offset
+
limit
,
arr
.
length
);
i
<
end
;
i
++)
{
rows
.
set
(
i
,
arr
[
i
]);
}
...
...
h2/src/main/org/h2/util/Utils.java
浏览文件 @
63857d77
...
...
@@ -436,60 +436,67 @@ public class Utils {
}
/**
*
Will find top limit values in array using given comparator and place them on positions like if
* full array sort
occur. Does not guarantee any sort order of these top elements
.
*
Find the top limit values using given comparator and place them as in a
* full array sort
, in descending order
.
*
* @param array the array.
* @param offset the offset.
* @param limit the limit.
* @param cmp the comparator.
* @param c
o
mp the comparator.
*/
public
static
<
X
>
void
topN
(
X
[]
array
,
int
offset
,
int
limit
,
Comparator
<?
super
X
>
cmp
)
{
partialQuickSort
(
array
,
0
,
array
.
length
-
1
,
cmp
,
offset
,
offset
+
limit
-
1
);
public
static
<
X
>
void
sortTopN
(
X
[]
array
,
int
offset
,
int
limit
,
Comparator
<?
super
X
>
comp
)
{
partitionTopN
(
array
,
offset
,
limit
,
comp
);
Arrays
.
sort
(
array
,
offset
,
Math
.
min
(
offset
+
limit
,
array
.
length
),
comp
);
}
/**
*
The same as {@link #topN(Object[], int, int, Comparator)}} but guarantees sort order of
* f
ound top element
s.
*
Find the top limit values using given comparator and place them as in a
* f
ull array sort. This method does not sort the top elements themselve
s.
*
* @param array the array
.
* @param offset the offset
.
* @param limit the limit
.
* @param c
mp the comparator.
* @param array the array
* @param offset the offset
* @param limit the limit
* @param c
omp the comparator
*/
p
ublic
static
<
X
>
void
topNSorted
(
X
[]
array
,
int
offset
,
int
limit
,
Comparator
<?
super
X
>
cmp
)
{
topN
(
array
,
offset
,
limit
,
cmp
);
Arrays
.
sort
(
array
,
offset
,
Math
.
min
(
offset
+
limit
,
array
.
length
),
cmp
);
p
rivate
static
<
X
>
void
partitionTopN
(
X
[]
array
,
int
offset
,
int
limit
,
Comparator
<?
super
X
>
comp
)
{
partialQuickSort
(
array
,
0
,
array
.
length
-
1
,
comp
,
offset
,
offset
+
limit
-
1
);
}
private
static
<
X
>
void
partialQuickSort
(
X
[]
array
,
int
low
,
int
high
,
Comparator
<?
super
X
>
cmp
,
int
start
,
int
end
)
{
if
(
low
>
end
||
high
<
start
||
(
low
>
start
&&
high
<
end
))
private
static
<
X
>
void
partialQuickSort
(
X
[]
array
,
int
low
,
int
high
,
Comparator
<?
super
X
>
comp
,
int
start
,
int
end
)
{
if
(
low
>
end
||
high
<
start
||
(
low
>
start
&&
high
<
end
))
{
return
;
}
int
i
=
low
,
j
=
high
;
X
pivot
=
array
[
low
+
(
high
-
low
)
/
2
];
// use a random pivot to protect against
// the worst case order
int
p
=
low
+
MathUtils
.
randomInt
(
high
-
low
);
X
pivot
=
array
[
p
];
int
m
=
(
low
+
high
)
>>>
1
;
X
temp
=
array
[
m
];
array
[
m
]
=
pivot
;
array
[
p
]
=
temp
;
while
(
i
<=
j
)
{
while
(
cmp
.
compare
(
array
[
i
],
pivot
)
<
0
)
{
while
(
c
o
mp
.
compare
(
array
[
i
],
pivot
)
<
0
)
{
i
++;
}
while
(
cmp
.
compare
(
array
[
j
],
pivot
)
>
0
)
{
while
(
c
o
mp
.
compare
(
array
[
j
],
pivot
)
>
0
)
{
j
--;
}
if
(
i
<=
j
)
{
X
x
=
array
[
i
];
array
[
i
]
=
array
[
j
];
array
[
j
]
=
x
;
i
++;
j
--;
temp
=
array
[
i
];
array
[
i
++]
=
array
[
j
];
array
[
j
--]
=
temp
;
}
}
if
(
low
<
j
)
partialQuickSort
(
array
,
low
,
j
,
cmp
,
start
,
end
);
if
(
i
<
high
)
partialQuickSort
(
array
,
i
,
high
,
cmp
,
start
,
end
);
if
(
low
<
j
)
{
partialQuickSort
(
array
,
low
,
j
,
comp
,
start
,
end
);
}
if
(
i
<
high
)
{
partialQuickSort
(
array
,
i
,
high
,
comp
,
start
,
end
);
}
}
/**
...
...
h2/src/test/org/h2/test/unit/TestUtils.java
浏览文件 @
63857d77
...
...
@@ -42,7 +42,7 @@ public class TestUtils extends TestBase {
testGetNonPrimitiveClass
();
testGetNonPrimitiveClass
();
testReflectionUtils
();
testTopN
();
test
Sort
TopN
();
}
private
void
testWriteReadLong
()
{
...
...
@@ -62,37 +62,27 @@ public class TestUtils extends TestBase {
}
}
private
void
testTopN
()
{
private
void
test
Sort
TopN
()
{
Random
rnd
=
new
Random
();
Comparator
<
Integer
>
cmp
=
new
Comparator
<
Integer
>()
{
Comparator
<
Integer
>
comp
=
new
Comparator
<
Integer
>()
{
@Override
public
int
compare
(
Integer
o1
,
Integer
o2
)
{
return
o1
.
compareTo
(
o2
);
}
};
for
(
int
z
=
0
;
z
<
10000
;
z
++)
{
Integer
[]
arr
=
new
Integer
[
1
+
rnd
.
nextInt
(
500
)];
for
(
int
i
=
0
;
i
<
arr
.
length
;
i
++)
{
arr
[
i
]
=
rnd
.
nextInt
(
50
);
}
Integer
[]
arr2
=
Arrays
.
copyOf
(
arr
,
arr
.
length
);
int
offset
=
rnd
.
nextInt
(
arr
.
length
);
int
limit
=
rnd
.
nextInt
(
arr
.
length
);
Utils
.
topNSorted
(
arr
,
offset
,
limit
,
cmp
);
Arrays
.
sort
(
arr2
,
cmp
);
Utils
.
sortTopN
(
arr
,
offset
,
limit
,
comp
);
Arrays
.
sort
(
arr2
,
comp
);
for
(
int
i
=
offset
,
end
=
Math
.
min
(
offset
+
limit
,
arr
.
length
);
i
<
end
;
i
++)
{
if
(!
arr
[
i
].
equals
(
arr2
[
i
]))
{
System
.
out
.
println
(
offset
+
" "
+
end
);
System
.
out
.
println
(
Arrays
.
toString
(
arr
));
System
.
out
.
println
(
Arrays
.
toString
(
arr2
));
fail
();
fail
(
offset
+
" "
+
end
+
"\n"
+
Arrays
.
toString
(
arr
)
+
"\n"
+
Arrays
.
toString
(
arr2
));
}
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论