Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
8b6752db
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
8b6752db
编写于
10月 30, 2009
作者:
A
alanb
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
6896573: Arrays.sort(long[]) fails with StackOverflowError
Reviewed-by: jjb Contributed-by: vladimir.yaroslavskiy@sun.com
上级
953dcdab
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
501 addition
and
7 deletion
+501
-7
src/share/classes/java/util/DualPivotQuicksort.java
src/share/classes/java/util/DualPivotQuicksort.java
+8
-7
test/java/util/Arrays/Sorting.java
test/java/util/Arrays/Sorting.java
+493
-0
未找到文件。
src/share/classes/java/util/DualPivotQuicksort.java
浏览文件 @
8b6752db
...
...
@@ -36,7 +36,7 @@ package java.util;
* @author Jon Bentley
* @author Josh Bloch
*
* @version 2009.10.2
2 m765.827.v4
* @version 2009.10.2
9 m765.827.v5
*/
final
class
DualPivotQuicksort
{
...
...
@@ -473,9 +473,10 @@ final class DualPivotQuicksort {
a
[
great
--]
=
pivot2
;
}
}
}
else
{
// Use Dual-Pivot Quicksort on large arrays
dualPivotQuicksort
(
a
,
left
,
right
);
}
// Sort center part recursively, excluding known pivot values
sort
(
a
,
less
,
great
);
}
/** The number of distinct short values */
...
...
@@ -507,7 +508,7 @@ final class DualPivotQuicksort {
for
(
int
i
=
left
;
i
<=
right
;
i
++)
{
count
[
a
[
i
]
-
Short
.
MIN_VALUE
]++;
}
for
(
int
i
=
0
,
k
=
left
;
i
<
count
.
length
&&
k
<
right
;
i
++)
{
for
(
int
i
=
0
,
k
=
left
;
i
<
count
.
length
&&
k
<
=
right
;
i
++)
{
short
value
=
(
short
)
(
i
+
Short
.
MIN_VALUE
);
for
(
int
s
=
count
[
i
];
s
>
0
;
s
--)
{
...
...
@@ -723,13 +724,13 @@ final class DualPivotQuicksort {
a
[
j
+
1
]
=
ak
;
}
}
else
if
(
right
-
left
+
1
>
COUNTING_SORT_THRESHOLD_FOR_BYTE
)
{
// Use counting sort on
lar
ge arrays
// Use counting sort on
hu
ge arrays
int
[]
count
=
new
int
[
NUM_BYTE_VALUES
];
for
(
int
i
=
left
;
i
<=
right
;
i
++)
{
count
[
a
[
i
]
-
Byte
.
MIN_VALUE
]++;
}
for
(
int
i
=
0
,
k
=
left
;
i
<
count
.
length
&&
k
<
right
;
i
++)
{
for
(
int
i
=
0
,
k
=
left
;
i
<
count
.
length
&&
k
<
=
right
;
i
++)
{
byte
value
=
(
byte
)
(
i
+
Byte
.
MIN_VALUE
);
for
(
int
s
=
count
[
i
];
s
>
0
;
s
--)
{
...
...
@@ -951,7 +952,7 @@ final class DualPivotQuicksort {
for
(
int
i
=
left
;
i
<=
right
;
i
++)
{
count
[
a
[
i
]]++;
}
for
(
int
i
=
0
,
k
=
left
;
i
<
count
.
length
&&
k
<
right
;
i
++)
{
for
(
int
i
=
0
,
k
=
left
;
i
<
count
.
length
&&
k
<
=
right
;
i
++)
{
for
(
int
s
=
count
[
i
];
s
>
0
;
s
--)
{
a
[
k
++]
=
(
char
)
i
;
}
...
...
test/java/util/Arrays/Sorting.java
0 → 100644
浏览文件 @
8b6752db
/*
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 6880672 6896573
* @summary Exercise Arrays.sort
* @build Sorting
* @run main Sorting -shortrun
* @author Vladimir Yaroslavskiy, Josh Bloch, Jon Bentley
*/
import
java.util.Arrays
;
import
java.util.Random
;
import
java.io.PrintStream
;
public
class
Sorting
{
static
final
PrintStream
out
=
System
.
out
;
static
final
PrintStream
err
=
System
.
err
;
// array lengths used in a long run (default)
static
final
int
[]
LONG_RUN
=
{
0
,
1
,
2
,
3
,
5
,
8
,
13
,
21
,
34
,
55
,
100
,
1000
,
10000
,
100000
,
1000000
};
// array lengths used in a short run
static
final
int
[]
SHORT_RUN
=
{
0
,
1
,
2
,
3
,
21
,
55
,
1000
,
10000
,
500000
};
public
static
void
main
(
String
[]
args
)
{
boolean
shortRun
=
false
;
if
(
args
.
length
>
0
&&
args
[
0
].
equals
(
"-shortrun"
))
shortRun
=
true
;
long
start
=
System
.
nanoTime
();
testAndCheck
((
shortRun
)
?
SHORT_RUN
:
LONG_RUN
);
long
end
=
System
.
nanoTime
();
out
.
println
();
out
.
format
(
"PASS in %ds%n"
,
Math
.
round
((
end
-
start
)
/
1
e9
));
}
static
void
testAndCheck
(
int
[]
lengths
)
{
for
(
int
len
:
lengths
)
{
out
.
println
();
ArrayBuilder
.
reset
();
int
[]
golden
=
new
int
[
len
];
for
(
int
m
=
1
;
m
<
2
*
len
;
m
*=
2
)
{
for
(
ArrayBuilder
builder
:
ArrayBuilder
.
values
())
{
builder
.
build
(
golden
,
m
);
int
[]
test
=
golden
.
clone
();
for
(
Converter
converter
:
Converter
.
values
())
{
out
.
println
(
"Test: "
+
converter
+
" "
+
builder
+
"len = "
+
len
+
", m = "
+
m
);
Object
convertedGolden
=
converter
.
convert
(
golden
);
Object
convertedTest
=
converter
.
convert
(
test
);
sort
(
convertedTest
);
checkWithCheckSum
(
convertedTest
,
convertedGolden
);
}
}
}
}
}
static
enum
Converter
{
INT
{
Object
convert
(
int
[]
a
)
{
return
a
;
}
},
LONG
{
Object
convert
(
int
[]
a
)
{
long
[]
b
=
new
long
[
a
.
length
];
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
b
[
i
]
=
(
int
)
a
[
i
];
}
return
b
;
}
},
BYTE
{
Object
convert
(
int
[]
a
)
{
byte
[]
b
=
new
byte
[
a
.
length
];
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
b
[
i
]
=
(
byte
)
a
[
i
];
}
return
b
;
}
},
SHORT
{
Object
convert
(
int
[]
a
)
{
short
[]
b
=
new
short
[
a
.
length
];
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
b
[
i
]
=
(
short
)
a
[
i
];
}
return
b
;
}
},
CHAR
{
Object
convert
(
int
[]
a
)
{
char
[]
b
=
new
char
[
a
.
length
];
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
b
[
i
]
=
(
char
)
a
[
i
];
}
return
b
;
}
},
FLOAT
{
Object
convert
(
int
[]
a
)
{
float
[]
b
=
new
float
[
a
.
length
];
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
b
[
i
]
=
(
float
)
a
[
i
];
}
return
b
;
}
},
DOUBLE
{
Object
convert
(
int
[]
a
)
{
double
[]
b
=
new
double
[
a
.
length
];
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
b
[
i
]
=
(
double
)
a
[
i
];
}
return
b
;
}
};
abstract
Object
convert
(
int
[]
a
);
@Override
public
String
toString
()
{
String
name
=
name
();
for
(
int
i
=
name
.
length
();
i
<
9
;
i
++)
{
name
+=
" "
;
}
return
name
;
}
}
static
enum
ArrayBuilder
{
RANDOM
{
void
build
(
int
[]
a
,
int
m
)
{
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
a
[
i
]
=
ourRandom
.
nextInt
();
}
}
},
ASCENDING
{
void
build
(
int
[]
a
,
int
m
)
{
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
a
[
i
]
=
m
+
i
;
}
}
},
DESCENDING
{
void
build
(
int
[]
a
,
int
m
)
{
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
a
[
i
]
=
a
.
length
-
m
-
i
;
}
}
},
ALL_EQUAL
{
void
build
(
int
[]
a
,
int
m
)
{
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
a
[
i
]
=
m
;
}
}
},
SAW
{
void
build
(
int
[]
a
,
int
m
)
{
int
incCount
=
1
;
int
decCount
=
a
.
length
;
int
i
=
0
;
int
period
=
m
;
m
--;
while
(
true
)
{
for
(
int
k
=
1
;
k
<=
period
;
k
++)
{
if
(
i
>=
a
.
length
)
{
return
;
}
a
[
i
++]
=
incCount
++;
}
period
+=
m
;
for
(
int
k
=
1
;
k
<=
period
;
k
++)
{
if
(
i
>=
a
.
length
)
{
return
;
}
a
[
i
++]
=
decCount
--;
}
period
+=
m
;
}
}
},
REPEATED
{
void
build
(
int
[]
a
,
int
m
)
{
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
a
[
i
]
=
i
%
m
;
}
}
},
DUPLICATED
{
void
build
(
int
[]
a
,
int
m
)
{
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
a
[
i
]
=
ourRandom
.
nextInt
(
m
);
}
}
},
ORGAN_PIPES
{
void
build
(
int
[]
a
,
int
m
)
{
int
middle
=
a
.
length
/
(
m
+
1
);
for
(
int
i
=
0
;
i
<
middle
;
i
++)
{
a
[
i
]
=
i
;
}
for
(
int
i
=
middle
;
i
<
a
.
length
;
i
++)
{
a
[
i
]
=
a
.
length
-
i
-
1
;
}
}
},
STAGGER
{
void
build
(
int
[]
a
,
int
m
)
{
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
a
[
i
]
=
(
i
*
m
+
i
)
%
a
.
length
;
}
}
},
PLATEAU
{
void
build
(
int
[]
a
,
int
m
)
{
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
a
[
i
]
=
Math
.
min
(
i
,
m
);
}
}
},
SHUFFLE
{
void
build
(
int
[]
a
,
int
m
)
{
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
a
[
i
]
=
ourRandom
.
nextBoolean
()
?
(
ourFirst
+=
2
)
:
(
ourSecond
+=
2
);
}
}
};
abstract
void
build
(
int
[]
a
,
int
m
);
static
void
reset
()
{
ourRandom
=
new
Random
(
666
);
ourFirst
=
0
;
ourSecond
=
0
;
}
@Override
public
String
toString
()
{
String
name
=
name
();
for
(
int
i
=
name
.
length
();
i
<
12
;
i
++)
{
name
+=
" "
;
}
return
name
;
}
private
static
int
ourFirst
;
private
static
int
ourSecond
;
private
static
Random
ourRandom
=
new
Random
(
666
);
}
static
void
checkWithCheckSum
(
Object
test
,
Object
golden
)
{
checkSorted
(
test
);
checkCheckSum
(
test
,
golden
);
}
static
void
failed
(
String
message
)
{
err
.
format
(
"***FAILED: %s%%n"
,
message
);
throw
new
RuntimeException
(
"Test failed - see log file for details"
);
}
static
void
failed
(
int
index
,
String
value1
,
String
value2
)
{
failed
(
"Array is not sorted at "
+
index
+
"-th position: "
+
value1
+
" and "
+
value2
);
}
static
void
checkSorted
(
Object
object
)
{
if
(
object
instanceof
int
[])
{
checkSorted
((
int
[])
object
);
}
else
if
(
object
instanceof
long
[])
{
checkSorted
((
long
[])
object
);
}
else
if
(
object
instanceof
short
[])
{
checkSorted
((
short
[])
object
);
}
else
if
(
object
instanceof
byte
[])
{
checkSorted
((
byte
[])
object
);
}
else
if
(
object
instanceof
char
[])
{
checkSorted
((
char
[])
object
);
}
else
if
(
object
instanceof
float
[])
{
checkSorted
((
float
[])
object
);
}
else
if
(
object
instanceof
double
[])
{
checkSorted
((
double
[])
object
);
}
else
{
failed
(
"Unknow type of array: "
+
object
+
" of class "
+
object
.
getClass
().
getName
());
}
}
static
void
checkSorted
(
int
[]
a
)
{
for
(
int
i
=
0
;
i
<
a
.
length
-
1
;
i
++)
{
if
(
a
[
i
]
>
a
[
i
+
1
])
{
failed
(
i
,
""
+
a
[
i
],
""
+
a
[
i
+
1
]);
}
}
}
static
void
checkSorted
(
long
[]
a
)
{
for
(
int
i
=
0
;
i
<
a
.
length
-
1
;
i
++)
{
if
(
a
[
i
]
>
a
[
i
+
1
])
{
failed
(
i
,
""
+
a
[
i
],
""
+
a
[
i
+
1
]);
}
}
}
static
void
checkSorted
(
short
[]
a
)
{
for
(
int
i
=
0
;
i
<
a
.
length
-
1
;
i
++)
{
if
(
a
[
i
]
>
a
[
i
+
1
])
{
failed
(
i
,
""
+
a
[
i
],
""
+
a
[
i
+
1
]);
}
}
}
static
void
checkSorted
(
byte
[]
a
)
{
for
(
int
i
=
0
;
i
<
a
.
length
-
1
;
i
++)
{
if
(
a
[
i
]
>
a
[
i
+
1
])
{
failed
(
i
,
""
+
a
[
i
],
""
+
a
[
i
+
1
]);
}
}
}
static
void
checkSorted
(
char
[]
a
)
{
for
(
int
i
=
0
;
i
<
a
.
length
-
1
;
i
++)
{
if
(
a
[
i
]
>
a
[
i
+
1
])
{
failed
(
i
,
""
+
a
[
i
],
""
+
a
[
i
+
1
]);
}
}
}
static
void
checkSorted
(
float
[]
a
)
{
for
(
int
i
=
0
;
i
<
a
.
length
-
1
;
i
++)
{
if
(
a
[
i
]
>
a
[
i
+
1
])
{
failed
(
i
,
""
+
a
[
i
],
""
+
a
[
i
+
1
]);
}
}
}
static
void
checkSorted
(
double
[]
a
)
{
for
(
int
i
=
0
;
i
<
a
.
length
-
1
;
i
++)
{
if
(
a
[
i
]
>
a
[
i
+
1
])
{
failed
(
i
,
""
+
a
[
i
],
""
+
a
[
i
+
1
]);
}
}
}
static
void
checkCheckSum
(
Object
test
,
Object
golden
)
{
if
(
checkSum
(
test
)
!=
checkSum
(
golden
))
{
failed
(
"Original and sorted arrays seems not identical"
);
}
}
static
int
checkSum
(
Object
object
)
{
if
(
object
instanceof
int
[])
{
return
checkSum
((
int
[])
object
);
}
else
if
(
object
instanceof
long
[])
{
return
checkSum
((
long
[])
object
);
}
else
if
(
object
instanceof
short
[])
{
return
checkSum
((
short
[])
object
);
}
else
if
(
object
instanceof
byte
[])
{
return
checkSum
((
byte
[])
object
);
}
else
if
(
object
instanceof
char
[])
{
return
checkSum
((
char
[])
object
);
}
else
if
(
object
instanceof
float
[])
{
return
checkSum
((
float
[])
object
);
}
else
if
(
object
instanceof
double
[])
{
return
checkSum
((
double
[])
object
);
}
else
{
failed
(
"Unknow type of array: "
+
object
+
" of class "
+
object
.
getClass
().
getName
());
return
-
1
;
}
}
static
int
checkSum
(
int
[]
a
)
{
int
checkSum
=
0
;
for
(
int
e
:
a
)
{
checkSum
^=
e
;
// xor
}
return
checkSum
;
}
static
int
checkSum
(
long
[]
a
)
{
long
checkSum
=
0
;
for
(
long
e
:
a
)
{
checkSum
^=
e
;
// xor
}
return
(
int
)
checkSum
;
}
static
int
checkSum
(
short
[]
a
)
{
short
checkSum
=
0
;
for
(
short
e
:
a
)
{
checkSum
^=
e
;
// xor
}
return
(
int
)
checkSum
;
}
static
int
checkSum
(
byte
[]
a
)
{
byte
checkSum
=
0
;
for
(
byte
e
:
a
)
{
checkSum
^=
e
;
// xor
}
return
(
int
)
checkSum
;
}
static
int
checkSum
(
char
[]
a
)
{
char
checkSum
=
0
;
for
(
char
e
:
a
)
{
checkSum
^=
e
;
// xor
}
return
(
int
)
checkSum
;
}
static
int
checkSum
(
float
[]
a
)
{
int
checkSum
=
0
;
for
(
float
e
:
a
)
{
checkSum
^=
(
int
)
e
;
// xor
}
return
checkSum
;
}
static
int
checkSum
(
double
[]
a
)
{
int
checkSum
=
0
;
for
(
double
e
:
a
)
{
checkSum
^=
(
int
)
e
;
// xor
}
return
checkSum
;
}
static
void
sort
(
Object
object
)
{
if
(
object
instanceof
int
[])
{
Arrays
.
sort
((
int
[])
object
);
}
else
if
(
object
instanceof
long
[])
{
Arrays
.
sort
((
long
[])
object
);
}
else
if
(
object
instanceof
short
[])
{
Arrays
.
sort
((
short
[])
object
);
}
else
if
(
object
instanceof
byte
[])
{
Arrays
.
sort
((
byte
[])
object
);
}
else
if
(
object
instanceof
char
[])
{
Arrays
.
sort
((
char
[])
object
);
}
else
if
(
object
instanceof
float
[])
{
Arrays
.
sort
((
float
[])
object
);
}
else
if
(
object
instanceof
double
[])
{
Arrays
.
sort
((
double
[])
object
);
}
else
{
failed
(
"Unknow type of array: "
+
object
+
" of class "
+
object
.
getClass
().
getName
());
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录