All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lambda.Debugger.VectorD Maven / Gradle / Ivy

The newest version!
/*                        VectorD.java

  Copyright 2003, Bil Lewis

  This program 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 of the License, or
  (at your option) any later version.
  
  This program 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 this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   
*/

/*

Use a duplicate of Sun's Vector class so that the ODB can load 
an instrumented version of Vector &c without blowing up.

And it's not synchronized. And it's final. (Faster)

But some of the Java classes appear to use Vectors. :-(


 * @(#)VectorD.java	1.71 00/04/18
 *
 * Copyright 1994-2000 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * This software is the proprietary information of Sun Microsystems, Inc.  
 * Use is subject to license terms.
 * 
 */

//package java.util;
package com.lambda.Debugger;

import java.util.*;

/**
 * The VectorD class implements a growable array of 
 * objects. Like an array, it contains components that can be 
 * accessed using an integer index. However, the size of a 
 * VectorD can grow or shrink as needed to accommodate 
 * adding and removing items after the VectorD has been created.

* * Each vector tries to optimize storage management by maintaining a * capacity and a capacityIncrement. The * capacity is always at least as large as the vector * size; it is usually larger because as components are added to the * vector, the vector's storage increases in chunks the size of * capacityIncrement. An application can increase the * capacity of a vector before inserting a large number of * components; this reduces the amount of incremental reallocation.

* * As of the Java 2 platform v1.2, this class has been retrofitted to * implement List, so that it becomes a part of Java's collection framework. * Unlike the new collection implementations, VectorD is .

* * The Iterators returned by VectorD's iterator and listIterator * methods are fail-fast: if the VectorD is structurally modified * at any time after the Iterator is created, in any way except through the * Iterator's own remove or add methods, the Iterator will throw a * ConcurrentModificationException. Thus, in the face of concurrent * modification, the Iterator fails quickly and cleanly, rather than risking * arbitrary, non-deterministic behavior at an undetermined time in the future. * The Enumerations returned by VectorD's elements method are not * fail-fast. * * @author Lee Boynton * @author Jonathan Payne * @version 1.71, 04/18/00 * @see Collection * @see List * @see ArrayList * @see LinkedList * @since JDK1.0 */ //public class VectorD extends MyAbstractList implements List, Cloneable, java.io.Serializable { public final class VectorD extends Vector { /** * The array buffer into which the components of the vector are * stored. The capacity of the vector is the length of this array buffer, * and is at least large enough to contain all the vector's elements.

* * Any array elements following the last element in the VectorD are null. * * @serial */ protected Object elementData[]; /** * The number of valid components in this VectorD object. * Components elementData[0] through * elementData[elementCount-1] are the actual items. * * @serial */ protected int elementCount; /** * The amount by which the capacity of the vector is automatically * incremented when its size becomes greater than its capacity. If * the capacity increment is less than or equal to zero, the capacity * of the vector is doubled each time it needs to grow. * * @serial */ protected int capacityIncrement; /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -2767605614048989439L; /** * Constructs an empty vector with the specified initial capacity and * capacity increment. * * @param initialCapacity the initial capacity of the vector. * @param capacityIncrement the amount by which the capacity is * increased when the vector overflows. * @exception IllegalArgumentException if the specified initial capacity * is negative */ public VectorD(int initialCapacity, int capacityIncrement) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; this.capacityIncrement = capacityIncrement; } /** * Constructs an empty vector with the specified initial capacity and * with its capacity increment equal to zero. * * @param initialCapacity the initial capacity of the vector. * @exception IllegalArgumentException if the specified initial capacity * is negative */ public VectorD(int initialCapacity) { this(initialCapacity, 0); } /** * Constructs an empty vector so that its internal data array * has size 10 and its standard capacity increment is * zero. */ public VectorD() { this(10); } /** * Constructs a vector containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. * * @param c the collection whose elements are to be placed into this * vector. * @since 1.2 */ public VectorD(Collection c) { elementCount = c.size(); elementData = new Object[(elementCount*110)/100]; // 10% for growth c.toArray(elementData); } /** * Copies the components of this vector into the specified array. The * item at index k in this vector is copied into component * k of anArray. The array must be big enough to hold * all the objects in this vector, else an * IndexOutOfBoundsException is thrown. * * @param anArray the array into which the components get copied. */ public void copyInto(Object anArray[]) { System.arraycopy(elementData, 0, anArray, 0, elementCount); } /** * Trims the capacity of this vector to be the vector's current * size. If the capacity of this cector is larger than its current * size, then the capacity is changed to equal the size by replacing * its internal data array, kept in the field elementData, * with a smaller one. An application can use this operation to * minimize the storage of a vector. */ public void trimToSize() { modCount++; int oldCapacity = elementData.length; if (elementCount < oldCapacity) { Object oldData[] = elementData; elementData = new Object[elementCount]; System.arraycopy(oldData, 0, elementData, 0, elementCount); } } /** * Increases the capacity of this vector, if necessary, to ensure * that it can hold at least the number of components specified by * the minimum capacity argument. * *

If the current capacity of this vector is less than * minCapacity, then its capacity is increased by replacing its * internal data array, kept in the field elementData, with a * larger one. The size of the new data array will be the old size plus * capacityIncrement, unless the value of * capacityIncrement is less than or equal to zero, in which case * the new capacity will be twice the old capacity; but if this new size * is still smaller than minCapacity, then the new capacity will * be minCapacity. * * @param minCapacity the desired minimum capacity. */ public void ensureCapacity(int minCapacity) { modCount++; ensureCapacityHelper(minCapacity); } /** * This implements the un semantics of ensureCapacity. * methods in this class can internally call this * method for ensuring capacity without incurring the cost of an * extra synchronization. * * @see java.util.VectorD#ensureCapacity(int) */ private void ensureCapacityHelper(int minCapacity) { int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { Object oldData[] = elementData; int newCapacity = (capacityIncrement > 0) ? (oldCapacity + capacityIncrement) : (oldCapacity * 4); if (newCapacity < minCapacity) { newCapacity = minCapacity; } elementData = new Object[newCapacity]; System.arraycopy(oldData, 0, elementData, 0, elementCount); } } /** * Sets the size of this vector. If the new size is greater than the * current size, new null items are added to the end of * the vector. If the new size is less than the current size, all * components at index newSize and greater are discarded. * * @param newSize the new size of this vector. * @throws ArrayIndexOutOfBoundsException if new size is negative. */ public void setSize(int newSize) { modCount++; if (newSize > elementCount) { ensureCapacityHelper(newSize); } else { for (int i = newSize ; i < elementCount ; i++) { elementData[i] = null; } } elementCount = newSize; } /** * Returns the current capacity of this vector. * * @return the current capacity (the length of its internal * data arary, kept in the field elementData * of this vector. */ public int capacity() { return elementData.length; } /** * Returns the number of components in this vector. * * @return the number of components in this vector. */ public int size() { return elementCount; } /** * Tests if this vector has no components. * * @return true if and only if this vector has * no components, that is, its size is zero; * false otherwise. */ public boolean isEmpty() { return elementCount == 0; } /** * Returns an enumeration of the components of this vector. The * returned Enumeration object will generate all items in * this vector. The first item generated is the item at index 0, * then the item at index 1, and so on. * * @return an enumeration of the components of this vector. * @see Enumeration * @see Iterator */ public Enumeration elements() { return new Enumeration() { int count = 0; public boolean hasMoreElements() { return count < elementCount; } public Object nextElement() { if (count < elementCount) { return elementData[count++]; } throw new NoSuchElementException("VectorD Enumeration"); } }; } /** * Tests if the specified object is a component in this vector. * * @param elem an object. * @return true if and only if the specified object * is the same as a component in this vector, as determined by the * equals method; false otherwise. */ public boolean contains(Object elem) { return indexOf(elem, 0) >= 0; } /** * Searches for the first occurence of the given argument, testing * for equality using the equals method. * * @param elem an object. * @return the index of the first occurrence of the argument in this * vector, that is, the smallest value k such that * elem.equals(elementData[k]) is true; * returns -1 if the object is not found. * @see Object#equals(Object) */ public int indexOf(Object elem) { return indexOf(elem, 0); } /** * Searches for the first occurence of the given argument, beginning * the search at index, and testing for equality using * the equals method. * * @param elem an object. * @param index the non-negative index to start searching from. * @return the index of the first occurrence of the object argument in * this vector at position index or later in the * vector, that is, the smallest value k such that * elem.equals(elementData[k]) and (k >= index) is * true; returns -1 if the object is not * found. (Returns -1 if index >= the * current size of this VectorD.) * @exception IndexOutOfBoundsException if index is negative. * @see Object#equals(Object) */ public int indexOf(Object elem, int index) { if (elem == null) { for (int i = index ; i < elementCount ; i++) if (elementData[i]==null) return i; } else { for (int i = index ; i < elementCount ; i++) if (elem == elementData[i]) return i; } return -1; } /** * Returns the index of the last occurrence of the specified object in * this vector. * * @param elem the desired component. * @return the index of the last occurrence of the specified object in * this vector, that is, the largest value k such that * elem.equals(elementData[k]) is true; * returns -1 if the object is not found. */ public int lastIndexOf(Object elem) { return lastIndexOf(elem, elementCount-1); } /** * Searches backwards for the specified object, starting from the * specified index, and returns an index to it. * * @param elem the desired component. * @param index the index to start searching from. * @return the index of the last occurrence of the specified object in this * vector at position less than or equal to index in * the vector, that is, the largest value k such that * elem.equals(elementData[k]) and (k <= index) is * true; -1 if the object is not found. * (Returns -1 if index is negative.) * @exception IndexOutOfBoundsException if index is greater * than or equal to the current size of this vector. */ public int lastIndexOf(Object elem, int index) { if (index >= elementCount) throw new IndexOutOfBoundsException(index + " >= "+ elementCount); if (elem == null) { for (int i = index; i >= 0; i--) if (elementData[i]==null) return i; } else { for (int i = index; i >= 0; i--) if (elem == elementData[i]) return i; } return -1; } /** * Returns the component at the specified index.

* * This method is identical in functionality to the get method * (which is part of the List interface). * * @param index an index into this vector. * @return the component at the specified index. * @exception ArrayIndexOutOfBoundsException if the index * is negative or not less than the current size of this * VectorD object. * given. * @see #get(int) * @see List */ public Object elementAt(int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } /* Since try/catch is free, except when the exception is thrown, put in this extra try/catch to catch negative indexes and display a more informative error message. This might not be appropriate, especially if we have a decent debugging environment - JP. */ try { return elementData[index]; } catch (ArrayIndexOutOfBoundsException e) { throw new ArrayIndexOutOfBoundsException(index + " < 0"); } } /** * Returns the first component (the item at index 0) of * this vector. * * @return the first component of this vector. * @exception NoSuchElementException if this vector has no components. */ public Object firstElement() { if (elementCount == 0) { throw new NoSuchElementException(); } return elementData[0]; } /** * Returns the last component of the vector. * * @return the last component of the vector, i.e., the component at index * size() - 1. * @exception NoSuchElementException if this vector is empty. */ public Object lastElement() { if (elementCount == 0) { throw new NoSuchElementException(); } return elementData[elementCount - 1]; } /** * Sets the component at the specified index of this * vector to be the specified object. The previous component at that * position is discarded.

* * The index must be a value greater than or equal to 0 * and less than the current size of the vector.

* * This method is identical in functionality to the set method * (which is part of the List interface). Note that the set method reverses * the order of the parameters, to more closely match array usage. Note * also that the set method returns the old value that was stored at the * specified position. * * @param obj what the component is to be set to. * @param index the specified index. * @exception ArrayIndexOutOfBoundsException if the index was invalid. * @see #size() * @see List * @see #set(int, java.lang.Object) */ public void setElementAt(Object obj, int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } elementData[index] = obj; } /** * Deletes the component at the specified index. Each component in * this vector with an index greater or equal to the specified * index is shifted downward to have an index one * smaller than the value it had previously. The size of this vector * is decreased by 1.

* * The index must be a value greater than or equal to 0 * and less than the current size of the vector.

* * This method is identical in functionality to the remove method * (which is part of the List interface). Note that the remove method * returns the old value that was stored at the specified position. * * @param index the index of the object to remove. * @exception ArrayIndexOutOfBoundsException if the index was invalid. * @see #size() * @see #remove(int) * @see List */ public void removeElementAt(int index) { modCount++; if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } else if (index < 0) { throw new ArrayIndexOutOfBoundsException(index); } int j = elementCount - index - 1; if (j > 0) { System.arraycopy(elementData, index + 1, elementData, index, j); } elementCount--; elementData[elementCount] = null; /* to let gc do its work */ } /** * Inserts the specified object as a component in this vector at the * specified index. Each component in this vector with * an index greater or equal to the specified index is * shifted upward to have an index one greater than the value it had * previously.

* * The index must be a value greater than or equal to 0 * and less than or equal to the current size of the vector. (If the * index is equal to the current size of the vector, the new element * is appended to the VectorD.)

* * This method is identical in functionality to the add(Object, int) method * (which is part of the List interface). Note that the add method reverses * the order of the parameters, to more closely match array usage. * * @param obj the component to insert. * @param index where to insert the new component. * @exception ArrayIndexOutOfBoundsException if the index was invalid. * @see #size() * @see #add(int, Object) * @see List */ public void insertElementAt(Object obj, int index) { modCount++; if (index >= elementCount + 1) { throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount); } ensureCapacityHelper(elementCount + 1); System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); elementData[index] = obj; elementCount++; } /** * Adds the specified component to the end of this vector, * increasing its size by one. The capacity of this vector is * increased if its size becomes greater than its capacity.

* * This method is identical in functionality to the add(Object) method * (which is part of the List interface). * * @param obj the component to be added. * @see #add(Object) * @see List */ public void addElement(Object obj) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = obj; } /** * Removes the first (lowest-indexed) occurrence of the argument * from this vector. If the object is found in this vector, each * component in the vector with an index greater or equal to the * object's index is shifted downward to have an index one smaller * than the value it had previously.

* * This method is identical in functionality to the remove(Object) * method (which is part of the List interface). * * @param obj the component to be removed. * @return true if the argument was a component of this * vector; false otherwise. * @see List#remove(Object) * @see List */ public boolean removeElement(Object obj) { modCount++; int i = indexOf(obj); if (i >= 0) { removeElementAt(i); return true; } return false; } /** * Removes all components from this vector and sets its size to zero.

* * This method is identical in functionality to the clear method * (which is part of the List interface). * * @see #clear * @see List */ public void removeAllElements() { // Let gc do its work for (int i = 0; i < elementCount; i++) elementData[i] = null; elementCount = 0; } /** * Returns a clone of this vector. The copy will contain a * reference to a clone of the internal data array, not a reference * to the original internal data array of this VectorD object. * * @return a clone of this vector. public Object clone() { try { VectorD v = (VectorD)super.clone(); v.elementData = new Object[elementCount]; System.arraycopy(elementData, 0, v.elementData, 0, elementCount); v.modCount = 0; return v; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(); } } */ /** * Returns an array containing all of the elements in this VectorD * in the correct order. * * @since 1.2 */ public Object[] toArray() { Object[] result = new Object[elementCount]; System.arraycopy(elementData, 0, result, 0, elementCount); return result; } /** * Returns an array containing all of the elements in this VectorD in the * correct order. The runtime type of the returned array is that of the * specified array. If the VectorD fits in the specified array, it is * returned therein. Otherwise, a new array is allocated with the runtime * type of the specified array and the size of this VectorD.

* * If the VectorD fits in the specified array with room to spare * (i.e., the array has more elements than the VectorD), * the element in the array immediately following the end of the * VectorD is set to null. This is useful in determining the length * of the VectorD only if the caller knows that the VectorD * does not contain any null elements. * * @param a the array into which the elements of the VectorD are to * be stored, if it is big enough; otherwise, a new array of the * same runtime type is allocated for this purpose. * @return an array containing the elements of the VectorD. * @exception ArrayStoreException the runtime type of a is not a supertype * of the runtime type of every element in this VectorD. */ public Object[] toArray(Object a[]) { if (a.length < elementCount) a = (Object[])java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), elementCount); System.arraycopy(elementData, 0, a, 0, elementCount); if (a.length > elementCount) a[elementCount] = null; return a; } // Positional Access Operations /** * Returns the element at the specified position in this VectorD. * * @param index index of element to return. * @exception ArrayIndexOutOfBoundsException index is out of range (index * < 0 || index >= size()). * @since 1.2 */ public Object get(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); return elementData[index]; } /** * Replaces the element at the specified position in this VectorD with the * specified element. * * @param index index of element to replace. * @param element element to be stored at the specified position. * @return the element previously at the specified position. * @exception ArrayIndexOutOfBoundsException index out of range * (index < 0 || index >= size()). * @exception IllegalArgumentException fromIndex > toIndex. * @since 1.2 */ public Object set(int index, Object element) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); Object oldValue = elementData[index]; elementData[index] = element; return oldValue; } /** * Appends the specified element to the end of this VectorD. * * @param o element to be appended to this VectorD. * @return true (as per the general contract of Collection.add). * @since 1.2 */ public boolean add(Object o) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = o; return true; } /** * Removes the first occurrence of the specified element in this VectorD * If the VectorD does not contain the element, it is unchanged. More * formally, removes the element with the lowest index i such that * (o==null ? get(i)==null : o.equals(get(i))) (if such * an element exists). * * @param o element to be removed from this VectorD, if present. * @return true if the VectorD contained the specified element. * @since 1.2 */ public boolean remove(Object o) { return removeElement(o); } /** * Inserts the specified element at the specified position in this VectorD. * Shifts the element currently at that position (if any) and any * subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted. * @param element element to be inserted. * @exception ArrayIndexOutOfBoundsException index is out of range * (index < 0 || index > size()). * @since 1.2 */ public void add(int index, Object element) { insertElementAt(element, index); } /** * Removes the element at the specified position in this VectorD. * shifts any subsequent elements to the left (subtracts one from their * indices). Returns the element that was removed from the VectorD. * * @exception ArrayIndexOutOfBoundsException index out of range (index * < 0 || index >= size()). * @since 1.2 */ public Object remove(int index) { modCount++; if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); Object oldValue = elementData[index]; int numMoved = elementCount - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--elementCount] = null; // Let gc do its work return oldValue; } /** * Removes all of the elements from this VectorD. The VectorD will * be empty after this call returns (unless it throws an exception). * * @since 1.2 */ public void clear() { removeAllElements(); } // Bulk Operations /** * Returns true if this VectorD contains all of the elements in the * specified Collection. * * @return true if this VectorD contains all of the elements in the * specified collection. */ public boolean containsAll(Collection c) { return super.containsAll(c); } /** * Appends all of the elements in the specified Collection to the end of * this VectorD, in the order that they are returned by the specified * Collection's Iterator. The behavior of this operation is undefined if * the specified Collection is modified while the operation is in progress. * (This implies that the behavior of this call is undefined if the * specified Collection is this VectorD, and this VectorD is nonempty.) * * @param c elements to be inserted into this VectorD. * @exception ArrayIndexOutOfBoundsException index out of range (index * < 0 || index > size()). * @since 1.2 */ public boolean addAll(Collection c) { modCount++; int numNew = c.size(); ensureCapacityHelper(elementCount + numNew); Iterator e = c.iterator(); for (int i=0; i elementCount) throw new ArrayIndexOutOfBoundsException(index); int numNew = c.size(); ensureCapacityHelper(elementCount + numNew); int numMoved = elementCount - index; if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved); Iterator e = c.iterator(); for (int i=0; iequal. (Two elements e1 and * e2 are equal if (e1==null ? e2==null : * e1.equals(e2)).) In other words, two Lists are defined to be * equal if they contain the same elements in the same order. * * @param o the Object to be compared for equality with this VectorD. * @return true if the specified Object is equal to this VectorD */ public boolean equals(Object o) { return super.equals(o); } /** * Returns the hash code value for this VectorD. */ public int hashCode() { return super.hashCode(); } /** * Returns a string representation of this VectorD, containing * the String representation of each element. */ protected int idCounter = 0; protected int id=idCounter++; public String toString() { return ""; } /** * Returns a view of the portion of this List between fromIndex, * inclusive, and toIndex, exclusive. (If fromIndex and ToIndex are * equal, the returned List is empty.) The returned List is backed by this * List, so changes in the returned List are reflected in this List, and * vice-versa. The returned List supports all of the optional List * operations supported by this List.

* * This method eliminates the need for explicit range operations (of * the sort that commonly exist for arrays). Any operation that expects * a List can be used as a range operation by operating on a subList view * instead of a whole List. For example, the following idiom * removes a range of elements from a List: *

     *	    list.subList(from, to).clear();
     * 
* Similar idioms may be constructed for indexOf and lastIndexOf, * and all of the algorithms in the Collections class can be applied to * a subList.

* * The semantics of the List returned by this method become undefined if * the backing list (i.e., this List) is structurally modified in * any way other than via the returned List. (Structural modifications are * those that change the size of the List, or otherwise perturb it in such * a fashion that iterations in progress may yield incorrect results.) * * @param fromIndex low endpoint (inclusive) of the subList. * @param toIndex high endpoint (exclusive) of the subList. * @return a view of the specified range within this List. * @throws IndexOutOfBoundsException endpoint index value out of range * (fromIndex < 0 || toIndex > size) * @throws IllegalArgumentException endpoint indices out of order * (fromIndex > toIndex) */ /* public List subList(int fromIndex, int toIndex) { return Collections.synchronizedList(super.subList(fromIndex, toIndex), this); } */ /** * Removes from this List all of the elements whose index is between * fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding * elements to the left (reduces their index). * This call shortens the ArrayList by (toIndex - fromIndex) elements. (If * toIndex==fromIndex, this operation has no effect.) * * @param fromIndex index of first element to be removed. * @param toIndex index after last element to be removed. */ protected void removeRange(int fromIndex, int toIndex) { modCount++; int numMoved = elementCount - toIndex; System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved); // Let gc do its work int newElementCount = elementCount - (toIndex-fromIndex); while (elementCount != newElementCount) elementData[--elementCount] = null; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy