/** * Calculates a new capacity based on the old capacity and th min capacity * * @param oldCapacity the old capacity of the container * @param minCapacity the min capacity of the container * @return the new calculated capacity */ public static int calculateNewCapacity(int oldCapacity, int minCapacity) { if (minCapacity - oldCapacity > 0) { int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) { newCapacity = minCapacity; } if (newCapacity - MAX_ARRAY_SIZE > 0) { newCapacity = hugeCapacity(minCapacity); } return newCapacity; } return -1; }
/** * 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. */ public static void rangeCheck(int index, int size) { if (index >= size) { throw new IndexOutOfBoundsException(outOfBoundsMsg(index, 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 */ public long get(int index) { rangeCheck(index, size); return longs[index]; }
/** * 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 */ public void add(int index, double element) { rangeCheckForAdd(index, size); int newCapacity = calculateNewCapacity(doubles.length, size + 1); growIfNeeded(newCapacity); System.arraycopy(doubles, index, doubles, index + 1, size - index); doubles[index] = element; size++; }
/** * 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 Collection#add) */ public boolean add(double e) { int newCapacity = calculateNewCapacity(doubles.length, size + 1); growIfNeeded(newCapacity); doubles[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 */ public void add(int index, long element) { rangeCheckForAdd(index, size); int newCapacity = calculateNewCapacity(longs.length, size + 1); growIfNeeded(newCapacity); System.arraycopy(longs, index, longs, index + 1, size - index); longs[index] = element; size++; }
/** * 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 Collection#add) */ public boolean add(long e) { int newCapacity = calculateNewCapacity(longs.length, size + 1); growIfNeeded(newCapacity); longs[size++] = e; return true; }
/** * Inserts all of the elements in the specified collection into this * list, starting at the specified position. Shifts the element * currently at that position (if any) and any subsequent elements to * the right (increases their indices). The new elements will appear * in the list in the order that they are returned by the * specified collection's iterator. * * @param index index at which to insert the first element from the * specified collection * @param c collection containing elements to be added to this list * @return <tt>true</tt> if this list changed as a result of the call * @throws IndexOutOfBoundsException * @throws NullPointerException if the specified collection is null */ public boolean addAll(int index, DoubleList c) { rangeCheckForAdd(index, size); double[] a = c.toArray(); int numNew = a.length; int newCapacity = calculateNewCapacity(doubles.length, size + numNew); growIfNeeded(newCapacity); int numMoved = size - index; if (numMoved > 0) { System.arraycopy(doubles, index, doubles, index + numNew, numMoved); } System.arraycopy(a, 0, doubles, index, numNew); size += numNew; return numNew != 0; }
/** * 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 */ public double get(int index) { rangeCheck(index, size); return doubles[index]; }
/** * Appends the long[] at the end of this long list. * * @param otherLongs the other long[] that is appended * @return <tt>true</tt> if this list changed as a result of the call * @throws NullPointerException if the specified array is null */ public boolean addAll(long[] otherLongs) { int numNew = otherLongs.length; int newCapacity = calculateNewCapacity(longs.length, size + numNew); growIfNeeded(newCapacity); System.arraycopy(otherLongs, 0, longs, size, numNew); size += numNew; return numNew != 0; }
/** * A version of rangeCheck used by add and addAll. * * @param index the accessed index * @param size the size of the container */ public static void rangeCheckForAdd(int index, int size) { if (index > size || index < 0) { throw new IndexOutOfBoundsException(outOfBoundsMsg(index, size)); } }
/** * Inserts all of the elements in the specified collection into this * list, starting at the specified position. Shifts the element * currently at that position (if any) and any subsequent elements to * the right (increases their indices). The new elements will appear * in the list in the order that they are returned by the * specified collection's iterator. * * @param index index at which to insert the first element from the * specified collection * @param c collection containing elements to be added to this list * @return <tt>true</tt> if this list changed as a result of the call * @throws IndexOutOfBoundsException * @throws NullPointerException if the specified collection is null */ public boolean addAll(int index, LongList c) { rangeCheckForAdd(index, size); long[] a = c.toArray(); int numNew = a.length; int newCapacity = calculateNewCapacity(longs.length, size + numNew); growIfNeeded(newCapacity); int numMoved = size - index; if (numMoved > 0) { System.arraycopy(longs, index, longs, index + numNew, numMoved); } System.arraycopy(a, 0, longs, index, numNew); size += numNew; return numNew != 0; }
/** * 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 */ public double set(int index, double element) { rangeCheck(index, size); double oldValue = doubles[index]; doubles[index] = element; return oldValue; }
/** * Appends the long[] at the end of this long list. * * @param otherDoubles the other double[] that is appended * @return <tt>true</tt> if this list changed as a result of the call * @throws NullPointerException if the specified array is null */ public boolean addAll(double[] otherDoubles) { int numNew = otherDoubles.length; int newCapacity = calculateNewCapacity(doubles.length, size + numNew); growIfNeeded(newCapacity); System.arraycopy(otherDoubles, 0, doubles, size, numNew); size += numNew; return numNew != 0; }
/** * 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 */ public long set(int index, long element) { rangeCheck(index, size); long oldValue = longs[index]; longs[index] = element; return oldValue; }
/** * Appends all of the elements in the specified collection to the end of * this list, 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 list, and this * list is nonempty.) * * @param c collection containing elements to be added to this list * @return <tt>true</tt> if this list changed as a result of the call * @throws NullPointerException if the specified collection is null */ public boolean addAll(DoubleList c) { double[] a = c.toArray(); int numNew = a.length; int newCapacity = calculateNewCapacity(doubles.length, size + numNew); growIfNeeded(newCapacity); System.arraycopy(a, 0, doubles, size, numNew); size += numNew; return numNew != 0; }
/** * 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 */ public double remove(int index) { rangeCheck(index, size); double oldValue = doubles[index]; int numMoved = size - index - 1; if (numMoved > 0) { System.arraycopy(doubles, index + 1, doubles, index, numMoved); } --size; return oldValue; }
/** * Appends all of the elements in the specified collection to the end of * this list, 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 list, and this * list is nonempty.) * * @param c collection containing elements to be added to this list * @return <tt>true</tt> if this list changed as a result of the call * @throws NullPointerException if the specified collection is null */ public boolean addAll(LongList c) { long[] a = c.toArray(); int numNew = a.length; int newCapacity = calculateNewCapacity(longs.length, size + numNew); growIfNeeded(newCapacity); System.arraycopy(a, 0, longs, size, numNew); size += numNew; return numNew != 0; }
/** * 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 */ public long remove(int index) { rangeCheck(index, size); long oldValue = longs[index]; int numMoved = size - index - 1; if (numMoved > 0) { System.arraycopy(longs, index + 1, longs, index, numMoved); } --size; //we do not override the value return oldValue; }