/** {@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 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.
  }
 /** {@inheritDoc} */
 @Override
 public short get(int offset) {
   if (offset >= _pos) {
     throw new ArrayIndexOutOfBoundsException(offset);
   }
   return _data.get(offset);
 }
 /** {@inheritDoc} */
 @Override
 public short sum() {
   short sum = 0;
   for (int i = 0; i < _pos; i++) {
     sum += _data.get(i);
   }
   return sum;
 }
 /** {@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 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 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 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 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 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 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;
 }
  @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));
    }
  }
 /** {@inheritDoc} */
 @Override
 public void transformValues(TShortFunction function) {
   for (int i = _pos; i-- > 0; ) {
     _data.put(i, function.execute(_data.get(i)));
   }
 }
 /** Returns the value at the specified offset without doing any bounds checking. */
 public short getQuick(int offset) {
   return _data.get(offset);
 }