/** * Replaces the part between <code>from</code> (inclusive) and <code>to</code> (inclusive) with * the other list's part between <code>otherFrom</code> and <code>otherTo</code>. Powerful (and * tricky) method! Both parts need not be of the same size (part A can both be smaller or larger * than part B). Parts may overlap. Receiver and other list may (but most not) be identical. If * <code>from > to</code>, then inserts other part before <code>from</code>. * * @param from the first element of the receiver (inclusive) * @param to the last element of the receiver (inclusive) * @param other the other list (may be identical with receiver) * @param otherFrom the first element of the other list (inclusive) * @param otherTo the last element of the other list (inclusive) * <p><b>Examples:</b> * <pre> * a=[0, 1, 2, 3, 4, 5, 6, 7] * b=[50, 60, 70, 80, 90] * a.R(...)=a.replaceFromToWithFromTo(...) * * a.R(3,5,b,0,4)-->[0, 1, 2, 50, 60, 70, 80, 90, 6, 7] * a.R(1,6,b,0,4)-->[0, 50, 60, 70, 80, 90, 7] * a.R(0,6,b,0,4)-->[50, 60, 70, 80, 90, 7] * a.R(3,5,b,1,2)-->[0, 1, 2, 60, 70, 6, 7] * a.R(1,6,b,1,2)-->[0, 60, 70, 7] * a.R(0,6,b,1,2)-->[60, 70, 7] * a.R(5,3,b,0,4)-->[0, 1, 2, 3, 4, 50, 60, 70, 80, 90, 5, 6, 7] * a.R(5,0,b,0,4)-->[0, 1, 2, 3, 4, 50, 60, 70, 80, 90, 5, 6, 7] * a.R(5,3,b,1,2)-->[0, 1, 2, 3, 4, 60, 70, 5, 6, 7] * a.R(5,0,b,1,2)-->[0, 1, 2, 3, 4, 60, 70, 5, 6, 7] * * Extreme cases: * a.R(5,3,b,0,0)-->[0, 1, 2, 3, 4, 50, 5, 6, 7] * a.R(5,3,b,4,4)-->[0, 1, 2, 3, 4, 90, 5, 6, 7] * a.R(3,5,a,0,1)-->[0, 1, 2, 0, 1, 6, 7] * a.R(3,5,a,3,5)-->[0, 1, 2, 3, 4, 5, 6, 7] * a.R(3,5,a,4,4)-->[0, 1, 2, 4, 6, 7] * a.R(5,3,a,0,4)-->[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7] * a.R(0,-1,b,0,4)-->[50, 60, 70, 80, 90, 0, 1, 2, 3, 4, 5, 6, 7] * a.R(0,-1,a,0,4)-->[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7] * a.R(8,0,a,0,4)-->[0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4] * </pre> */ public void replaceFromToWithFromTo( int from, int to, AbstractLongList other, int otherFrom, int otherTo) { if (otherFrom > otherTo) { throw new IndexOutOfBoundsException("otherFrom: " + otherFrom + ", otherTo: " + otherTo); } if (this == other && to - from != otherTo - otherFrom) { // avoid stumbling over my own feet replaceFromToWithFromTo(from, to, partFromTo(otherFrom, otherTo), 0, otherTo - otherFrom); return; } int length = otherTo - otherFrom + 1; int diff = length; int theLast = from - 1; if (to >= from) { diff -= (to - from + 1); theLast = to; } if (diff > 0) { beforeInsertDummies(theLast + 1, diff); } else { if (diff < 0) { removeFromTo(theLast + diff, theLast - 1); } } if (length > 0) { replaceFromToWithFrom(from, from + length - 1, other, otherFrom); } }
/** * Inserts <tt>length</tt> dummy elements before the specified position into the receiver. Shifts * the element currently at that position (if any) and any subsequent elements to the right. * <b>This method must set the new size to be <tt>size()+length</tt>. * * @param index index before which to insert dummy elements (must be in [0,size]).. * @param length number of dummy elements to be inserted. * @throws IndexOutOfBoundsException if <tt>index < 0 || index > size()</tt>. */ protected void beforeInsertDummies(int index, int length) { if (index > size || index < 0) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); if (length > 0) { ensureCapacity(size + length); setSizeRaw(size + length); replaceFromToWithFrom(index + length, size - 1, this, index); } }
/** * 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); }