//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ // PLEASE DO NOT EDIT WITHOUT THE EXPLICIT CONSENT OF THE AUTHOR! \\ //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ namespace Ilog.Language.Util { using IEnumerator = System.Collections.IEnumerator; using ICollection = System.Collections.ICollection; using StringBuilder = System.Text.StringBuilder; using Math = System.Math; /** * This class implements java.util.ArrayList in C# keeping * the same API (up to having all method names capitalized, adding an * indexer, Count and IsEmpty properties, and * removing the size() method. The only reason to have this * instead of System.Collections.ArrayList is that it is the * base class of Ilog.Language.Util.Stack, which itself is a * reimplementation of java.util.Stack. The reason for the * reimplementation of Stack is because the one in * System.Collections provides no methods for peeking deep * into the stack, which is crucial to its use as a parse stack in * Ilog.Language.Parsing parsers implementation. This class * is not synchronized. * * @version Last modified on Sat May 28 16:11:54 2005 by hak * @author Hassan Aït-Kaci * @copyright © 2002 ILOG, S.A. */ public class Vector : Listable { // FIELDS: /** * 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. */ protected object[] elements; /** * The number of valid components in this Vector object. Components * elements[0] through elements[count-1] are the * actual elements. */ protected int count; /** * 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 multiplied by incFactor each time it needs * to grow. The default value of incFactor is 2.0 (so growing * objects the capacity). It may be reset with * SetIncrementFactor(float) to any fractional value between * 1.1 and 5.0. */ protected int deltaCap; // CONSTRUCTORS: /** * Constructs an empty Vector with the specified initial capacity * and capacity increment. * * @param initialCapacity the initial capacity of the Vector. * @param deltaCap amount by which the capacity is increased on overflow. * @exception ArgumentOutOfRangeException if the specified initial capacity is negative. */ public Vector (int initialCapacity, int deltaCap) : this(initialCapacity) { this.deltaCap = deltaCap; } /** * 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 ArgumentOutOfRangeException if the specified initial capacity is negative. */ public Vector (int initialCapacity) { elements = new object[initialCapacity]; } /** * Constructs an empty Vector so that its internal data array has * size 10 and its standard capacity increment is zero. */ public Vector () : this(10) { } /** * Constructs a Vector containing the objects contained in the * specified array, in the same order. * * @param a an array of objects. */ public Vector (object[] a) { elements = new object[a.Length]; for (int i=0; i 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 elements, with a larger * one. The size of the new data array will be the old size plus * deltaCap, unless the value of deltaCap is less than or equal to * zero, in which case the new capacity will be the old capacity * multiplied by incFactor; 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) { if (elements.Length >= minCapacity) return; object[] newArray = new object[Math.Max(minCapacity,IncreasedCapacity())]; for (int i=0; inewSize;) elements[--count] = null; } /** * Returns the current capacity of this Vector. * * @return the current capacity the length of its internal data array. */ public int Capacity () { return elements.Length; } /** * Denotes the number of components in this Vector. * * @return the number of components in this Vector. */ public int Count { get { return count; } } /** * True iff this Vector has no components. * * @return true if this Vector's size is zero; false otherwise. */ public bool IsEmpty { get { return count == 0; } } /** * Tests if the specified object is a component in this Vector. * * @param element a object. * @return true if the specified object is in this Vector; false otherwise. */ public bool Contains (object element) { for (int i=0; i0;) if (elements[i].Equals(element)) return i; return -1; } /** * Searches backwards for the specified object, starting from the * specified index, and returns its index, or -1 if not found. * * @param element a object. * @param index the non-negative index to start searching from. * @exception ArgumentOutOfRangeException if index is invalid. */ public int LastIndexOf (object element, int index) { for (int i=index; i>=0; i--) if (elements[i].Equals(element)) return i; return -1; } /** * Returns the component at the specified index. This method is * identical in functionality to the Get method. * * @param index an index into this Vector. * @exception ArgumentOutOfRangeException if the index is invalid. */ public object ElementAt (int index) { return elements[index]; } /** * Returns the first component (the element at index 0) of this Vector. * * @return the first component of this Vector. * @exception ArgumentOutOfRangeException if this Vector is empty. */ public object FirstElement () { return elements[0]; } /** * Returns the last component of the Vector. * * @return the last component of the Vector, i.e., the component at size() - 1. * @exception ArgumentOutOfRangeException if this Vector is empty. */ public object LastElement () { return elements[count-1]; } /** * Sets the component at the specified index of this Vector to be the * specified object. The previous component at that position is lost. *

* 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 except * that the latter returns the old object that was stored at the specified * position and it has its argument in a different order. * * @param element what the component is to be set to. * @param index the specified index. * @exception ArgumentOutOfRangeException if the index was invalid. */ public void SetElementAt (object element, int index) { Set(index,element); } /** * 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(int) method * except that the remove(int) method returns the old value that was * stored at the specified position. * * @param index the index of the object to remove. * @exception ArgumentOutOfRangeException if the index was invalid. */ public void RemoveElementAt (int index) { Remove(index); } /** * 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 Vector.) *

* This method is identical in functionality to the add(int, object) * method except that the latter method reverses the order of the * arguments, to match array usage more closely. * * @param element the element to insert. * @param index where to insert the new component. * @exception ArgumentOutOfRangeException if the index was invalid. */ public void InsertElementAt (object element, int index) { Add(index,element); } /** * 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 * except that the latter returns a bool. * * @param element the component to be added. */ public void AddElement (object element) { Add(element); } /** * Removes all components from this Vector. * This method is identical in functionality to the Clear method. * */ public void RemoveAllElements () { count = 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 Vector object. * * @return a clone of this Vector. */ public object Clone () { object[] newArray = new object[elements.Length]; for (int i=0; ijava.util.Vector API). */ public bool Add (object element) { if (count == elements.Length) Grow(); elements[count++] = element; return true; } /** * Removes the first occurrence of the specified element. If this Vector * does not contain the element, it is left unchanged. *

* N.B.Because remove(object) is ambiguous, it is not * supported for Vectors; its functionality is provided by * removeIndex(int) and removeElement(object). * * @param element element to be removed from this Vector, if present. * @return true if this Vector contained the specified element. */ public bool Remove (object element) { int i = 0; for (i=0; iindex; i--) elements[i] = elements[i-1]; elements[index] = element; } /** * Removes the element at the specified position and shifts any * subsequent elements to the left (subtracts one from their * indices). Returns the element that was removed. * * @param index the index of the element to removed. * @return the element that was at this index position. * @exception ArgumentOutOfRangeException if index is out of range. */ public object Remove (int index) { object old = elements[index]; for (int i=index; i0;) if (!Contains(a[i])) return false; return true; } /** * Returns true if this Vector contains all of the elements in the * specified Vector. * * @param v Vector of elements to be tested for membership. * @return true if all the elements of the specified collection belong to this. */ public bool ContainsAll (Vector v) { foreach (object elt in v) if (!Contains(elt)) return false; return true; } /** * Appends all of the elements in the specified array to the end of * this Vector, in the order of the array. * * @param a array containing the elements to be inserted. * @exception ClassCastException if the collection contains a non-number. * @return true (following the java.util.Vector API). */ public bool AddAll (object[] a) { EnsureCapacity(count+a.Length); for (int i=0; ijava.util.Vector API). */ public bool AddAll (Vector v) { int size = v.Count; EnsureCapacity(count+size); foreach (object elt in v) elements[count++] = elt; return true; } /** * Removes from this Vector all of its elements that are contained * in the specified array. Note that all occurrences of such elemeents * are removed - not just the first. * * @param a array of elements to be removed. * @return true if this Vector changed as a result of the call. */ public bool RemoveAll (object[] a) { Vector indices = new Vector(); for (int i=0; i0;) if (elt.Equals(elements[i])) indices.Add(i); RemoveAllIndices(indices); return !indices.IsEmpty; } /** * Removes from this Vector the first occurrence of the elements * that are contained in the specified array * * @param a array of elements to be removed. * @return true if this Vector changed as a result of the call. */ public bool RemoveAllFirst (object[] a) { Vector indices = new Vector(); for (int i=0; i 0) code <<= 1; else code >>= 1; code ^= elements[i].GetHashCode(); shift = -shift; } return (int)code; } /** * Returns a string representation of this Vector. * * @return a string representation of this collection. */ public override string ToString () { StringBuilder buf = new StringBuilder("["); string sep = ""; for (int i=0; i * N.B. Note that this method is public, whereas - strangely * enough, it is protected in the java.util.Vector API! * * @param fromIndex index of first element to be removed. * @param toIndex index after last element to be removed. */ public void RemoveRange (int fromIndex, int toIndex) { if (toIndex <= fromIndex) return; toIndex = Math.Min(toIndex, count); int gap = toIndex - fromIndex; for (int i=fromIndex; i 5.0) throw new System.ArgumentException("Increment factor "+incFactor +" must be in [1.1,5.0] range"); this.incFactor = incFactor; } // PRIVATES: /** * A constructor used privately for cloning only... */ private Vector (object[] elements, int count, int deltaCap, float incFactor) { this.elements = elements; this.count = count; this.deltaCap = deltaCap; this.incFactor = incFactor; } /** * Grows the size of the internal array. */ private void Grow () { object[] newArray = new object[IncreasedCapacity()]; for (int i=0; i