Example #1
0
 /** Prepare the input vector by replicating its elements. */
 private static RVector<?> handleEach(RAbstractVector x, int each) {
   RVector<?> r = x.createEmptySameType(x.getLength() * each, x.isComplete());
   for (int i = 0; i < x.getLength(); i++) {
     for (int j = i * each; j < (i + 1) * each; j++) {
       r.transferElementSameType(j, x, i);
     }
   }
   return r;
 }
Example #2
0
 /** 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;
   }
 }