📄 tlongarraylist.java
字号:
///////////////////////////////////////////////////////////////////////////////// Copyright (c) 2001, Eric D. Friedman All Rights Reserved.//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.///////////////////////////////////////////////////////////////////////////////package gnu.trove;import java.io.Serializable;import java.util.Arrays;import java.util.Random;/** * A resizable, array-backed list of long primitives. * * Created: Sat Dec 29 14:21:12 2001 * * @author Eric D. Friedman * @version $Id: TLongArrayList.java,v 1.1.1.1 2003/07/14 19:36:04 mccallum Exp $ */public class TLongArrayList implements Serializable, Cloneable { /** the data of the list */ protected long[] _data; /** the index after the last entry in the list */ protected int _pos; /** the default capacity for new lists */ protected static final int DEFAULT_CAPACITY = 10; /** * Creates a new <code>TLongArrayList</code> instance with the * default capacity. */ public TLongArrayList() { this(DEFAULT_CAPACITY); } /** * Creates a new <code>TLongArrayList</code> instance with the * specified capacity. * * @param capacity an <code>int</code> value */ public TLongArrayList(int capacity) { _data = new long[capacity]; _pos = 0; } /** * Creates a new <code>TLongArrayList</code> instance whose * capacity is the greater of the length of <tt>values</tt> and * DEFAULT_CAPACITY and whose initial contents are the specified * values. * * @param values an <code>long[]</code> value */ public TLongArrayList(long[] values) { this(Math.max(values.length, DEFAULT_CAPACITY)); add(values); } // sizing /** * Grow the internal array as needed to accomodate the specified * number of elements. The size of the array doubles on each * resize unless <tt>capacity</tt> requires more than twice the * current capacity. * * @param capacity an <code>int</code> value */ public void ensureCapacity(int capacity) { if (capacity > _data.length) { int newCap = Math.max(_data.length << 1, capacity); long[] tmp = new long[newCap]; System.arraycopy(_data, 0, tmp, 0, _data.length); _data = tmp; } } /** * Returns the number of values in the list. * * @return the number of values in the list. */ public int size() { return _pos; } /** * Tests whether this list contains any values. * * @return true if the list is empty. */ public boolean isEmpty() { return _pos == 0; } /** * Sheds any excess capacity above and beyond the current size of * the list. */ public void trimToSize() { if (_data.length > size()) { long[] tmp = new long[size()]; toNativeArray(tmp, 0, tmp.length); _data = tmp; } } // modifying /** * Adds <tt>val</tt> to the end of the list, growing as needed. * * @param val an <code>long</code> value */ public void add(long val) { ensureCapacity(_pos + 1); _data[_pos++] = val; } /** * Adds the values in the array <tt>vals</tt> to the end of the * list, in order. * * @param vals an <code>long[]</code> value */ public void add(long[] vals) { add(vals, 0, vals.length); } /** * Adds a subset of the values in the array <tt>vals</tt> to the * end of the list, in order. * * @param vals an <code>long[]</code> value * @param offset the offset at which to start copying * @param length the number of values to copy. */ public void add(long[] vals, int offset, int length) { ensureCapacity(_pos + length); System.arraycopy(vals, offset, _data, _pos, length); _pos += length; } /** * 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>long</code> value */ public void insert(int offset, long 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++; } /** * Inserts 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>long[]</code> value */ public void insert(int offset, long[] values) { insert(offset, values, 0, values.length); } /** * 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>long[]</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, long[] 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; } /** * Returns the value at the specified offset. * * @param offset an <code>int</code> value * @return an <code>long</code> value */ public long get(int offset) { if (offset >= _pos) { throw new ArrayIndexOutOfBoundsException(offset); } return _data[offset]; } /** * Returns the value at the specified offset without doing any * bounds checking. * * @param offset an <code>int</code> value * @return an <code>long</code> value */ public long getQuick(int offset) { return _data[offset]; } /** * Sets the value at the specified offset. * * @param offset an <code>int</code> value * @param val an <code>long</code> value */ public void set(int offset, long val) { if (offset >= _pos) { throw new ArrayIndexOutOfBoundsException(offset); } _data[offset] = val; } /** * Sets the value at the specified offset and returns the * previously stored value. * * @param offset an <code>int</code> value * @param val an <code>long</code> value * @return the value previously stored at offset. */ public long getSet(int offset, long val) { if (offset >= _pos) { throw new ArrayIndexOutOfBoundsException(offset); } long old = _data[offset]; _data[offset] = val; return old; } /** * Replace the values in the list starting at <tt>offset</tt> with * the contents of the <tt>values</tt> array. * * @param offset the first offset to replace * @param values the source of the new values */ public void set(int offset, long[] values) { set(offset, values, 0, values.length); } /** * Replace the values in the list starting at <tt>offset</tt> with * <tt>length</tt> values from the <tt>values</tt> array, starting * at valOffset. * * @param offset the first offset to replace * @param values the source of the new values * @param valOffset the first value to copy from the values array * @param length the number of values to copy */ public void set(int offset, long[] values, int valOffset, int length) { if (offset < 0 || offset + length >= _pos) { throw new ArrayIndexOutOfBoundsException(offset); } System.arraycopy(_data, offset, values, valOffset, length); } /** * Sets the value at the specified offset without doing any bounds * checking. * * @param offset an <code>int</code> value * @param val an <code>long</code> value */ public void setQuick(int offset, long val) { _data[offset] = val; } /** * Flushes the internal state of the list, resetting the capacity * to the default. */ public void clear() { clear(DEFAULT_CAPACITY); } /** * Flushes the internal state of the list, setting the capacity of * the empty list to <tt>capacity</tt>. * * @param capacity an <code>int</code> value */ public void clear(int capacity) { _data = new long[capacity]; _pos = 0; } /** * 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(0); } /** * 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. This method differs * from {@link #reset reset} in that it does not clear the old * values in the backing array. Thus, it is possible for {@link * #getQuick getQuick} to return stale data if this method is used * and the caller is careless about bounds checking. * * @see #reset * @see #clear * @see #getQuick */ public void resetQuick() { _pos = 0; } /** * Removes the value at <tt>offset</tt> from the list. * * @param offset an <code>int</code> value * @return the value previously stored at offset. */ public long remove(int offset) { long old = get(offset); remove(offset, 1); return old; } /** * Removes <tt>length</tt> values from the list, starting at * <tt>offset</tt> * * @param offset an <code>int</code> value * @param length an <code>int</code> value */ public void remove(int offset, int length) { if (offset < 0 || offset >= _pos) { throw new ArrayIndexOutOfBoundsException(offset); } if (offset == 0) { // data at the front System.arraycopy(_data, length, _data, 0, _pos - length); } else if (_pos - length == offset) { // no copy to make, decrementing pos "deletes" values at // the end } else { // data in the middle System.arraycopy(_data, offset + length, _data, offset, _pos - (offset + length)); } _pos -= length; // no need to clear old values beyond _pos, because this is a // primitive collection and 0 takes as much room as any other // value } /** * Transform each value in the list using the specified function. * * @param function a <code>TLongFunction</code> value */ public void transformValues(TLongFunction function) { for (int i = _pos; i-- > 0;) { _data[i] = function.execute(_data[i]); } } /** * Reverse the order of the elements in the list. */ public void reverse() { reverse(0, _pos); } /** * Reverse the order of the elements in the range of the list.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -