📄 tfloatarraylist.java
字号:
* * @param from the inclusive index at which to start reversing * @param to the exclusive index at which to stop reversing */ public void reverse(int from, int to) { if (from == to) { return; // nothing to do } if (from > to) { throw new IllegalArgumentException("from cannot be greater than to"); } for (int i = from, j = to - 1; i < j; i++, j--) { swap(i, j); } } /** * Shuffle the elements of the list using the specified random * number generator. * * @param rand a <code>Random</code> value */ public void shuffle(Random rand) { for (int i = _pos; i-- > 1;) { swap(i, rand.nextInt(i)); } } /** * Swap the values at offsets <tt>i</tt> and <tt>j</tt>. * * @param i an offset into the data array * @param j an offset into the data array */ private final void swap(int i, int j) { float tmp = _data[i]; _data[i] = _data[j]; _data[j] = tmp; } // copying /** * Returns a clone of this list. Since this is a primitive * collection, this will be a deep clone. * * @return a deep clone of the list. */ public Object clone() { Object o = null; try { o = super.clone(); } catch (CloneNotSupportedException e) { // it's supported } // end of try-catch return o; } /** * Copies the contents of the list into a native array. * * @return an <code>float[]</code> value */ public float[] toNativeArray() { return toNativeArray(0, _pos); } /** * 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>float[]</code> value */ public float[] toNativeArray(int offset, int len) { float[] rv = new float[len]; toNativeArray(rv, offset, len); return rv; } /** * Copies a slice of the list into a native array. * * @param dest the array to copy into. * @param offset the offset of the first value to copy * @param len the number of values to copy. */ public void toNativeArray(float[] dest, int offset, int len) { if (len == 0) { return; // nothing to copy } if (offset < 0 || offset >= _pos) { throw new ArrayIndexOutOfBoundsException(offset); } System.arraycopy(_data, 0, dest, offset, len); } // comparing /** * Compares this list to another list, value by value. * * @param other the object to compare against * @return true if other is a TFloatArrayList and has exactly the * same values. */ public boolean equals(Object other) { if (other == this) { return true; } else if (other instanceof TFloatArrayList) { TFloatArrayList that = (TFloatArrayList)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; } } // procedures /** * Applies the procedure to each value in the list in ascending * (front to back) order. * * @param procedure a <code>TFloatProcedure</code> value * @return true if the procedure did not terminate prematurely. */ public boolean forEach(TFloatProcedure procedure) { for (int i = 0; i < _pos; i++) { if (! procedure.execute(_data[i])) { return false; } } return true; } /** * Applies the procedure to each value in the list in descending * (back to front) order. * * @param procedure a <code>TFloatProcedure</code> value * @return true if the procedure did not terminate prematurely. */ public boolean forEachDescending(TFloatProcedure procedure) { for (int i = _pos; i-- > 0;) { if (! procedure.execute(_data[i])) { return false; } } return true; } // sorting /** * Sort the values in the list (ascending) using the Sun quicksort * implementation. * * @see java.util.Arrays#sort */ public void sort() { Arrays.sort(_data, 0, _pos); } /** * Sort a slice of the list (ascending) using the Sun quicksort * implementation. * * @param fromIndex the index at which to start sorting (inclusive) * @param toIndex the index at which to stop sorting (exclusive) * @see java.util.Arrays#sort */ public void sort(int fromIndex, int toIndex) { Arrays.sort(_data, fromIndex, toIndex); } // filling /** * Fills every slot in the list with the specified value. * * @param val the value to use when filling */ public void fill(float val) { Arrays.fill(_data, 0, _pos, val); } /** * Fills a range in the list with the specified value. * * @param fromIndex the offset at which to start filling (inclusive) * @param toIndex the offset at which to stop filling (exclusive) * @param val the value to use when filling */ public void fill(int fromIndex, int toIndex, float val) { Arrays.fill(_data, fromIndex, toIndex, val); } // searching /** * Performs a binary search for <tt>value</tt> in the entire list. * Note that you <b>must</b> @{link #sort sort} the list before * doing a search. * * @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. */ public int binarySearch(float value) { return binarySearch(value, 0, _pos); } /** * Performs a binary search for <tt>value</tt> in the specified * range. Note that you <b>must</b> @{link #sort sort} the list * or the range before doing a search. * * @param value the value to search for * @param fromIndex the lower boundary of the range (inclusive) * @param toIndex the upper boundary of the range (exclusive) * @return the absolute offset in the list of the value, or its * negative insertion point into the sorted list. */ public int binarySearch(float 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; float midVal = _data[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. } /** * Searches the list front to back for the index of * <tt>value</tt>. * * @param value an <code>float</code> value * @return the first offset of the value, or -1 if it is not in * the list. * @see #binarySearch for faster searches on sorted lists */ public int indexOf(float value) { return indexOf(0, value); } /** * Searches the list front to back for the index of * <tt>value</tt>, starting at <tt>offset</tt>. * * @param offset the offset at which to start the linear search * (inclusive) * @param value an <code>float</code> value * @return the first offset of the value, or -1 if it is not in * the list. * @see #binarySearch for faster searches on sorted lists */ public int indexOf(int offset, float value) { for (int i = offset; i < _pos; i++) { if (_data[i] == value) { return i; } } return -1; } /** * Searches the list back to front for the last index of * <tt>value</tt>. * * @param value an <code>float</code> value * @return the last offset of the value, or -1 if it is not in * the list. * @see #binarySearch for faster searches on sorted lists */ public int lastIndexOf(float value) { return lastIndexOf(_pos, value); } /** * Searches the list back to front for the last index of * <tt>value</tt>, starting at <tt>offset</tt>. * * @param offset the offset at which to start the linear search * (exclusive) * @param value an <code>float</code> value * @return the last offset of the value, or -1 if it is not in * the list. * @see #binarySearch for faster searches on sorted lists */ public int lastIndexOf(int offset, float value) { for (int i = offset; i-- > 0;) { if (_data[i] == value) { return i; } } return -1; } /** * Searches the list for <tt>value</tt> * * @param value an <code>float</code> value * @return true if value is in the list. */ public boolean contains(float value) { return lastIndexOf(value) >= 0; } /** * 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 TFloatArrayList grep(TFloatProcedure condition) { TFloatArrayList list = new TFloatArrayList(); for (int i = 0; i < _pos; i++) { if (condition.execute(_data[i])) { list.add(_data[i]); } } return list; } /** * 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 TFloatArrayList inverseGrep(TFloatProcedure condition) { TFloatArrayList list = new TFloatArrayList(); for (int i = 0; i < _pos; i++) { if (! condition.execute(_data[i])) { list.add(_data[i]); } } return list; } /** * Finds the maximum value in the list. * * @return the largest value in the list. * @exception IllegalStateException if the list is empty */ public float max() { if (size() == 0) { throw new IllegalStateException("cannot find maximum of an empty list"); } float max = _data[_pos - 1]; for (int i = _pos - 1; i-- > 0;) { max = Math.max(max, _data[_pos]); } return max; } /** * Finds the minimum value in the list. * * @return the smallest value in the list. * @exception IllegalStateException if the list is empty */ public float min() { if (size() == 0) { throw new IllegalStateException("cannot find minimum of an empty list"); } float min = _data[_pos - 1]; for (int i = _pos - 1; i-- > 0;) { min = Math.min(min, _data[_pos]); } return min; } // stringification /** * Returns a String representation of the list, front to back. * * @return a <code>String</code> value */ public String toString() { final StringBuffer buf = new StringBuffer("{"); forEach(new TFloatProcedure() { public boolean execute(float val) { buf.append(val); buf.append(", "); return true; } }); buf.append("}"); return buf.toString(); }} // TFloatArrayList
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -