/** * Adds the values in the array <tt>vals</tt> to the end of the list, in order. * * @param vals an <code>int[]</code> value */ public void add(int[] vals) { add(vals, 0, vals.length); }
/** * Sheds any excess capacity above and beyond the current size of the list. */ public void trimToSize() { if (_data.length > size()) { int[] tmp = new int[size()]; toNativeArray(tmp, 0, tmp.length); _data = tmp; } }
/** * Performs a binary search for <tt>value</tt> in the entire list. Note that you <b>must</b> * * @param value the value to search for * @return the absolute offset in the list of the value, or its negative insertion point into * the sorted list. * @{link #sort sort} the list before doing a search. */ public int binarySearch(int value) { return binarySearch(value, 0, _pos); }
/** * Searches the list for values satisfying <tt>condition</tt> in the manner of the *nix * <tt>grep</tt> utility. * * @param condition a condition to apply to each element in the list * @return a list of values which match the condition. */ public TIntArrayList grep(TIntProcedure condition) { TIntArrayList list = new TIntArrayList(); for (int i = 0; i < _pos; i++) { if (condition.execute(_data[i])) { list.add(_data[i]); } } return list; }
@Override public int size() { return list.size(); }
/** * Inserts <tt>value</tt> into the list at <tt>offset</tt>. All values including and to the * right of <tt>offset</tt> are shifted to the right. * * @param offset an <code>int</code> value * @param value an <code>int</code> value */ public void insert(int offset, int value) { if (offset == _pos) { add(value); return; } ensureCapacity(_pos + 1); // shift right System.arraycopy(_data, offset, _data, offset + 1, _pos - offset); // insert _data[offset] = value; _pos++; }
/** * Copies the contents of the list into a native array. * * @return an <code>int[]</code> value */ public int[] toNativeArray() { return toNativeArray(0, _pos); }
/** * Adds <tt>val</tt> to the end of the list, growing as needed. * * @param val an <code>int</code> value */ public void add(int val) { ensureCapacity(_pos + 1); _data[_pos++] = val; }
/** * Sets the size of the list to 0, but does not change its capacity. This method can be used as * an alternative to the {@link #clear clear} method if you want to recyle a list without * allocating new backing arrays. * * @see #clear */ public void reset() { _pos = 0; fill((int) 0); }
/** * Flushes the internal state of the list, resetting the capacity to the default. */ public void clear() { clear(DEFAULT_CAPACITY); }
@Override public int getQuick(int pos) { return list.getQuick(pos); }
/** * Searches the list for values which do <b>not</b> satisfy <tt>condition</tt>. This is akin to * *nix <code>grep -v</code>. * * @param condition a condition to apply to each element in the list * @return a list of values which do not match the condition. */ public TIntArrayList inverseGrep(TIntProcedure condition) { TIntArrayList list = new TIntArrayList(); for (int i = 0; i < _pos; i++) { if (!condition.execute(_data[i])) { list.add(_data[i]); } } return list; }
/** * Compares this list to another list, value by value. * * @param other the object to compare against * @return true if other is a TIntArrayList and has exactly the same values. */ public boolean equals(Object other) { if (other == this) { return true; } else if (other instanceof TIntArrayList) { TIntArrayList that = (TIntArrayList) other; if (that.size() != this.size()) { return false; } else { for (int i = _pos; i-- > 0; ) { if (this._data[i] != that._data[i]) { return false; } } return true; } } else { return false; } }
/** * Inserts a slice of the array of <tt>values</tt> into the list at <tt>offset</tt>. All values * including and to the right of <tt>offset</tt> are shifted to the right. * * @param offset an <code>int</code> value * @param values an <code>int[]</code> value * @param valOffset the offset in the values array at which to start copying. * @param len the number of values to copy from the values array */ public void insert(int offset, int[] values, int valOffset, int len) { if (offset == _pos) { add(values, valOffset, len); return; } ensureCapacity(_pos + len); // shift right System.arraycopy(_data, offset, _data, offset + len, _pos - offset); // insert System.arraycopy(values, valOffset, _data, offset, len); _pos += len; }
/** * Copies a slice of the list into a native array. * * @param offset the offset at which to start copying * @param len the number of values to copy. * @return an <code>int[]</code> value */ public int[] toNativeArray(int offset, int len) { int[] rv = new int[len]; toNativeArray(rv, offset, len); return rv; }
/** * Adds a subset of the values in the array <tt>vals</tt> to the end of the list, in order. * * @param vals an <code>int[]</code> value * @param offset the offset at which to start copying * @param length the number of values to copy. */ public void add(int[] vals, int offset, int length) { ensureCapacity(_pos + length); System.arraycopy(vals, offset, _data, _pos, length); _pos += length; }
/** * Returns a sublist of this list. * * @param begin low endpoint (inclusive) of the subList. * @param end high endpoint (exclusive) of the subList. * @return sublist of this list from begin, inclusive to end, exclusive. * @throws IndexOutOfBoundsException - endpoint out of range * @throws IllegalArgumentException - endpoints out of order (end > begin) */ public TIntArrayList subList(int begin, int end) { if (end < begin) throw new IllegalArgumentException("end index " + end + " greater than begin index " + begin); if (begin < 0) throw new IndexOutOfBoundsException("begin index can not be < 0"); if (end > _data.length) throw new IndexOutOfBoundsException("end index < " + _data.length); TIntArrayList list = new TIntArrayList(end - begin); for (int i = begin; i < end; i++) { list.add(_data[i]); } return list; }
/** * Finds the minimum value in the list. * * @return the smallest value in the list. * @throws IllegalStateException if the list is empty */ public int min() { if (size() == 0) { throw new IllegalStateException("cannot find minimum of an empty list"); } int min = Integer.MAX_VALUE; for (int i = 0; i < _pos; i++) { if (_data[i] < min) { min = _data[i]; } } return min; }
@Override public int[] toNativeArray() { return list.toNativeArray(); } }