# HG changeset patch
# User lana
# Date 1288115855 25200
# Node ID 85bb633e7d6f814b18f3d5cd5af190de1a1acf82
# Parent c75c404c3c2c5b675d291be5164bbf98164325a4# Parent 4acdf8b6df2fb62ef0b639e6ca9307b808477f43
Merge
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/make/java/java/FILES_java.gmk
--- a/jdk/make/java/java/FILES_java.gmk Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/make/java/java/FILES_java.gmk Tue Oct 26 10:57:35 2010 -0700
@@ -465,14 +465,11 @@
java/security/ProtectionDomain.java \
java/net/URLClassLoader.java \
java/net/URLConnection.java \
+ sun/misc/BootClassLoaderHook.java \
sun/misc/Launcher.java \
sun/misc/MetaIndex.java \
sun/misc/URLClassPath.java \
sun/misc/Version.java \
- sun/net/www/protocol/jar/Handler.java \
- sun/net/www/protocol/jar/JarURLConnection.java \
- sun/net/www/protocol/file/Handler.java \
- sun/net/www/protocol/file/FileURLConnection.java \
sun/misc/FileURLMapper.java \
sun/misc/MessageUtils.java \
sun/misc/GC.java \
@@ -482,6 +479,10 @@
sun/misc/JavaIOFileDescriptorAccess.java \
sun/misc/JavaNioAccess.java \
sun/misc/Perf.java \
- sun/misc/PerfCounter.java
+ sun/misc/PerfCounter.java \
+ sun/net/www/protocol/jar/Handler.java \
+ sun/net/www/protocol/jar/JarURLConnection.java \
+ sun/net/www/protocol/file/Handler.java \
+ sun/net/www/protocol/file/FileURLConnection.java
FILES_java = $(JAVA_JAVA_java)
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/make/sun/image/generic/Makefile
--- a/jdk/make/sun/image/generic/Makefile Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/make/sun/image/generic/Makefile Tue Oct 26 10:57:35 2010 -0700
@@ -69,5 +69,8 @@
-I$(PLATFORM_SRC)/native/$(PKGDIR)/medialib
OTHER_CFLAGS += -D__USE_J2D_NAMES -D__MEDIALIB_OLD_NAMES
-OTHER_LDLIBS = $(LIBM) -ldl
+ifneq ($(PLATFORM), windows)
+ OTHER_LDLIBS = $(LIBM) -ldl
+endif
+
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/plugins/bmp/BMPImageReaderSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/plugins/bmp/BMPImageReaderSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/plugins/bmp/BMPImageReaderSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -45,7 +45,7 @@
private boolean registered = false;
public BMPImageReaderSpi() {
- super("Sun Microsystems, Inc.",
+ super("Oracle Corporation",
"1.0",
formatNames,
entensions,
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/plugins/bmp/BMPImageWriterSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/plugins/bmp/BMPImageWriterSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/plugins/bmp/BMPImageWriterSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -50,7 +50,7 @@
private boolean registered = false;
public BMPImageWriterSpi() {
- super("Sun Microsystems, Inc.",
+ super("Oracle Corporation",
"1.0",
formatNames,
entensions,
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/plugins/gif/GIFImageReaderSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/plugins/gif/GIFImageReaderSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/plugins/gif/GIFImageReaderSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -36,7 +36,7 @@
public class GIFImageReaderSpi extends ImageReaderSpi {
- private static final String vendorName = "Sun Microsystems, Inc.";
+ private static final String vendorName = "Oracle Corporation";
private static final String version = "1.0";
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/plugins/gif/GIFImageWriterSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/plugins/gif/GIFImageWriterSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/plugins/gif/GIFImageWriterSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -36,7 +36,7 @@
public class GIFImageWriterSpi extends ImageWriterSpi {
- private static final String vendorName = "Sun Microsystems, Inc.";
+ private static final String vendorName = "Oracle Corporation";
private static final String version = "1.0";
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/plugins/jpeg/JPEG.java
--- a/jdk/src/share/classes/com/sun/imageio/plugins/jpeg/JPEG.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/plugins/jpeg/JPEG.java Tue Oct 26 10:57:35 2010 -0700
@@ -169,7 +169,7 @@
public static final int ADOBE_YCCK = 2;
// Spi initialization stuff
- public static final String vendor = "Sun Microsystems, Inc.";
+ public static final String vendor = "Oracle Corporation";
public static final String version = "0.5";
// Names of the formats we can read or write
public static final String [] names = {"JPEG", "jpeg", "JPG", "jpg"};
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/plugins/png/PNGImageReaderSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/plugins/png/PNGImageReaderSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/plugins/png/PNGImageReaderSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -36,7 +36,7 @@
public class PNGImageReaderSpi extends ImageReaderSpi {
- private static final String vendorName = "Sun Microsystems, Inc.";
+ private static final String vendorName = "Oracle Corporation";
private static final String version = "1.0";
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/plugins/png/PNGImageWriterSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/plugins/png/PNGImageWriterSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/plugins/png/PNGImageWriterSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -38,7 +38,7 @@
public class PNGImageWriterSpi extends ImageWriterSpi {
- private static final String vendorName = "Sun Microsystems, Inc.";
+ private static final String vendorName = "Oracle Corporation";
private static final String version = "1.0";
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/plugins/wbmp/WBMPImageReaderSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/plugins/wbmp/WBMPImageReaderSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/plugins/wbmp/WBMPImageReaderSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -49,7 +49,7 @@
private boolean registered = false;
public WBMPImageReaderSpi() {
- super("Sun Microsystems, Inc.",
+ super("Oracle Corporation",
"1.0",
formatNames,
entensions,
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/plugins/wbmp/WBMPImageWriterSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/plugins/wbmp/WBMPImageWriterSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/plugins/wbmp/WBMPImageWriterSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -49,7 +49,7 @@
private boolean registered = false;
public WBMPImageWriterSpi() {
- super("Sun Microsystems, Inc.",
+ super("Oracle Corporation",
"1.0",
formatNames,
entensions,
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/spi/FileImageInputStreamSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/spi/FileImageInputStreamSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/spi/FileImageInputStreamSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -33,7 +33,7 @@
public class FileImageInputStreamSpi extends ImageInputStreamSpi {
- private static final String vendorName = "Sun Microsystems, Inc.";
+ private static final String vendorName = "Oracle Corporation";
private static final String version = "1.0";
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/spi/FileImageOutputStreamSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/spi/FileImageOutputStreamSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/spi/FileImageOutputStreamSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -33,7 +33,7 @@
public class FileImageOutputStreamSpi extends ImageOutputStreamSpi {
- private static final String vendorName = "Sun Microsystems, Inc.";
+ private static final String vendorName = "Oracle Corporation";
private static final String version = "1.0";
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/spi/InputStreamImageInputStreamSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/spi/InputStreamImageInputStreamSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/spi/InputStreamImageInputStreamSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -36,7 +36,7 @@
public class InputStreamImageInputStreamSpi extends ImageInputStreamSpi {
- private static final String vendorName = "Sun Microsystems, Inc.";
+ private static final String vendorName = "Oracle Corporation";
private static final String version = "1.0";
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/spi/OutputStreamImageOutputStreamSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/spi/OutputStreamImageOutputStreamSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/spi/OutputStreamImageOutputStreamSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -36,7 +36,7 @@
public class OutputStreamImageOutputStreamSpi extends ImageOutputStreamSpi {
- private static final String vendorName = "Sun Microsystems, Inc.";
+ private static final String vendorName = "Oracle Corporation";
private static final String version = "1.0";
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/spi/RAFImageInputStreamSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/spi/RAFImageInputStreamSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/spi/RAFImageInputStreamSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -34,7 +34,7 @@
public class RAFImageInputStreamSpi extends ImageInputStreamSpi {
- private static final String vendorName = "Sun Microsystems, Inc.";
+ private static final String vendorName = "Oracle Corporation";
private static final String version = "1.0";
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/imageio/spi/RAFImageOutputStreamSpi.java
--- a/jdk/src/share/classes/com/sun/imageio/spi/RAFImageOutputStreamSpi.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/imageio/spi/RAFImageOutputStreamSpi.java Tue Oct 26 10:57:35 2010 -0700
@@ -34,7 +34,7 @@
public class RAFImageOutputStreamSpi extends ImageOutputStreamSpi {
- private static final String vendorName = "Sun Microsystems, Inc.";
+ private static final String vendorName = "Oracle Corporation";
private static final String version = "1.0";
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/java/util/jar/pack/Attribute.java
--- a/jdk/src/share/classes/com/sun/java/util/jar/pack/Attribute.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/java/util/jar/pack/Attribute.java Tue Oct 26 10:57:35 2010 -0700
@@ -654,8 +654,8 @@
String layout;
public FormatException(String message,
int ctype, String name, String layout) {
- super(ATTR_CONTEXT_NAME[ctype]+"."+name
- +(message == null? "": (": "+message)));
+ super(ATTR_CONTEXT_NAME[ctype]+ " attribute \"" + name + "\"" +
+ (message == null? "" : (": " + message)));
this.ctype = ctype;
this.name = name;
this.layout = layout;
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/java/util/jar/pack/ClassReader.java
--- a/jdk/src/share/classes/com/sun/java/util/jar/pack/ClassReader.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/java/util/jar/pack/ClassReader.java Tue Oct 26 10:57:35 2010 -0700
@@ -30,6 +30,7 @@
import com.sun.java.util.jar.pack.Package.Class;
import com.sun.java.util.jar.pack.Package.InnerClass;
import com.sun.java.util.jar.pack.ConstantPool.*;
+import com.sun.tools.classfile.AttributeException;
/**
* Reader for a class file that is being incorporated into a package.
@@ -246,7 +247,9 @@
fixups[fptr++] = in.readUnsignedShort();
break;
default:
- throw new IOException("Bad constant pool tag "+tag);
+ throw new ClassFormatException("Bad constant pool tag " +
+ tag + " in File: " + cls.file.nameString +
+ " at pos: " + inPos);
}
}
@@ -403,7 +406,7 @@
skip(length, "unknown "+name+" attribute in "+h);
continue;
} else {
- String message = "unknown in "+h;
+ String message = " is unknown attribute in class " + h;
throw new Attribute.FormatException(message, ctype, name,
unknownAttrCommand);
}
@@ -415,7 +418,12 @@
if (a.name() == "Code") {
Class.Method m = (Class.Method) h;
m.code = new Code(m);
- readCode(m.code);
+ try {
+ readCode(m.code);
+ } catch (Instruction.FormatException iie) {
+ String message = iie.getMessage() + " in " + h;
+ throw new ClassReader.ClassFormatException(message);
+ }
} else {
assert(h == cls);
readInnerClasses(cls);
@@ -427,6 +435,10 @@
in.readFully(bytes);
a = a.addContent(bytes);
}
+ if (a.size() == 0 && !a.layout().isEmpty()) {
+ throw new ClassFormatException(name +
+ ": attribute length cannot be zero, in " + h);
+ }
h.addAttribute(a);
if (verbose > 2)
Utils.log.fine("read "+a);
@@ -438,6 +450,7 @@
code.max_locals = readUnsignedShort();
code.bytes = new byte[readInt()];
in.readFully(code.bytes);
+ Instruction.opcodeChecker(code.bytes);
int nh = readUnsignedShort();
code.setHandlerCount(nh);
for (int i = 0; i < nh; i++) {
@@ -463,4 +476,10 @@
cls.innerClasses = ics; // set directly; do not use setInnerClasses.
// (Later, ics may be transferred to the pkg.)
}
+
+ class ClassFormatException extends IOException {
+ public ClassFormatException(String message) {
+ super(message);
+ }
+ }
}
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/java/util/jar/pack/Instruction.java
--- a/jdk/src/share/classes/com/sun/java/util/jar/pack/Instruction.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/java/util/jar/pack/Instruction.java Tue Oct 26 10:57:35 2010 -0700
@@ -25,6 +25,8 @@
package com.sun.java.util.jar.pack;
+import java.io.IOException;
+
/**
* A parsed bytecode instruction.
* Provides accessors to various relevant bits.
@@ -628,4 +630,21 @@
}
}
}
+
+ public static void opcodeChecker(byte[] code) throws FormatException {
+ Instruction i = at(code, 0);
+ while (i != null) {
+ int opcode = i.getBC();
+ if (opcode == _xxxunusedxxx || opcode < _nop || opcode > _jsr_w) {
+ String message = "illegal opcode: " + opcode + " " + i;
+ throw new FormatException(message);
+ }
+ i = i.next();
+ }
+ }
+ static class FormatException extends IOException {
+ FormatException(String message) {
+ super(message);
+ }
+ }
}
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/java/util/jar/pack/PackerImpl.java
--- a/jdk/src/share/classes/com/sun/java/util/jar/pack/PackerImpl.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/java/util/jar/pack/PackerImpl.java Tue Oct 26 10:57:35 2010 -0700
@@ -496,15 +496,29 @@
reader.unknownAttrCommand = unknownAttrCommand;
try {
reader.read();
- } catch (Attribute.FormatException ee) {
- // He passed up the category to us in layout.
- if (ee.layout.equals(Pack200.Packer.PASS)) {
- Utils.log.warning("Passing class file uncompressed due to unrecognized attribute: "+fname);
- Utils.log.info(ee.toString());
- return null;
+ } catch (IOException ioe) {
+ String message = "Passing class file uncompressed due to";
+ if (ioe instanceof Attribute.FormatException) {
+ Attribute.FormatException ee = (Attribute.FormatException) ioe;
+ // He passed up the category to us in layout.
+ if (ee.layout.equals(Pack200.Packer.PASS)) {
+ Utils.log.info(ee.toString());
+ Utils.log.warning(message + " unrecognized attribute: " +
+ fname);
+ return null;
+ }
+ } else if (ioe instanceof ClassReader.ClassFormatException) {
+ ClassReader.ClassFormatException ce = (ClassReader.ClassFormatException) ioe;
+ // %% TODO: Do we invent a new property for this or reuse %%
+ if (unknownAttrCommand.equals(Pack200.Packer.PASS)) {
+ Utils.log.info(ce.toString());
+ Utils.log.warning(message + " unknown class format: " +
+ fname);
+ return null;
+ }
}
// Otherwise, it must be an error.
- throw ee;
+ throw ioe;
}
pkg.addClass(cls);
return cls.file;
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/java/util/jar/pack/Utils.java
--- a/jdk/src/share/classes/com/sun/java/util/jar/pack/Utils.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/java/util/jar/pack/Utils.java Tue Oct 26 10:57:35 2010 -0700
@@ -182,11 +182,8 @@
}
public void warning(String msg, Object param) {
- int verbose = currentPropMap().getInteger(DEBUG_VERBOSE);
- if (verbose > 0) {
getLogger().warning(msg, param);
}
- }
public void warning(String msg) {
warning(msg, null);
@@ -216,7 +213,9 @@
// Returns the Max Version String of this implementation
static String getVersionString() {
- return "Pack200, Vendor: Sun Microsystems, Version: " +
+ return "Pack200, Vendor: " +
+ System.getProperty("java.vendor") +
+ ", Version: " +
Constants.JAVA6_PACKAGE_MAJOR_VERSION + "." +
Constants.JAVA6_PACKAGE_MINOR_VERSION;
}
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/com/sun/jndi/dns/DnsContextFactory.java
--- a/jdk/src/share/classes/com/sun/jndi/dns/DnsContextFactory.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/com/sun/jndi/dns/DnsContextFactory.java Tue Oct 26 10:57:35 2010 -0700
@@ -54,6 +54,7 @@
public class DnsContextFactory implements InitialContextFactory {
private static final String DEFAULT_URL = "dns:";
+ private static final int DEFAULT_PORT = 53;
public Context getInitialContext(Hashtable,?> env) throws NamingException {
@@ -89,7 +90,9 @@
* Public for use by product test suite.
*/
public static boolean platformServersAvailable() {
- return !ResolverConfiguration.open().nameservers().isEmpty();
+ return !filterNameServers(
+ ResolverConfiguration.open().nameservers(), true
+ ).isEmpty();
}
private static Context urlToContext(String url, Hashtable env)
@@ -142,8 +145,8 @@
// No server or port given, so look to underlying platform.
// ResolverConfiguration does some limited caching, so the
// following is reasonably efficient even if called rapid-fire.
- List
+ * In environments in which the delegation model is not strictly
+ * hierarchical, class loaders need to be parallel capable, otherwise class
* loading can lead to deadlocks because the loader lock is held for the
* duration of the class loading process (see {@link #loadClass
* loadClass} methods).
@@ -1218,14 +1221,14 @@
private static native Class extends ClassLoader> getCaller(int index);
/**
- * Registers the caller class loader as parallel capable.
- * In order for the registration to succeed, all super classes
- * of the caller class loader must also be registered as
- * parallel capable when this method is called.
LinkageError
indicate that a class has
+ * Subclasses of {@code LinkageError} indicate that a class has
* some dependency on another class; however, the latter class has
* incompatibly changed after the compilation of the former class.
*
@@ -39,14 +39,14 @@
private static final long serialVersionUID = 3579600108157160122L;
/**
- * Constructs a LinkageError
with no detail message.
+ * Constructs a {@code LinkageError} with no detail message.
*/
public LinkageError() {
super();
}
/**
- * Constructs a LinkageError
with the specified detail
+ * Constructs a {@code LinkageError} with the specified detail
* message.
*
* @param s the detail message.
@@ -54,4 +54,16 @@
public LinkageError(String s) {
super(s);
}
+
+ /**
+ * Constructs a {@code LinkageError} with the specified detail
+ * message and cause.
+ *
+ * @param s the detail message.
+ * @param cause the cause, may be {@code null}
+ * @since 1.7
+ */
+ public LinkageError(String s, Throwable cause) {
+ super(s, cause);
+ }
}
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/java/lang/System.java
--- a/jdk/src/share/classes/java/lang/System.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/java/lang/System.java Tue Oct 26 10:57:35 2010 -0700
@@ -53,7 +53,13 @@
*/
public final class System {
- /* First thing---register the natives */
+ /* register the natives via the static initializer.
+ *
+ * VM will invoke the initializeSystemClass method to complete
+ * the initialization for this class separated from clinit.
+ * Note that to use properties set by the VM, see the constraints
+ * described in the initializeSystemClass method.
+ */
private static native void registerNatives();
static {
registerNatives();
@@ -1096,17 +1102,21 @@
* Initialize the system class. Called after thread initialization.
*/
private static void initializeSystemClass() {
- props = new Properties();
- initProperties(props);
+ // There are certain system configurations that may be controlled by
+ // VM options such as the maximum amount of direct memory and
+ // Integer cache size used to support the object identity semantics
+ // of autoboxing. Typically, the library will obtain these values
+ // from the properties set by the VM. If the properties are for
+ // internal implementation use only, these properties should be
+ // removed from the system properties.
+ //
+ // See java.lang.Integer.IntegerCache and the
+ // sun.misc.VM.saveAndRemoveProperties method for example.
+ props = initSystemProperties();
+
lineSeparator = props.getProperty("line.separator");
sun.misc.Version.init();
- // Gets and removes system properties that configure the Integer
- // cache used to support the object identity semantics of autoboxing.
- // At this time, the size of the cache may be controlled by the
- // vm option -XX:AutoBoxCacheMax=checkConnect
* method is called for each InetAddress. Only InetAddresses where
* the checkConnect
doesn't throw a SecurityException
- * will be returned in the Enumeration.
+ * will be returned in the Enumeration. However, if the caller has the
+ * {@link NetPermission}("getNetworkInformation") permission, then all
+ * InetAddresses are returned.
* @return an Enumeration object with all or a subset of the InetAddresses
* bound to this network interface
*/
@@ -99,11 +101,19 @@
checkedAddresses() {
local_addrs = new InetAddress[addrs.length];
+ boolean trusted = true;
SecurityManager sec = System.getSecurityManager();
+ if (sec != null) {
+ try {
+ sec.checkPermission(new NetPermission("getNetworkInformation"));
+ } catch (SecurityException e) {
+ trusted = false;
+ }
+ }
for (int j=0; jnull
if
+ * the address doesn't exist, is not accessible or a security
+ * manager is set and the caller does not have the permission
+ * NetPermission("getNetworkInformation")
+ *
* @exception SocketException if an I/O error occurs.
* @since 1.6
*/
public byte[] getHardwareAddress() throws SocketException {
+ SecurityManager sec = System.getSecurityManager();
+ if (sec != null) {
+ try {
+ sec.checkPermission(new NetPermission("getNetworkInformation"));
+ } catch (SecurityException e) {
+ if (!getInetAddresses().hasMoreElements()) {
+ // don't have connect permission to any local address
+ return null;
+ }
+ }
+ }
for (InetAddress addr : addrs) {
if (addr instanceof Inet4Address) {
return getMacAddr0(((Inet4Address)addr).getAddress(), name, index);
@@ -523,11 +549,10 @@
}
public int hashCode() {
- int count = 0;
- if (addrs != null) {
- for (int i = 0; i < addrs.length; i++) {
- count += addrs[i].hashCode();
- }
+ int count = name == null? 0: name.hashCode();
+ EnumerationX500Principal
that represents the name of the
+ * authority that signed the certificate's revocation status information
*/
private final X500Principal authority;
@@ -79,8 +79,9 @@
* @param extensions a map of X.509 Extensions. Each key is an OID String
* that maps to the corresponding Extension. The map is copied to
* prevent subsequent modification.
- * @param authority the name of the authority that signed the certificate's
- * revocation status information
+ * @param authority the X500Principal
that represents the name
+ * of the authority that signed the certificate's revocation status
+ * information
* @throws NullPointerException if revocationDate
,
* reason
, authority
, or
* extensions
is null
@@ -121,8 +122,8 @@
* Returns the name of the authority that signed the certificate's
* revocation status information.
*
- * @return the name of the authority that signed the certificate's
- * revocation status information
+ * @return the X500Principal
that represents the name of the
+ * authority that signed the certificate's revocation status information
*/
public X500Principal getAuthorityName() {
return authority;
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/java/text/RuleBasedCollator.java
--- a/jdk/src/share/classes/java/text/RuleBasedCollator.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/java/text/RuleBasedCollator.java Tue Oct 26 10:57:35 2010 -0700
@@ -335,9 +335,15 @@
* collation rules. Returns information about whether a string is less
* than, greater than or equal to another string in a language.
* This can be overriden in a subclass.
+ *
+ * @exception NullPointerException if source
or target
is null.
*/
public synchronized int compare(String source, String target)
{
+ if (source == null || target == null) {
+ throw new NullPointerException();
+ }
+
// The basic algorithm here is that we use CollationElementIterators
// to step through both the source and target strings. We compare each
// collation element in the source string against the corresponding one
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/java/util/Arrays.java
--- a/jdk/src/share/classes/java/util/Arrays.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/java/util/Arrays.java Tue Oct 26 10:57:35 2010 -0700
@@ -97,7 +97,8 @@
* if {@code fromIndex < 0} or {@code toIndex > a.length}
*/
public static void sort(int[] a, int fromIndex, int toIndex) {
- DualPivotQuicksort.sort(a, fromIndex, toIndex);
+ rangeCheck(a.length, fromIndex, toIndex);
+ DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
}
/**
@@ -136,7 +137,8 @@
* if {@code fromIndex < 0} or {@code toIndex > a.length}
*/
public static void sort(long[] a, int fromIndex, int toIndex) {
- DualPivotQuicksort.sort(a, fromIndex, toIndex);
+ rangeCheck(a.length, fromIndex, toIndex);
+ DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
}
/**
@@ -175,7 +177,8 @@
* if {@code fromIndex < 0} or {@code toIndex > a.length}
*/
public static void sort(short[] a, int fromIndex, int toIndex) {
- DualPivotQuicksort.sort(a, fromIndex, toIndex);
+ rangeCheck(a.length, fromIndex, toIndex);
+ DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
}
/**
@@ -214,7 +217,8 @@
* if {@code fromIndex < 0} or {@code toIndex > a.length}
*/
public static void sort(char[] a, int fromIndex, int toIndex) {
- DualPivotQuicksort.sort(a, fromIndex, toIndex);
+ rangeCheck(a.length, fromIndex, toIndex);
+ DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
}
/**
@@ -253,7 +257,8 @@
* if {@code fromIndex < 0} or {@code toIndex > a.length}
*/
public static void sort(byte[] a, int fromIndex, int toIndex) {
- DualPivotQuicksort.sort(a, fromIndex, toIndex);
+ rangeCheck(a.length, fromIndex, toIndex);
+ DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
}
/**
@@ -308,7 +313,8 @@
* if {@code fromIndex < 0} or {@code toIndex > a.length}
*/
public static void sort(float[] a, int fromIndex, int toIndex) {
- DualPivotQuicksort.sort(a, fromIndex, toIndex);
+ rangeCheck(a.length, fromIndex, toIndex);
+ DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
}
/**
@@ -363,12 +369,12 @@
* if {@code fromIndex < 0} or {@code toIndex > a.length}
*/
public static void sort(double[] a, int fromIndex, int toIndex) {
- DualPivotQuicksort.sort(a, fromIndex, toIndex);
+ rangeCheck(a.length, fromIndex, toIndex);
+ DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
}
/*
* Sorting of complex type arrays.
- *
*/
/**
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/java/util/DualPivotQuicksort.java
--- a/jdk/src/share/classes/java/util/DualPivotQuicksort.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/java/util/DualPivotQuicksort.java Tue Oct 26 10:57:35 2010 -0700
@@ -36,7 +36,7 @@
* @author Jon Bentley
* @author Josh Bloch
*
- * @version 2010.06.21 m765.827.12i:5\7
+ * @version 2010.10.13 m765.827.12i:5\7p
* @since 1.7
*/
final class DualPivotQuicksort {
@@ -54,26 +54,26 @@
* If the length of an array to be sorted is less than this
* constant, insertion sort is used in preference to Quicksort.
*/
- private static final int INSERTION_SORT_THRESHOLD = 32;
+ private static final int INSERTION_SORT_THRESHOLD = 47;
/**
- * If the length of a byte array to be sorted is greater than
- * this constant, counting sort is used in preference to Quicksort.
+ * If the length of a byte array to be sorted is greater than this
+ * constant, counting sort is used in preference to insertion sort.
*/
- private static final int COUNTING_SORT_THRESHOLD_FOR_BYTE = 128;
+ private static final int COUNTING_SORT_THRESHOLD_FOR_BYTE = 29;
/**
* If the length of a short or char array to be sorted is greater
* than this constant, counting sort is used in preference to Quicksort.
*/
- private static final int COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR = 32768;
+ private static final int COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR = 3200;
/*
* Sorting methods for seven primitive types.
*/
/**
- * Sorts the specified array into ascending numerical order.
+ * Sorts the specified array.
*
* @param a the array to be sorted
*/
@@ -82,58 +82,34 @@
}
/**
- * Sorts the specified range of the array into ascending order. The range
- * to be sorted extends from the index {@code fromIndex}, inclusive, to
- * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
- * the range to be sorted is empty (and the call is a no-op).
- *
- * @param a the array to be sorted
- * @param fromIndex the index of the first element, inclusive, to be sorted
- * @param toIndex the index of the last element, exclusive, to be sorted
- * @throws IllegalArgumentException if {@code fromIndex > toIndex}
- * @throws ArrayIndexOutOfBoundsException
- * if {@code fromIndex < 0} or {@code toIndex > a.length}
- */
- public static void sort(int[] a, int fromIndex, int toIndex) {
- rangeCheck(a.length, fromIndex, toIndex);
- sort(a, fromIndex, toIndex - 1, true);
- }
-
- /**
- * Sorts the specified range of the array into ascending order by the
- * Dual-Pivot Quicksort algorithm. This method differs from the public
- * {@code sort} method in that the {@code right} index is inclusive,
- * it does no range checking on {@code left} or {@code right}, and has
- * boolean flag whether insertion sort with sentinel is used or not.
+ * Sorts the specified range of the array.
*
* @param a the array to be sorted
* @param left the index of the first element, inclusive, to be sorted
* @param right the index of the last element, inclusive, to be sorted
- * @param leftmost indicates if the part is the most left in the range
+ */
+ public static void sort(int[] a, int left, int right) {
+ sort(a, left, right, true);
+ }
+
+ /**
+ * Sorts the specified range of the array by Dual-Pivot Quicksort.
+ *
+ * @param a the array to be sorted
+ * @param left the index of the first element, inclusive, to be sorted
+ * @param right the index of the last element, inclusive, to be sorted
+ * @param leftmost indicates if this part is the leftmost in the range
*/
private static void sort(int[] a, int left, int right, boolean leftmost) {
int length = right - left + 1;
- // Use insertion sort on tiny arrays
+ // Use insertion sort on small arrays
if (length < INSERTION_SORT_THRESHOLD) {
- if (!leftmost) {
+ if (leftmost) {
/*
- * Every element in adjoining part plays the role
- * of sentinel, therefore this allows us to avoid
- * the j >= left check on each iteration.
- */
- for (int j, i = left + 1; i <= right; i++) {
- int ai = a[i];
- for (j = i - 1; ai < a[j]; j--) {
- // assert j >= left;
- a[j + 1] = a[j];
- }
- a[j + 1] = ai;
- }
- } else {
- /*
- * For case of leftmost part traditional (without a sentinel)
- * insertion sort, optimized for server JVM, is used.
+ * Traditional (without sentinel) insertion sort,
+ * optimized for server VM, is used in case of
+ * the leftmost part.
*/
for (int i = left, j = i; i < right; j = ++i) {
int ai = a[i + 1];
@@ -145,12 +121,54 @@
}
a[j + 1] = ai;
}
+ } else {
+ /*
+ * Skip the longest ascending sequence.
+ */
+ do {
+ if (left++ >= right) {
+ return;
+ }
+ } while (a[left - 1] <= a[left]);
+
+ /*
+ * Every element from adjoining part plays the role
+ * of sentinel, therefore this allows us to avoid the
+ * left range check on each iteration. Moreover, we use
+ * the best improved algorithm, so called pair insertion
+ * sort, which is faster than traditional implementation
+ * in the context of Dual-Pivot Quicksort.
+ */
+ for (int k = left--; (left += 2) <= right; ) {
+ int a1, a2; k = left - 1;
+
+ if (a[k] < a[left]) {
+ a2 = a[k]; a1 = a[left];
+ } else {
+ a1 = a[k]; a2 = a[left];
+ }
+ while (a1 < a[--k]) {
+ a[k + 2] = a[k];
+ }
+ a[++k + 1] = a1;
+
+ while (a2 < a[--k]) {
+ a[k + 1] = a[k];
+ }
+ a[k + 1] = a2;
+ }
+ int last = a[right];
+
+ while (last < a[--right]) {
+ a[right + 1] = a[right];
+ }
+ a[right + 1] = last;
}
return;
}
// Inexpensive approximation of length / 7
- int seventh = (length >>> 3) + (length >>> 6) + 1;
+ int seventh = (length >> 3) + (length >> 6) + 1;
/*
* Sort five evenly spaced elements around (and including) the
@@ -232,10 +250,14 @@
* Pointer k is the first index of ?-part.
*/
outer:
- for (int k = less; k <= great; k++) {
+ for (int k = less - 1; ++k <= great; ) {
int ak = a[k];
if (ak < pivot1) { // Move a[k] to left part
a[k] = a[less];
+ /*
+ * Here and below we use "a[i] = b; i++;" instead
+ * of "a[i++] = b;" due to performance issue.
+ */
a[less] = ak;
less++;
} else if (ak > pivot2) { // Move a[k] to right part
@@ -244,13 +266,17 @@
break outer;
}
}
- if (a[great] < pivot1) {
+ if (a[great] < pivot1) { // a[great] <= pivot2
a[k] = a[less];
a[less] = a[great];
less++;
} else { // pivot1 <= a[great] <= pivot2
a[k] = a[great];
}
+ /*
+ * Here and below we use "a[i] = b; i--;" instead
+ * of "a[i--] = b;" due to performance issue.
+ */
a[great] = ak;
great--;
}
@@ -265,7 +291,7 @@
sort(a, great + 2, right, false);
/*
- * If center part is too large (comprises > 5/7 of the array),
+ * If center part is too large (comprises > 4/7 of the array),
* swap internal pivot values to ends.
*/
if (less < e1 && e5 < great) {
@@ -299,7 +325,7 @@
* Pointer k is the first index of ?-part.
*/
outer:
- for (int k = less; k <= great; k++) {
+ for (int k = less - 1; ++k <= great; ) {
int ak = a[k];
if (ak == pivot1) { // Move a[k] to left part
a[k] = a[less];
@@ -311,7 +337,7 @@
break outer;
}
}
- if (a[great] == pivot1) {
+ if (a[great] == pivot1) { // a[great] < pivot2
a[k] = a[less];
/*
* Even though a[great] equals to pivot1, the
@@ -337,7 +363,7 @@
} else { // Pivots are equal
/*
- * Partition degenerates to the traditional 3-way
+ * Partitioning degenerates to the traditional 3-way
* (or "Dutch National Flag") schema:
*
* left part center part right part
@@ -356,28 +382,20 @@
*
* Pointer k is the first index of ?-part.
*/
- for (int k = left; k <= great; k++) {
+ for (int k = less; k <= great; ++k) {
if (a[k] == pivot1) {
continue;
}
int ak = a[k];
-
if (ak < pivot1) { // Move a[k] to left part
a[k] = a[less];
a[less] = ak;
less++;
} else { // a[k] > pivot1 - Move a[k] to right part
- /*
- * We know that pivot1 == a[e3] == pivot2. Thus, we know
- * that great will still be >= k when the following loop
- * terminates, even though we don't test for it explicitly.
- * In other words, a[e3] acts as a sentinel for great.
- */
while (a[great] > pivot1) {
- // assert great > k;
great--;
}
- if (a[great] < pivot1) {
+ if (a[great] < pivot1) { // a[great] <= pivot1
a[k] = a[less];
a[less] = a[great];
less++;
@@ -397,14 +415,18 @@
}
}
- // Sort left and right parts recursively
+ /*
+ * Sort left and right parts recursively.
+ * All elements from center part are equal
+ * and, therefore, already sorted.
+ */
sort(a, left, less - 1, leftmost);
sort(a, great + 1, right, false);
}
}
/**
- * Sorts the specified array into ascending numerical order.
+ * Sorts the specified array.
*
* @param a the array to be sorted
*/
@@ -413,58 +435,34 @@
}
/**
- * Sorts the specified range of the array into ascending order. The range
- * to be sorted extends from the index {@code fromIndex}, inclusive, to
- * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
- * the range to be sorted is empty (and the call is a no-op).
- *
- * @param a the array to be sorted
- * @param fromIndex the index of the first element, inclusive, to be sorted
- * @param toIndex the index of the last element, exclusive, to be sorted
- * @throws IllegalArgumentException if {@code fromIndex > toIndex}
- * @throws ArrayIndexOutOfBoundsException
- * if {@code fromIndex < 0} or {@code toIndex > a.length}
- */
- public static void sort(long[] a, int fromIndex, int toIndex) {
- rangeCheck(a.length, fromIndex, toIndex);
- sort(a, fromIndex, toIndex - 1, true);
- }
-
- /**
- * Sorts the specified range of the array into ascending order by the
- * Dual-Pivot Quicksort algorithm. This method differs from the public
- * {@code sort} method in that the {@code right} index is inclusive,
- * it does no range checking on {@code left} or {@code right}, and has
- * boolean flag whether insertion sort with sentinel is used or not.
+ * Sorts the specified range of the array.
*
* @param a the array to be sorted
* @param left the index of the first element, inclusive, to be sorted
* @param right the index of the last element, inclusive, to be sorted
- * @param leftmost indicates if the part is the most left in the range
+ */
+ public static void sort(long[] a, int left, int right) {
+ sort(a, left, right, true);
+ }
+
+ /**
+ * Sorts the specified range of the array by Dual-Pivot Quicksort.
+ *
+ * @param a the array to be sorted
+ * @param left the index of the first element, inclusive, to be sorted
+ * @param right the index of the last element, inclusive, to be sorted
+ * @param leftmost indicates if this part is the leftmost in the range
*/
private static void sort(long[] a, int left, int right, boolean leftmost) {
int length = right - left + 1;
- // Use insertion sort on tiny arrays
+ // Use insertion sort on small arrays
if (length < INSERTION_SORT_THRESHOLD) {
- if (!leftmost) {
+ if (leftmost) {
/*
- * Every element in adjoining part plays the role
- * of sentinel, therefore this allows us to avoid
- * the j >= left check on each iteration.
- */
- for (int j, i = left + 1; i <= right; i++) {
- long ai = a[i];
- for (j = i - 1; ai < a[j]; j--) {
- // assert j >= left;
- a[j + 1] = a[j];
- }
- a[j + 1] = ai;
- }
- } else {
- /*
- * For case of leftmost part traditional (without a sentinel)
- * insertion sort, optimized for server JVM, is used.
+ * Traditional (without sentinel) insertion sort,
+ * optimized for server VM, is used in case of
+ * the leftmost part.
*/
for (int i = left, j = i; i < right; j = ++i) {
long ai = a[i + 1];
@@ -476,12 +474,54 @@
}
a[j + 1] = ai;
}
+ } else {
+ /*
+ * Skip the longest ascending sequence.
+ */
+ do {
+ if (left++ >= right) {
+ return;
+ }
+ } while (a[left - 1] <= a[left]);
+
+ /*
+ * Every element from adjoining part plays the role
+ * of sentinel, therefore this allows us to avoid the
+ * left range check on each iteration. Moreover, we use
+ * the best improved algorithm, so called pair insertion
+ * sort, which is faster than traditional implementation
+ * in the context of Dual-Pivot Quicksort.
+ */
+ for (int k = left--; (left += 2) <= right; ) {
+ long a1, a2; k = left - 1;
+
+ if (a[k] < a[left]) {
+ a2 = a[k]; a1 = a[left];
+ } else {
+ a1 = a[k]; a2 = a[left];
+ }
+ while (a1 < a[--k]) {
+ a[k + 2] = a[k];
+ }
+ a[++k + 1] = a1;
+
+ while (a2 < a[--k]) {
+ a[k + 1] = a[k];
+ }
+ a[k + 1] = a2;
+ }
+ long last = a[right];
+
+ while (last < a[--right]) {
+ a[right + 1] = a[right];
+ }
+ a[right + 1] = last;
}
return;
}
// Inexpensive approximation of length / 7
- int seventh = (length >>> 3) + (length >>> 6) + 1;
+ int seventh = (length >> 3) + (length >> 6) + 1;
/*
* Sort five evenly spaced elements around (and including) the
@@ -563,10 +603,14 @@
* Pointer k is the first index of ?-part.
*/
outer:
- for (int k = less; k <= great; k++) {
+ for (int k = less - 1; ++k <= great; ) {
long ak = a[k];
if (ak < pivot1) { // Move a[k] to left part
a[k] = a[less];
+ /*
+ * Here and below we use "a[i] = b; i++;" instead
+ * of "a[i++] = b;" due to performance issue.
+ */
a[less] = ak;
less++;
} else if (ak > pivot2) { // Move a[k] to right part
@@ -575,13 +619,17 @@
break outer;
}
}
- if (a[great] < pivot1) {
+ if (a[great] < pivot1) { // a[great] <= pivot2
a[k] = a[less];
a[less] = a[great];
less++;
} else { // pivot1 <= a[great] <= pivot2
a[k] = a[great];
}
+ /*
+ * Here and below we use "a[i] = b; i--;" instead
+ * of "a[i--] = b;" due to performance issue.
+ */
a[great] = ak;
great--;
}
@@ -596,7 +644,7 @@
sort(a, great + 2, right, false);
/*
- * If center part is too large (comprises > 5/7 of the array),
+ * If center part is too large (comprises > 4/7 of the array),
* swap internal pivot values to ends.
*/
if (less < e1 && e5 < great) {
@@ -630,7 +678,7 @@
* Pointer k is the first index of ?-part.
*/
outer:
- for (int k = less; k <= great; k++) {
+ for (int k = less - 1; ++k <= great; ) {
long ak = a[k];
if (ak == pivot1) { // Move a[k] to left part
a[k] = a[less];
@@ -642,7 +690,7 @@
break outer;
}
}
- if (a[great] == pivot1) {
+ if (a[great] == pivot1) { // a[great] < pivot2
a[k] = a[less];
/*
* Even though a[great] equals to pivot1, the
@@ -668,7 +716,7 @@
} else { // Pivots are equal
/*
- * Partition degenerates to the traditional 3-way
+ * Partitioning degenerates to the traditional 3-way
* (or "Dutch National Flag") schema:
*
* left part center part right part
@@ -687,28 +735,20 @@
*
* Pointer k is the first index of ?-part.
*/
- for (int k = left; k <= great; k++) {
+ for (int k = less; k <= great; ++k) {
if (a[k] == pivot1) {
continue;
}
long ak = a[k];
-
if (ak < pivot1) { // Move a[k] to left part
a[k] = a[less];
a[less] = ak;
less++;
} else { // a[k] > pivot1 - Move a[k] to right part
- /*
- * We know that pivot1 == a[e3] == pivot2. Thus, we know
- * that great will still be >= k when the following loop
- * terminates, even though we don't test for it explicitly.
- * In other words, a[e3] acts as a sentinel for great.
- */
while (a[great] > pivot1) {
- // assert great > k;
great--;
}
- if (a[great] < pivot1) {
+ if (a[great] < pivot1) { // a[great] <= pivot1
a[k] = a[less];
a[less] = a[great];
less++;
@@ -728,45 +768,51 @@
}
}
- // Sort left and right parts recursively
+ /*
+ * Sort left and right parts recursively.
+ * All elements from center part are equal
+ * and, therefore, already sorted.
+ */
sort(a, left, less - 1, leftmost);
sort(a, great + 1, right, false);
}
}
/**
- * Sorts the specified array into ascending numerical order.
+ * Sorts the specified array.
*
* @param a the array to be sorted
*/
public static void sort(short[] a) {
- if (a.length > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
- countingSort(a, 0, a.length - 1);
- } else {
- sort(a, 0, a.length - 1, true);
- }
+ sort(a, 0, a.length - 1);
}
/**
- * Sorts the specified range of the array into ascending order. The range
- * to be sorted extends from the index {@code fromIndex}, inclusive, to
- * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
- * the range to be sorted is empty (and the call is a no-op).
+ * Sorts the specified range of the array.
*
* @param a the array to be sorted
- * @param fromIndex the index of the first element, inclusive, to be sorted
- * @param toIndex the index of the last element, exclusive, to be sorted
- * @throws IllegalArgumentException if {@code fromIndex > toIndex}
- * @throws ArrayIndexOutOfBoundsException
- * if {@code fromIndex < 0} or {@code toIndex > a.length}
+ * @param left the index of the first element, inclusive, to be sorted
+ * @param right the index of the last element, inclusive, to be sorted
*/
- public static void sort(short[] a, int fromIndex, int toIndex) {
- rangeCheck(a.length, fromIndex, toIndex);
+ public static void sort(short[] a, int left, int right) {
+ // Use counting sort on large arrays
+ if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
+ int[] count = new int[NUM_SHORT_VALUES];
- if (toIndex - fromIndex > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
- countingSort(a, fromIndex, toIndex - 1);
- } else {
- sort(a, fromIndex, toIndex - 1, true);
+ for (int i = left - 1; ++i <= right; ) {
+ count[a[i] - Short.MIN_VALUE]++;
+ }
+ for (int i = NUM_SHORT_VALUES, k = right + 1; k > left; ) {
+ while (count[--i] == 0);
+ short value = (short) (i + Short.MIN_VALUE);
+ int s = count[i];
+
+ do {
+ a[--k] = value;
+ } while (--s > 0);
+ }
+ } else { // Use Dual-Pivot Quicksort on small arrays
+ sort(a, left, right, true);
}
}
@@ -774,66 +820,23 @@
private static final int NUM_SHORT_VALUES = 1 << 16;
/**
- * Sorts the specified range of the array by counting sort.
+ * Sorts the specified range of the array by Dual-Pivot Quicksort.
*
* @param a the array to be sorted
* @param left the index of the first element, inclusive, to be sorted
* @param right the index of the last element, inclusive, to be sorted
- */
- private static void countingSort(short[] a, int left, int right) {
- int[] count = new int[NUM_SHORT_VALUES];
-
- for (int i = left; i <= right; i++) {
- count[a[i] - Short.MIN_VALUE]++;
- }
- for (int i = NUM_SHORT_VALUES - 1, k = right; k >= left; i--) {
- while (count[i] == 0) {
- i--;
- }
- short value = (short) (i + Short.MIN_VALUE);
- int s = count[i];
-
- do {
- a[k--] = value;
- } while (--s > 0);
- }
- }
-
- /**
- * Sorts the specified range of the array into ascending order by the
- * Dual-Pivot Quicksort algorithm. This method differs from the public
- * {@code sort} method in that the {@code right} index is inclusive,
- * it does no range checking on {@code left} or {@code right}, and has
- * boolean flag whether insertion sort with sentinel is used or not.
- *
- * @param a the array to be sorted
- * @param left the index of the first element, inclusive, to be sorted
- * @param right the index of the last element, inclusive, to be sorted
- * @param leftmost indicates if the part is the most left in the range
+ * @param leftmost indicates if this part is the leftmost in the range
*/
private static void sort(short[] a, int left, int right,boolean leftmost) {
int length = right - left + 1;
- // Use insertion sort on tiny arrays
+ // Use insertion sort on small arrays
if (length < INSERTION_SORT_THRESHOLD) {
- if (!leftmost) {
+ if (leftmost) {
/*
- * Every element in adjoining part plays the role
- * of sentinel, therefore this allows us to avoid
- * the j >= left check on each iteration.
- */
- for (int j, i = left + 1; i <= right; i++) {
- short ai = a[i];
- for (j = i - 1; ai < a[j]; j--) {
- // assert j >= left;
- a[j + 1] = a[j];
- }
- a[j + 1] = ai;
- }
- } else {
- /*
- * For case of leftmost part traditional (without a sentinel)
- * insertion sort, optimized for server JVM, is used.
+ * Traditional (without sentinel) insertion sort,
+ * optimized for server VM, is used in case of
+ * the leftmost part.
*/
for (int i = left, j = i; i < right; j = ++i) {
short ai = a[i + 1];
@@ -845,12 +848,54 @@
}
a[j + 1] = ai;
}
+ } else {
+ /*
+ * Skip the longest ascending sequence.
+ */
+ do {
+ if (left++ >= right) {
+ return;
+ }
+ } while (a[left - 1] <= a[left]);
+
+ /*
+ * Every element from adjoining part plays the role
+ * of sentinel, therefore this allows us to avoid the
+ * left range check on each iteration. Moreover, we use
+ * the best improved algorithm, so called pair insertion
+ * sort, which is faster than traditional implementation
+ * in the context of Dual-Pivot Quicksort.
+ */
+ for (int k = left--; (left += 2) <= right; ) {
+ short a1, a2; k = left - 1;
+
+ if (a[k] < a[left]) {
+ a2 = a[k]; a1 = a[left];
+ } else {
+ a1 = a[k]; a2 = a[left];
+ }
+ while (a1 < a[--k]) {
+ a[k + 2] = a[k];
+ }
+ a[++k + 1] = a1;
+
+ while (a2 < a[--k]) {
+ a[k + 1] = a[k];
+ }
+ a[k + 1] = a2;
+ }
+ short last = a[right];
+
+ while (last < a[--right]) {
+ a[right + 1] = a[right];
+ }
+ a[right + 1] = last;
}
return;
}
// Inexpensive approximation of length / 7
- int seventh = (length >>> 3) + (length >>> 6) + 1;
+ int seventh = (length >> 3) + (length >> 6) + 1;
/*
* Sort five evenly spaced elements around (and including) the
@@ -932,10 +977,14 @@
* Pointer k is the first index of ?-part.
*/
outer:
- for (int k = less; k <= great; k++) {
+ for (int k = less - 1; ++k <= great; ) {
short ak = a[k];
if (ak < pivot1) { // Move a[k] to left part
a[k] = a[less];
+ /*
+ * Here and below we use "a[i] = b; i++;" instead
+ * of "a[i++] = b;" due to performance issue.
+ */
a[less] = ak;
less++;
} else if (ak > pivot2) { // Move a[k] to right part
@@ -944,13 +993,17 @@
break outer;
}
}
- if (a[great] < pivot1) {
+ if (a[great] < pivot1) { // a[great] <= pivot2
a[k] = a[less];
a[less] = a[great];
less++;
} else { // pivot1 <= a[great] <= pivot2
a[k] = a[great];
}
+ /*
+ * Here and below we use "a[i] = b; i--;" instead
+ * of "a[i--] = b;" due to performance issue.
+ */
a[great] = ak;
great--;
}
@@ -965,7 +1018,7 @@
sort(a, great + 2, right, false);
/*
- * If center part is too large (comprises > 5/7 of the array),
+ * If center part is too large (comprises > 4/7 of the array),
* swap internal pivot values to ends.
*/
if (less < e1 && e5 < great) {
@@ -999,7 +1052,7 @@
* Pointer k is the first index of ?-part.
*/
outer:
- for (int k = less; k <= great; k++) {
+ for (int k = less - 1; ++k <= great; ) {
short ak = a[k];
if (ak == pivot1) { // Move a[k] to left part
a[k] = a[less];
@@ -1011,7 +1064,7 @@
break outer;
}
}
- if (a[great] == pivot1) {
+ if (a[great] == pivot1) { // a[great] < pivot2
a[k] = a[less];
/*
* Even though a[great] equals to pivot1, the
@@ -1037,7 +1090,7 @@
} else { // Pivots are equal
/*
- * Partition degenerates to the traditional 3-way
+ * Partitioning degenerates to the traditional 3-way
* (or "Dutch National Flag") schema:
*
* left part center part right part
@@ -1056,28 +1109,20 @@
*
* Pointer k is the first index of ?-part.
*/
- for (int k = left; k <= great; k++) {
+ for (int k = less; k <= great; ++k) {
if (a[k] == pivot1) {
continue;
}
short ak = a[k];
-
if (ak < pivot1) { // Move a[k] to left part
a[k] = a[less];
a[less] = ak;
less++;
} else { // a[k] > pivot1 - Move a[k] to right part
- /*
- * We know that pivot1 == a[e3] == pivot2. Thus, we know
- * that great will still be >= k when the following loop
- * terminates, even though we don't test for it explicitly.
- * In other words, a[e3] acts as a sentinel for great.
- */
while (a[great] > pivot1) {
- // assert great > k;
great--;
}
- if (a[great] < pivot1) {
+ if (a[great] < pivot1) { // a[great] <= pivot1
a[k] = a[less];
a[less] = a[great];
less++;
@@ -1097,45 +1142,51 @@
}
}
- // Sort left and right parts recursively
+ /*
+ * Sort left and right parts recursively.
+ * All elements from center part are equal
+ * and, therefore, already sorted.
+ */
sort(a, left, less - 1, leftmost);
sort(a, great + 1, right, false);
}
}
/**
- * Sorts the specified array into ascending numerical order.
+ * Sorts the specified array.
*
* @param a the array to be sorted
*/
public static void sort(char[] a) {
- if (a.length > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
- countingSort(a, 0, a.length - 1);
- } else {
- sort(a, 0, a.length - 1, true);
- }
+ sort(a, 0, a.length - 1);
}
/**
- * Sorts the specified range of the array into ascending order. The range
- * to be sorted extends from the index {@code fromIndex}, inclusive, to
- * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
- * the range to be sorted is empty (and the call is a no-op).
+ * Sorts the specified range of the array.
*
* @param a the array to be sorted
- * @param fromIndex the index of the first element, inclusive, to be sorted
- * @param toIndex the index of the last element, exclusive, to be sorted
- * @throws IllegalArgumentException if {@code fromIndex > toIndex}
- * @throws ArrayIndexOutOfBoundsException
- * if {@code fromIndex < 0} or {@code toIndex > a.length}
+ * @param left the index of the first element, inclusive, to be sorted
+ * @param right the index of the last element, inclusive, to be sorted
*/
- public static void sort(char[] a, int fromIndex, int toIndex) {
- rangeCheck(a.length, fromIndex, toIndex);
+ public static void sort(char[] a, int left, int right) {
+ // Use counting sort on large arrays
+ if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
+ int[] count = new int[NUM_CHAR_VALUES];
- if (toIndex - fromIndex > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
- countingSort(a, fromIndex, toIndex - 1);
- } else {
- sort(a, fromIndex, toIndex - 1, true);
+ for (int i = left - 1; ++i <= right; ) {
+ count[a[i]]++;
+ }
+ for (int i = NUM_CHAR_VALUES, k = right + 1; k > left; ) {
+ while (count[--i] == 0);
+ char value = (char) i;
+ int s = count[i];
+
+ do {
+ a[--k] = value;
+ } while (--s > 0);
+ }
+ } else { // Use Dual-Pivot Quicksort on small arrays
+ sort(a, left, right, true);
}
}
@@ -1143,66 +1194,23 @@
private static final int NUM_CHAR_VALUES = 1 << 16;
/**
- * Sorts the specified range of the array by counting sort.
+ * Sorts the specified range of the array by Dual-Pivot Quicksort.
*
* @param a the array to be sorted
* @param left the index of the first element, inclusive, to be sorted
* @param right the index of the last element, inclusive, to be sorted
- */
- private static void countingSort(char[] a, int left, int right) {
- int[] count = new int[NUM_CHAR_VALUES];
-
- for (int i = left; i <= right; i++) {
- count[a[i]]++;
- }
- for (int i = 0, k = left; k <= right; i++) {
- while (count[i] == 0) {
- i++;
- }
- char value = (char) i;
- int s = count[i];
-
- do {
- a[k++] = value;
- } while (--s > 0);
- }
- }
-
- /**
- * Sorts the specified range of the array into ascending order by the
- * Dual-Pivot Quicksort algorithm. This method differs from the public
- * {@code sort} method in that the {@code right} index is inclusive,
- * it does no range checking on {@code left} or {@code right}, and has
- * boolean flag whether insertion sort with sentinel is used or not.
- *
- * @param a the array to be sorted
- * @param left the index of the first element, inclusive, to be sorted
- * @param right the index of the last element, inclusive, to be sorted
- * @param leftmost indicates if the part is the most left in the range
+ * @param leftmost indicates if this part is the leftmost in the range
*/
private static void sort(char[] a, int left, int right, boolean leftmost) {
int length = right - left + 1;
- // Use insertion sort on tiny arrays
+ // Use insertion sort on small arrays
if (length < INSERTION_SORT_THRESHOLD) {
- if (!leftmost) {
+ if (leftmost) {
/*
- * Every element in adjoining part plays the role
- * of sentinel, therefore this allows us to avoid
- * the j >= left check on each iteration.
- */
- for (int j, i = left + 1; i <= right; i++) {
- char ai = a[i];
- for (j = i - 1; ai < a[j]; j--) {
- // assert j >= left;
- a[j + 1] = a[j];
- }
- a[j + 1] = ai;
- }
- } else {
- /*
- * For case of leftmost part traditional (without a sentinel)
- * insertion sort, optimized for server JVM, is used.
+ * Traditional (without sentinel) insertion sort,
+ * optimized for server VM, is used in case of
+ * the leftmost part.
*/
for (int i = left, j = i; i < right; j = ++i) {
char ai = a[i + 1];
@@ -1214,12 +1222,54 @@
}
a[j + 1] = ai;
}
+ } else {
+ /*
+ * Skip the longest ascending sequence.
+ */
+ do {
+ if (left++ >= right) {
+ return;
+ }
+ } while (a[left - 1] <= a[left]);
+
+ /*
+ * Every element from adjoining part plays the role
+ * of sentinel, therefore this allows us to avoid the
+ * left range check on each iteration. Moreover, we use
+ * the best improved algorithm, so called pair insertion
+ * sort, which is faster than traditional implementation
+ * in the context of Dual-Pivot Quicksort.
+ */
+ for (int k = left--; (left += 2) <= right; ) {
+ char a1, a2; k = left - 1;
+
+ if (a[k] < a[left]) {
+ a2 = a[k]; a1 = a[left];
+ } else {
+ a1 = a[k]; a2 = a[left];
+ }
+ while (a1 < a[--k]) {
+ a[k + 2] = a[k];
+ }
+ a[++k + 1] = a1;
+
+ while (a2 < a[--k]) {
+ a[k + 1] = a[k];
+ }
+ a[k + 1] = a2;
+ }
+ char last = a[right];
+
+ while (last < a[--right]) {
+ a[right + 1] = a[right];
+ }
+ a[right + 1] = last;
}
return;
}
// Inexpensive approximation of length / 7
- int seventh = (length >>> 3) + (length >>> 6) + 1;
+ int seventh = (length >> 3) + (length >> 6) + 1;
/*
* Sort five evenly spaced elements around (and including) the
@@ -1301,10 +1351,14 @@
* Pointer k is the first index of ?-part.
*/
outer:
- for (int k = less; k <= great; k++) {
+ for (int k = less - 1; ++k <= great; ) {
char ak = a[k];
if (ak < pivot1) { // Move a[k] to left part
a[k] = a[less];
+ /*
+ * Here and below we use "a[i] = b; i++;" instead
+ * of "a[i++] = b;" due to performance issue.
+ */
a[less] = ak;
less++;
} else if (ak > pivot2) { // Move a[k] to right part
@@ -1313,13 +1367,17 @@
break outer;
}
}
- if (a[great] < pivot1) {
+ if (a[great] < pivot1) { // a[great] <= pivot2
a[k] = a[less];
a[less] = a[great];
less++;
} else { // pivot1 <= a[great] <= pivot2
a[k] = a[great];
}
+ /*
+ * Here and below we use "a[i] = b; i--;" instead
+ * of "a[i--] = b;" due to performance issue.
+ */
a[great] = ak;
great--;
}
@@ -1334,7 +1392,7 @@
sort(a, great + 2, right, false);
/*
- * If center part is too large (comprises > 5/7 of the array),
+ * If center part is too large (comprises > 4/7 of the array),
* swap internal pivot values to ends.
*/
if (less < e1 && e5 < great) {
@@ -1368,7 +1426,7 @@
* Pointer k is the first index of ?-part.
*/
outer:
- for (int k = less; k <= great; k++) {
+ for (int k = less - 1; ++k <= great; ) {
char ak = a[k];
if (ak == pivot1) { // Move a[k] to left part
a[k] = a[less];
@@ -1380,7 +1438,7 @@
break outer;
}
}
- if (a[great] == pivot1) {
+ if (a[great] == pivot1) { // a[great] < pivot2
a[k] = a[less];
/*
* Even though a[great] equals to pivot1, the
@@ -1406,7 +1464,7 @@
} else { // Pivots are equal
/*
- * Partition degenerates to the traditional 3-way
+ * Partitioning degenerates to the traditional 3-way
* (or "Dutch National Flag") schema:
*
* left part center part right part
@@ -1425,28 +1483,20 @@
*
* Pointer k is the first index of ?-part.
*/
- for (int k = left; k <= great; k++) {
+ for (int k = less; k <= great; ++k) {
if (a[k] == pivot1) {
continue;
}
char ak = a[k];
-
if (ak < pivot1) { // Move a[k] to left part
a[k] = a[less];
a[less] = ak;
less++;
} else { // a[k] > pivot1 - Move a[k] to right part
- /*
- * We know that pivot1 == a[e3] == pivot2. Thus, we know
- * that great will still be >= k when the following loop
- * terminates, even though we don't test for it explicitly.
- * In other words, a[e3] acts as a sentinel for great.
- */
while (a[great] > pivot1) {
- // assert great > k;
great--;
}
- if (a[great] < pivot1) {
+ if (a[great] < pivot1) { // a[great] <= pivot1
a[k] = a[less];
a[less] = a[great];
less++;
@@ -1466,442 +1516,90 @@
}
}
- // Sort left and right parts recursively
+ /*
+ * Sort left and right parts recursively.
+ * All elements from center part are equal
+ * and, therefore, already sorted.
+ */
sort(a, left, less - 1, leftmost);
sort(a, great + 1, right, false);
}
}
- /**
- * Sorts the specified array into ascending numerical order.
- *
- * @param a the array to be sorted
- */
- public static void sort(byte[] a) {
- if (a.length > COUNTING_SORT_THRESHOLD_FOR_BYTE) {
- countingSort(a, 0, a.length - 1);
- } else {
- sort(a, 0, a.length - 1, true);
- }
- }
-
- /**
- * Sorts the specified range of the array into ascending order. The range
- * to be sorted extends from the index {@code fromIndex}, inclusive, to
- * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
- * the range to be sorted is empty (and the call is a no-op).
- *
- * @param a the array to be sorted
- * @param fromIndex the index of the first element, inclusive, to be sorted
- * @param toIndex the index of the last element, exclusive, to be sorted
- * @throws IllegalArgumentException if {@code fromIndex > toIndex}
- * @throws ArrayIndexOutOfBoundsException
- * if {@code fromIndex < 0} or {@code toIndex > a.length}
- */
- public static void sort(byte[] a, int fromIndex, int toIndex) {
- rangeCheck(a.length, fromIndex, toIndex);
-
- if (toIndex - fromIndex > COUNTING_SORT_THRESHOLD_FOR_BYTE) {
- countingSort(a, fromIndex, toIndex - 1);
- } else {
- sort(a, fromIndex, toIndex - 1, true);
- }
- }
-
/** The number of distinct byte values. */
private static final int NUM_BYTE_VALUES = 1 << 8;
/**
- * Sorts the specified range of the array by counting sort.
+ * Sorts the specified array.
+ *
+ * @param a the array to be sorted
+ */
+ public static void sort(byte[] a) {
+ sort(a, 0, a.length - 1);
+ }
+
+ /**
+ * Sorts the specified range of the array.
*
* @param a the array to be sorted
* @param left the index of the first element, inclusive, to be sorted
* @param right the index of the last element, inclusive, to be sorted
*/
- private static void countingSort(byte[] a, int left, int right) {
- int[] count = new int[NUM_BYTE_VALUES];
+ public static void sort(byte[] a, int left, int right) {
+ // Use counting sort on large arrays
+ if (right - left > COUNTING_SORT_THRESHOLD_FOR_BYTE) {
+ int[] count = new int[NUM_BYTE_VALUES];
+
+ for (int i = left - 1; ++i <= right; ) {
+ count[a[i] - Byte.MIN_VALUE]++;
+ }
+ for (int i = NUM_BYTE_VALUES, k = right + 1; k > left; ) {
+ while (count[--i] == 0);
+ byte value = (byte) (i + Byte.MIN_VALUE);
+ int s = count[i];
- for (int i = left; i <= right; i++) {
- count[a[i] - Byte.MIN_VALUE]++;
- }
- for (int i = NUM_BYTE_VALUES - 1, k = right; k >= left; i--) {
- while (count[i] == 0) {
- i--;
+ do {
+ a[--k] = value;
+ } while (--s > 0);
}
- byte value = (byte) (i + Byte.MIN_VALUE);
- int s = count[i];
-
- do {
- a[k--] = value;
- } while (--s > 0);
+ } else { // Use insertion sort on small arrays
+ for (int i = left, j = i; i < right; j = ++i) {
+ byte ai = a[i + 1];
+ while (ai < a[j]) {
+ a[j + 1] = a[j];
+ if (j-- == left) {
+ break;
+ }
+ }
+ a[j + 1] = ai;
+ }
}
}
/**
- * Sorts the specified range of the array into ascending order by the
- * Dual-Pivot Quicksort algorithm. This method differs from the public
- * {@code sort} method in that the {@code right} index is inclusive,
- * it does no range checking on {@code left} or {@code right}, and has
- * boolean flag whether insertion sort with sentinel is used or not.
- *
- * @param a the array to be sorted
- * @param left the index of the first element, inclusive, to be sorted
- * @param right the index of the last element, inclusive, to be sorted
- * @param leftmost indicates if the part is the most left in the range
- */
- private static void sort(byte[] a, int left, int right, boolean leftmost) {
- int length = right - left + 1;
-
- // Use insertion sort on tiny arrays
- if (length < INSERTION_SORT_THRESHOLD) {
- if (!leftmost) {
- /*
- * Every element in adjoining part plays the role
- * of sentinel, therefore this allows us to avoid
- * the j >= left check on each iteration.
- */
- for (int j, i = left + 1; i <= right; i++) {
- byte ai = a[i];
- for (j = i - 1; ai < a[j]; j--) {
- // assert j >= left;
- a[j + 1] = a[j];
- }
- a[j + 1] = ai;
- }
- } else {
- /*
- * For case of leftmost part traditional (without a sentinel)
- * insertion sort, optimized for server JVM, is used.
- */
- for (int i = left, j = i; i < right; j = ++i) {
- byte ai = a[i + 1];
- while (ai < a[j]) {
- a[j + 1] = a[j];
- if (j-- == left) {
- break;
- }
- }
- a[j + 1] = ai;
- }
- }
- return;
- }
-
- // Inexpensive approximation of length / 7
- int seventh = (length >>> 3) + (length >>> 6) + 1;
-
- /*
- * Sort five evenly spaced elements around (and including) the
- * center element in the range. These elements will be used for
- * pivot selection as described below. The choice for spacing
- * these elements was empirically determined to work well on
- * a wide variety of inputs.
- */
- int e3 = (left + right) >>> 1; // The midpoint
- int e2 = e3 - seventh;
- int e1 = e2 - seventh;
- int e4 = e3 + seventh;
- int e5 = e4 + seventh;
-
- // Sort these elements using insertion sort
- if (a[e2] < a[e1]) { byte t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
-
- if (a[e3] < a[e2]) { byte t = a[e3]; a[e3] = a[e2]; a[e2] = t;
- if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
- }
- if (a[e4] < a[e3]) { byte t = a[e4]; a[e4] = a[e3]; a[e3] = t;
- if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
- if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
- }
- }
- if (a[e5] < a[e4]) { byte t = a[e5]; a[e5] = a[e4]; a[e4] = t;
- if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
- if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
- if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
- }
- }
- }
-
- /*
- * Use the second and fourth of the five sorted elements as pivots.
- * These values are inexpensive approximations of the first and
- * second terciles of the array. Note that pivot1 <= pivot2.
- */
- byte pivot1 = a[e2];
- byte pivot2 = a[e4];
-
- // Pointers
- int less = left; // The index of the first element of center part
- int great = right; // The index before the first element of right part
-
- if (pivot1 != pivot2) {
- /*
- * The first and the last elements to be sorted are moved to the
- * locations formerly occupied by the pivots. When partitioning
- * is complete, the pivots are swapped back into their final
- * positions, and excluded from subsequent sorting.
- */
- a[e2] = a[left];
- a[e4] = a[right];
-
- /*
- * Skip elements, which are less or greater than pivot values.
- */
- while (a[++less] < pivot1);
- while (a[--great] > pivot2);
-
- /*
- * Partitioning:
- *
- * left part center part right part
- * +--------------------------------------------------------------+
- * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
- * +--------------------------------------------------------------+
- * ^ ^ ^
- * | | |
- * less k great
- *
- * Invariants:
- *
- * all in (left, less) < pivot1
- * pivot1 <= all in [less, k) <= pivot2
- * all in (great, right) > pivot2
- *
- * Pointer k is the first index of ?-part.
- */
- outer:
- for (int k = less; k <= great; k++) {
- byte ak = a[k];
- if (ak < pivot1) { // Move a[k] to left part
- a[k] = a[less];
- a[less] = ak;
- less++;
- } else if (ak > pivot2) { // Move a[k] to right part
- while (a[great] > pivot2) {
- if (great-- == k) {
- break outer;
- }
- }
- if (a[great] < pivot1) {
- a[k] = a[less];
- a[less] = a[great];
- less++;
- } else { // pivot1 <= a[great] <= pivot2
- a[k] = a[great];
- }
- a[great] = ak;
- great--;
- }
- }
-
- // Swap pivots into their final positions
- a[left] = a[less - 1]; a[less - 1] = pivot1;
- a[right] = a[great + 1]; a[great + 1] = pivot2;
-
- // Sort left and right parts recursively, excluding known pivots
- sort(a, left, less - 2, leftmost);
- sort(a, great + 2, right, false);
-
- /*
- * If center part is too large (comprises > 5/7 of the array),
- * swap internal pivot values to ends.
- */
- if (less < e1 && e5 < great) {
- /*
- * Skip elements, which are equal to pivot values.
- */
- while (a[less] == pivot1) {
- less++;
- }
- while (a[great] == pivot2) {
- great--;
- }
-
- /*
- * Partitioning:
- *
- * left part center part right part
- * +----------------------------------------------------------+
- * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
- * +----------------------------------------------------------+
- * ^ ^ ^
- * | | |
- * less k great
- *
- * Invariants:
- *
- * all in (*, less) == pivot1
- * pivot1 < all in [less, k) < pivot2
- * all in (great, *) == pivot2
- *
- * Pointer k is the first index of ?-part.
- */
- outer:
- for (int k = less; k <= great; k++) {
- byte ak = a[k];
- if (ak == pivot1) { // Move a[k] to left part
- a[k] = a[less];
- a[less] = ak;
- less++;
- } else if (ak == pivot2) { // Move a[k] to right part
- while (a[great] == pivot2) {
- if (great-- == k) {
- break outer;
- }
- }
- if (a[great] == pivot1) {
- a[k] = a[less];
- /*
- * Even though a[great] equals to pivot1, the
- * assignment a[less] = pivot1 may be incorrect,
- * if a[great] and pivot1 are floating-point zeros
- * of different signs. Therefore in float and
- * double sorting methods we have to use more
- * accurate assignment a[less] = a[great].
- */
- a[less] = pivot1;
- less++;
- } else { // pivot1 < a[great] < pivot2
- a[k] = a[great];
- }
- a[great] = ak;
- great--;
- }
- }
- }
-
- // Sort center part recursively
- sort(a, less, great, false);
-
- } else { // Pivots are equal
- /*
- * Partition degenerates to the traditional 3-way
- * (or "Dutch National Flag") schema:
- *
- * left part center part right part
- * +-------------------------------------------------+
- * | < pivot | == pivot | ? | > pivot |
- * +-------------------------------------------------+
- * ^ ^ ^
- * | | |
- * less k great
- *
- * Invariants:
- *
- * all in (left, less) < pivot
- * all in [less, k) == pivot
- * all in (great, right) > pivot
- *
- * Pointer k is the first index of ?-part.
- */
- for (int k = left; k <= great; k++) {
- if (a[k] == pivot1) {
- continue;
- }
- byte ak = a[k];
-
- if (ak < pivot1) { // Move a[k] to left part
- a[k] = a[less];
- a[less] = ak;
- less++;
- } else { // a[k] > pivot1 - Move a[k] to right part
- /*
- * We know that pivot1 == a[e3] == pivot2. Thus, we know
- * that great will still be >= k when the following loop
- * terminates, even though we don't test for it explicitly.
- * In other words, a[e3] acts as a sentinel for great.
- */
- while (a[great] > pivot1) {
- // assert great > k;
- great--;
- }
- if (a[great] < pivot1) {
- a[k] = a[less];
- a[less] = a[great];
- less++;
- } else { // a[great] == pivot1
- /*
- * Even though a[great] equals to pivot1, the
- * assignment a[k] = pivot1 may be incorrect,
- * if a[great] and pivot1 are floating-point
- * zeros of different signs. Therefore in float
- * and double sorting methods we have to use
- * more accurate assignment a[k] = a[great].
- */
- a[k] = pivot1;
- }
- a[great] = ak;
- great--;
- }
- }
-
- // Sort left and right parts recursively
- sort(a, left, less - 1, leftmost);
- sort(a, great + 1, right, false);
- }
- }
-
- /**
- * Sorts the specified array into ascending numerical order.
- *
- * The {@code <} relation does not provide a total order on all float - * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} - * value compares neither less than, greater than, nor equal to any value, - * even itself. This method uses the total order imposed by the method - * {@link Float#compareTo}: {@code -0.0f} is treated as less than value - * {@code 0.0f} and {@code Float.NaN} is considered greater than any - * other value and all {@code Float.NaN} values are considered equal. + * Sorts the specified array. * * @param a the array to be sorted */ public static void sort(float[] a) { - sortNegZeroAndNaN(a, 0, a.length - 1); + sort(a, 0, a.length - 1); } /** - * Sorts the specified range of the array into ascending order. The range - * to be sorted extends from the index {@code fromIndex}, inclusive, to - * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, - * the range to be sorted is empty (and the call is a no-op). - * - *
The {@code <} relation does not provide a total order on all float - * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} - * value compares neither less than, greater than, nor equal to any value, - * even itself. This method uses the total order imposed by the method - * {@link Float#compareTo}: {@code -0.0f} is treated as less than value - * {@code 0.0f} and {@code Float.NaN} is considered greater than any - * other value and all {@code Float.NaN} values are considered equal. - * - * @param a the array to be sorted - * @param fromIndex the index of the first element, inclusive, to be sorted - * @param toIndex the index of the last element, exclusive, to be sorted - * @throws IllegalArgumentException if {@code fromIndex > toIndex} - * @throws ArrayIndexOutOfBoundsException - * if {@code fromIndex < 0} or {@code toIndex > a.length} - */ - public static void sort(float[] a, int fromIndex, int toIndex) { - rangeCheck(a.length, fromIndex, toIndex); - sortNegZeroAndNaN(a, fromIndex, toIndex - 1); - } - - /** - * Sorts the specified range of the array into ascending order. The - * sort is done in three phases to avoid expensive comparisons in the - * inner loop. The comparisons would be expensive due to anomalies - * associated with negative zero {@code -0.0f} and {@code Float.NaN}. + * Sorts the specified range of the array. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted */ - private static void sortNegZeroAndNaN(float[] a, int left, int right) { + public static void sort(float[] a, int left, int right) { /* * Phase 1: Move NaNs to the end of the array. */ while (left <= right && Float.isNaN(a[right])) { right--; } - for (int k = right - 1; k >= left; k--) { + for (int k = right; --k >= left; ) { float ak = a[k]; if (ak != ak) { // a[k] is NaN a[k] = a[right]; @@ -1921,7 +1619,7 @@ int hi = right; /* - * Search first zero, or first positive, or last negative element. + * Find the first zero, or first positive, or last negative element. */ while (left < hi) { int middle = (left + hi) >>> 1; @@ -1946,12 +1644,12 @@ * * Partitioning: * - * +---------------------------------------------------+ - * | < 0.0 | -0.0 | 0.0 | ? ( >= 0.0 ) | - * +---------------------------------------------------+ - * ^ ^ ^ - * | | | - * left p k + * +----------------------------------------------------+ + * | < 0.0 | -0.0 | 0.0 | ? ( >= 0.0 ) | + * +----------------------------------------------------+ + * ^ ^ ^ + * | | | + * left p k * * Invariants: * @@ -1962,53 +1660,36 @@ * * Pointer k is the first index of ?-part. */ - for (int k = left + 1, p = left; k <= right; k++) { + for (int k = left, p = left - 1; ++k <= right; ) { float ak = a[k]; if (ak != 0.0f) { break; } if (Float.floatToRawIntBits(ak) < 0) { // ak is -0.0f a[k] = 0.0f; - a[p++] = -0.0f; + a[++p] = -0.0f; } } } /** - * Sorts the specified range of the array into ascending order by the - * Dual-Pivot Quicksort algorithm. This method differs from the public - * {@code sort} method in that the {@code right} index is inclusive, - * it does no range checking on {@code left} or {@code right}, and has - * boolean flag whether insertion sort with sentinel is used or not. + * Sorts the specified range of the array by Dual-Pivot Quicksort. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted - * @param leftmost indicates if the part is the most left in the range + * @param leftmost indicates if this part is the leftmost in the range */ private static void sort(float[] a, int left, int right,boolean leftmost) { int length = right - left + 1; - // Use insertion sort on tiny arrays + // Use insertion sort on small arrays if (length < INSERTION_SORT_THRESHOLD) { - if (!leftmost) { + if (leftmost) { /* - * Every element in adjoining part plays the role - * of sentinel, therefore this allows us to avoid - * the j >= left check on each iteration. - */ - for (int j, i = left + 1; i <= right; i++) { - float ai = a[i]; - for (j = i - 1; ai < a[j]; j--) { - // assert j >= left; - a[j + 1] = a[j]; - } - a[j + 1] = ai; - } - } else { - /* - * For case of leftmost part traditional (without a sentinel) - * insertion sort, optimized for server JVM, is used. + * Traditional (without sentinel) insertion sort, + * optimized for server VM, is used in case of + * the leftmost part. */ for (int i = left, j = i; i < right; j = ++i) { float ai = a[i + 1]; @@ -2020,12 +1701,54 @@ } a[j + 1] = ai; } + } else { + /* + * Skip the longest ascending sequence. + */ + do { + if (left++ >= right) { + return; + } + } while (a[left - 1] <= a[left]); + + /* + * Every element from adjoining part plays the role + * of sentinel, therefore this allows us to avoid the + * left range check on each iteration. Moreover, we use + * the best improved algorithm, so called pair insertion + * sort, which is faster than traditional implementation + * in the context of Dual-Pivot Quicksort. + */ + for (int k = left--; (left += 2) <= right; ) { + float a1, a2; k = left - 1; + + if (a[k] < a[left]) { + a2 = a[k]; a1 = a[left]; + } else { + a1 = a[k]; a2 = a[left]; + } + while (a1 < a[--k]) { + a[k + 2] = a[k]; + } + a[++k + 1] = a1; + + while (a2 < a[--k]) { + a[k + 1] = a[k]; + } + a[k + 1] = a2; + } + float last = a[right]; + + while (last < a[--right]) { + a[right + 1] = a[right]; + } + a[right + 1] = last; } return; } // Inexpensive approximation of length / 7 - int seventh = (length >>> 3) + (length >>> 6) + 1; + int seventh = (length >> 3) + (length >> 6) + 1; /* * Sort five evenly spaced elements around (and including) the @@ -2107,10 +1830,14 @@ * Pointer k is the first index of ?-part. */ outer: - for (int k = less; k <= great; k++) { + for (int k = less - 1; ++k <= great; ) { float ak = a[k]; if (ak < pivot1) { // Move a[k] to left part a[k] = a[less]; + /* + * Here and below we use "a[i] = b; i++;" instead + * of "a[i++] = b;" due to performance issue. + */ a[less] = ak; less++; } else if (ak > pivot2) { // Move a[k] to right part @@ -2119,13 +1846,17 @@ break outer; } } - if (a[great] < pivot1) { + if (a[great] < pivot1) { // a[great] <= pivot2 a[k] = a[less]; a[less] = a[great]; less++; } else { // pivot1 <= a[great] <= pivot2 a[k] = a[great]; } + /* + * Here and below we use "a[i] = b; i--;" instead + * of "a[i--] = b;" due to performance issue. + */ a[great] = ak; great--; } @@ -2140,7 +1871,7 @@ sort(a, great + 2, right, false); /* - * If center part is too large (comprises > 5/7 of the array), + * If center part is too large (comprises > 4/7 of the array), * swap internal pivot values to ends. */ if (less < e1 && e5 < great) { @@ -2174,7 +1905,7 @@ * Pointer k is the first index of ?-part. */ outer: - for (int k = less; k <= great; k++) { + for (int k = less - 1; ++k <= great; ) { float ak = a[k]; if (ak == pivot1) { // Move a[k] to left part a[k] = a[less]; @@ -2186,7 +1917,7 @@ break outer; } } - if (a[great] == pivot1) { + if (a[great] == pivot1) { // a[great] < pivot2 a[k] = a[less]; /* * Even though a[great] equals to pivot1, the @@ -2212,7 +1943,7 @@ } else { // Pivots are equal /* - * Partition degenerates to the traditional 3-way + * Partitioning degenerates to the traditional 3-way * (or "Dutch National Flag") schema: * * left part center part right part @@ -2231,28 +1962,20 @@ * * Pointer k is the first index of ?-part. */ - for (int k = left; k <= great; k++) { + for (int k = less; k <= great; ++k) { if (a[k] == pivot1) { continue; } float ak = a[k]; - if (ak < pivot1) { // Move a[k] to left part a[k] = a[less]; a[less] = ak; less++; } else { // a[k] > pivot1 - Move a[k] to right part - /* - * We know that pivot1 == a[e3] == pivot2. Thus, we know - * that great will still be >= k when the following loop - * terminates, even though we don't test for it explicitly. - * In other words, a[e3] acts as a sentinel for great. - */ while (a[great] > pivot1) { - // assert great > k; great--; } - if (a[great] < pivot1) { + if (a[great] < pivot1) { // a[great] <= pivot1 a[k] = a[less]; a[less] = a[great]; less++; @@ -2272,73 +1995,40 @@ } } - // Sort left and right parts recursively + /* + * Sort left and right parts recursively. + * All elements from center part are equal + * and, therefore, already sorted. + */ sort(a, left, less - 1, leftmost); sort(a, great + 1, right, false); } } /** - * Sorts the specified array into ascending numerical order. - * - *
The {@code <} relation does not provide a total order on all double - * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} - * value compares neither less than, greater than, nor equal to any value, - * even itself. This method uses the total order imposed by the method - * {@link Double#compareTo}: {@code -0.0d} is treated as less than value - * {@code 0.0d} and {@code Double.NaN} is considered greater than any - * other value and all {@code Double.NaN} values are considered equal. + * Sorts the specified array. * * @param a the array to be sorted */ public static void sort(double[] a) { - sortNegZeroAndNaN(a, 0, a.length - 1); + sort(a, 0, a.length - 1); } /** - * Sorts the specified range of the array into ascending order. The range - * to be sorted extends from the index {@code fromIndex}, inclusive, to - * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, - * the range to be sorted is empty (and the call is a no-op). - * - *
The {@code <} relation does not provide a total order on all double
- * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
- * value compares neither less than, greater than, nor equal to any value,
- * even itself. This method uses the total order imposed by the method
- * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
- * {@code 0.0d} and {@code Double.NaN} is considered greater than any
- * other value and all {@code Double.NaN} values are considered equal.
- *
- * @param a the array to be sorted
- * @param fromIndex the index of the first element, inclusive, to be sorted
- * @param toIndex the index of the last element, exclusive, to be sorted
- * @throws IllegalArgumentException if {@code fromIndex > toIndex}
- * @throws ArrayIndexOutOfBoundsException
- * if {@code fromIndex < 0} or {@code toIndex > a.length}
- */
- public static void sort(double[] a, int fromIndex, int toIndex) {
- rangeCheck(a.length, fromIndex, toIndex);
- sortNegZeroAndNaN(a, fromIndex, toIndex - 1);
- }
-
- /**
- * Sorts the specified range of the array into ascending order. The
- * sort is done in three phases to avoid expensive comparisons in the
- * inner loop. The comparisons would be expensive due to anomalies
- * associated with negative zero {@code -0.0d} and {@code Double.NaN}.
+ * Sorts the specified range of the array.
*
* @param a the array to be sorted
* @param left the index of the first element, inclusive, to be sorted
* @param right the index of the last element, inclusive, to be sorted
*/
- private static void sortNegZeroAndNaN(double[] a, int left, int right) {
+ public static void sort(double[] a, int left, int right) {
/*
* Phase 1: Move NaNs to the end of the array.
*/
while (left <= right && Double.isNaN(a[right])) {
right--;
}
- for (int k = right - 1; k >= left; k--) {
+ for (int k = right; --k >= left; ) {
double ak = a[k];
if (ak != ak) { // a[k] is NaN
a[k] = a[right];
@@ -2358,7 +2048,7 @@
int hi = right;
/*
- * Search first zero, or first positive, or last negative element.
+ * Find the first zero, or first positive, or last negative element.
*/
while (left < hi) {
int middle = (left + hi) >>> 1;
@@ -2383,12 +2073,12 @@
*
* Partitioning:
*
- * +---------------------------------------------------+
- * | < 0.0 | -0.0 | 0.0 | ? ( >= 0.0 ) |
- * +---------------------------------------------------+
- * ^ ^ ^
- * | | |
- * left p k
+ * +----------------------------------------------------+
+ * | < 0.0 | -0.0 | 0.0 | ? ( >= 0.0 ) |
+ * +----------------------------------------------------+
+ * ^ ^ ^
+ * | | |
+ * left p k
*
* Invariants:
*
@@ -2399,53 +2089,36 @@
*
* Pointer k is the first index of ?-part.
*/
- for (int k = left + 1, p = left; k <= right; k++) {
+ for (int k = left, p = left - 1; ++k <= right; ) {
double ak = a[k];
if (ak != 0.0d) {
break;
}
if (Double.doubleToRawLongBits(ak) < 0) { // ak is -0.0d
a[k] = 0.0d;
- a[p++] = -0.0d;
+ a[++p] = -0.0d;
}
}
}
/**
- * Sorts the specified range of the array into ascending order by the
- * Dual-Pivot Quicksort algorithm. This method differs from the public
- * {@code sort} method in that the {@code right} index is inclusive,
- * it does no range checking on {@code left} or {@code right}, and has
- * boolean flag whether insertion sort with sentinel is used or not.
+ * Sorts the specified range of the array by Dual-Pivot Quicksort.
*
* @param a the array to be sorted
* @param left the index of the first element, inclusive, to be sorted
* @param right the index of the last element, inclusive, to be sorted
- * @param leftmost indicates if the part is the most left in the range
+ * @param leftmost indicates if this part is the leftmost in the range
*/
private static void sort(double[] a, int left,int right,boolean leftmost) {
int length = right - left + 1;
- // Use insertion sort on tiny arrays
+ // Use insertion sort on small arrays
if (length < INSERTION_SORT_THRESHOLD) {
- if (!leftmost) {
+ if (leftmost) {
/*
- * Every element in adjoining part plays the role
- * of sentinel, therefore this allows us to avoid
- * the j >= left check on each iteration.
- */
- for (int j, i = left + 1; i <= right; i++) {
- double ai = a[i];
- for (j = i - 1; ai < a[j]; j--) {
- // assert j >= left;
- a[j + 1] = a[j];
- }
- a[j + 1] = ai;
- }
- } else {
- /*
- * For case of leftmost part traditional (without a sentinel)
- * insertion sort, optimized for server JVM, is used.
+ * Traditional (without sentinel) insertion sort,
+ * optimized for server VM, is used in case of
+ * the leftmost part.
*/
for (int i = left, j = i; i < right; j = ++i) {
double ai = a[i + 1];
@@ -2457,12 +2130,54 @@
}
a[j + 1] = ai;
}
+ } else {
+ /*
+ * Skip the longest ascending sequence.
+ */
+ do {
+ if (left++ >= right) {
+ return;
+ }
+ } while (a[left - 1] <= a[left]);
+
+ /*
+ * Every element from adjoining part plays the role
+ * of sentinel, therefore this allows us to avoid the
+ * left range check on each iteration. Moreover, we use
+ * the best improved algorithm, so called pair insertion
+ * sort, which is faster than traditional implementation
+ * in the context of Dual-Pivot Quicksort.
+ */
+ for (int k = left--; (left += 2) <= right; ) {
+ double a1, a2; k = left - 1;
+
+ if (a[k] < a[left]) {
+ a2 = a[k]; a1 = a[left];
+ } else {
+ a1 = a[k]; a2 = a[left];
+ }
+ while (a1 < a[--k]) {
+ a[k + 2] = a[k];
+ }
+ a[++k + 1] = a1;
+
+ while (a2 < a[--k]) {
+ a[k + 1] = a[k];
+ }
+ a[k + 1] = a2;
+ }
+ double last = a[right];
+
+ while (last < a[--right]) {
+ a[right + 1] = a[right];
+ }
+ a[right + 1] = last;
}
return;
}
// Inexpensive approximation of length / 7
- int seventh = (length >>> 3) + (length >>> 6) + 1;
+ int seventh = (length >> 3) + (length >> 6) + 1;
/*
* Sort five evenly spaced elements around (and including) the
@@ -2544,10 +2259,14 @@
* Pointer k is the first index of ?-part.
*/
outer:
- for (int k = less; k <= great; k++) {
+ for (int k = less - 1; ++k <= great; ) {
double ak = a[k];
if (ak < pivot1) { // Move a[k] to left part
a[k] = a[less];
+ /*
+ * Here and below we use "a[i] = b; i++;" instead
+ * of "a[i++] = b;" due to performance issue.
+ */
a[less] = ak;
less++;
} else if (ak > pivot2) { // Move a[k] to right part
@@ -2556,13 +2275,17 @@
break outer;
}
}
- if (a[great] < pivot1) {
+ if (a[great] < pivot1) { // a[great] <= pivot2
a[k] = a[less];
a[less] = a[great];
less++;
} else { // pivot1 <= a[great] <= pivot2
a[k] = a[great];
}
+ /*
+ * Here and below we use "a[i] = b; i--;" instead
+ * of "a[i--] = b;" due to performance issue.
+ */
a[great] = ak;
great--;
}
@@ -2577,7 +2300,7 @@
sort(a, great + 2, right, false);
/*
- * If center part is too large (comprises > 5/7 of the array),
+ * If center part is too large (comprises > 4/7 of the array),
* swap internal pivot values to ends.
*/
if (less < e1 && e5 < great) {
@@ -2611,7 +2334,7 @@
* Pointer k is the first index of ?-part.
*/
outer:
- for (int k = less; k <= great; k++) {
+ for (int k = less - 1; ++k <= great; ) {
double ak = a[k];
if (ak == pivot1) { // Move a[k] to left part
a[k] = a[less];
@@ -2623,7 +2346,7 @@
break outer;
}
}
- if (a[great] == pivot1) {
+ if (a[great] == pivot1) { // a[great] < pivot2
a[k] = a[less];
/*
* Even though a[great] equals to pivot1, the
@@ -2649,7 +2372,7 @@
} else { // Pivots are equal
/*
- * Partition degenerates to the traditional 3-way
+ * Partitioning degenerates to the traditional 3-way
* (or "Dutch National Flag") schema:
*
* left part center part right part
@@ -2668,28 +2391,20 @@
*
* Pointer k is the first index of ?-part.
*/
- for (int k = left; k <= great; k++) {
+ for (int k = less; k <= great; ++k) {
if (a[k] == pivot1) {
continue;
}
double ak = a[k];
-
if (ak < pivot1) { // Move a[k] to left part
a[k] = a[less];
a[less] = ak;
less++;
} else { // a[k] > pivot1 - Move a[k] to right part
- /*
- * We know that pivot1 == a[e3] == pivot2. Thus, we know
- * that great will still be >= k when the following loop
- * terminates, even though we don't test for it explicitly.
- * In other words, a[e3] acts as a sentinel for great.
- */
while (a[great] > pivot1) {
- // assert great > k;
great--;
}
- if (a[great] < pivot1) {
+ if (a[great] < pivot1) { // a[great] <= pivot1
a[k] = a[less];
a[less] = a[great];
less++;
@@ -2709,26 +2424,13 @@
}
}
- // Sort left and right parts recursively
+ /*
+ * Sort left and right parts recursively.
+ * All elements from center part are equal
+ * and, therefore, already sorted.
+ */
sort(a, left, less - 1, leftmost);
sort(a, great + 1, right, false);
}
}
-
- /**
- * Checks that {@code fromIndex} and {@code toIndex} are in the range,
- * otherwise throws an appropriate exception.
- */
- private static void rangeCheck(int length, int fromIndex, int toIndex) {
- if (fromIndex > toIndex) {
- throw new IllegalArgumentException(
- "fromIndex: " + fromIndex + " > toIndex: " + toIndex);
- }
- if (fromIndex < 0) {
- throw new ArrayIndexOutOfBoundsException(fromIndex);
- }
- if (toIndex > length) {
- throw new ArrayIndexOutOfBoundsException(toIndex);
- }
- }
}
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/java/util/Locale.java
--- a/jdk/src/share/classes/java/util/Locale.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/java/util/Locale.java Tue Oct 26 10:57:35 2010 -0700
@@ -1715,6 +1715,7 @@
OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
String languageName = getDisplayLanguage(inLocale);
+ String scriptName = getDisplayScript(inLocale);
String countryName = getDisplayCountry(inLocale);
String[] variantNames = getDisplayVariantArray(bundle, inLocale);
@@ -1735,25 +1736,40 @@
String mainName = null;
String[] qualifierNames = null;
- // The main name is the language, or if there is no language, the country.
- // If there is neither language nor country (an anomalous situation) then
- // the display name is simply the variant's display name.
- if (languageName.length() != 0) {
- mainName = languageName;
- if (countryName.length() != 0) {
- qualifierNames = new String[variantNames.length + 1];
- System.arraycopy(variantNames, 0, qualifierNames, 1, variantNames.length);
- qualifierNames[0] = countryName;
+ // The main name is the language, or if there is no language, the script,
+ // then if no script, the country. If there is no language/script/country
+ // (an anomalous situation) then the display name is simply the variant's
+ // display name.
+ if (languageName.length() == 0 && scriptName.length() == 0 && countryName.length() == 0) {
+ if (variantNames.length == 0) {
+ return "";
+ } else {
+ return formatList(variantNames, listPattern, listCompositionPattern);
}
- else qualifierNames = variantNames;
+ }
+ ArrayList This method is equivalent to:
+ * This method is equivalent to:
+ * This method is equivalent to:
+ * When looking up a time zone name, the {@linkplain
+ * ResourceBundle.Control#getCandidateLocales(String,Locale) default
+ * Properties can be used to specify the default mixer
* for specific line types.
* Both system properties and a properties file are considered.
- * In the Sun reference implementation, the properties file is
+ * In the Oracle reference implementation, the properties file is
* "lib/sound.properties" in the JRE
* directory. If a property exists both as a system property and in the
* properties file, the system property takes precedence. If none is
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/javax/sql/rowset/RowSetProvider.java
--- a/jdk/src/share/classes/javax/sql/rowset/RowSetProvider.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/javax/sql/rowset/RowSetProvider.java Tue Oct 26 10:57:35 2010 -0700
@@ -28,6 +28,7 @@
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.SQLException;
+import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
/**
@@ -255,13 +256,19 @@
* Use the ServiceLoader mechanism to load the default RowSetFactory
* @return default RowSetFactory Implementation
*/
- static private RowSetFactory loadViaServiceLoader() {
+ static private RowSetFactory loadViaServiceLoader() throws SQLException {
RowSetFactory theFactory = null;
- trace("***in loadViaServiceLoader()");
- for (RowSetFactory factory : ServiceLoader.load(javax.sql.rowset.RowSetFactory.class)) {
- trace(" Loading done by the java.util.ServiceLoader :" + factory.getClass().getName());
- theFactory = factory;
- break;
+ try {
+ trace("***in loadViaServiceLoader():");
+ for (RowSetFactory factory : ServiceLoader.load(javax.sql.rowset.RowSetFactory.class)) {
+ trace(" Loading done by the java.util.ServiceLoader :" + factory.getClass().getName());
+ theFactory = factory;
+ break;
+ }
+ } catch (ServiceConfigurationError e) {
+ throw new SQLException(
+ "RowSetFactory: Error locating RowSetFactory using Service "
+ + "Loader API: " + e, e);
}
return theFactory;
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/javax/sql/rowset/spi/SyncFactory.java
--- a/jdk/src/share/classes/javax/sql/rowset/spi/SyncFactory.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/javax/sql/rowset/spi/SyncFactory.java Tue Oct 26 10:57:35 2010 -0700
@@ -652,7 +652,10 @@
* required
* @throws java.lang.SecurityException if a security manager exists and its
* {@code checkPermission} method denies calling {@code setLogger}
+ * @throws java.util.logging.LoggingPermission if a security manager exists and its
+ * {@code checkPermission} method denies calling {@code setLevel}
* @see SecurityManager#checkPermission
+ * @see LoggingPermission
*/
public static void setLogger(Logger logger, Level level) {
// singleton
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/javax/swing/JLayer.java
--- a/jdk/src/share/classes/javax/swing/JLayer.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/javax/swing/JLayer.java Tue Oct 26 10:57:35 2010 -0700
@@ -29,6 +29,7 @@
import javax.swing.plaf.LayerUI;
import javax.swing.border.Border;
+import javax.accessibility.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
@@ -149,7 +150,7 @@
*/
public final class JLayer
- * This is a JavaBeans bound property.
+ * This is a JavaBeans bound property.
*
* @param selectionForeground the
- * This is a JavaBeans bound property.
+ * This is a JavaBeans bound property.
*
* @param selectionBackground the Locale
.
+ * Deserializes this Locale
.
* @param in the ObjectInputStream
to read
* @throws IOException
* @throws ClassNotFoundException
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/java/util/Properties.java
--- a/jdk/src/share/classes/java/util/Properties.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/java/util/Properties.java Tue Oct 26 10:57:35 2010 -0700
@@ -705,7 +705,7 @@
* Strings
.
*/
@Deprecated
- public synchronized void save(OutputStream out, String comments) {
+ public void save(OutputStream out, String comments) {
try {
store(out, comments);
} catch (IOException e) {
@@ -890,7 +890,7 @@
* @see #loadFromXML(InputStream)
* @since 1.5
*/
- public synchronized void storeToXML(OutputStream os, String comment)
+ public void storeToXML(OutputStream os, String comment)
throws IOException
{
if (os == null)
@@ -929,8 +929,7 @@
* @see #loadFromXML(InputStream)
* @since 1.5
*/
- public synchronized void storeToXML(OutputStream os, String comment,
- String encoding)
+ public void storeToXML(OutputStream os, String comment, String encoding)
throws IOException
{
if (os == null)
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/java/util/TimeZone.java
--- a/jdk/src/share/classes/java/util/TimeZone.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/java/util/TimeZone.java Tue Oct 26 10:57:35 2010 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2010, Oracle and/or its affiliates. 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
@@ -302,60 +302,94 @@
}
/**
- * Returns a name of this time zone suitable for presentation to the user
- * in the default locale.
- * This method returns the long name, not including daylight savings.
- * If the display name is not available for the locale,
- * then this method returns a string in the
- * normalized custom ID format.
+ * Returns a long standard time name of this {@code TimeZone} suitable for
+ * presentation to the user in the default locale.
+ *
+ *
+ *
* @return the human-readable name of this time zone in the default locale.
* @since 1.2
+ * @see #getDisplayName(boolean, int, Locale)
+ * @see Locale#getDefault(Locale.Category)
+ * @see Locale.Category
*/
public final String getDisplayName() {
- return getDisplayName(false, LONG, Locale.getDefault(Locale.Category.DISPLAY));
+ return getDisplayName(false, LONG,
+ Locale.getDefault(Locale.Category.DISPLAY));
}
/**
- * Returns a name of this time zone suitable for presentation to the user
- * in the specified locale.
- * This method returns the long name, not including daylight savings.
- * If the display name is not available for the locale,
- * then this method returns a string in the
- * normalized custom ID format.
+ * Returns a long standard time name of this {@code TimeZone} suitable for
+ * presentation to the user in the specified {@code locale}.
+ *
+ *
+ * getDisplayName(false, {@link #LONG},
+ * Locale.getDefault({@link Locale.Category#DISPLAY}))
+ *
+ *
* @param locale the locale in which to supply the display name.
* @return the human-readable name of this time zone in the given locale.
+ * @exception NullPointerException if {@code locale} is {@code null}.
* @since 1.2
+ * @see #getDisplayName(boolean, int, Locale)
*/
public final String getDisplayName(Locale locale) {
return getDisplayName(false, LONG, locale);
}
/**
- * Returns a name of this time zone suitable for presentation to the user
- * in the default locale.
- * If the display name is not available for the locale, then this
- * method returns a string in the
- * normalized custom ID format.
- * @param daylight if true, return the daylight savings name.
- * @param style either
+ * getDisplayName(false, {@link #LONG}, locale)
+ *
LONG
or SHORT
+ * Returns a name in the specified {@code style} of this {@code TimeZone}
+ * suitable for presentation to the user in the default locale. If the
+ * specified {@code daylight} is {@code true}, a daylight saving time name
+ * is returned. Otherwise, a standard time name is returned.
+ *
+ *
+ *
+ * @param daylight if {@code true}, return the daylight saving time name.
+ * @param style either {@link #LONG} or {@link #SHORT}
* @return the human-readable name of this time zone in the default locale.
+ * @exception IllegalArgumentException if {@code style} is invalid.
* @since 1.2
+ * @see #getDisplayName(boolean, int, Locale)
+ * @see Locale#getDefault(Locale.Category)
+ * @see Locale.Category
*/
public final String getDisplayName(boolean daylight, int style) {
- return getDisplayName(daylight, style, Locale.getDefault(Locale.Category.DISPLAY));
+ return getDisplayName(daylight, style,
+ Locale.getDefault(Locale.Category.DISPLAY));
}
/**
- * Returns a name of this time zone suitable for presentation to the user
- * in the specified locale.
- * If the display name is not available for the locale,
- * then this method returns a string in the
- * normalized custom ID format.
- * @param daylight if true, return the daylight savings name.
- * @param style either
+ * getDisplayName(daylight, style,
+ * Locale.getDefault({@link Locale.Category#DISPLAY}))
+ *
LONG
or SHORT
+ * Returns a name in the specified {@code style} of this {@code TimeZone}
+ * suitable for presentation to the user in the specified {@code
+ * locale}. If the specified {@code daylight} is {@code true}, a daylight
+ * saving time name is returned. Otherwise, a standard time name is
+ * returned.
+ *
+ * Locale
search path of ResourceBundle
} derived
+ * from the specified {@code locale} is used. (No {@linkplain
+ * ResourceBundle.Control#getFallbackLocale(String,Locale) fallback
+ * Locale
} search is performed.) If a time zone name in any
+ * {@code Locale} of the search path, including {@link Locale#ROOT}, is
+ * found, the name is returned. Otherwise, a string in the
+ * normalized custom ID format is returned.
+ *
+ * @param daylight if {@code true}, return the daylight saving time name.
+ * @param style either {@link #LONG} or {@link #SHORT}
* @param locale the locale in which to supply the display name.
* @return the human-readable name of this time zone in the given locale.
- * @exception IllegalArgumentException style is invalid.
+ * @exception IllegalArgumentException if {@code style} is invalid.
+ * @exception NullPointerException if {@code locale} is {@code null}.
* @since 1.2
*/
public String getDisplayName(boolean daylight, int style, Locale locale) {
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/java/util/XMLUtils.java
--- a/jdk/src/share/classes/java/util/XMLUtils.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/java/util/XMLUtils.java Tue Oct 26 10:57:35 2010 -0700
@@ -141,14 +141,13 @@
comments.appendChild(doc.createTextNode(comment));
}
- Set keys = props.keySet();
- Iterator i = keys.iterator();
- while(i.hasNext()) {
- String key = (String)i.next();
- Element entry = (Element)properties.appendChild(
- doc.createElement("entry"));
- entry.setAttribute("key", key);
- entry.appendChild(doc.createTextNode(props.getProperty(key)));
+ synchronized (props) {
+ for (String key : props.stringPropertyNames()) {
+ Element entry = (Element)properties.appendChild(
+ doc.createElement("entry"));
+ entry.setAttribute("key", key);
+ entry.appendChild(doc.createTextNode(props.getProperty(key)));
+ }
}
emitDocument(doc, os, encoding);
}
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/java/util/zip/ZipFile.java
--- a/jdk/src/share/classes/java/util/zip/ZipFile.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/java/util/zip/ZipFile.java Tue Oct 26 10:57:35 2010 -0700
@@ -85,8 +85,7 @@
static {
// A system prpperty to disable mmap use to avoid vm crash when
// in-use zip file is accidently overwritten by others.
- String prop = AccessController.doPrivileged(
- new GetPropertyAction("sun.zip.disableMemoryMapping"));
+ String prop = sun.misc.VM.getSavedProperty("sun.zip.disableMemoryMapping");
usemmap = (prop == null ||
!(prop.length() == 0 || prop.equalsIgnoreCase("true")));
}
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/javax/sound/sampled/AudioSystem.java
--- a/jdk/src/share/classes/javax/sound/sampled/AudioSystem.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/javax/sound/sampled/AudioSystem.java Tue Oct 26 10:57:35 2010 -0700
@@ -63,7 +63,7 @@
* Color
to use in the foreground
* for selected list items
@@ -2529,7 +2529,7 @@
* The default value of this property is defined by the look
* and feel implementation.
* Color
to use for the background
* of selected cells
diff -r c75c404c3c2c -r 85bb633e7d6f jdk/src/share/classes/javax/swing/UIDefaults.java
--- a/jdk/src/share/classes/javax/swing/UIDefaults.java Thu Oct 21 17:12:41 2010 -0700
+++ b/jdk/src/share/classes/javax/swing/UIDefaults.java Tue Oct 26 10:57:35 2010 -0700
@@ -52,6 +52,7 @@
import java.security.PrivilegedAction;
import sun.reflect.misc.MethodUtil;
+import sun.reflect.misc.ReflectUtil;
import sun.util.CoreResourceBundleControl;
/**
@@ -1078,6 +1079,9 @@
// In order to pick up the security policy in effect at the
// time of creation we use a doPrivileged with the
// AccessControlContext that was in place when this was created.
+ if (acc == null && System.getSecurityManager() != null) {
+ throw new SecurityException("null AccessControlContext");
+ }
return AccessController.doPrivileged(new PrivilegedAction