コード例 #1
0
  /** {@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;
  }
コード例 #2
0
 /** {@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();
 }
コード例 #3
0
 /** {@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);
 }
コード例 #4
0
 /** {@inheritDoc} */
 @Override
 public short get(int offset) {
   if (offset >= _pos) {
     throw new ArrayIndexOutOfBoundsException(offset);
   }
   return _data.get(offset);
 }
コード例 #5
0
  /** {@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.
  }
コード例 #6
0
 /**
  * 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);
   }
 }
コード例 #7
0
 /** {@inheritDoc} */
 @Override
 public int hashCode() {
   int h = 0;
   for (int i = _pos; i-- > 0; ) {
     h += HashFunctions.hash(_data.get(i));
   }
   return h;
 }
コード例 #8
0
 /** {@inheritDoc} */
 @Override
 public short sum() {
   short sum = 0;
   for (int i = 0; i < _pos; i++) {
     sum += _data.get(i);
   }
   return sum;
 }
コード例 #9
0
 /** {@inheritDoc} */
 @Override
 public boolean forEachDescending(TShortProcedure procedure) {
   for (int i = _pos; i-- > 0; ) {
     if (!procedure.execute(_data.get(i))) {
       return false;
     }
   }
   return true;
 }
コード例 #10
0
 /** {@inheritDoc} */
 @Override
 public boolean forEach(TShortProcedure procedure) {
   for (int i = 0; i < _pos; i++) {
     if (!procedure.execute(_data.get(i))) {
       return false;
     }
   }
   return true;
 }
コード例 #11
0
 /** {@inheritDoc} */
 @Override
 public int lastIndexOf(int offset, short value) {
   for (int i = offset; i-- > 0; ) {
     if (_data.get(i) == value) {
       return i;
     }
   }
   return -1;
 }
コード例 #12
0
 /** {@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;
 }
コード例 #13
0
 /** {@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;
 }
コード例 #14
0
 /** {@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;
 }
コード例 #15
0
 /** {@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;
 }
コード例 #16
0
 /** {@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;
 }
コード例 #17
0
 /** {@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;
 }
コード例 #18
0
  @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));
    }
  }
コード例 #19
0
  @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());
    }
  }
コード例 #20
0
 /** {@inheritDoc} */
 @Override
 public void add(short[] vals, int offset, int length) {
   ensureCapacity(_pos + length);
   _data.fromArray(vals, offset, _pos, length);
   _pos += length;
 }
コード例 #21
0
 /** {@inheritDoc} */
 @Override
 public boolean add(short val) {
   ensureCapacity(_pos + 1);
   _data.put(_pos++, val);
   return true;
 }
コード例 #22
0
 /**
  * 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;
 }
コード例 #23
0
 /** {@inheritDoc} */
 @Override
 public void transformValues(TShortFunction function) {
   for (int i = _pos; i-- > 0; ) {
     _data.put(i, function.execute(_data.get(i)));
   }
 }
コード例 #24
0
 /** Sheds any excess capacity above and beyond the current size of the list. */
 public void trimToSize() {
   if (capacity() > size()) {
     _data.resize(size());
   }
 }
コード例 #25
0
 /** Sets the value at the specified offset without doing any bounds checking. */
 public void setQuick(int offset, short val) {
   _data.put(offset, val);
 }
コード例 #26
0
 protected int capacity() {
   return (int) _data.capacity();
 }
コード例 #27
0
 /** Returns the value at the specified offset without doing any bounds checking. */
 public short getQuick(int offset) {
   return _data.get(offset);
 }