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;
 }
  @Theory
  public void testCopyAttributes(UnaryArithmeticFactory factory, RAbstractVector originalOperand) {
    RAbstractVector operand = originalOperand.copy();
    // we have to e careful not to change mutable vectors
    RAbstractVector a = operand.copy();
    if (a instanceof RShareable) {
      ((RShareable) a).incRefCount();
    }

    RVector<?> aMaterialized = a.copy().materialize();
    aMaterialized.setAttr("a", "a");
    assertAttributes(executeArithmetic(factory, aMaterialized.copy()), "a");
  }
  @Specialization(guards = {"!copyAllAttributes || target != source", "containsMetadata(source)"})
  protected RAbstractVector copySameLength(
      RAbstractVector target,
      RAbstractVector source, //
      @Cached("create()") CopyOfRegAttributesNode copyOfReg, //
      @Cached("createDim()") RemoveFixedAttributeNode removeDim, //
      @Cached("createDimNames()") RemoveFixedAttributeNode removeDimNames, //
      @Cached("create()") InitAttributesNode initAttributes, //
      @Cached("createNames()") SetFixedAttributeNode putNames, //
      @Cached("createDim()") SetFixedAttributeNode putDim, //
      @Cached("createDimNames()") SetFixedAttributeNode putDimNames, //
      @Cached("createBinaryProfile()") ConditionProfile noDimensions, //
      @Cached("createBinaryProfile()") ConditionProfile hasNamesSource, //
      @Cached("createBinaryProfile()") ConditionProfile hasDimNames,
      @Cached("create()") GetDimAttributeNode getDimsNode) {
    RVector<?> result = target.materialize();

    if (copyAllAttributes) {
      copyOfReg.execute(source, result);
    }

    int[] newDimensions = getDimsNode.getDimensions(source);
    if (noDimensions.profile(newDimensions == null)) {
      DynamicObject attributes = result.getAttributes();
      if (attributes != null) {
        removeDim.execute(attributes);
        removeDimNames.execute(attributes);
      }

      RStringVector vecNames = getNamesNode.getNames(source);
      if (hasNamesSource.profile(vecNames != null)) {
        putNames.execute(initAttributes.execute(result), vecNames);
        return result;
      }
      return result;
    }

    putDim.execute(
        initAttributes.execute(result),
        RDataFactory.createIntVector(newDimensions, RDataFactory.COMPLETE_VECTOR));

    RList newDimNames = getDimNamesNode.getDimNames(source);
    if (hasDimNames.profile(newDimNames != null)) {
      putDimNames.execute(result.getAttributes(), newDimNames);
      newDimNames.elementNamePrefix = RRuntime.DIMNAMES_LIST_ELEMENT_NAME_PREFIX;
      return result;
    }
    return result;
  }
  @Specialization(
      guards = {
        "!copyAllAttributes || target != source",
        "containsMetadata(source, attrSourceProfiles)"
      })
  public RAbstractVector copySameLength(
      RAbstractVector target,
      RAbstractVector source, //
      @Cached("create()") CopyOfRegAttributesNode copyOfReg, //
      @Cached("createDim()") RemoveAttributeNode removeDim, //
      @Cached("createDimNames()") RemoveAttributeNode removeDimNames, //
      @Cached("create()") InitAttributesNode initAttributes, //
      @Cached("createNames()") PutAttributeNode putNames, //
      @Cached("createDim()") PutAttributeNode putDim, //
      @Cached("createBinaryProfile()") ConditionProfile noDimensions, //
      @Cached("createBinaryProfile()") ConditionProfile hasNamesSource, //
      @Cached("createBinaryProfile()") ConditionProfile hasDimNames) {
    RVector result = target.materialize();

    if (copyAllAttributes) {
      copyOfReg.execute(source, result);
    }

    int[] newDimensions = source.getDimensions();
    if (noDimensions.profile(newDimensions == null)) {
      RAttributes attributes = result.getAttributes();
      if (attributes != null) {
        removeDim.execute(attributes);
        removeDimNames.execute(attributes);
        result.setInternalDimNames(null);
      }
      result.setInternalDimensions(null);

      RStringVector vecNames = source.getNames(attrSourceProfiles);
      if (hasNamesSource.profile(vecNames != null)) {
        putNames.execute(initAttributes.execute(result), vecNames);
        result.setInternalNames(vecNames);
        return result;
      }
      return result;
    }

    putDim.execute(
        initAttributes.execute(result),
        RDataFactory.createIntVector(newDimensions, RDataFactory.COMPLETE_VECTOR));
    result.setInternalDimensions(newDimensions);

    RList newDimNames = source.getDimNames(attrSourceProfiles);
    if (hasDimNames.profile(newDimNames != null)) {
      result.getAttributes().put(RRuntime.DIMNAMES_ATTR_KEY, newDimNames);
      newDimNames.elementNamePrefix = RRuntime.DIMNAMES_LIST_ELEMENT_NAME_PREFIX;
      result.setInternalDimNames(newDimNames);
      return result;
    }
    return result;
  }
Example #5
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;
   }
 }