96dd155e2c
2005-02-17 Michael Koch <konqueror@gmx.de> * gnu/java/awt/peer/gtk/GThreadNativeMethodRunner.java, gnu/java/awt/peer/gtk/GdkFontPeer.java, gnu/java/awt/peer/gtk/GdkGlyphVector.java, gnu/java/awt/peer/gtk/GdkGraphics.java, gnu/java/awt/peer/gtk/GdkGraphics2D.java, gnu/java/awt/peer/gtk/GdkGraphicsConfiguration.java, gnu/java/awt/peer/gtk/GdkGraphicsEnvironment.java, gnu/java/awt/peer/gtk/GdkPixbufDecoder.java, gnu/java/awt/peer/gtk/GdkRobotPeer.java, gnu/java/awt/peer/gtk/GdkScreenGraphicsDevice.java, gnu/java/awt/peer/gtk/GdkTextLayout.java, gnu/java/awt/peer/gtk/GtkCheckboxMenuItemPeer.java, gnu/java/awt/peer/gtk/GtkChoicePeer.java, gnu/java/awt/peer/gtk/GtkClipboard.java, gnu/java/awt/peer/gtk/GtkComponentPeer.java, gnu/java/awt/peer/gtk/GtkFileDialogPeer.java, gnu/java/awt/peer/gtk/GtkFontPeer.java, gnu/java/awt/peer/gtk/GtkLabelPeer.java, gnu/java/awt/peer/gtk/GtkMenuBarPeer.java, gnu/java/awt/peer/gtk/GtkMenuItemPeer.java, gnu/java/awt/peer/gtk/GtkMenuPeer.java, gnu/java/awt/peer/gtk/GtkScrollPanePeer.java, gnu/java/awt/peer/gtk/GtkScrollbarPeer.java, gnu/java/awt/peer/gtk/GtkToolkit.java, gnu/java/awt/peer/gtk/GtkWindowPeer.java, java/applet/Applet.java, java/awt/AWTError.java, java/awt/AWTEvent.java, java/awt/AWTEventMulticaster.java, java/awt/AWTException.java, java/awt/AWTKeyStroke.java, java/awt/AWTPermission.java, java/awt/ActiveEvent.java, java/awt/Adjustable.java, java/awt/AlphaComposite.java, java/awt/AttributeValue.java, java/awt/BorderLayout.java, java/awt/BufferCapabilities.java, java/awt/Button.java, java/awt/Canvas.java, java/awt/CheckboxGroup.java, java/awt/CheckboxMenuItem.java, java/awt/Color.java, java/awt/ColorPaintContext.java, java/awt/Component.java, java/awt/ComponentOrientation.java, java/awt/Composite.java, java/awt/CompositeContext.java, java/awt/Container.java, java/awt/ContainerOrderFocusTraversalPolicy.java, java/awt/DefaultFocusTraversalPolicy.java, java/awt/Dialog.java, java/awt/Dimension.java, java/awt/DisplayMode.java, java/awt/EventQueue.java, java/awt/FileDialog.java, java/awt/FontFormatException.java, java/awt/Graphics.java, java/awt/Graphics2D.java, java/awt/GraphicsConfigTemplate.java, java/awt/GraphicsConfiguration.java, java/awt/GraphicsDevice.java, java/awt/GridBagLayout.java, java/awt/GridBagLayoutInfo.java, java/awt/GridLayout.java, java/awt/HeadlessException.java, java/awt/IllegalComponentStateException.java, java/awt/Image.java, java/awt/Insets.java, java/awt/ItemSelectable.java, java/awt/JobAttributes.java, java/awt/KeyEventDispatcher.java, java/awt/KeyEventPostProcessor.java, java/awt/KeyboardFocusManager.java, java/awt/Label.java, java/awt/LayoutManager.java, java/awt/MenuBar.java, java/awt/MenuComponent.java, java/awt/MenuContainer.java, java/awt/PageAttributes.java, java/awt/Paint.java, java/awt/PaintContext.java, java/awt/Panel.java, java/awt/Point.java, java/awt/Polygon.java, java/awt/PrintGraphics.java, java/awt/PrintJob.java, java/awt/Rectangle.java, java/awt/RenderingHints.java, java/awt/Shape.java, java/awt/Stroke.java, java/awt/SystemColor.java, java/awt/Toolkit.java, java/awt/Transparency.java, java/awt/Window.java, java/awt/color/CMMException.java, java/awt/color/ColorSpace.java, java/awt/color/ICC_ColorSpace.java, java/awt/color/ICC_Profile.java, java/awt/color/ProfileDataException.java, java/awt/datatransfer/FlavorTable.java, java/awt/datatransfer/MimeTypeParseException.java, java/awt/datatransfer/Transferable.java, java/awt/datatransfer/UnsupportedFlavorException.java, java/awt/dnd/Autoscroll.java, java/awt/dnd/DnDConstants.java, java/awt/dnd/DragGestureListener.java, java/awt/dnd/DragSourceAdapter.java, java/awt/dnd/DragSourceDropEvent.java, java/awt/dnd/DragSourceListener.java, java/awt/dnd/DragSourceMotionListener.java, java/awt/dnd/DropTargetAdapter.java, java/awt/dnd/DropTargetContext.java, java/awt/dnd/DropTargetListener.java, java/awt/dnd/InvalidDnDOperationException.java, java/awt/dnd/MouseDragGestureRecognizer.java, java/awt/dnd/peer/DropTargetContextPeer.java, java/awt/event/AWTEventListener.java, java/awt/event/AWTEventListenerProxy.java, java/awt/event/ActionEvent.java, java/awt/event/AdjustmentEvent.java, java/awt/event/AdjustmentListener.java, java/awt/event/ComponentAdapter.java, java/awt/event/ComponentEvent.java, java/awt/event/ComponentListener.java, java/awt/event/ContainerAdapter.java, java/awt/event/ContainerEvent.java, java/awt/event/ContainerListener.java, java/awt/event/FocusAdapter.java, java/awt/event/FocusEvent.java, java/awt/event/FocusListener.java, java/awt/event/InputEvent.java, java/awt/event/InputMethodEvent.java, java/awt/event/InputMethodListener.java, java/awt/event/InvocationEvent.java, java/awt/event/ItemEvent.java, java/awt/event/ItemListener.java, java/awt/event/KeyAdapter.java, java/awt/event/KeyEvent.java, java/awt/event/KeyListener.java, java/awt/event/MouseAdapter.java, java/awt/event/MouseEvent.java, java/awt/event/MouseListener.java, java/awt/event/MouseMotionAdapter.java, java/awt/event/MouseMotionListener.java, java/awt/event/MouseWheelEvent.java, java/awt/event/MouseWheelListener.java, java/awt/event/PaintEvent.java, java/awt/event/TextEvent.java, java/awt/event/TextListener.java, java/awt/event/WindowAdapter.java, java/awt/event/WindowEvent.java, java/awt/event/WindowFocusListener.java, java/awt/event/WindowListener.java, java/awt/event/WindowStateListener.java, java/awt/font/TextHitInfo.java, java/awt/geom/CubicCurve2D.java, java/awt/geom/Dimension2D.java, java/awt/geom/Ellipse2D.java, java/awt/geom/IllegalPathStateException.java, java/awt/geom/Line2D.java, java/awt/geom/NoninvertibleTransformException.java, java/awt/geom/PathIterator.java, java/awt/geom/Point2D.java, java/awt/geom/QuadCurve2D.java, java/awt/geom/RectangularShape.java, java/awt/geom/RoundRectangle2D.java, java/awt/im/InputContext.java, java/awt/im/InputMethodHighlight.java, java/awt/im/InputMethodRequests.java, java/awt/im/InputSubset.java, java/awt/im/spi/InputMethod.java, java/awt/im/spi/InputMethodDescriptor.java, java/awt/image/BandCombineOp.java, java/awt/image/BandedSampleModel.java, java/awt/image/ByteLookupTable.java, java/awt/image/ComponentSampleModel.java, java/awt/image/DataBuffer.java, java/awt/image/DataBufferByte.java, java/awt/image/DataBufferDouble.java, java/awt/image/DataBufferFloat.java, java/awt/image/DataBufferInt.java, java/awt/image/DataBufferShort.java, java/awt/image/DataBufferUShort.java, java/awt/image/ImagingOpException.java, java/awt/image/Kernel.java, java/awt/image/LookupTable.java, java/awt/image/MultiPixelPackedSampleModel.java, java/awt/image/PackedColorModel.java, java/awt/image/PixelInterleavedSampleModel.java, java/awt/image/RGBImageFilter.java, java/awt/image/Raster.java, java/awt/image/RasterFormatException.java, java/awt/image/SampleModel.java, java/awt/image/ShortLookupTable.java, java/awt/image/SinglePixelPackedSampleModel.java, java/awt/image/WritableRaster.java, java/awt/print/PrinterAbortException.java, java/awt/print/PrinterException.java, java/awt/print/PrinterIOException.java, java/beans/AppletInitializer.java, java/beans/ExceptionListener.java, java/beans/PropertyChangeEvent.java, java/beans/PropertyChangeListenerProxy.java, java/beans/PropertyChangeSupport.java, java/beans/PropertyDescriptor.java, java/beans/VetoableChangeListenerProxy.java, java/beans/VetoableChangeSupport.java, java/io/BufferedReader.java, java/io/ByteArrayInputStream.java, java/io/ByteArrayOutputStream.java, java/io/CharArrayReader.java, java/io/CharArrayWriter.java, java/io/CharConversionException.java, java/io/DataInput.java, java/io/DataInputStream.java, java/io/DataOutput.java, java/io/DataOutputStream.java, java/io/EOFException.java, java/io/FileInputStream.java, java/io/FileNotFoundException.java, java/io/FileOutputStream.java, java/io/FilenameFilter.java, java/io/FilterInputStream.java, java/io/FilterOutputStream.java, java/io/FilterReader.java, java/io/FilterWriter.java, java/io/IOException.java, java/io/InputStream.java, java/io/InterruptedIOException.java, java/io/LineNumberInputStream.java, java/io/LineNumberReader.java, java/io/ObjectInputStream.java, java/io/ObjectStreamException.java, java/io/OptionalDataException.java, java/io/OutputStream.java, java/io/OutputStreamWriter.java, java/io/PrintStream.java, java/io/PrintWriter.java, java/io/PushbackInputStream.java, java/io/PushbackReader.java, java/io/RandomAccessFile.java, java/io/Reader.java, java/io/SequenceInputStream.java, java/io/Serializable.java, java/io/StreamCorruptedException.java, java/io/StreamTokenizer.java, java/io/StringBufferInputStream.java, java/io/StringWriter.java, java/io/SyncFailedException.java, java/io/UTFDataFormatException.java, java/io/UnsupportedEncodingException.java, java/io/WriteAbortedException.java, java/io/Writer.java, java/lang/AbstractMethodError.java, java/lang/ArithmeticException.java, java/lang/ArrayIndexOutOfBoundsException.java, java/lang/ArrayStoreException.java, java/lang/AssertionError.java, java/lang/Boolean.java, java/lang/Byte.java, java/lang/Class.java, java/lang/ClassCastException.java, java/lang/ClassCircularityError.java, java/lang/ClassLoader.java, java/lang/ClassNotFoundException.java, java/lang/CloneNotSupportedException.java, java/lang/Cloneable.java, java/lang/Comparable.java, java/lang/Compiler.java, java/lang/Double.java, java/lang/Error.java, java/lang/Exception.java, java/lang/ExceptionInInitializerError.java, java/lang/Float.java, java/lang/IllegalAccessError.java, java/lang/IllegalAccessException.java, java/lang/IllegalArgumentException.java, java/lang/IllegalMonitorStateException.java, java/lang/IllegalStateException.java, java/lang/IllegalThreadStateException.java, java/lang/IncompatibleClassChangeError.java, java/lang/IndexOutOfBoundsException.java, java/lang/InheritableThreadLocal.java, java/lang/InstantiationError.java, java/lang/InstantiationException.java, java/lang/Integer.java, java/lang/InternalError.java, java/lang/InterruptedException.java, java/lang/LinkageError.java, java/lang/Long.java, java/lang/NegativeArraySizeException.java, java/lang/NoClassDefFoundError.java, java/lang/NoSuchFieldError.java, java/lang/NoSuchFieldException.java, java/lang/NoSuchMethodError.java, java/lang/NoSuchMethodException.java, java/lang/NullPointerException.java, java/lang/Number.java, java/lang/NumberFormatException.java, java/lang/OutOfMemoryError.java, java/lang/Process.java, java/lang/Runnable.java, java/lang/Runtime.java, java/lang/RuntimeException.java, java/lang/RuntimePermission.java, java/lang/SecurityException.java, java/lang/SecurityManager.java, java/lang/Short.java, java/lang/StackOverflowError.java, java/lang/StackTraceElement.java, java/lang/StringBuffer.java, java/lang/StringIndexOutOfBoundsException.java, java/lang/ThreadDeath.java, java/lang/ThreadGroup.java, java/lang/Throwable.java, java/lang/UnsatisfiedLinkError.java, java/lang/UnsupportedOperationException.java, java/lang/VerifyError.java, java/lang/VirtualMachineError.java, java/lang/reflect/AccessibleObject.java, java/lang/reflect/Array.java, java/lang/reflect/InvocationTargetException.java, java/lang/reflect/Member.java, java/lang/reflect/Modifier.java, java/lang/reflect/Proxy.java, java/lang/reflect/ReflectPermission.java, java/lang/reflect/UndeclaredThrowableException.java, java/math/BigInteger.java, java/net/NetworkInterface.java, java/nio/ByteBufferHelper.java, java/nio/ByteBufferImpl.java, java/nio/DirectByteBufferImpl.java, java/nio/DoubleBufferImpl.java, java/nio/FloatBufferImpl.java, java/nio/IntBufferImpl.java, java/nio/LongBufferImpl.java, java/nio/ShortBufferImpl.java, java/nio/channels/Channel.java, java/nio/channels/IllegalBlockingModeException.java, java/nio/charset/spi/CharsetProvider.java, java/security/AccessControlException.java, java/security/BasicPermission.java, java/security/DigestException.java, java/security/DigestInputStream.java, java/security/DigestOutputStream.java, java/security/GeneralSecurityException.java, java/security/Guard.java, java/security/GuardedObject.java, java/security/InvalidAlgorithmParameterException.java, java/security/InvalidKeyException.java, java/security/InvalidParameterException.java, java/security/Key.java, java/security/KeyException.java, java/security/KeyManagementException.java, java/security/KeyStoreException.java, java/security/MessageDigestSpi.java, java/security/NoSuchAlgorithmException.java, java/security/NoSuchProviderException.java, java/security/Permission.java, java/security/PermissionCollection.java, java/security/Permissions.java, java/security/Principal.java, java/security/PrivateKey.java, java/security/PrivilegedActionException.java, java/security/ProviderException.java, java/security/PublicKey.java, java/security/SecureRandom.java, java/security/SecureRandomSpi.java, java/security/SignatureException.java, java/security/SignatureSpi.java, java/security/SignedObject.java, java/security/Signer.java, java/security/UnrecoverableKeyException.java, java/security/UnresolvedPermission.java, java/security/acl/AclNotFoundException.java, java/security/acl/LastOwnerException.java, java/security/acl/NotOwnerException.java, java/security/cert/CertPath.java, java/security/cert/CertPathBuilderException.java, java/security/cert/CertPathValidatorException.java, java/security/cert/CertStoreException.java, java/text/BreakIterator.java, java/text/ChoiceFormat.java, java/text/CollationElementIterator.java, java/text/CollationKey.java, java/text/Collator.java, java/text/DateFormat.java, java/text/DateFormatSymbols.java, java/text/DecimalFormat.java, java/text/DecimalFormatSymbols.java, java/text/FieldPosition.java, java/text/Format.java, java/text/NumberFormat.java, java/text/ParseException.java, java/text/ParsePosition.java, java/text/StringCharacterIterator.java, java/util/AbstractCollection.java, java/util/AbstractList.java, java/util/AbstractMap.java, java/util/AbstractSequentialList.java, java/util/AbstractSet.java, java/util/ArrayList.java, java/util/Arrays.java, java/util/BitSet.java, java/util/Collection.java, java/util/Collections.java, java/util/Comparator.java, java/util/ConcurrentModificationException.java, java/util/Currency.java, java/util/Date.java, java/util/EmptyStackException.java, java/util/Enumeration.java, java/util/EventListener.java, java/util/EventListenerProxy.java, java/util/EventObject.java, java/util/HashMap.java, java/util/HashSet.java, java/util/Hashtable.java, java/util/IdentityHashMap.java, java/util/Iterator.java, java/util/LinkedHashSet.java, java/util/LinkedList.java, java/util/List.java, java/util/ListIterator.java, java/util/ListResourceBundle.java, java/util/Map.java, java/util/MissingResourceException.java, java/util/NoSuchElementException.java, java/util/Observable.java, java/util/Observer.java, java/util/Properties.java, java/util/PropertyPermissionCollection.java, java/util/RandomAccess.java, java/util/Set.java, java/util/SortedMap.java, java/util/SortedSet.java, java/util/Stack.java, java/util/StringTokenizer.java, java/util/TooManyListenersException.java, java/util/TreeMap.java, java/util/TreeSet.java, java/util/Vector.java, java/util/logging/FileHandler.java, java/util/logging/Level.java, java/util/prefs/BackingStoreException.java, java/util/prefs/InvalidPreferencesFormatException.java, java/util/prefs/Preferences.java, javax/accessibility/Accessible.java, javax/accessibility/AccessibleAction.java, javax/accessibility/AccessibleBundle.java, javax/accessibility/AccessibleComponent.java, javax/accessibility/AccessibleContext.java, javax/accessibility/AccessibleEditableText.java, javax/accessibility/AccessibleExtendedComponent.java, javax/accessibility/AccessibleExtendedTable.java, javax/accessibility/AccessibleHyperlink.java, javax/accessibility/AccessibleHypertext.java, javax/accessibility/AccessibleIcon.java, javax/accessibility/AccessibleKeyBinding.java, javax/accessibility/AccessibleRelation.java, javax/accessibility/AccessibleRelationSet.java, javax/accessibility/AccessibleResourceBundle.java, javax/accessibility/AccessibleRole.java, javax/accessibility/AccessibleSelection.java, javax/accessibility/AccessibleState.java, javax/accessibility/AccessibleStateSet.java, javax/accessibility/AccessibleTable.java, javax/accessibility/AccessibleTableModelChange.java, javax/accessibility/AccessibleText.java, javax/accessibility/AccessibleValue.java, javax/imageio/IIOException.java, javax/imageio/ImageIO.java, javax/imageio/spi/IIOServiceProvider.java, javax/imageio/spi/ImageInputStreamSpi.java, javax/imageio/spi/ImageOutputStreamSpi.java, javax/imageio/spi/ImageReaderWriterSpi.java, javax/imageio/spi/ImageTranscoderSpi.java, javax/imageio/spi/RegisterableService.java, javax/imageio/spi/ServiceRegistry.java, javax/imageio/stream/IIOByteBuffer.java, javax/imageio/stream/ImageInputStream.java, javax/imageio/stream/ImageOutputStream.java, javax/naming/Binding.java, javax/naming/CannotProceedException.java, javax/naming/CompositeName.java, javax/naming/CompoundName.java, javax/naming/LinkException.java, javax/naming/LinkRef.java, javax/naming/NameClassPair.java, javax/naming/Reference.java, javax/naming/ReferralException.java, javax/naming/directory/Attribute.java, javax/naming/directory/AttributeModificationException.java, javax/naming/directory/Attributes.java, javax/naming/directory/DirContext.java, javax/naming/directory/ModificationItem.java, javax/naming/directory/SearchControls.java, javax/naming/directory/SearchResult.java, javax/naming/event/EventContext.java, javax/naming/event/EventDirContext.java, javax/naming/event/NamespaceChangeListener.java, javax/naming/event/NamingExceptionEvent.java, javax/naming/event/NamingListener.java, javax/naming/event/ObjectChangeListener.java, javax/naming/ldap/Control.java, javax/naming/ldap/ExtendedResponse.java, javax/naming/ldap/HasControls.java, javax/naming/ldap/LdapContext.java, javax/naming/ldap/UnsolicitedNotification.java, javax/naming/ldap/UnsolicitedNotificationEvent.java, javax/naming/ldap/UnsolicitedNotificationListener.java, javax/naming/spi/DirectoryManager.java, javax/naming/spi/ResolveResult.java, javax/naming/spi/Resolver.java, javax/security/auth/callback/Callback.java, javax/security/auth/callback/CallbackHandler.java, javax/security/auth/callback/ChoiceCallback.java, javax/security/auth/callback/ConfirmationCallback.java, javax/security/auth/callback/LanguageCallback.java, javax/security/auth/callback/NameCallback.java, javax/security/auth/callback/PasswordCallback.java, javax/security/auth/callback/TextInputCallback.java, javax/security/auth/callback/TextOutputCallback.java, javax/security/auth/callback/UnsupportedCallbackException.java, javax/security/sasl/Sasl.java, javax/security/sasl/SaslServerFactory.java, javax/swing/DefaultBoundedRangeModel.java, javax/swing/JComponent.java, javax/swing/JList.java, javax/swing/JProgressBar.java, javax/swing/JRadioButton.java, javax/swing/JTextArea.java, javax/swing/JTextField.java, javax/swing/JToggleButton.java, javax/swing/JTree.java, javax/swing/SpinnerListModel.java, javax/swing/event/EventListenerList.java, javax/swing/text/JTextComponent.java, javax/swing/text/StringContent.java, javax/swing/text/Utilities.java, javax/swing/undo/UndoManager.java, javax/swing/undo/UndoableEditSupport.java, javax/transaction/HeuristicCommitException.java, javax/transaction/HeuristicMixedException.java, javax/transaction/HeuristicRollbackException.java, javax/transaction/InvalidTransactionException.java, javax/transaction/NotSupportedException.java, javax/transaction/RollbackException.java, javax/transaction/Status.java, javax/transaction/Synchronization.java, javax/transaction/SystemException.java, javax/transaction/Transaction.java, javax/transaction/TransactionManager.java, javax/transaction/TransactionRequiredException.java, javax/transaction/TransactionRolledbackException.java, javax/transaction/UserTransaction.java, javax/transaction/xa/XAException.java, javax/transaction/xa/XAResource.java, javax/transaction/xa/Xid.java, javax/xml/XMLConstants.java, javax/xml/datatype/DatatypeConfigurationException.java, javax/xml/datatype/DatatypeConstants.java, javax/xml/datatype/DatatypeFactory.java, javax/xml/datatype/Duration.java, javax/xml/datatype/XMLGregorianCalendar.java, javax/xml/namespace/NamespaceContext.java, javax/xml/namespace/QName.java, javax/xml/parsers/DocumentBuilder.java, javax/xml/parsers/DocumentBuilderFactory.java, javax/xml/parsers/FactoryConfigurationError.java, javax/xml/parsers/ParserConfigurationException.java, javax/xml/parsers/SAXParser.java, javax/xml/parsers/SAXParserFactory.java, javax/xml/transform/ErrorListener.java, javax/xml/transform/OutputKeys.java, javax/xml/transform/Result.java, javax/xml/transform/Source.java, javax/xml/transform/SourceLocator.java, javax/xml/transform/Templates.java, javax/xml/transform/Transformer.java, javax/xml/transform/TransformerConfigurationException.java, javax/xml/transform/TransformerException.java, javax/xml/transform/TransformerFactory.java, javax/xml/transform/TransformerFactoryConfigurationError.java, javax/xml/transform/URIResolver.java, javax/xml/transform/dom/DOMLocator.java, javax/xml/transform/dom/DOMResult.java, javax/xml/transform/dom/DOMSource.java, javax/xml/transform/sax/SAXResult.java, javax/xml/transform/sax/SAXSource.java, javax/xml/transform/sax/SAXTransformerFactory.java, javax/xml/transform/sax/TemplatesHandler.java, javax/xml/transform/sax/TransformerHandler.java, javax/xml/transform/stream/StreamResult.java, javax/xml/transform/stream/StreamSource.java, javax/xml/validation/Schema.java, javax/xml/validation/SchemaFactory.java, javax/xml/validation/TypeInfoProvider.java, javax/xml/validation/Validator.java, javax/xml/validation/ValidatorHandler.java, javax/xml/xpath/XPathConstants.java, javax/xml/xpath/XPathException.java, javax/xml/xpath/XPathExpression.java, javax/xml/xpath/XPathExpressionException.java, javax/xml/xpath/XPathFactory.java, javax/xml/xpath/XPathFactoryConfigurationException.java, javax/xml/xpath/XPathFunction.java, javax/xml/xpath/XPathFunctionException.java, javax/xml/xpath/XPathFunctionResolver.java: Fixed usage of @author tag, reworked import statements, removed CVS tags, changed the modifier order and fixed HTML usage in javadocs. From-SVN: r95152
2511 lines
72 KiB
Java
2511 lines
72 KiB
Java
/* Arrays.java -- Utility class with methods to operate on arrays
|
||
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
|
||
Free Software Foundation, Inc.
|
||
|
||
This file is part of GNU Classpath.
|
||
|
||
GNU Classpath is free software; you can redistribute it and/or modify
|
||
it under the terms of the GNU General Public License as published by
|
||
the Free Software Foundation; either version 2, or (at your option)
|
||
any later version.
|
||
|
||
GNU Classpath is distributed in the hope that it will be useful, but
|
||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
General Public License for more details.
|
||
|
||
You should have received a copy of the GNU General Public License
|
||
along with GNU Classpath; see the file COPYING. If not, write to the
|
||
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
||
02111-1307 USA.
|
||
|
||
Linking this library statically or dynamically with other modules is
|
||
making a combined work based on this library. Thus, the terms and
|
||
conditions of the GNU General Public License cover the whole
|
||
combination.
|
||
|
||
As a special exception, the copyright holders of this library give you
|
||
permission to link this library with independent modules to produce an
|
||
executable, regardless of the license terms of these independent
|
||
modules, and to copy and distribute the resulting executable under
|
||
terms of your choice, provided that you also meet, for each linked
|
||
independent module, the terms and conditions of the license of that
|
||
module. An independent module is a module which is not derived from
|
||
or based on this library. If you modify this library, you may extend
|
||
this exception to your version of the library, but you are not
|
||
obligated to do so. If you do not wish to do so, delete this
|
||
exception statement from your version. */
|
||
|
||
|
||
package java.util;
|
||
|
||
import java.io.Serializable;
|
||
import java.lang.reflect.Array;
|
||
|
||
/**
|
||
* This class contains various static utility methods performing operations on
|
||
* arrays, and a method to provide a List "view" of an array to facilitate
|
||
* using arrays with Collection-based APIs. All methods throw a
|
||
* {@link NullPointerException} if the parameter array is null.
|
||
* <p>
|
||
*
|
||
* Implementations may use their own algorithms, but must obey the general
|
||
* properties; for example, the sort must be stable and n*log(n) complexity.
|
||
* Sun's implementation of sort, and therefore ours, is a tuned quicksort,
|
||
* adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort
|
||
* Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265
|
||
* (November 1993). This algorithm offers n*log(n) performance on many data
|
||
* sets that cause other quicksorts to degrade to quadratic performance.
|
||
*
|
||
* @author Original author unknown
|
||
* @author Bryce McKinlay
|
||
* @author Eric Blake (ebb9@email.byu.edu)
|
||
* @see Comparable
|
||
* @see Comparator
|
||
* @since 1.2
|
||
* @status updated to 1.4
|
||
*/
|
||
public class Arrays
|
||
{
|
||
/**
|
||
* This class is non-instantiable.
|
||
*/
|
||
private Arrays()
|
||
{
|
||
}
|
||
|
||
|
||
// binarySearch
|
||
/**
|
||
* Perform a binary search of a byte array for a key. The array must be
|
||
* sorted (as by the sort() method) - if it is not, the behaviour of this
|
||
* method is undefined, and may be an infinite loop. If the array contains
|
||
* the key more than once, any one of them may be found. Note: although the
|
||
* specification allows for an infinite loop if the array is unsorted, it
|
||
* will not happen in this implementation.
|
||
*
|
||
* @param a the array to search (must be sorted)
|
||
* @param key the value to search for
|
||
* @return the index at which the key was found, or -n-1 if it was not
|
||
* found, where n is the index of the first value higher than key or
|
||
* a.length if there is no such value.
|
||
*/
|
||
public static int binarySearch(byte[] a, byte key)
|
||
{
|
||
int low = 0;
|
||
int hi = a.length - 1;
|
||
int mid = 0;
|
||
while (low <= hi)
|
||
{
|
||
mid = (low + hi) >> 1;
|
||
final byte d = a[mid];
|
||
if (d == key)
|
||
return mid;
|
||
else if (d > key)
|
||
hi = mid - 1;
|
||
else
|
||
// This gets the insertion point right on the last loop.
|
||
low = ++mid;
|
||
}
|
||
return -mid - 1;
|
||
}
|
||
|
||
/**
|
||
* Perform a binary search of a char array for a key. The array must be
|
||
* sorted (as by the sort() method) - if it is not, the behaviour of this
|
||
* method is undefined, and may be an infinite loop. If the array contains
|
||
* the key more than once, any one of them may be found. Note: although the
|
||
* specification allows for an infinite loop if the array is unsorted, it
|
||
* will not happen in this implementation.
|
||
*
|
||
* @param a the array to search (must be sorted)
|
||
* @param key the value to search for
|
||
* @return the index at which the key was found, or -n-1 if it was not
|
||
* found, where n is the index of the first value higher than key or
|
||
* a.length if there is no such value.
|
||
*/
|
||
public static int binarySearch(char[] a, char key)
|
||
{
|
||
int low = 0;
|
||
int hi = a.length - 1;
|
||
int mid = 0;
|
||
while (low <= hi)
|
||
{
|
||
mid = (low + hi) >> 1;
|
||
final char d = a[mid];
|
||
if (d == key)
|
||
return mid;
|
||
else if (d > key)
|
||
hi = mid - 1;
|
||
else
|
||
// This gets the insertion point right on the last loop.
|
||
low = ++mid;
|
||
}
|
||
return -mid - 1;
|
||
}
|
||
|
||
/**
|
||
* Perform a binary search of a short array for a key. The array must be
|
||
* sorted (as by the sort() method) - if it is not, the behaviour of this
|
||
* method is undefined, and may be an infinite loop. If the array contains
|
||
* the key more than once, any one of them may be found. Note: although the
|
||
* specification allows for an infinite loop if the array is unsorted, it
|
||
* will not happen in this implementation.
|
||
*
|
||
* @param a the array to search (must be sorted)
|
||
* @param key the value to search for
|
||
* @return the index at which the key was found, or -n-1 if it was not
|
||
* found, where n is the index of the first value higher than key or
|
||
* a.length if there is no such value.
|
||
*/
|
||
public static int binarySearch(short[] a, short key)
|
||
{
|
||
int low = 0;
|
||
int hi = a.length - 1;
|
||
int mid = 0;
|
||
while (low <= hi)
|
||
{
|
||
mid = (low + hi) >> 1;
|
||
final short d = a[mid];
|
||
if (d == key)
|
||
return mid;
|
||
else if (d > key)
|
||
hi = mid - 1;
|
||
else
|
||
// This gets the insertion point right on the last loop.
|
||
low = ++mid;
|
||
}
|
||
return -mid - 1;
|
||
}
|
||
|
||
/**
|
||
* Perform a binary search of an int array for a key. The array must be
|
||
* sorted (as by the sort() method) - if it is not, the behaviour of this
|
||
* method is undefined, and may be an infinite loop. If the array contains
|
||
* the key more than once, any one of them may be found. Note: although the
|
||
* specification allows for an infinite loop if the array is unsorted, it
|
||
* will not happen in this implementation.
|
||
*
|
||
* @param a the array to search (must be sorted)
|
||
* @param key the value to search for
|
||
* @return the index at which the key was found, or -n-1 if it was not
|
||
* found, where n is the index of the first value higher than key or
|
||
* a.length if there is no such value.
|
||
*/
|
||
public static int binarySearch(int[] a, int key)
|
||
{
|
||
int low = 0;
|
||
int hi = a.length - 1;
|
||
int mid = 0;
|
||
while (low <= hi)
|
||
{
|
||
mid = (low + hi) >> 1;
|
||
final int d = a[mid];
|
||
if (d == key)
|
||
return mid;
|
||
else if (d > key)
|
||
hi = mid - 1;
|
||
else
|
||
// This gets the insertion point right on the last loop.
|
||
low = ++mid;
|
||
}
|
||
return -mid - 1;
|
||
}
|
||
|
||
/**
|
||
* Perform a binary search of a long array for a key. The array must be
|
||
* sorted (as by the sort() method) - if it is not, the behaviour of this
|
||
* method is undefined, and may be an infinite loop. If the array contains
|
||
* the key more than once, any one of them may be found. Note: although the
|
||
* specification allows for an infinite loop if the array is unsorted, it
|
||
* will not happen in this implementation.
|
||
*
|
||
* @param a the array to search (must be sorted)
|
||
* @param key the value to search for
|
||
* @return the index at which the key was found, or -n-1 if it was not
|
||
* found, where n is the index of the first value higher than key or
|
||
* a.length if there is no such value.
|
||
*/
|
||
public static int binarySearch(long[] a, long key)
|
||
{
|
||
int low = 0;
|
||
int hi = a.length - 1;
|
||
int mid = 0;
|
||
while (low <= hi)
|
||
{
|
||
mid = (low + hi) >> 1;
|
||
final long d = a[mid];
|
||
if (d == key)
|
||
return mid;
|
||
else if (d > key)
|
||
hi = mid - 1;
|
||
else
|
||
// This gets the insertion point right on the last loop.
|
||
low = ++mid;
|
||
}
|
||
return -mid - 1;
|
||
}
|
||
|
||
/**
|
||
* Perform a binary search of a float array for a key. The array must be
|
||
* sorted (as by the sort() method) - if it is not, the behaviour of this
|
||
* method is undefined, and may be an infinite loop. If the array contains
|
||
* the key more than once, any one of them may be found. Note: although the
|
||
* specification allows for an infinite loop if the array is unsorted, it
|
||
* will not happen in this implementation.
|
||
*
|
||
* @param a the array to search (must be sorted)
|
||
* @param key the value to search for
|
||
* @return the index at which the key was found, or -n-1 if it was not
|
||
* found, where n is the index of the first value higher than key or
|
||
* a.length if there is no such value.
|
||
*/
|
||
public static int binarySearch(float[] a, float key)
|
||
{
|
||
// Must use Float.compare to take into account NaN, +-0.
|
||
int low = 0;
|
||
int hi = a.length - 1;
|
||
int mid = 0;
|
||
while (low <= hi)
|
||
{
|
||
mid = (low + hi) >> 1;
|
||
final int r = Float.compare(a[mid], key);
|
||
if (r == 0)
|
||
return mid;
|
||
else if (r > 0)
|
||
hi = mid - 1;
|
||
else
|
||
// This gets the insertion point right on the last loop
|
||
low = ++mid;
|
||
}
|
||
return -mid - 1;
|
||
}
|
||
|
||
/**
|
||
* Perform a binary search of a double array for a key. The array must be
|
||
* sorted (as by the sort() method) - if it is not, the behaviour of this
|
||
* method is undefined, and may be an infinite loop. If the array contains
|
||
* the key more than once, any one of them may be found. Note: although the
|
||
* specification allows for an infinite loop if the array is unsorted, it
|
||
* will not happen in this implementation.
|
||
*
|
||
* @param a the array to search (must be sorted)
|
||
* @param key the value to search for
|
||
* @return the index at which the key was found, or -n-1 if it was not
|
||
* found, where n is the index of the first value higher than key or
|
||
* a.length if there is no such value.
|
||
*/
|
||
public static int binarySearch(double[] a, double key)
|
||
{
|
||
// Must use Double.compare to take into account NaN, +-0.
|
||
int low = 0;
|
||
int hi = a.length - 1;
|
||
int mid = 0;
|
||
while (low <= hi)
|
||
{
|
||
mid = (low + hi) >> 1;
|
||
final int r = Double.compare(a[mid], key);
|
||
if (r == 0)
|
||
return mid;
|
||
else if (r > 0)
|
||
hi = mid - 1;
|
||
else
|
||
// This gets the insertion point right on the last loop
|
||
low = ++mid;
|
||
}
|
||
return -mid - 1;
|
||
}
|
||
|
||
/**
|
||
* Perform a binary search of an Object array for a key, using the natural
|
||
* ordering of the elements. The array must be sorted (as by the sort()
|
||
* method) - if it is not, the behaviour of this method is undefined, and may
|
||
* be an infinite loop. Further, the key must be comparable with every item
|
||
* in the array. If the array contains the key more than once, any one of
|
||
* them may be found. Note: although the specification allows for an infinite
|
||
* loop if the array is unsorted, it will not happen in this (JCL)
|
||
* implementation.
|
||
*
|
||
* @param a the array to search (must be sorted)
|
||
* @param key the value to search for
|
||
* @return the index at which the key was found, or -n-1 if it was not
|
||
* found, where n is the index of the first value higher than key or
|
||
* a.length if there is no such value.
|
||
* @throws ClassCastException if key could not be compared with one of the
|
||
* elements of a
|
||
* @throws NullPointerException if a null element in a is compared
|
||
*/
|
||
public static int binarySearch(Object[] a, Object key)
|
||
{
|
||
return binarySearch(a, key, null);
|
||
}
|
||
|
||
/**
|
||
* Perform a binary search of an Object array for a key, using a supplied
|
||
* Comparator. The array must be sorted (as by the sort() method with the
|
||
* same Comparator) - if it is not, the behaviour of this method is
|
||
* undefined, and may be an infinite loop. Further, the key must be
|
||
* comparable with every item in the array. If the array contains the key
|
||
* more than once, any one of them may be found. Note: although the
|
||
* specification allows for an infinite loop if the array is unsorted, it
|
||
* will not happen in this (JCL) implementation.
|
||
*
|
||
* @param a the array to search (must be sorted)
|
||
* @param key the value to search for
|
||
* @param c the comparator by which the array is sorted; or null to
|
||
* use the elements' natural order
|
||
* @return the index at which the key was found, or -n-1 if it was not
|
||
* found, where n is the index of the first value higher than key or
|
||
* a.length if there is no such value.
|
||
* @throws ClassCastException if key could not be compared with one of the
|
||
* elements of a
|
||
* @throws NullPointerException if a null element is compared with natural
|
||
* ordering (only possible when c is null)
|
||
*/
|
||
public static int binarySearch(Object[] a, Object key, Comparator c)
|
||
{
|
||
int low = 0;
|
||
int hi = a.length - 1;
|
||
int mid = 0;
|
||
while (low <= hi)
|
||
{
|
||
mid = (low + hi) >> 1;
|
||
final int d = Collections.compare(key, a[mid], c);
|
||
if (d == 0)
|
||
return mid;
|
||
else if (d < 0)
|
||
hi = mid - 1;
|
||
else
|
||
// This gets the insertion point right on the last loop
|
||
low = ++mid;
|
||
}
|
||
return -mid - 1;
|
||
}
|
||
|
||
|
||
// equals
|
||
/**
|
||
* Compare two boolean arrays for equality.
|
||
*
|
||
* @param a1 the first array to compare
|
||
* @param a2 the second array to compare
|
||
* @return true if a1 and a2 are both null, or if a2 is of the same length
|
||
* as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
|
||
*/
|
||
public static boolean equals(boolean[] a1, boolean[] a2)
|
||
{
|
||
// Quick test which saves comparing elements of the same array, and also
|
||
// catches the case that both are null.
|
||
if (a1 == a2)
|
||
return true;
|
||
|
||
if (null == a1 || null == a2)
|
||
return false;
|
||
|
||
// If they're the same length, test each element
|
||
if (a1.length == a2.length)
|
||
{
|
||
int i = a1.length;
|
||
while (--i >= 0)
|
||
if (a1[i] != a2[i])
|
||
return false;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* Compare two byte arrays for equality.
|
||
*
|
||
* @param a1 the first array to compare
|
||
* @param a2 the second array to compare
|
||
* @return true if a1 and a2 are both null, or if a2 is of the same length
|
||
* as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
|
||
*/
|
||
public static boolean equals(byte[] a1, byte[] a2)
|
||
{
|
||
// Quick test which saves comparing elements of the same array, and also
|
||
// catches the case that both are null.
|
||
if (a1 == a2)
|
||
return true;
|
||
|
||
if (null == a1 || null == a2)
|
||
return false;
|
||
|
||
// If they're the same length, test each element
|
||
if (a1.length == a2.length)
|
||
{
|
||
int i = a1.length;
|
||
while (--i >= 0)
|
||
if (a1[i] != a2[i])
|
||
return false;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* Compare two char arrays for equality.
|
||
*
|
||
* @param a1 the first array to compare
|
||
* @param a2 the second array to compare
|
||
* @return true if a1 and a2 are both null, or if a2 is of the same length
|
||
* as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
|
||
*/
|
||
public static boolean equals(char[] a1, char[] a2)
|
||
{
|
||
// Quick test which saves comparing elements of the same array, and also
|
||
// catches the case that both are null.
|
||
if (a1 == a2)
|
||
return true;
|
||
|
||
if (null == a1 || null == a2)
|
||
return false;
|
||
|
||
// If they're the same length, test each element
|
||
if (a1.length == a2.length)
|
||
{
|
||
int i = a1.length;
|
||
while (--i >= 0)
|
||
if (a1[i] != a2[i])
|
||
return false;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* Compare two short arrays for equality.
|
||
*
|
||
* @param a1 the first array to compare
|
||
* @param a2 the second array to compare
|
||
* @return true if a1 and a2 are both null, or if a2 is of the same length
|
||
* as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
|
||
*/
|
||
public static boolean equals(short[] a1, short[] a2)
|
||
{
|
||
// Quick test which saves comparing elements of the same array, and also
|
||
// catches the case that both are null.
|
||
if (a1 == a2)
|
||
return true;
|
||
|
||
if (null == a1 || null == a2)
|
||
return false;
|
||
|
||
// If they're the same length, test each element
|
||
if (a1.length == a2.length)
|
||
{
|
||
int i = a1.length;
|
||
while (--i >= 0)
|
||
if (a1[i] != a2[i])
|
||
return false;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* Compare two int arrays for equality.
|
||
*
|
||
* @param a1 the first array to compare
|
||
* @param a2 the second array to compare
|
||
* @return true if a1 and a2 are both null, or if a2 is of the same length
|
||
* as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
|
||
*/
|
||
public static boolean equals(int[] a1, int[] a2)
|
||
{
|
||
// Quick test which saves comparing elements of the same array, and also
|
||
// catches the case that both are null.
|
||
if (a1 == a2)
|
||
return true;
|
||
|
||
if (null == a1 || null == a2)
|
||
return false;
|
||
|
||
// If they're the same length, test each element
|
||
if (a1.length == a2.length)
|
||
{
|
||
int i = a1.length;
|
||
while (--i >= 0)
|
||
if (a1[i] != a2[i])
|
||
return false;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* Compare two long arrays for equality.
|
||
*
|
||
* @param a1 the first array to compare
|
||
* @param a2 the second array to compare
|
||
* @return true if a1 and a2 are both null, or if a2 is of the same length
|
||
* as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
|
||
*/
|
||
public static boolean equals(long[] a1, long[] a2)
|
||
{
|
||
// Quick test which saves comparing elements of the same array, and also
|
||
// catches the case that both are null.
|
||
if (a1 == a2)
|
||
return true;
|
||
|
||
if (null == a1 || null == a2)
|
||
return false;
|
||
|
||
// If they're the same length, test each element
|
||
if (a1.length == a2.length)
|
||
{
|
||
int i = a1.length;
|
||
while (--i >= 0)
|
||
if (a1[i] != a2[i])
|
||
return false;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* Compare two float arrays for equality.
|
||
*
|
||
* @param a1 the first array to compare
|
||
* @param a2 the second array to compare
|
||
* @return true if a1 and a2 are both null, or if a2 is of the same length
|
||
* as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
|
||
*/
|
||
public static boolean equals(float[] a1, float[] a2)
|
||
{
|
||
// Quick test which saves comparing elements of the same array, and also
|
||
// catches the case that both are null.
|
||
if (a1 == a2)
|
||
return true;
|
||
|
||
if (null == a1 || null == a2)
|
||
return false;
|
||
|
||
// Must use Float.compare to take into account NaN, +-0.
|
||
// If they're the same length, test each element
|
||
if (a1.length == a2.length)
|
||
{
|
||
int i = a1.length;
|
||
while (--i >= 0)
|
||
if (Float.compare(a1[i], a2[i]) != 0)
|
||
return false;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* Compare two double arrays for equality.
|
||
*
|
||
* @param a1 the first array to compare
|
||
* @param a2 the second array to compare
|
||
* @return true if a1 and a2 are both null, or if a2 is of the same length
|
||
* as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
|
||
*/
|
||
public static boolean equals(double[] a1, double[] a2)
|
||
{
|
||
// Quick test which saves comparing elements of the same array, and also
|
||
// catches the case that both are null.
|
||
if (a1 == a2)
|
||
return true;
|
||
|
||
if (null == a1 || null == a2)
|
||
return false;
|
||
|
||
// Must use Double.compare to take into account NaN, +-0.
|
||
// If they're the same length, test each element
|
||
if (a1.length == a2.length)
|
||
{
|
||
int i = a1.length;
|
||
while (--i >= 0)
|
||
if (Double.compare(a1[i], a2[i]) != 0)
|
||
return false;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* Compare two Object arrays for equality.
|
||
*
|
||
* @param a1 the first array to compare
|
||
* @param a2 the second array to compare
|
||
* @return true if a1 and a2 are both null, or if a1 is of the same length
|
||
* as a2, and for each 0 <= i < a.length, a1[i] == null ?
|
||
* a2[i] == null : a1[i].equals(a2[i]).
|
||
*/
|
||
public static boolean equals(Object[] a1, Object[] a2)
|
||
{
|
||
// Quick test which saves comparing elements of the same array, and also
|
||
// catches the case that both are null.
|
||
if (a1 == a2)
|
||
return true;
|
||
|
||
if (null == a1 || null == a2)
|
||
return false;
|
||
|
||
// If they're the same length, test each element
|
||
if (a1.length == a2.length)
|
||
{
|
||
int i = a1.length;
|
||
while (--i >= 0)
|
||
if (! AbstractCollection.equals(a1[i], a2[i]))
|
||
return false;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
// fill
|
||
/**
|
||
* Fill an array with a boolean value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param val the value to fill it with
|
||
*/
|
||
public static void fill(boolean[] a, boolean val)
|
||
{
|
||
fill(a, 0, a.length, val);
|
||
}
|
||
|
||
/**
|
||
* Fill a range of an array with a boolean value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param fromIndex the index to fill from, inclusive
|
||
* @param toIndex the index to fill to, exclusive
|
||
* @param val the value to fill with
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
for (int i = fromIndex; i < toIndex; i++)
|
||
a[i] = val;
|
||
}
|
||
|
||
/**
|
||
* Fill an array with a byte value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param val the value to fill it with
|
||
*/
|
||
public static void fill(byte[] a, byte val)
|
||
{
|
||
fill(a, 0, a.length, val);
|
||
}
|
||
|
||
/**
|
||
* Fill a range of an array with a byte value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param fromIndex the index to fill from, inclusive
|
||
* @param toIndex the index to fill to, exclusive
|
||
* @param val the value to fill with
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void fill(byte[] a, int fromIndex, int toIndex, byte val)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
for (int i = fromIndex; i < toIndex; i++)
|
||
a[i] = val;
|
||
}
|
||
|
||
/**
|
||
* Fill an array with a char value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param val the value to fill it with
|
||
*/
|
||
public static void fill(char[] a, char val)
|
||
{
|
||
fill(a, 0, a.length, val);
|
||
}
|
||
|
||
/**
|
||
* Fill a range of an array with a char value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param fromIndex the index to fill from, inclusive
|
||
* @param toIndex the index to fill to, exclusive
|
||
* @param val the value to fill with
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void fill(char[] a, int fromIndex, int toIndex, char val)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
for (int i = fromIndex; i < toIndex; i++)
|
||
a[i] = val;
|
||
}
|
||
|
||
/**
|
||
* Fill an array with a short value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param val the value to fill it with
|
||
*/
|
||
public static void fill(short[] a, short val)
|
||
{
|
||
fill(a, 0, a.length, val);
|
||
}
|
||
|
||
/**
|
||
* Fill a range of an array with a short value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param fromIndex the index to fill from, inclusive
|
||
* @param toIndex the index to fill to, exclusive
|
||
* @param val the value to fill with
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void fill(short[] a, int fromIndex, int toIndex, short val)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
for (int i = fromIndex; i < toIndex; i++)
|
||
a[i] = val;
|
||
}
|
||
|
||
/**
|
||
* Fill an array with an int value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param val the value to fill it with
|
||
*/
|
||
public static void fill(int[] a, int val)
|
||
{
|
||
fill(a, 0, a.length, val);
|
||
}
|
||
|
||
/**
|
||
* Fill a range of an array with an int value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param fromIndex the index to fill from, inclusive
|
||
* @param toIndex the index to fill to, exclusive
|
||
* @param val the value to fill with
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void fill(int[] a, int fromIndex, int toIndex, int val)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
for (int i = fromIndex; i < toIndex; i++)
|
||
a[i] = val;
|
||
}
|
||
|
||
/**
|
||
* Fill an array with a long value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param val the value to fill it with
|
||
*/
|
||
public static void fill(long[] a, long val)
|
||
{
|
||
fill(a, 0, a.length, val);
|
||
}
|
||
|
||
/**
|
||
* Fill a range of an array with a long value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param fromIndex the index to fill from, inclusive
|
||
* @param toIndex the index to fill to, exclusive
|
||
* @param val the value to fill with
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void fill(long[] a, int fromIndex, int toIndex, long val)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
for (int i = fromIndex; i < toIndex; i++)
|
||
a[i] = val;
|
||
}
|
||
|
||
/**
|
||
* Fill an array with a float value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param val the value to fill it with
|
||
*/
|
||
public static void fill(float[] a, float val)
|
||
{
|
||
fill(a, 0, a.length, val);
|
||
}
|
||
|
||
/**
|
||
* Fill a range of an array with a float value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param fromIndex the index to fill from, inclusive
|
||
* @param toIndex the index to fill to, exclusive
|
||
* @param val the value to fill with
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void fill(float[] a, int fromIndex, int toIndex, float val)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
for (int i = fromIndex; i < toIndex; i++)
|
||
a[i] = val;
|
||
}
|
||
|
||
/**
|
||
* Fill an array with a double value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param val the value to fill it with
|
||
*/
|
||
public static void fill(double[] a, double val)
|
||
{
|
||
fill(a, 0, a.length, val);
|
||
}
|
||
|
||
/**
|
||
* Fill a range of an array with a double value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param fromIndex the index to fill from, inclusive
|
||
* @param toIndex the index to fill to, exclusive
|
||
* @param val the value to fill with
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void fill(double[] a, int fromIndex, int toIndex, double val)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
for (int i = fromIndex; i < toIndex; i++)
|
||
a[i] = val;
|
||
}
|
||
|
||
/**
|
||
* Fill an array with an Object value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param val the value to fill it with
|
||
* @throws ClassCastException if val is not an instance of the element
|
||
* type of a.
|
||
*/
|
||
public static void fill(Object[] a, Object val)
|
||
{
|
||
fill(a, 0, a.length, val);
|
||
}
|
||
|
||
/**
|
||
* Fill a range of an array with an Object value.
|
||
*
|
||
* @param a the array to fill
|
||
* @param fromIndex the index to fill from, inclusive
|
||
* @param toIndex the index to fill to, exclusive
|
||
* @param val the value to fill with
|
||
* @throws ClassCastException if val is not an instance of the element
|
||
* type of a.
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void fill(Object[] a, int fromIndex, int toIndex, Object val)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
for (int i = fromIndex; i < toIndex; i++)
|
||
a[i] = val;
|
||
}
|
||
|
||
|
||
// sort
|
||
// Thanks to Paul Fisher (rao@gnu.org) for finding this quicksort algorithm
|
||
// as specified by Sun and porting it to Java. The algorithm is an optimised
|
||
// quicksort, as described in Jon L. Bentley and M. Douglas McIlroy's
|
||
// "Engineering a Sort Function", Software-Practice and Experience, Vol.
|
||
// 23(11) P. 1249-1265 (November 1993). This algorithm gives n*log(n)
|
||
// performance on many arrays that would take quadratic time with a standard
|
||
// quicksort.
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the byte array to sort
|
||
*/
|
||
public static void sort(byte[] a)
|
||
{
|
||
qsort(a, 0, a.length);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the byte array to sort
|
||
* @param fromIndex the first index to sort (inclusive)
|
||
* @param toIndex the last index to sort (exclusive)
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void sort(byte[] a, int fromIndex, int toIndex)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
if (fromIndex < 0)
|
||
throw new ArrayIndexOutOfBoundsException();
|
||
qsort(a, fromIndex, toIndex - fromIndex);
|
||
}
|
||
|
||
/**
|
||
* Finds the index of the median of three array elements.
|
||
*
|
||
* @param a the first index
|
||
* @param b the second index
|
||
* @param c the third index
|
||
* @param d the array
|
||
* @return the index (a, b, or c) which has the middle value of the three
|
||
*/
|
||
private static int med3(int a, int b, int c, byte[] d)
|
||
{
|
||
return (d[a] < d[b]
|
||
? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
|
||
: (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
|
||
}
|
||
|
||
/**
|
||
* Swaps the elements at two locations of an array
|
||
*
|
||
* @param i the first index
|
||
* @param j the second index
|
||
* @param a the array
|
||
*/
|
||
private static void swap(int i, int j, byte[] a)
|
||
{
|
||
byte c = a[i];
|
||
a[i] = a[j];
|
||
a[j] = c;
|
||
}
|
||
|
||
/**
|
||
* Swaps two ranges of an array.
|
||
*
|
||
* @param i the first range start
|
||
* @param j the second range start
|
||
* @param n the element count
|
||
* @param a the array
|
||
*/
|
||
private static void vecswap(int i, int j, int n, byte[] a)
|
||
{
|
||
for ( ; n > 0; i++, j++, n--)
|
||
swap(i, j, a);
|
||
}
|
||
|
||
/**
|
||
* Performs a recursive modified quicksort.
|
||
*
|
||
* @param array the array to sort
|
||
* @param from the start index (inclusive)
|
||
* @param count the number of elements to sort
|
||
*/
|
||
private static void qsort(byte[] array, int from, int count)
|
||
{
|
||
// Use an insertion sort on small arrays.
|
||
if (count <= 7)
|
||
{
|
||
for (int i = from + 1; i < from + count; i++)
|
||
for (int j = i; j > from && array[j - 1] > array[j]; j--)
|
||
swap(j, j - 1, array);
|
||
return;
|
||
}
|
||
|
||
// Determine a good median element.
|
||
int mid = count / 2;
|
||
int lo = from;
|
||
int hi = from + count - 1;
|
||
|
||
if (count > 40)
|
||
{ // big arrays, pseudomedian of 9
|
||
int s = count / 8;
|
||
lo = med3(lo, lo + s, lo + 2 * s, array);
|
||
mid = med3(mid - s, mid, mid + s, array);
|
||
hi = med3(hi - 2 * s, hi - s, hi, array);
|
||
}
|
||
mid = med3(lo, mid, hi, array);
|
||
|
||
int a, b, c, d;
|
||
int comp;
|
||
|
||
// Pull the median element out of the fray, and use it as a pivot.
|
||
swap(from, mid, array);
|
||
a = b = from;
|
||
c = d = from + count - 1;
|
||
|
||
// Repeatedly move b and c to each other, swapping elements so
|
||
// that all elements before index b are less than the pivot, and all
|
||
// elements after index c are greater than the pivot. a and b track
|
||
// the elements equal to the pivot.
|
||
while (true)
|
||
{
|
||
while (b <= c && (comp = array[b] - array[from]) <= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(a, b, array);
|
||
a++;
|
||
}
|
||
b++;
|
||
}
|
||
while (c >= b && (comp = array[c] - array[from]) >= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(c, d, array);
|
||
d--;
|
||
}
|
||
c--;
|
||
}
|
||
if (b > c)
|
||
break;
|
||
swap(b, c, array);
|
||
b++;
|
||
c--;
|
||
}
|
||
|
||
// Swap pivot(s) back in place, the recurse on left and right sections.
|
||
hi = from + count;
|
||
int span;
|
||
span = Math.min(a - from, b - a);
|
||
vecswap(from, b - span, span, array);
|
||
|
||
span = Math.min(d - c, hi - d - 1);
|
||
vecswap(b, hi - span, span, array);
|
||
|
||
span = b - a;
|
||
if (span > 1)
|
||
qsort(array, from, span);
|
||
|
||
span = d - c;
|
||
if (span > 1)
|
||
qsort(array, hi - span, span);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the char array to sort
|
||
*/
|
||
public static void sort(char[] a)
|
||
{
|
||
qsort(a, 0, a.length);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the char array to sort
|
||
* @param fromIndex the first index to sort (inclusive)
|
||
* @param toIndex the last index to sort (exclusive)
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void sort(char[] a, int fromIndex, int toIndex)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
if (fromIndex < 0)
|
||
throw new ArrayIndexOutOfBoundsException();
|
||
qsort(a, fromIndex, toIndex - fromIndex);
|
||
}
|
||
|
||
/**
|
||
* Finds the index of the median of three array elements.
|
||
*
|
||
* @param a the first index
|
||
* @param b the second index
|
||
* @param c the third index
|
||
* @param d the array
|
||
* @return the index (a, b, or c) which has the middle value of the three
|
||
*/
|
||
private static int med3(int a, int b, int c, char[] d)
|
||
{
|
||
return (d[a] < d[b]
|
||
? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
|
||
: (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
|
||
}
|
||
|
||
/**
|
||
* Swaps the elements at two locations of an array
|
||
*
|
||
* @param i the first index
|
||
* @param j the second index
|
||
* @param a the array
|
||
*/
|
||
private static void swap(int i, int j, char[] a)
|
||
{
|
||
char c = a[i];
|
||
a[i] = a[j];
|
||
a[j] = c;
|
||
}
|
||
|
||
/**
|
||
* Swaps two ranges of an array.
|
||
*
|
||
* @param i the first range start
|
||
* @param j the second range start
|
||
* @param n the element count
|
||
* @param a the array
|
||
*/
|
||
private static void vecswap(int i, int j, int n, char[] a)
|
||
{
|
||
for ( ; n > 0; i++, j++, n--)
|
||
swap(i, j, a);
|
||
}
|
||
|
||
/**
|
||
* Performs a recursive modified quicksort.
|
||
*
|
||
* @param array the array to sort
|
||
* @param from the start index (inclusive)
|
||
* @param count the number of elements to sort
|
||
*/
|
||
private static void qsort(char[] array, int from, int count)
|
||
{
|
||
// Use an insertion sort on small arrays.
|
||
if (count <= 7)
|
||
{
|
||
for (int i = from + 1; i < from + count; i++)
|
||
for (int j = i; j > from && array[j - 1] > array[j]; j--)
|
||
swap(j, j - 1, array);
|
||
return;
|
||
}
|
||
|
||
// Determine a good median element.
|
||
int mid = count / 2;
|
||
int lo = from;
|
||
int hi = from + count - 1;
|
||
|
||
if (count > 40)
|
||
{ // big arrays, pseudomedian of 9
|
||
int s = count / 8;
|
||
lo = med3(lo, lo + s, lo + 2 * s, array);
|
||
mid = med3(mid - s, mid, mid + s, array);
|
||
hi = med3(hi - 2 * s, hi - s, hi, array);
|
||
}
|
||
mid = med3(lo, mid, hi, array);
|
||
|
||
int a, b, c, d;
|
||
int comp;
|
||
|
||
// Pull the median element out of the fray, and use it as a pivot.
|
||
swap(from, mid, array);
|
||
a = b = from;
|
||
c = d = from + count - 1;
|
||
|
||
// Repeatedly move b and c to each other, swapping elements so
|
||
// that all elements before index b are less than the pivot, and all
|
||
// elements after index c are greater than the pivot. a and b track
|
||
// the elements equal to the pivot.
|
||
while (true)
|
||
{
|
||
while (b <= c && (comp = array[b] - array[from]) <= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(a, b, array);
|
||
a++;
|
||
}
|
||
b++;
|
||
}
|
||
while (c >= b && (comp = array[c] - array[from]) >= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(c, d, array);
|
||
d--;
|
||
}
|
||
c--;
|
||
}
|
||
if (b > c)
|
||
break;
|
||
swap(b, c, array);
|
||
b++;
|
||
c--;
|
||
}
|
||
|
||
// Swap pivot(s) back in place, the recurse on left and right sections.
|
||
hi = from + count;
|
||
int span;
|
||
span = Math.min(a - from, b - a);
|
||
vecswap(from, b - span, span, array);
|
||
|
||
span = Math.min(d - c, hi - d - 1);
|
||
vecswap(b, hi - span, span, array);
|
||
|
||
span = b - a;
|
||
if (span > 1)
|
||
qsort(array, from, span);
|
||
|
||
span = d - c;
|
||
if (span > 1)
|
||
qsort(array, hi - span, span);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the short array to sort
|
||
*/
|
||
public static void sort(short[] a)
|
||
{
|
||
qsort(a, 0, a.length);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the short array to sort
|
||
* @param fromIndex the first index to sort (inclusive)
|
||
* @param toIndex the last index to sort (exclusive)
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void sort(short[] a, int fromIndex, int toIndex)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
if (fromIndex < 0)
|
||
throw new ArrayIndexOutOfBoundsException();
|
||
qsort(a, fromIndex, toIndex - fromIndex);
|
||
}
|
||
|
||
/**
|
||
* Finds the index of the median of three array elements.
|
||
*
|
||
* @param a the first index
|
||
* @param b the second index
|
||
* @param c the third index
|
||
* @param d the array
|
||
* @return the index (a, b, or c) which has the middle value of the three
|
||
*/
|
||
private static int med3(int a, int b, int c, short[] d)
|
||
{
|
||
return (d[a] < d[b]
|
||
? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
|
||
: (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
|
||
}
|
||
|
||
/**
|
||
* Swaps the elements at two locations of an array
|
||
*
|
||
* @param i the first index
|
||
* @param j the second index
|
||
* @param a the array
|
||
*/
|
||
private static void swap(int i, int j, short[] a)
|
||
{
|
||
short c = a[i];
|
||
a[i] = a[j];
|
||
a[j] = c;
|
||
}
|
||
|
||
/**
|
||
* Swaps two ranges of an array.
|
||
*
|
||
* @param i the first range start
|
||
* @param j the second range start
|
||
* @param n the element count
|
||
* @param a the array
|
||
*/
|
||
private static void vecswap(int i, int j, int n, short[] a)
|
||
{
|
||
for ( ; n > 0; i++, j++, n--)
|
||
swap(i, j, a);
|
||
}
|
||
|
||
/**
|
||
* Performs a recursive modified quicksort.
|
||
*
|
||
* @param array the array to sort
|
||
* @param from the start index (inclusive)
|
||
* @param count the number of elements to sort
|
||
*/
|
||
private static void qsort(short[] array, int from, int count)
|
||
{
|
||
// Use an insertion sort on small arrays.
|
||
if (count <= 7)
|
||
{
|
||
for (int i = from + 1; i < from + count; i++)
|
||
for (int j = i; j > from && array[j - 1] > array[j]; j--)
|
||
swap(j, j - 1, array);
|
||
return;
|
||
}
|
||
|
||
// Determine a good median element.
|
||
int mid = count / 2;
|
||
int lo = from;
|
||
int hi = from + count - 1;
|
||
|
||
if (count > 40)
|
||
{ // big arrays, pseudomedian of 9
|
||
int s = count / 8;
|
||
lo = med3(lo, lo + s, lo + 2 * s, array);
|
||
mid = med3(mid - s, mid, mid + s, array);
|
||
hi = med3(hi - 2 * s, hi - s, hi, array);
|
||
}
|
||
mid = med3(lo, mid, hi, array);
|
||
|
||
int a, b, c, d;
|
||
int comp;
|
||
|
||
// Pull the median element out of the fray, and use it as a pivot.
|
||
swap(from, mid, array);
|
||
a = b = from;
|
||
c = d = from + count - 1;
|
||
|
||
// Repeatedly move b and c to each other, swapping elements so
|
||
// that all elements before index b are less than the pivot, and all
|
||
// elements after index c are greater than the pivot. a and b track
|
||
// the elements equal to the pivot.
|
||
while (true)
|
||
{
|
||
while (b <= c && (comp = array[b] - array[from]) <= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(a, b, array);
|
||
a++;
|
||
}
|
||
b++;
|
||
}
|
||
while (c >= b && (comp = array[c] - array[from]) >= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(c, d, array);
|
||
d--;
|
||
}
|
||
c--;
|
||
}
|
||
if (b > c)
|
||
break;
|
||
swap(b, c, array);
|
||
b++;
|
||
c--;
|
||
}
|
||
|
||
// Swap pivot(s) back in place, the recurse on left and right sections.
|
||
hi = from + count;
|
||
int span;
|
||
span = Math.min(a - from, b - a);
|
||
vecswap(from, b - span, span, array);
|
||
|
||
span = Math.min(d - c, hi - d - 1);
|
||
vecswap(b, hi - span, span, array);
|
||
|
||
span = b - a;
|
||
if (span > 1)
|
||
qsort(array, from, span);
|
||
|
||
span = d - c;
|
||
if (span > 1)
|
||
qsort(array, hi - span, span);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the int array to sort
|
||
*/
|
||
public static void sort(int[] a)
|
||
{
|
||
qsort(a, 0, a.length);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the int array to sort
|
||
* @param fromIndex the first index to sort (inclusive)
|
||
* @param toIndex the last index to sort (exclusive)
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void sort(int[] a, int fromIndex, int toIndex)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
if (fromIndex < 0)
|
||
throw new ArrayIndexOutOfBoundsException();
|
||
qsort(a, fromIndex, toIndex - fromIndex);
|
||
}
|
||
|
||
/**
|
||
* Finds the index of the median of three array elements.
|
||
*
|
||
* @param a the first index
|
||
* @param b the second index
|
||
* @param c the third index
|
||
* @param d the array
|
||
* @return the index (a, b, or c) which has the middle value of the three
|
||
*/
|
||
private static int med3(int a, int b, int c, int[] d)
|
||
{
|
||
return (d[a] < d[b]
|
||
? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
|
||
: (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
|
||
}
|
||
|
||
/**
|
||
* Swaps the elements at two locations of an array
|
||
*
|
||
* @param i the first index
|
||
* @param j the second index
|
||
* @param a the array
|
||
*/
|
||
private static void swap(int i, int j, int[] a)
|
||
{
|
||
int c = a[i];
|
||
a[i] = a[j];
|
||
a[j] = c;
|
||
}
|
||
|
||
/**
|
||
* Swaps two ranges of an array.
|
||
*
|
||
* @param i the first range start
|
||
* @param j the second range start
|
||
* @param n the element count
|
||
* @param a the array
|
||
*/
|
||
private static void vecswap(int i, int j, int n, int[] a)
|
||
{
|
||
for ( ; n > 0; i++, j++, n--)
|
||
swap(i, j, a);
|
||
}
|
||
|
||
/**
|
||
* Compares two integers in natural order, since a - b is inadequate.
|
||
*
|
||
* @param a the first int
|
||
* @param b the second int
|
||
* @return < 0, 0, or > 0 accorting to the comparison
|
||
*/
|
||
private static int compare(int a, int b)
|
||
{
|
||
return a < b ? -1 : a == b ? 0 : 1;
|
||
}
|
||
|
||
/**
|
||
* Performs a recursive modified quicksort.
|
||
*
|
||
* @param array the array to sort
|
||
* @param from the start index (inclusive)
|
||
* @param count the number of elements to sort
|
||
*/
|
||
private static void qsort(int[] array, int from, int count)
|
||
{
|
||
// Use an insertion sort on small arrays.
|
||
if (count <= 7)
|
||
{
|
||
for (int i = from + 1; i < from + count; i++)
|
||
for (int j = i; j > from && array[j - 1] > array[j]; j--)
|
||
swap(j, j - 1, array);
|
||
return;
|
||
}
|
||
|
||
// Determine a good median element.
|
||
int mid = count / 2;
|
||
int lo = from;
|
||
int hi = from + count - 1;
|
||
|
||
if (count > 40)
|
||
{ // big arrays, pseudomedian of 9
|
||
int s = count / 8;
|
||
lo = med3(lo, lo + s, lo + 2 * s, array);
|
||
mid = med3(mid - s, mid, mid + s, array);
|
||
hi = med3(hi - 2 * s, hi - s, hi, array);
|
||
}
|
||
mid = med3(lo, mid, hi, array);
|
||
|
||
int a, b, c, d;
|
||
int comp;
|
||
|
||
// Pull the median element out of the fray, and use it as a pivot.
|
||
swap(from, mid, array);
|
||
a = b = from;
|
||
c = d = from + count - 1;
|
||
|
||
// Repeatedly move b and c to each other, swapping elements so
|
||
// that all elements before index b are less than the pivot, and all
|
||
// elements after index c are greater than the pivot. a and b track
|
||
// the elements equal to the pivot.
|
||
while (true)
|
||
{
|
||
while (b <= c && (comp = compare(array[b], array[from])) <= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(a, b, array);
|
||
a++;
|
||
}
|
||
b++;
|
||
}
|
||
while (c >= b && (comp = compare(array[c], array[from])) >= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(c, d, array);
|
||
d--;
|
||
}
|
||
c--;
|
||
}
|
||
if (b > c)
|
||
break;
|
||
swap(b, c, array);
|
||
b++;
|
||
c--;
|
||
}
|
||
|
||
// Swap pivot(s) back in place, the recurse on left and right sections.
|
||
hi = from + count;
|
||
int span;
|
||
span = Math.min(a - from, b - a);
|
||
vecswap(from, b - span, span, array);
|
||
|
||
span = Math.min(d - c, hi - d - 1);
|
||
vecswap(b, hi - span, span, array);
|
||
|
||
span = b - a;
|
||
if (span > 1)
|
||
qsort(array, from, span);
|
||
|
||
span = d - c;
|
||
if (span > 1)
|
||
qsort(array, hi - span, span);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the long array to sort
|
||
*/
|
||
public static void sort(long[] a)
|
||
{
|
||
qsort(a, 0, a.length);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the long array to sort
|
||
* @param fromIndex the first index to sort (inclusive)
|
||
* @param toIndex the last index to sort (exclusive)
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void sort(long[] a, int fromIndex, int toIndex)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
if (fromIndex < 0)
|
||
throw new ArrayIndexOutOfBoundsException();
|
||
qsort(a, fromIndex, toIndex - fromIndex);
|
||
}
|
||
|
||
/**
|
||
* Finds the index of the median of three array elements.
|
||
*
|
||
* @param a the first index
|
||
* @param b the second index
|
||
* @param c the third index
|
||
* @param d the array
|
||
* @return the index (a, b, or c) which has the middle value of the three
|
||
*/
|
||
private static int med3(int a, int b, int c, long[] d)
|
||
{
|
||
return (d[a] < d[b]
|
||
? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
|
||
: (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
|
||
}
|
||
|
||
/**
|
||
* Swaps the elements at two locations of an array
|
||
*
|
||
* @param i the first index
|
||
* @param j the second index
|
||
* @param a the array
|
||
*/
|
||
private static void swap(int i, int j, long[] a)
|
||
{
|
||
long c = a[i];
|
||
a[i] = a[j];
|
||
a[j] = c;
|
||
}
|
||
|
||
/**
|
||
* Swaps two ranges of an array.
|
||
*
|
||
* @param i the first range start
|
||
* @param j the second range start
|
||
* @param n the element count
|
||
* @param a the array
|
||
*/
|
||
private static void vecswap(int i, int j, int n, long[] a)
|
||
{
|
||
for ( ; n > 0; i++, j++, n--)
|
||
swap(i, j, a);
|
||
}
|
||
|
||
/**
|
||
* Compares two longs in natural order, since a - b is inadequate.
|
||
*
|
||
* @param a the first long
|
||
* @param b the second long
|
||
* @return < 0, 0, or > 0 accorting to the comparison
|
||
*/
|
||
private static int compare(long a, long b)
|
||
{
|
||
return a < b ? -1 : a == b ? 0 : 1;
|
||
}
|
||
|
||
/**
|
||
* Performs a recursive modified quicksort.
|
||
*
|
||
* @param array the array to sort
|
||
* @param from the start index (inclusive)
|
||
* @param count the number of elements to sort
|
||
*/
|
||
private static void qsort(long[] array, int from, int count)
|
||
{
|
||
// Use an insertion sort on small arrays.
|
||
if (count <= 7)
|
||
{
|
||
for (int i = from + 1; i < from + count; i++)
|
||
for (int j = i; j > from && array[j - 1] > array[j]; j--)
|
||
swap(j, j - 1, array);
|
||
return;
|
||
}
|
||
|
||
// Determine a good median element.
|
||
int mid = count / 2;
|
||
int lo = from;
|
||
int hi = from + count - 1;
|
||
|
||
if (count > 40)
|
||
{ // big arrays, pseudomedian of 9
|
||
int s = count / 8;
|
||
lo = med3(lo, lo + s, lo + 2 * s, array);
|
||
mid = med3(mid - s, mid, mid + s, array);
|
||
hi = med3(hi - 2 * s, hi - s, hi, array);
|
||
}
|
||
mid = med3(lo, mid, hi, array);
|
||
|
||
int a, b, c, d;
|
||
int comp;
|
||
|
||
// Pull the median element out of the fray, and use it as a pivot.
|
||
swap(from, mid, array);
|
||
a = b = from;
|
||
c = d = from + count - 1;
|
||
|
||
// Repeatedly move b and c to each other, swapping elements so
|
||
// that all elements before index b are less than the pivot, and all
|
||
// elements after index c are greater than the pivot. a and b track
|
||
// the elements equal to the pivot.
|
||
while (true)
|
||
{
|
||
while (b <= c && (comp = compare(array[b], array[from])) <= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(a, b, array);
|
||
a++;
|
||
}
|
||
b++;
|
||
}
|
||
while (c >= b && (comp = compare(array[c], array[from])) >= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(c, d, array);
|
||
d--;
|
||
}
|
||
c--;
|
||
}
|
||
if (b > c)
|
||
break;
|
||
swap(b, c, array);
|
||
b++;
|
||
c--;
|
||
}
|
||
|
||
// Swap pivot(s) back in place, the recurse on left and right sections.
|
||
hi = from + count;
|
||
int span;
|
||
span = Math.min(a - from, b - a);
|
||
vecswap(from, b - span, span, array);
|
||
|
||
span = Math.min(d - c, hi - d - 1);
|
||
vecswap(b, hi - span, span, array);
|
||
|
||
span = b - a;
|
||
if (span > 1)
|
||
qsort(array, from, span);
|
||
|
||
span = d - c;
|
||
if (span > 1)
|
||
qsort(array, hi - span, span);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the float array to sort
|
||
*/
|
||
public static void sort(float[] a)
|
||
{
|
||
qsort(a, 0, a.length);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the float array to sort
|
||
* @param fromIndex the first index to sort (inclusive)
|
||
* @param toIndex the last index to sort (exclusive)
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void sort(float[] a, int fromIndex, int toIndex)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
if (fromIndex < 0)
|
||
throw new ArrayIndexOutOfBoundsException();
|
||
qsort(a, fromIndex, toIndex - fromIndex);
|
||
}
|
||
|
||
/**
|
||
* Finds the index of the median of three array elements.
|
||
*
|
||
* @param a the first index
|
||
* @param b the second index
|
||
* @param c the third index
|
||
* @param d the array
|
||
* @return the index (a, b, or c) which has the middle value of the three
|
||
*/
|
||
private static int med3(int a, int b, int c, float[] d)
|
||
{
|
||
return (Float.compare(d[a], d[b]) < 0
|
||
? (Float.compare(d[b], d[c]) < 0 ? b
|
||
: Float.compare(d[a], d[c]) < 0 ? c : a)
|
||
: (Float.compare(d[b], d[c]) > 0 ? b
|
||
: Float.compare(d[a], d[c]) > 0 ? c : a));
|
||
}
|
||
|
||
/**
|
||
* Swaps the elements at two locations of an array
|
||
*
|
||
* @param i the first index
|
||
* @param j the second index
|
||
* @param a the array
|
||
*/
|
||
private static void swap(int i, int j, float[] a)
|
||
{
|
||
float c = a[i];
|
||
a[i] = a[j];
|
||
a[j] = c;
|
||
}
|
||
|
||
/**
|
||
* Swaps two ranges of an array.
|
||
*
|
||
* @param i the first range start
|
||
* @param j the second range start
|
||
* @param n the element count
|
||
* @param a the array
|
||
*/
|
||
private static void vecswap(int i, int j, int n, float[] a)
|
||
{
|
||
for ( ; n > 0; i++, j++, n--)
|
||
swap(i, j, a);
|
||
}
|
||
|
||
/**
|
||
* Performs a recursive modified quicksort.
|
||
*
|
||
* @param array the array to sort
|
||
* @param from the start index (inclusive)
|
||
* @param count the number of elements to sort
|
||
*/
|
||
private static void qsort(float[] array, int from, int count)
|
||
{
|
||
// Use an insertion sort on small arrays.
|
||
if (count <= 7)
|
||
{
|
||
for (int i = from + 1; i < from + count; i++)
|
||
for (int j = i;
|
||
j > from && Float.compare(array[j - 1], array[j]) > 0;
|
||
j--)
|
||
{
|
||
swap(j, j - 1, array);
|
||
}
|
||
return;
|
||
}
|
||
|
||
// Determine a good median element.
|
||
int mid = count / 2;
|
||
int lo = from;
|
||
int hi = from + count - 1;
|
||
|
||
if (count > 40)
|
||
{ // big arrays, pseudomedian of 9
|
||
int s = count / 8;
|
||
lo = med3(lo, lo + s, lo + 2 * s, array);
|
||
mid = med3(mid - s, mid, mid + s, array);
|
||
hi = med3(hi - 2 * s, hi - s, hi, array);
|
||
}
|
||
mid = med3(lo, mid, hi, array);
|
||
|
||
int a, b, c, d;
|
||
int comp;
|
||
|
||
// Pull the median element out of the fray, and use it as a pivot.
|
||
swap(from, mid, array);
|
||
a = b = from;
|
||
c = d = from + count - 1;
|
||
|
||
// Repeatedly move b and c to each other, swapping elements so
|
||
// that all elements before index b are less than the pivot, and all
|
||
// elements after index c are greater than the pivot. a and b track
|
||
// the elements equal to the pivot.
|
||
while (true)
|
||
{
|
||
while (b <= c && (comp = Float.compare(array[b], array[from])) <= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(a, b, array);
|
||
a++;
|
||
}
|
||
b++;
|
||
}
|
||
while (c >= b && (comp = Float.compare(array[c], array[from])) >= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(c, d, array);
|
||
d--;
|
||
}
|
||
c--;
|
||
}
|
||
if (b > c)
|
||
break;
|
||
swap(b, c, array);
|
||
b++;
|
||
c--;
|
||
}
|
||
|
||
// Swap pivot(s) back in place, the recurse on left and right sections.
|
||
hi = from + count;
|
||
int span;
|
||
span = Math.min(a - from, b - a);
|
||
vecswap(from, b - span, span, array);
|
||
|
||
span = Math.min(d - c, hi - d - 1);
|
||
vecswap(b, hi - span, span, array);
|
||
|
||
span = b - a;
|
||
if (span > 1)
|
||
qsort(array, from, span);
|
||
|
||
span = d - c;
|
||
if (span > 1)
|
||
qsort(array, hi - span, span);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the double array to sort
|
||
*/
|
||
public static void sort(double[] a)
|
||
{
|
||
qsort(a, 0, a.length);
|
||
}
|
||
|
||
/**
|
||
* Performs a stable sort on the elements, arranging them according to their
|
||
* natural order.
|
||
*
|
||
* @param a the double array to sort
|
||
* @param fromIndex the first index to sort (inclusive)
|
||
* @param toIndex the last index to sort (exclusive)
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0
|
||
* || toIndex > a.length
|
||
*/
|
||
public static void sort(double[] a, int fromIndex, int toIndex)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException();
|
||
if (fromIndex < 0)
|
||
throw new ArrayIndexOutOfBoundsException();
|
||
qsort(a, fromIndex, toIndex - fromIndex);
|
||
}
|
||
|
||
/**
|
||
* Finds the index of the median of three array elements.
|
||
*
|
||
* @param a the first index
|
||
* @param b the second index
|
||
* @param c the third index
|
||
* @param d the array
|
||
* @return the index (a, b, or c) which has the middle value of the three
|
||
*/
|
||
private static int med3(int a, int b, int c, double[] d)
|
||
{
|
||
return (Double.compare(d[a], d[b]) < 0
|
||
? (Double.compare(d[b], d[c]) < 0 ? b
|
||
: Double.compare(d[a], d[c]) < 0 ? c : a)
|
||
: (Double.compare(d[b], d[c]) > 0 ? b
|
||
: Double.compare(d[a], d[c]) > 0 ? c : a));
|
||
}
|
||
|
||
/**
|
||
* Swaps the elements at two locations of an array
|
||
*
|
||
* @param i the first index
|
||
* @param j the second index
|
||
* @param a the array
|
||
*/
|
||
private static void swap(int i, int j, double[] a)
|
||
{
|
||
double c = a[i];
|
||
a[i] = a[j];
|
||
a[j] = c;
|
||
}
|
||
|
||
/**
|
||
* Swaps two ranges of an array.
|
||
*
|
||
* @param i the first range start
|
||
* @param j the second range start
|
||
* @param n the element count
|
||
* @param a the array
|
||
*/
|
||
private static void vecswap(int i, int j, int n, double[] a)
|
||
{
|
||
for ( ; n > 0; i++, j++, n--)
|
||
swap(i, j, a);
|
||
}
|
||
|
||
/**
|
||
* Performs a recursive modified quicksort.
|
||
*
|
||
* @param array the array to sort
|
||
* @param from the start index (inclusive)
|
||
* @param count the number of elements to sort
|
||
*/
|
||
private static void qsort(double[] array, int from, int count)
|
||
{
|
||
// Use an insertion sort on small arrays.
|
||
if (count <= 7)
|
||
{
|
||
for (int i = from + 1; i < from + count; i++)
|
||
for (int j = i;
|
||
j > from && Double.compare(array[j - 1], array[j]) > 0;
|
||
j--)
|
||
{
|
||
swap(j, j - 1, array);
|
||
}
|
||
return;
|
||
}
|
||
|
||
// Determine a good median element.
|
||
int mid = count / 2;
|
||
int lo = from;
|
||
int hi = from + count - 1;
|
||
|
||
if (count > 40)
|
||
{ // big arrays, pseudomedian of 9
|
||
int s = count / 8;
|
||
lo = med3(lo, lo + s, lo + 2 * s, array);
|
||
mid = med3(mid - s, mid, mid + s, array);
|
||
hi = med3(hi - 2 * s, hi - s, hi, array);
|
||
}
|
||
mid = med3(lo, mid, hi, array);
|
||
|
||
int a, b, c, d;
|
||
int comp;
|
||
|
||
// Pull the median element out of the fray, and use it as a pivot.
|
||
swap(from, mid, array);
|
||
a = b = from;
|
||
c = d = from + count - 1;
|
||
|
||
// Repeatedly move b and c to each other, swapping elements so
|
||
// that all elements before index b are less than the pivot, and all
|
||
// elements after index c are greater than the pivot. a and b track
|
||
// the elements equal to the pivot.
|
||
while (true)
|
||
{
|
||
while (b <= c && (comp = Double.compare(array[b], array[from])) <= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(a, b, array);
|
||
a++;
|
||
}
|
||
b++;
|
||
}
|
||
while (c >= b && (comp = Double.compare(array[c], array[from])) >= 0)
|
||
{
|
||
if (comp == 0)
|
||
{
|
||
swap(c, d, array);
|
||
d--;
|
||
}
|
||
c--;
|
||
}
|
||
if (b > c)
|
||
break;
|
||
swap(b, c, array);
|
||
b++;
|
||
c--;
|
||
}
|
||
|
||
// Swap pivot(s) back in place, the recurse on left and right sections.
|
||
hi = from + count;
|
||
int span;
|
||
span = Math.min(a - from, b - a);
|
||
vecswap(from, b - span, span, array);
|
||
|
||
span = Math.min(d - c, hi - d - 1);
|
||
vecswap(b, hi - span, span, array);
|
||
|
||
span = b - a;
|
||
if (span > 1)
|
||
qsort(array, from, span);
|
||
|
||
span = d - c;
|
||
if (span > 1)
|
||
qsort(array, hi - span, span);
|
||
}
|
||
|
||
/**
|
||
* Sort an array of Objects according to their natural ordering. The sort is
|
||
* guaranteed to be stable, that is, equal elements will not be reordered.
|
||
* The sort algorithm is a mergesort with the merge omitted if the last
|
||
* element of one half comes before the first element of the other half. This
|
||
* algorithm gives guaranteed O(n*log(n)) time, at the expense of making a
|
||
* copy of the array.
|
||
*
|
||
* @param a the array to be sorted
|
||
* @throws ClassCastException if any two elements are not mutually
|
||
* comparable
|
||
* @throws NullPointerException if an element is null (since
|
||
* null.compareTo cannot work)
|
||
* @see Comparable
|
||
*/
|
||
public static void sort(Object[] a)
|
||
{
|
||
sort(a, 0, a.length, null);
|
||
}
|
||
|
||
/**
|
||
* Sort an array of Objects according to a Comparator. The sort is
|
||
* guaranteed to be stable, that is, equal elements will not be reordered.
|
||
* The sort algorithm is a mergesort with the merge omitted if the last
|
||
* element of one half comes before the first element of the other half. This
|
||
* algorithm gives guaranteed O(n*log(n)) time, at the expense of making a
|
||
* copy of the array.
|
||
*
|
||
* @param a the array to be sorted
|
||
* @param c a Comparator to use in sorting the array; or null to indicate
|
||
* the elements' natural order
|
||
* @throws ClassCastException if any two elements are not mutually
|
||
* comparable by the Comparator provided
|
||
* @throws NullPointerException if a null element is compared with natural
|
||
* ordering (only possible when c is null)
|
||
*/
|
||
public static void sort(Object[] a, Comparator c)
|
||
{
|
||
sort(a, 0, a.length, c);
|
||
}
|
||
|
||
/**
|
||
* Sort an array of Objects according to their natural ordering. The sort is
|
||
* guaranteed to be stable, that is, equal elements will not be reordered.
|
||
* The sort algorithm is a mergesort with the merge omitted if the last
|
||
* element of one half comes before the first element of the other half. This
|
||
* algorithm gives guaranteed O(n*log(n)) time, at the expense of making a
|
||
* copy of the array.
|
||
*
|
||
* @param a the array to be sorted
|
||
* @param fromIndex the index of the first element to be sorted
|
||
* @param toIndex the index of the last element to be sorted plus one
|
||
* @throws ClassCastException if any two elements are not mutually
|
||
* comparable
|
||
* @throws NullPointerException if an element is null (since
|
||
* null.compareTo cannot work)
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex and toIndex
|
||
* are not in range.
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
*/
|
||
public static void sort(Object[] a, int fromIndex, int toIndex)
|
||
{
|
||
sort(a, fromIndex, toIndex, null);
|
||
}
|
||
|
||
/**
|
||
* Sort an array of Objects according to a Comparator. The sort is
|
||
* guaranteed to be stable, that is, equal elements will not be reordered.
|
||
* The sort algorithm is a mergesort with the merge omitted if the last
|
||
* element of one half comes before the first element of the other half. This
|
||
* algorithm gives guaranteed O(n*log(n)) time, at the expense of making a
|
||
* copy of the array.
|
||
*
|
||
* @param a the array to be sorted
|
||
* @param fromIndex the index of the first element to be sorted
|
||
* @param toIndex the index of the last element to be sorted plus one
|
||
* @param c a Comparator to use in sorting the array; or null to indicate
|
||
* the elements' natural order
|
||
* @throws ClassCastException if any two elements are not mutually
|
||
* comparable by the Comparator provided
|
||
* @throws ArrayIndexOutOfBoundsException if fromIndex and toIndex
|
||
* are not in range.
|
||
* @throws IllegalArgumentException if fromIndex > toIndex
|
||
* @throws NullPointerException if a null element is compared with natural
|
||
* ordering (only possible when c is null)
|
||
*/
|
||
public static void sort(Object[] a, int fromIndex, int toIndex, Comparator c)
|
||
{
|
||
if (fromIndex > toIndex)
|
||
throw new IllegalArgumentException("fromIndex " + fromIndex
|
||
+ " > toIndex " + toIndex);
|
||
if (fromIndex < 0)
|
||
throw new ArrayIndexOutOfBoundsException();
|
||
|
||
// In general, the code attempts to be simple rather than fast, the
|
||
// idea being that a good optimising JIT will be able to optimise it
|
||
// better than I can, and if I try it will make it more confusing for
|
||
// the JIT. First presort the array in chunks of length 6 with insertion
|
||
// sort. A mergesort would give too much overhead for this length.
|
||
for (int chunk = fromIndex; chunk < toIndex; chunk += 6)
|
||
{
|
||
int end = Math.min(chunk + 6, toIndex);
|
||
for (int i = chunk + 1; i < end; i++)
|
||
{
|
||
if (Collections.compare(a[i - 1], a[i], c) > 0)
|
||
{
|
||
// not already sorted
|
||
int j = i;
|
||
Object elem = a[j];
|
||
do
|
||
{
|
||
a[j] = a[j - 1];
|
||
j--;
|
||
}
|
||
while (j > chunk
|
||
&& Collections.compare(a[j - 1], elem, c) > 0);
|
||
a[j] = elem;
|
||
}
|
||
}
|
||
}
|
||
|
||
int len = toIndex - fromIndex;
|
||
// If length is smaller or equal 6 we are done.
|
||
if (len <= 6)
|
||
return;
|
||
|
||
Object[] src = a;
|
||
Object[] dest = new Object[len];
|
||
Object[] t = null; // t is used for swapping src and dest
|
||
|
||
// The difference of the fromIndex of the src and dest array.
|
||
int srcDestDiff = -fromIndex;
|
||
|
||
// The merges are done in this loop
|
||
for (int size = 6; size < len; size <<= 1)
|
||
{
|
||
for (int start = fromIndex; start < toIndex; start += size << 1)
|
||
{
|
||
// mid is the start of the second sublist;
|
||
// end the start of the next sublist (or end of array).
|
||
int mid = start + size;
|
||
int end = Math.min(toIndex, mid + size);
|
||
|
||
// The second list is empty or the elements are already in
|
||
// order - no need to merge
|
||
if (mid >= end
|
||
|| Collections.compare(src[mid - 1], src[mid], c) <= 0)
|
||
{
|
||
System.arraycopy(src, start,
|
||
dest, start + srcDestDiff, end - start);
|
||
|
||
// The two halves just need swapping - no need to merge
|
||
}
|
||
else if (Collections.compare(src[start], src[end - 1], c) > 0)
|
||
{
|
||
System.arraycopy(src, start,
|
||
dest, end - size + srcDestDiff, size);
|
||
System.arraycopy(src, mid,
|
||
dest, start + srcDestDiff, end - mid);
|
||
|
||
}
|
||
else
|
||
{
|
||
// Declare a lot of variables to save repeating
|
||
// calculations. Hopefully a decent JIT will put these
|
||
// in registers and make this fast
|
||
int p1 = start;
|
||
int p2 = mid;
|
||
int i = start + srcDestDiff;
|
||
|
||
// The main merge loop; terminates as soon as either
|
||
// half is ended
|
||
while (p1 < mid && p2 < end)
|
||
{
|
||
dest[i++] =
|
||
src[(Collections.compare(src[p1], src[p2], c) <= 0
|
||
? p1++ : p2++)];
|
||
}
|
||
|
||
// Finish up by copying the remainder of whichever half
|
||
// wasn't finished.
|
||
if (p1 < mid)
|
||
System.arraycopy(src, p1, dest, i, mid - p1);
|
||
else
|
||
System.arraycopy(src, p2, dest, i, end - p2);
|
||
}
|
||
}
|
||
// swap src and dest ready for the next merge
|
||
t = src;
|
||
src = dest;
|
||
dest = t;
|
||
fromIndex += srcDestDiff;
|
||
toIndex += srcDestDiff;
|
||
srcDestDiff = -srcDestDiff;
|
||
}
|
||
|
||
// make sure the result ends up back in the right place. Note
|
||
// that src and dest may have been swapped above, so src
|
||
// contains the sorted array.
|
||
if (src != a)
|
||
{
|
||
// Note that fromIndex == 0.
|
||
System.arraycopy(src, 0, a, srcDestDiff, toIndex);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Returns a list "view" of the specified array. This method is intended to
|
||
* make it easy to use the Collections API with existing array-based APIs and
|
||
* programs. Changes in the list or the array show up in both places. The
|
||
* list does not support element addition or removal, but does permit
|
||
* value modification. The returned list implements both Serializable and
|
||
* RandomAccess.
|
||
*
|
||
* @param a the array to return a view of
|
||
* @return a fixed-size list, changes to which "write through" to the array
|
||
* @see Serializable
|
||
* @see RandomAccess
|
||
* @see Arrays.ArrayList
|
||
*/
|
||
public static List asList(final Object[] a)
|
||
{
|
||
return new Arrays.ArrayList(a);
|
||
}
|
||
|
||
/**
|
||
* Inner class used by {@link #asList(Object[])} to provide a list interface
|
||
* to an array. The name, though it clashes with java.util.ArrayList, is
|
||
* Sun's choice for Serialization purposes. Element addition and removal
|
||
* is prohibited, but values can be modified.
|
||
*
|
||
* @author Eric Blake (ebb9@email.byu.edu)
|
||
* @status updated to 1.4
|
||
*/
|
||
private static final class ArrayList extends AbstractList
|
||
implements Serializable, RandomAccess
|
||
{
|
||
// We override the necessary methods, plus others which will be much
|
||
// more efficient with direct iteration rather than relying on iterator().
|
||
|
||
/**
|
||
* Compatible with JDK 1.4.
|
||
*/
|
||
private static final long serialVersionUID = -2764017481108945198L;
|
||
|
||
/**
|
||
* The array we are viewing.
|
||
* @serial the array
|
||
*/
|
||
private final Object[] a;
|
||
|
||
/**
|
||
* Construct a list view of the array.
|
||
* @param a the array to view
|
||
* @throws NullPointerException if a is null
|
||
*/
|
||
ArrayList(Object[] a)
|
||
{
|
||
// We have to explicitly check.
|
||
if (a == null)
|
||
throw new NullPointerException();
|
||
this.a = a;
|
||
}
|
||
|
||
/**
|
||
* Returns the object at the specified index in
|
||
* the array.
|
||
*
|
||
* @param index The index to retrieve an object from.
|
||
* @return The object at the array index specified.
|
||
*/
|
||
public Object get(int index)
|
||
{
|
||
return a[index];
|
||
}
|
||
|
||
/**
|
||
* Returns the size of the array.
|
||
*
|
||
* @return The size.
|
||
*/
|
||
public int size()
|
||
{
|
||
return a.length;
|
||
}
|
||
|
||
/**
|
||
* Replaces the object at the specified index
|
||
* with the supplied element.
|
||
*
|
||
* @param index The index at which to place the new object.
|
||
* @param element The new object.
|
||
* @return The object replaced by this operation.
|
||
*/
|
||
public Object set(int index, Object element)
|
||
{
|
||
Object old = a[index];
|
||
a[index] = element;
|
||
return old;
|
||
}
|
||
|
||
/**
|
||
* Returns true if the array contains the
|
||
* supplied object.
|
||
*
|
||
* @param o The object to look for.
|
||
* @return True if the object was found.
|
||
*/
|
||
public boolean contains(Object o)
|
||
{
|
||
return lastIndexOf(o) >= 0;
|
||
}
|
||
|
||
/**
|
||
* Returns the first index at which the
|
||
* object, o, occurs in the array.
|
||
*
|
||
* @param o The object to search for.
|
||
* @return The first relevant index.
|
||
*/
|
||
public int indexOf(Object o)
|
||
{
|
||
int size = a.length;
|
||
for (int i = 0; i < size; i++)
|
||
if (ArrayList.equals(o, a[i]))
|
||
return i;
|
||
return -1;
|
||
}
|
||
|
||
/**
|
||
* Returns the last index at which the
|
||
* object, o, occurs in the array.
|
||
*
|
||
* @param o The object to search for.
|
||
* @return The last relevant index.
|
||
*/
|
||
public int lastIndexOf(Object o)
|
||
{
|
||
int i = a.length;
|
||
while (--i >= 0)
|
||
if (ArrayList.equals(o, a[i]))
|
||
return i;
|
||
return -1;
|
||
}
|
||
|
||
/**
|
||
* Transforms the list into an array of
|
||
* objects, by simplying cloning the array
|
||
* wrapped by this list.
|
||
*
|
||
* @return A clone of the internal array.
|
||
*/
|
||
public Object[] toArray()
|
||
{
|
||
return (Object[]) a.clone();
|
||
}
|
||
|
||
/**
|
||
* Copies the objects from this list into
|
||
* the supplied array. The supplied array
|
||
* is shrunk or enlarged to the size of the
|
||
* internal array, and filled with its objects.
|
||
*
|
||
* @param array The array to fill with the objects in this list.
|
||
* @return The array containing the objects in this list,
|
||
* which may or may not be == to array.
|
||
*/
|
||
public Object[] toArray(Object[] array)
|
||
{
|
||
int size = a.length;
|
||
if (array.length < size)
|
||
array = (Object[])
|
||
Array.newInstance(array.getClass().getComponentType(), size);
|
||
else if (array.length > size)
|
||
array[size] = null;
|
||
|
||
System.arraycopy(a, 0, array, 0, size);
|
||
return array;
|
||
}
|
||
}
|
||
}
|