/** Extend or truncate the vector to a specified length. */ private static RVector<?> handleLengthOut( RAbstractVector x, int lengthOut, boolean copyIfSameSize) { if (x.getLength() == lengthOut) { return (RVector<?>) (copyIfSameSize ? x.copy() : x); } return x.copyResized(lengthOut, false); }
/** Replicate the vector a given number of times. */ private RVector<?> handleTimes( RAbstractVector x, RAbstractIntVector times, boolean copyIfSameSize) { if (oneTimeGiven.profile(times.getLength() == 1)) { // only one times value is given final int howManyTimes = times.getDataAt(0); if (howManyTimes < 0) { errorBranch.enter(); throw invalidTimes(); } if (replicateOnce.profile(howManyTimes == 1)) { return (RVector<?>) (copyIfSameSize ? x.copy() : x); } else { return x.copyResized(x.getLength() * howManyTimes, false); } } else { // times is a vector with several elements if (x.getLength() != times.getLength()) { errorBranch.enter(); invalidTimes(); } // iterate once over the times vector to determine result vector size int resultLength = 0; for (int i = 0; i < times.getLength(); i++) { int t = times.getDataAt(i); if (t < 0) { errorBranch.enter(); throw invalidTimes(); } resultLength += t; } // create and populate result vector RVector<?> r = x.createEmptySameType(resultLength, x.isComplete()); int wp = 0; // write pointer for (int i = 0; i < x.getLength(); i++) { for (int j = 0; j < times.getDataAt(i); ++j, ++wp) { r.transferElementSameType(wp, x, i); } } return r; } }