/** * Returns a list which is a concatenation of <code>times</code> times the receiver. * @param times the number of times the receiver shall be copied. */ public AbstractShortList times(int times) { AbstractShortList newList = new ShortArrayList(times*size()); for (int i=times; --i >= 0; ) { newList.addAllOfFromTo(this,0,size()-1); } return newList; } /**
/** * Appends the specified element to the end of this list. * * @param element element to be appended to this list. */ public void add(short element) { beforeInsert(size,element); } /**
/** * Appends the part of the specified list between <code>from</code> (inclusive) and <code>to</code> (inclusive) to the receiver. * * @param other the list to be added to the receiver. * @param from the index of the first element to be appended (inclusive). * @param to the index of the last element to be appended (inclusive). * @exception IndexOutOfBoundsException index is out of range (<tt>other.size()>0 && (from<0 || from>to || to>=other.size())</tt>). */ public void addAllOfFromTo(AbstractShortList other, int from, int to) { beforeInsertAllOfFromTo(size,other,from,to); } /**
/** * Sets the specified range of elements in the specified array to the specified value. * * @param from the index of the first element (inclusive) to be filled with the specified value. * @param to the index of the last element (inclusive) to be filled with the specified value. * @param val the value to be stored in the specified elements of the receiver. */ public void fillFromToWith(int from, int to, short val) { checkRangeFromTo(from,to,this.size); for (int i=from; i<=to;) setQuick(i++,val); } /**
/** * Inserts the specified element before the specified position into the receiver. * Shifts the element currently at that position (if any) and * any subsequent elements to the right. * * @param index index before which the specified element is to be inserted (must be in [0,size]). * @param element element to be inserted. * @exception IndexOutOfBoundsException index is out of range (<tt>index < 0 || index > size()</tt>). */ public void beforeInsert(int index, short element) { beforeInsertDummies(index,1); set(index,element); } /**
/** * Inserts the part of the specified list between <code>otherFrom</code> (inclusive) and <code>otherTo</code> (inclusive) before the specified position into the receiver. * Shifts the element currently at that position (if any) and * any subsequent elements to the right. * * @param index index before which to insert first element from the specified list (must be in [0,size]).. * @param other list of which a part is to be inserted into the receiver. * @param from the index of the first element to be inserted (inclusive). * @param to the index of the last element to be inserted (inclusive). * @exception IndexOutOfBoundsException index is out of range (<tt>other.size()>0 && (from<0 || from>to || to>=other.size())</tt>). * @exception IndexOutOfBoundsException index is out of range (<tt>index < 0 || index > size()</tt>). */ public void beforeInsertAllOfFromTo(int index, AbstractShortList other, int from, int to) { int length=to-from+1; this.beforeInsertDummies(index, length); this.replaceFromToWithFrom(index, index+length-1, other, from); } /**
/** * Sorts the specified range of the receiver into * ascending numerical order. The sorting algorithm is a tuned quicksort, * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a * Sort Function", Software-Practice and Experience, Vol. 23(11) * P. 1249-1265 (November 1993). This algorithm offers n*log(n) * performance on many data sets that cause other quicksorts to degrade to * quadratic performance. * * <p><b>You should never call this method unless you are sure that this particular sorting algorithm is the right one for your data set.</b> * It is generally better to call <tt>sort()</tt> or <tt>sortFromTo(...)</tt> instead, because those methods automatically choose the best sorting algorithm. * * @param from the index of the first element (inclusive) to be sorted. * @param to the index of the last element (inclusive) to be sorted. * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public void quickSortFromTo(int from, int to) { int mySize = size(); checkRangeFromTo(from, to, mySize); short[] myElements = elements(); java.util.Arrays.sort(myElements, from, to+1); elements(myElements); setSizeRaw(mySize); } /**
/** * Replaces a number of elements in the receiver with the same number of elements of another list. * Replaces elements in the receiver, between <code>from</code> (inclusive) and <code>to</code> (inclusive), * with elements of <code>other</code>, starting from <code>otherFrom</code> (inclusive). * * @param from the position of the first element to be replaced in the receiver * @param to the position of the last element to be replaced in the receiver * @param other list holding elements to be copied into the receiver. * @param otherFrom position of first element within other list to be copied. */ public void replaceFromToWithFrom(int from, int to, AbstractShortList other, int otherFrom) { int length=to-from+1; if (length>0) { checkRangeFromTo(from, to, size()); checkRangeFromTo(otherFrom,otherFrom+length-1,other.size()); // unambiguous copy (it may hold other==this) if (from<=otherFrom) { for (; --length >= 0; ) setQuick(from++,other.getQuick(otherFrom++)); } else { int otherTo = otherFrom+length-1; for (; --length >= 0; ) setQuick(to--,other.getQuick(otherTo--)); } } } /**
replaceFromToWithFromTo(from, to, partFromTo(otherFrom, otherTo), 0, otherTo-otherFrom); return; beforeInsertDummies(theLast+1, diff); removeFromTo(theLast+diff, theLast-1); replaceFromToWithFrom(from,from+length-1,other,otherFrom);
if (! (other instanceof ShortArrayList)) return super.retainAll(other); int limit = other.size()-1; int j=0; short[] theElements = elements; int mySize = size(); double N=(double) other.size(); double M=(double) mySize; if ( (N+M)*cern.jet.math.Arithmetic.log2(N) < M*N ) { ShortArrayList sortedList = (ShortArrayList) other.clone(); sortedList.quickSort(); if (other.indexOfFromTo(theElements[i], 0, limit) >= 0) theElements[j++]=theElements[i];
if (! (other instanceof ShortArrayList)) return super.removeAll(other); if (other.size()==0) {return false;} //nothing to do int limit = other.size()-1; int j=0; short[] theElements = elements; int mySize = size(); double N=(double) other.size(); double M=(double) mySize; if ( (N+M)*cern.jet.math.Arithmetic.log2(N) < M*N ) { ShortArrayList sortedList = (ShortArrayList) other.clone(); sortedList.quickSort(); if (other.indexOfFromTo(theElements[i], 0, limit) < 0) theElements[j++]=theElements[i];
/** * Returns a string representation of the receiver, containing * the String representation of each element. */ public String toString() { return cern.colt.Arrays.toString(partFromTo(0, size()-1).elements()); } }
/** * Replaces the part of the receiver starting at <code>from</code> (inclusive) with all the elements of the specified collection. * Does not alter the size of the receiver. * Replaces exactly <tt>Math.max(0,Math.min(size()-from, other.size()))</tt> elements. * * @param from the index at which to copy the first element from the specified collection. * @param other Collection to replace part of the receiver * @exception IndexOutOfBoundsException index is out of range (index < 0 || index >= size()). */ public void replaceFromWith(int from, java.util.Collection other) { checkRange(from,size()); java.util.Iterator e = other.iterator(); int index=from; int limit = Math.min(size()-from, other.size()); for (int i=0; i<limit; i++) set(index++,((Number) e.next()).shortValue()); //delta } /**
/** * Returns the index of the last occurrence of the specified * element. Returns <code>-1</code> if the receiver does not contain this element. * Searches beginning at <code>to</code>, inclusive until <code>from</code>, inclusive. * Tests for identity. * * @param element element to search for. * @param from the leftmost search position, inclusive. * @param to the rightmost search position, inclusive. * @return the index of the last occurrence of the element in the receiver; returns <code>-1</code> if the element is not found. * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public int lastIndexOfFromTo(short element, int from, int to) { checkRangeFromTo(from, to, size()); for (int i = to ; i >= from; i--) { if (element==getQuick(i)) return i; //found } return -1; //not found } /**
/** * Removes from the receiver all elements whose index is between * <code>from</code>, inclusive and <code>to</code>, inclusive. Shifts any succeeding * elements to the left (reduces their index). * This call shortens the list by <tt>(to - from + 1)</tt> elements. * * @param from index of first element to be removed. * @param to index of last element to be removed. * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public void removeFromTo(int from, int to) { checkRangeFromTo(from, to, size); int numMoved = size - to - 1; if (numMoved > 0) { replaceFromToWithFrom(from, from-1+numMoved, this, to+1); //fillFromToWith(from+numMoved, size-1, 0.0f); //delta } int width = to-from+1; if (width>0) setSizeRaw(size-width); } /**
/** * Sets the receiver's elements to be the specified array. * The size and capacity of the list is the length of the array. * <b>WARNING:</b> For efficiency reasons and to keep memory usage low, this method may decide <b>not to copy the array</b>. * So if subsequently you modify the returned array directly via the [] operator, be sure you know what you're doing. * * @param elements the new elements to be stored. * @return the receiver itself. */ public AbstractShortList elements(short[] elements) { clear(); addAllOfFromTo(new ShortArrayList(elements),0,elements.length-1); return this; } /**
/** * Returns the index of the first occurrence of the specified * element. Returns <code>-1</code> if the receiver does not contain this element. * Searches between <code>from</code>, inclusive and <code>to</code>, inclusive. * Tests for identity. * * @param element element to search for. * @param from the leftmost search position, inclusive. * @param to the rightmost search position, inclusive. * @return the index of the first occurrence of the element in the receiver; returns <code>-1</code> if the element is not found. * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public int indexOfFromTo(short element, int from, int to) { checkRangeFromTo(from, to, size); for (int i = from ; i <= to; i++) { if (element==getQuick(i)) return i; //found } return -1; //not found } /**
/** * Returns a new list of the part of the receiver between <code>from</code>, inclusive, and <code>to</code>, inclusive. * @param from the index of the first element (inclusive). * @param to the index of the last element (inclusive). * @return a new list * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public AbstractShortList partFromTo(int from, int to) { checkRangeFromTo(from, to, size); int length = to-from+1; ShortArrayList part = new ShortArrayList(length); part.addAllOfFromTo(this,from,to); return part; } /**
/** * Searches the receiver for the specified value using * the binary search algorithm. The receiver must <strong>must</strong> be * sorted (as by the sort method) prior to making this call. If * it is not sorted, the results are undefined: in particular, the call * may enter an infinite loop. If the receiver contains multiple elements * equal to the specified object, there is no guarantee which instance * will be found. * * @param key the value to be searched for. * @return index of the search key, if it is contained in the receiver; * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The <i>insertion * point</i> is defined as the the point at which the value would * be inserted into the receiver: the index of the first * element greater than the key, or <tt>receiver.size()</tt>, if all * elements in the receiver are less than the specified key. Note * that this guarantees that the return value will be >= 0 if * and only if the key is found. * @see java.util.Arrays */ public int binarySearch(short key) { return this.binarySearchFromTo(key, 0, size-1); } /**
/** * Sorts the specified range of the receiver into * ascending numerical order. The sorting algorithm is a tuned quicksort, * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a * Sort Function", Software-Practice and Experience, Vol. 23(11) * P. 1249-1265 (November 1993). This algorithm offers n*log(n) * performance on many data sets that cause other quicksorts to degrade to * quadratic performance. * * <p><b>You should never call this method unless you are sure that this particular sorting algorithm is the right one for your data set.</b> * It is generally better to call <tt>sort()</tt> or <tt>sortFromTo(...)</tt> instead, because those methods automatically choose the best sorting algorithm. * * @param from the index of the first element (inclusive) to be sorted. * @param to the index of the last element (inclusive) to be sorted. * @exception IndexOutOfBoundsException index is out of range (<tt>size()>0 && (from<0 || from>to || to>=size())</tt>). */ public void quickSortFromTo(int from, int to) { int mySize = size(); checkRangeFromTo(from, to, mySize); short[] myElements = elements(); java.util.Arrays.sort(myElements, from, to+1); elements(myElements); setSizeRaw(mySize); } /**