package fr.unistra.pelican.util.specifiedArrayList; import java.util.Arrays; /** * This class is reduced and specialized version of ArrayList dedicated to double. * * Faster than generic ArrayList * * @author Jonathan Weber */ public class DoubleArrayList { private static final long serialVersionUID = 8683452581122892189L; /** * The array buffer into which the elements of the ArrayList are stored. The * capacity of the ArrayList is the length of this array buffer. */ private transient double[] elementData; /** * The size of the ArrayList (the number of elements it contains). * * @serial */ private int size; /** * Constructs an empty list with the specified initial capacity. * * @param initialCapacity * the initial capacity of the list * @exception IllegalArgumentException * if the specified initial capacity is negative */ public DoubleArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); this.elementData = new double[initialCapacity]; } /** * Constructs an empty list with an initial capacity of ten. */ public DoubleArrayList() { this(10); } /** * Returns the number of elements in this list. * * @return the number of elements in this list */ public int size() { return size; } /** * Returns <tt>true</tt> if this list contains no elements. * * @return <tt>true</tt> if this list contains no elements */ public boolean isEmpty() { return size == 0; } /** * Returns an array containing all of the elements in this list in proper * sequence (from first to last element). * * <p> * The returned array will be "safe" in that no references to it are * maintained by this list. (In other words, this method must allocate a new * array). The caller is thus free to modify the returned array. * * <p> * This method acts as bridge between array-based and collection-based APIs. * * @return an array containing all of the elements in this list in proper * sequence */ public double[] toArray() { return Arrays.copyOf(elementData, size); } /** * Returns the element at the specified position in this list. * * @param index * index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException * {@inheritDoc} */ public double get(int index) { RangeCheck(index); return elementData[index]; } /** * Replaces the element at the specified position in this list with the * specified element. * * @param index * index of the element to replace * @param element * element to be stored at the specified position * @return the element previously at the specified position * @throws IndexOutOfBoundsException * {@inheritDoc} */ public double set(int index, double element) { RangeCheck(index); double oldValue = elementData[index]; elementData[index] = element; return oldValue; } public void ensureCapacity(int minCapacity) { int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { int newCapacity = (oldCapacity * 3) ; if (newCapacity < minCapacity) newCapacity = minCapacity; // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } } /** * Appends the specified element to the end of this list. * * @param e * element to be appended to this list * @return <tt>true</tt> (as specified by {@link Collection#add}) */ public boolean add(double e) { ensureCapacity(size + 1); // Increments modCount!! elementData[size++] = e; return true; } /** * Inserts the specified element at the specified position in this list. * 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 * @throws IndexOutOfBoundsException * {@inheritDoc} */ public void add(int index, double element) { if (index > size || index < 0) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); ensureCapacity(size + 1); // Increments modCount!! System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; } /** * Removes the element at the specified position in this list. Shifts any * subsequent elements to the left (subtracts one from their indices). * * @param index * the index of the element to be removed * @return the element that was removed from the list * @throws IndexOutOfBoundsException * {@inheritDoc} */ public double remove(int index) { RangeCheck(index); double oldValue = elementData[index]; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index + 1, elementData, index, numMoved); size--; return oldValue; } /** * Removes all of the elements from this list. The list will be empty after * this call returns. */ public void clear() { // Let gc do its work for (int i = 0; i < size; i++) elementData[i] = 0.0; size = 0; } /** * Checks if the given index is in range. If not, throws an appropriate * runtime exception. This method does *not* check if the index is negative: * It is always used immediately prior to an array access, which throws an * ArrayIndexOutOfBoundsException if index is negative. */ private void RangeCheck(int index) { if (index >= size) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); } /** * Returns <tt>true</tt> if this list contains the specified element. * More formally, returns <tt>true</tt> if and only if this list contains * at least one element <tt>e</tt> such that * <tt>(o==null ? e==null : o.equals(e))</tt>. * * @param o element whose presence in this list is to be tested * @return <tt>true</tt> if this list contains the specified element */ public boolean contains(double d) { for (int i = 0; i < size; i++) { if (d==elementData[i]) { return true; } } return false; } }