/** {@inheritDoc} */ @Override public short set(int offset, short val) { if (offset >= _pos) { throw new ArrayIndexOutOfBoundsException(offset); } short prev_val = _data.get(offset); _data.put(offset, val); return prev_val; }
/** {@inheritDoc} */ @Override public String toString() { final StringBuilder buf = new StringBuilder("{"); for (int i = 0, end = _pos - 1; i < end; i++) { buf.append(_data.get(i)); buf.append(", "); } if (size() > 0) { buf.append(_data.get(_pos - 1)); } buf.append("}"); return buf.toString(); }
/** {@inheritDoc} */ @Override public void set(int offset, short[] values, int valOffset, int length) { if (offset < 0 || offset + length > _pos) { throw new ArrayIndexOutOfBoundsException(offset); } _data.fromArray(values, valOffset, offset, length); }
/** {@inheritDoc} */ @Override public short get(int offset) { if (offset >= _pos) { throw new ArrayIndexOutOfBoundsException(offset); } return _data.get(offset); }
/** {@inheritDoc} */ @Override public int binarySearch(short value, int fromIndex, int toIndex) { if (fromIndex < 0) { throw new ArrayIndexOutOfBoundsException(fromIndex); } if (toIndex > _pos) { throw new ArrayIndexOutOfBoundsException(toIndex); } int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; short midVal = _data.get(mid); if (midVal < value) { low = mid + 1; } else if (midVal > value) { high = mid - 1; } else { return mid; // value found } } return -(low + 1); // value not found. }
/** * Grow the internal array as needed to accommodate the specified number of elements. The size of * the array bytes on each resize unless capacity requires more than twice the current capacity. */ public void ensureCapacity(int capacity) { int oldCapacity = capacity(); if (capacity > oldCapacity) { int newCap = Math.max(oldCapacity << 1, capacity); _data.resize(newCap); } }
/** {@inheritDoc} */ @Override public int hashCode() { int h = 0; for (int i = _pos; i-- > 0; ) { h += HashFunctions.hash(_data.get(i)); } return h; }
/** {@inheritDoc} */ @Override public short sum() { short sum = 0; for (int i = 0; i < _pos; i++) { sum += _data.get(i); } return sum; }
/** {@inheritDoc} */ @Override public boolean forEachDescending(TShortProcedure procedure) { for (int i = _pos; i-- > 0; ) { if (!procedure.execute(_data.get(i))) { return false; } } return true; }
/** {@inheritDoc} */ @Override public boolean forEach(TShortProcedure procedure) { for (int i = 0; i < _pos; i++) { if (!procedure.execute(_data.get(i))) { return false; } } return true; }
/** {@inheritDoc} */ @Override public int lastIndexOf(int offset, short value) { for (int i = offset; i-- > 0; ) { if (_data.get(i) == value) { return i; } } return -1; }
/** {@inheritDoc} */ @Override public int indexOf(int offset, short value) { for (int i = offset; i < _pos; i++) { if (_data.get(i) == value) { return i; } } return -1; }
/** {@inheritDoc} */ @Override public TShortList inverseGrep(TShortProcedure condition) { TShortArrayList list = new TShortArrayList(); for (int i = 0; i < _pos; i++) { short val = _data.get(i); if (!condition.execute(val)) { list.add(val); } } return list; }
/** {@inheritDoc} */ @Override public short[] toArray(short[] dest, int source_pos, int dest_pos, int len) { if (len == 0) { return dest; // nothing to copy } if (source_pos < 0 || source_pos >= _pos) { throw new ArrayIndexOutOfBoundsException(source_pos); } _data.toArray(source_pos, dest, dest_pos, len); return dest; }
/** {@inheritDoc} */ @Override public short max() { if (size() == 0) { throw new IllegalStateException("cannot find maximum of an empty list"); } short max = Short.MIN_VALUE; for (int i = 0; i < _pos; i++) { short val = _data.get(i); if (val > max) { max = val; } } return max; }
/** {@inheritDoc} */ @Override public short min() { if (size() == 0) { throw new IllegalStateException("cannot find minimum of an empty list"); } short min = Short.MAX_VALUE; for (int i = 0; i < _pos; i++) { short val = _data.get(i); if (val < min) { min = val; } } return min; }
/** {@inheritDoc} */ @Override public TShortList 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 > capacity()) { throw new IndexOutOfBoundsException("end index < " + capacity()); } TShortArrayList list = new TShortArrayList(end - begin); _data.toArray(begin, list._data, 0, end - begin); list._pos = end - begin; return list; }
@Override public void writeExternal(ObjectOutput out) throws IOException { // VERSION out.writeByte(0); // POSITION out.writeInt(_pos); // NO_ENTRY_VALUE out.writeShort(no_entry_value); // ENTRIES int len = capacity(); out.writeInt(len); for (int i = 0; i < len; i++) { out.writeShort(_data.get(i)); } }
@Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { // VERSION in.readByte(); // POSITION _pos = in.readInt(); // NO_ENTRY_VALUE no_entry_value = in.readShort(); // ENTRIES int len = in.readInt(); _data = new TShortOffheapArray(len); for (int i = 0; i < len; i++) { _data.put(i, in.readShort()); } }
/** {@inheritDoc} */ @Override public void add(short[] vals, int offset, int length) { ensureCapacity(_pos + length); _data.fromArray(vals, offset, _pos, length); _pos += length; }
/** {@inheritDoc} */ @Override public boolean add(short val) { ensureCapacity(_pos + 1); _data.put(_pos++, val); return true; }
/** * Flushes the internal state of the list, setting the capacity of the empty list to * <tt>capacity</tt>. */ public void clear(int capacity) { _data.resize(capacity); _data.clear(); _pos = 0; }
/** {@inheritDoc} */ @Override public void transformValues(TShortFunction function) { for (int i = _pos; i-- > 0; ) { _data.put(i, function.execute(_data.get(i))); } }
/** Sheds any excess capacity above and beyond the current size of the list. */ public void trimToSize() { if (capacity() > size()) { _data.resize(size()); } }
/** Sets the value at the specified offset without doing any bounds checking. */ public void setQuick(int offset, short val) { _data.put(offset, val); }
protected int capacity() { return (int) _data.capacity(); }
/** Returns the value at the specified offset without doing any bounds checking. */ public short getQuick(int offset) { return _data.get(offset); }