Exemple #1
0
 @TruffleBoundary
 protected static Object encodeArgumentPairList(RArgsValuesAndNames args, String symbolName) {
   Object list = RNull.instance;
   for (int i = args.getLength() - 1; i >= 0; i--) {
     String name = args.getSignature().getName(i);
     list =
         RDataFactory.createPairList(
             args.getArgument(i),
             list,
             name == null ? RNull.instance : RDataFactory.createSymbolInterned(name));
   }
   list = RDataFactory.createPairList(symbolName, list);
   return list;
 }
Exemple #2
0
 private RIntVector createResult(RAbstractVector x, int[] data, boolean useNames) {
   RIntVector result = RDataFactory.createIntVector(data, RDataFactory.COMPLETE_VECTOR);
   if (useNames) {
     result.copyNamesFrom(attrProfiles, x);
   }
   return result;
 }
Exemple #3
0
 @Specialization
 protected Object castFunction(RFunction f) {
   if (isNonVectorPreserved()) {
     return f;
   } else {
     return RDataFactory.createList();
   }
 }
Exemple #4
0
 @Specialization
 protected Object castNull(@SuppressWarnings("unused") RNull rnull) {
   if (isNonVectorPreserved()) {
     return RNull.instance;
   } else {
     return RDataFactory.createList();
   }
 }
Exemple #5
0
 @SuppressWarnings("unused")
 @Specialization
 protected RDoubleVector gc(boolean verbose, boolean reset) {
   doGc();
   // TODO: somehow produce the (semi?) correct values
   double[] data = new double[14];
   Arrays.fill(data, RRuntime.DOUBLE_NA);
   return RDataFactory.createDoubleVector(data, RDataFactory.INCOMPLETE_VECTOR);
 }
 public final RIntVector materialize() {
   int length = getLength();
   int[] result = new int[length];
   for (int i = 0; i < length; i++) {
     int data = getDataAt(i);
     result[i] = data;
   }
   return RDataFactory.createIntVector(result, vector.isComplete());
 }
  @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;
  }
Exemple #8
0
 @Override
 public Object getRelementImpl(int index) {
   if (index == 0) {
     String name = getBuiltin().getName();
     assert name == name.intern();
     return RDataFactory.createSymbol(name);
   } else {
     throw RInternalError.unimplemented();
   }
 }
 @Override
 public RAbstractVector tryFoldConstantTime(RAbstractVector operand, int operandLength) {
   if (arithmetic instanceof Plus) {
     return operand;
   } else if (arithmetic instanceof Negate && operand instanceof RSequence) {
     if (operand instanceof RIntSequence) {
       int start = ((RIntSequence) operand).getStart();
       int stride = ((RIntSequence) operand).getStride();
       return RDataFactory.createIntSequence(
           applyInteger(start), applyInteger(stride), operandLength);
     } else if (operand instanceof RDoubleSequence) {
       double start = ((RDoubleSequence) operand).getStart();
       double stride = ((RDoubleSequence) operand).getStride();
       return RDataFactory.createDoubleSequence(
           applyDouble(start), applyDouble(stride), operandLength);
     }
   }
   return null;
 }
  @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;
  }
Exemple #11
0
 @Specialization(
     guards = {"x.getLength() == 1", "times.getLength() == 1", "each <= 1", "!hasNames(x)"})
 protected RAbstractVector repNoEachNoNamesSimple(
     RAbstractDoubleVector x,
     RAbstractIntVector times,
     int lengthOut,
     @SuppressWarnings("unused") int each) {
   int t = times.getDataAt(0);
   if (t < 0) {
     errorBranch.enter();
     throw invalidTimes();
   }
   int length = lengthOutOrTimes.profile(!RRuntime.isNA(lengthOut)) ? lengthOut : t;
   double[] data = new double[length];
   Arrays.fill(data, x.getDataAt(0));
   return RDataFactory.createDoubleVector(data, !RRuntime.isNA(x.getDataAt(0)));
 }
Exemple #12
0
 public final RVector createEmptySameType(int newLength, boolean newIsComplete) {
   return RDataFactory.createIntVector(new int[newLength], newIsComplete);
 }
Exemple #13
0
 @Specialization
 protected RIntVector doNull(
     @SuppressWarnings("unused") RNull x, @SuppressWarnings("unused") boolean useNames) {
   return RDataFactory.createEmptyIntVector();
 }
Exemple #14
0
  private static void printDimList(RAbstractListVector s, PrintContext printCtx)
      throws IOException {
    final PrintParameters pp = printCtx.parameters();

    int ns = s.getLength();
    String[] t = new String[ns];
    for (int i = 0; i < ns; i++) {
      Object tmp = RRuntime.asAbstractVector(s.getDataAtAsObject(i));
      final String pbuf;
      if (tmp == null || tmp == RNull.instance) {
        pbuf = RRuntime.NULL;
      } else if (tmp instanceof RAbstractLogicalVector) {
        RAbstractLogicalVector lv = (RAbstractLogicalVector) tmp;
        if (lv.getLength() == 1) {
          FormatMetrics fm = LogicalVectorPrinter.formatLogicalVector(lv, 0, 1, pp.getNaWidth());
          pbuf =
              snprintf(
                  115, "%s", LogicalVectorPrinter.encodeLogical(lv.getDataAt(0), fm.maxWidth, pp));
        } else {
          pbuf = snprintf(115, "Logical,%d", lv.getLength());
        }
      } else if (tmp instanceof RAbstractIntVector) {
        RAbstractIntVector iv = (RAbstractIntVector) tmp;
        if (printCtx.printerNode().inherits(iv, RRuntime.CLASS_FACTOR)) {
          /* factors are stored as integers */
          pbuf = snprintf(115, "factor,%d", iv.getLength());
        } else {
          if (iv.getLength() == 1) {
            FormatMetrics fm = IntegerVectorPrinter.formatIntVector(iv, 0, 1, pp.getNaWidth());
            pbuf =
                snprintf(
                    115,
                    "%s",
                    IntegerVectorPrinter.encodeInteger(iv.getDataAt(0), fm.maxWidth, pp));
          } else {
            pbuf = snprintf(115, "Integer,%d", iv.getLength());
          }
        }
      } else if (tmp instanceof RAbstractDoubleVector) {
        RAbstractDoubleVector dv = (RAbstractDoubleVector) tmp;
        if (dv.getLength() == 1) {
          DoubleVectorMetrics fm = DoubleVectorPrinter.formatDoubleVector(dv, 0, 1, 0, pp);
          pbuf = snprintf(115, "%s", DoubleVectorPrinter.encodeReal(dv.getDataAt(0), fm, pp));
        } else {
          pbuf = snprintf(115, "Numeric,%d", dv.getLength());
        }
      } else if (tmp instanceof RAbstractComplexVector) {
        RAbstractComplexVector cv = (RAbstractComplexVector) tmp;
        if (cv.getLength() == 1) {
          RComplex x = cv.getDataAt(0);
          if (RRuntime.isNA(x.getRealPart()) || RRuntime.isNA(x.getImaginaryPart())) {
            /* formatReal(NA) --> w=R_print.na_width, d=0, e=0 */
            pbuf =
                snprintf(
                    115,
                    "%s",
                    DoubleVectorPrinter.encodeReal(
                        RRuntime.DOUBLE_NA, pp.getNaWidth(), 0, 0, '.', pp));
          } else {
            ComplexVectorMetrics cvm = ComplexVectorPrinter.formatComplexVector(x, 0, 1, 0, pp);
            pbuf = snprintf(115, "%s", ComplexVectorPrinter.encodeComplex(x, cvm, pp));
          }
        } else {
          pbuf = snprintf(115, "Complex,%d", cv.getLength());
        }
      } else if (tmp instanceof RAbstractStringVector) {
        RAbstractStringVector sv = (RAbstractStringVector) tmp;
        if (sv.getLength() == 1) {
          String ctmp = sv.getDataAt(0);
          int len = ctmp.length();
          if (len < 100) {
            pbuf = snprintf(115, "\"%s\"", ctmp);
          } else {
            pbuf = snprintf(101, "\"%s", ctmp) + "\" [truncated]";
          }
        } else {
          pbuf = snprintf(115, "Character,%d", sv.getLength());
        }
      } else if (tmp instanceof RAbstractRawVector) {
        pbuf = snprintf(115, "Raw,%d", ((RAbstractRawVector) (tmp)).getLength());
      } else if (tmp instanceof RAbstractListVector) {
        pbuf = snprintf(115, "List,%d", ((RAbstractListVector) (tmp)).getLength());
      } else if (tmp instanceof RLanguage) {
        pbuf = snprintf(115, "Expression");
      } else {
        pbuf = snprintf(115, "?");
      }

      t[i] = pbuf;
    }

    RStringVector tt = RDataFactory.createStringVector(t, true, s.getDimensions());
    Object dimNames = s.getAttr(RRuntime.DIMNAMES_ATTR_KEY);
    tt.setAttr(RRuntime.DIMNAMES_ATTR_KEY, dimNames);

    PrintContext cc = printCtx.cloneContext();
    cc.parameters().setQuote(false);
    StringVectorPrinter.INSTANCE.print(tt, cc);
  }