/** * Increase the capacity of the bag. * * @throws ArrayIndexOutOfBoundsException if new capacity is smaller than old */ @SuppressWarnings("unchecked") private void grow() { grow(data.length * 2); }
/** * Adds the specified element to the end of this bag. * <p> * If required, it also increases the capacity of the bag. * </p> * * @param e * element to be added to this list */ public void add(E e) { // is size greater than capacity increase capacity if (size == data.length) grow(data.length * 2); data[size++] = e; }
/** * Adds the specified element to the end of this bag. * <p> * If required, it also increases the capacity of the bag. * </p> * * @param e * element to be added to this list */ public void add(E e) { // is size greater than capacity increase capacity if (size == data.length) grow(data.length * 2); data[size++] = e; }
/** * Check if an item, if added at the given item will fit into the bag. * <p> * If not, the bag capacity will be increased to hold an item at the index. * </p> * * <p>yeah, sorry, it's weird, but we don't want to change existing change behavior</p> * * @param index * index to check */ public void ensureCapacity(int index) { if(index >= data.length) { grow(index + 1); } }
/** * Check if an item, if added at the given item will fit into the bag. * <p> * If not, the bag capacity will be increased to hold an item at the index. * </p> * * <p>yeah, sorry, it's weird, but we don't want to change existing change behavior</p> * * @param index * index to check */ public void ensureCapacity(int index) { if(index >= data.length) { grow(index + 1); } }
/** * Adds the specified element to the end of this bag. * <p> * If required, it also increases the capacity of the bag. * </p> * * @param e * element to be added to this list */ public void add(E e) { // is size greater than capacity increase capacity if (size == data.length) grow(data.length * 2); data[size++] = e; }
/** * Check if an item, if added at the given item will fit into the bag. * <p> * If not, the bag capacity will be increased to hold an item at the index. * </p> * * <p>yeah, sorry, it's weird, but we don't want to change existing change behavior</p> * * @param index * index to check */ public void ensureCapacity(int index) { if(index >= data.length) { grow(index + 1); } }
/** * Returns the element at the specified position in Bag. This method * ensures that the bag grows if the requested index is outside the bounds * of the current backing array. * * @param index * index of the element to return * * @return the element at the specified position in bag * */ public E safeGet(int index) { if(index >= data.length) grow(Math.max((2 * data.length), (3 * index) / 2)); return data[index]; }
/** * Returns the element at the specified position in Bag. This method * ensures that the bag grows if the requested index is outside the bounds * of the current backing array. * * @param index * index of the element to return * * @return the element at the specified position in bag * */ public E safeGet(int index) { if(index >= data.length) grow(Math.max((2 * data.length), (3 * index) / 2)); return data[index]; }
/** * Returns the element at the specified position in Bag. This method * ensures that the bag grows if the requested index is outside the bounds * of the current backing array. * * @param index * index of the element to return * * @return the element at the specified position in bag * */ public E safeGet(int index) { if(index >= data.length) grow(Math.max((2 * data.length), (3 * index) / 2)); return data[index]; }
/** * Set element at specified index in the bag. * * @param index * position of element * @param e * the element */ public void set(int index, E e) { if(index >= data.length) grow(max((2 * data.length), index + 1)); size = Math.max(size, index + 1); data[index] = e; }
/** * Set element at specified index in the bag. * * @param index * position of element * @param e * the element */ public void set(int index, E e) { if(index >= data.length) grow(max((2 * data.length), index + 1)); size = Math.max(size, index + 1); unsafeSet(index, e); }
/** * Set element at specified index in the bag. * * @param index * position of element * @param e * the element */ public void set(int index, E e) { if(index >= data.length) grow(max((2 * data.length), index + 1)); size = Math.max(size, index + 1); unsafeSet(index, e); }