Ejemplo n.º 1
0
  @Override
  @TruffleBoundary
  protected void printValue(RAbstractListVector s, PrintContext printCtx) throws IOException {
    RAbstractIntVector dims = Utils.<RAbstractIntVector>castTo(s.getAttr(RRuntime.DIM_ATTR_KEY));

    if (dims != null && dims.getLength() > 1) {
      printDimList(s, printCtx);
    } else {
      // no dim()
      printNoDimList(s, printCtx);
    }
  }
Ejemplo n.º 2
0
 @Specialization(guards = {"each > 1", "hasNames(x)"})
 protected RAbstractVector repEachNames(
     RAbstractVector x,
     RAbstractIntVector times,
     int lengthOut,
     int each,
     @Cached("create()") InitAttributesNode initAttributes,
     @Cached("createNames()") SetFixedAttributeNode putNames) {
   if (times.getLength() > 1) {
     errorBranch.enter();
     throw invalidTimes();
   }
   RAbstractVector input = handleEach(x, each);
   RStringVector names = (RStringVector) handleEach(getNames.getNames(x), each);
   RVector<?> r;
   if (lengthOutOrTimes.profile(!RRuntime.isNA(lengthOut))) {
     names = (RStringVector) handleLengthOut(names, lengthOut, false);
     r = handleLengthOut(input, lengthOut, false);
   } else {
     names = (RStringVector) handleTimes(names, times, false);
     r = handleTimes(input, times, false);
   }
   putNames.execute(initAttributes.execute(r), names);
   return r;
 }
Ejemplo n.º 3
0
 @Specialization(guards = {"each > 1", "!hasNames(x)"})
 protected RAbstractVector repEachNoNames(
     RAbstractVector x, RAbstractIntVector times, int lengthOut, int each) {
   if (times.getLength() > 1) {
     errorBranch.enter();
     throw invalidTimes();
   }
   RAbstractVector input = handleEach(x, each);
   if (lengthOutOrTimes.profile(!RRuntime.isNA(lengthOut))) {
     return handleLengthOut(input, lengthOut, false);
   } else {
     return handleTimes(input, times, false);
   }
 }
Ejemplo n.º 4
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)));
 }
Ejemplo n.º 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;
   }
 }
Ejemplo n.º 6
0
 protected static boolean isLengthOne(RAbstractIntVector length) {
   return length.getLength() == 1;
 }
Ejemplo n.º 7
0
 @Specialization(guards = "isLengthOne(lengthVector)")
 protected RAbstractContainer updateLength(
     RAbstractContainer container, RAbstractIntVector lengthVector) {
   controlVisibility();
   return container.resize(lengthVector.getDataAt(0));
 }
Ejemplo n.º 8
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);
  }