This is the mail archive of the java-patches@sources.redhat.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

PATCH: More vector tweaks


Minor fixes for vector. I've checked this patch in.

Vector could be made a bit more efficient yet by moving some of the core
functionality (indexOf, removeElementAt, insertElementAt) into private
implementation methods. This way the compiler can make inline them
easily. Right now I'm more concerned about correctness and simplicity.

regards

  [ bryce ]


2000-11-23  Bryce McKinlay  <bryce@albatross.co.nz>

	* java/util/Vector.java: Improve exception messages.
	(Vector): Check initialCapacity for IllegalArgumentException.
	(tromToSize): Don't check for elementCount == elementData.length
	case.
	(toArray): Don't try to set null marker if target array is the same
	length as the vector.

Index: Vector.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/util/Vector.java,v
retrieving revision 1.8
diff -u -r1.8 Vector.java
--- Vector.java	2000/11/22 11:59:59	1.8
+++ Vector.java	2000/11/23 05:10:12
@@ -115,6 +115,8 @@
    */
   public Vector(int initialCapacity, int capacityIncrement)
   {
+    if (initialCapacity < 0)
+      throw new IllegalArgumentException();
     elementData = new Object[initialCapacity];
     this.capacityIncrement = capacityIncrement;
   }
@@ -126,6 +128,8 @@
    */
   public Vector(int initialCapacity)
   {
+    if (initialCapacity < 0)
+      throw new IllegalArgumentException();
     elementData = new Object[initialCapacity];
   }
 
@@ -152,12 +156,11 @@
    */
   public synchronized void trimToSize()
   {
-    // Check if the Vector is already trimmed, to save execution time
-    if (elementCount == elementData.length)
-      return;
-    // Guess not
+    // Don't bother checking for the case where size() == the capacity of the
+    // vector since that is a much less likely case; it's more efficient to
+    // not do the check and lose a bit of performance in that infrequent case
 
-    Object[]newArray = new Object[elementCount];
+    Object[] newArray = new Object[elementCount];
     System.arraycopy(elementData, 0, newArray, 0, elementCount);
     elementData = newArray;
   }
@@ -296,7 +299,7 @@
   public synchronized int lastIndexOf(Object e, int index)
   {
     if (index >= elementCount)
-      throw new ArrayIndexOutOfBoundsException(index);
+      throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
 
     for (int i = index; i >= 0; i--)
       {
@@ -332,7 +335,7 @@
     //Within the bounds of this Vector does not necessarily mean within 
     //the bounds of the internal array
     if (index >= elementCount)
-      throw new ArrayIndexOutOfBoundsException(index);
+      throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
 
     return elementData[index];
   }
@@ -378,8 +381,8 @@
    */
   public synchronized void setElementAt(Object obj, int index)
   {
-    if ((index < 0) || (index >= elementCount))
-      throw new ArrayIndexOutOfBoundsException(index);
+    if (index >= elementCount)
+      throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
 
     elementData[index] = obj;
   }
@@ -397,7 +400,7 @@
   public synchronized Object set(int index, Object element)
   {
     if (index >= elementCount)
-      throw new ArrayIndexOutOfBoundsException(index);
+      throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
 
     Object temp = elementData[index];
     elementData[index] = element;
@@ -413,7 +416,7 @@
   public synchronized void removeElementAt(int index)
   {
     if (index >= elementCount)
-      throw new ArrayIndexOutOfBoundsException(index);
+      throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
 
     modCount++;
     elementCount--;
@@ -434,10 +437,11 @@
    */
   public void insertElementAt(Object obj, int index)
   {
-    if ((index < 0) || (index > elementCount))
-      throw new ArrayIndexOutOfBoundsException(index);
+    if (index > elementCount)
+      throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount);
 
-    ensureCapacity(++elementCount);
+    if (elementCount == elementData.length)
+      ensureCapacity(++elementCount);
     modCount++;
     System.arraycopy(elementData, index, elementData, index + 1,
 		     elementCount - 1 - index);
@@ -454,7 +458,8 @@
    */
   public synchronized void addElement(Object obj)
   {
-    ensureCapacity(elementCount + 1);
+    if (elementCount == elementData.length)
+      ensureCapacity(++elementCount);
     modCount++;
     elementData[elementCount++] = obj;
   }
@@ -488,7 +493,7 @@
     if (elementCount == 0)
       return;
 
-    for (int i = 0; i < elementCount; i++)
+    for (int i = elementCount - 1; i >= 0; --i)
       {
 	elementData[i] = null;
       }
@@ -553,7 +558,7 @@
     if (array.length < elementCount)
       array = (Object[]) Array.newInstance(array.getClass().getComponentType(), 
         				   elementCount);
-    else
+    else if (array.length > elementCount)
       array[elementCount] = null;
     System.arraycopy(elementData, 0, array, 0, elementCount);
     return array;
@@ -617,7 +622,7 @@
   public synchronized Object remove(int index)
   {
     if (index >= elementCount)
-      throw new ArrayIndexOutOfBoundsException(index);
+      throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
   
     Object temp = elementData[index];
     removeElementAt(index);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]