Package org.renjin.util

Examples of org.renjin.util.NamesBuilder


public class NamesBuilderTest {

  @Test
  public void build() {
    NamesBuilder builder = NamesBuilder.withInitialLength(3);
    assertThat(builder.haveNames(), equalTo(false));
   
    builder.set(0, StringVector.NA);
    builder.set(1, StringVector.NA);
    assertThat(builder.haveNames(), equalTo(false));

    builder.add("Foo");
    assertThat(builder.haveNames(), equalTo(true));
   
  }
View Full Code Here


  @Internal
  public static IntVector which(Vector x) {
    IntArrayVector.Builder indices = new IntArrayVector.Builder();
    Vector xn = x.getNames();

    NamesBuilder names = NamesBuilder.withInitialLength(0);
    for(int i=0;i!=x.length();++i) {
      if(x.isElementTrue(i)) {
        indices.add(i+1);
        if(xn != Null.INSTANCE) {
          names.add(xn.getElementAsString(i));
        }
      }
    }
    return indices
      .setAttribute(Symbols.NAMES, names.build())
      .build();
  }
View Full Code Here

    /**
     * Go ahead and allocate and fill the memory
     */
    Vector.Builder result = x.newBuilderWithInitialCapacity(resultLength);
    NamesBuilder names = NamesBuilder.withInitialCapacity(resultLength);
    int result_i = 0;

    if(times.length() == 1) {
      for(int i=0;i!=resultLength;++i) {
        int x_i = (i / each) % x.length();
        result.setFrom(result_i++, x, x_i);
        names.add(x.getName(x_i));
      }
    } else {
      for(int x_i=0;x_i!=x.length();++x_i) {
        for(int j=0;j<times.getElementAsInt(x_i);++j) {
          result.setFrom(result_i++, x, x_i);
          names.add(x.getName(x_i));
        }
      }
    }
    if(names.haveNames()) {
      result.setAttribute(Symbols.NAMES, names.build(resultLength));
    }

    return result.build();
  }
View Full Code Here

  @Generic
  @Internal("as.vector")
  public static SEXP asVector(PairList x, String mode) {
    Vector.Builder result;
    NamesBuilder names = NamesBuilder.withInitialCapacity(x.length());
    if ("character".equals(mode)) {
      result = new StringVector.Builder();
    } else if ("logical".equals(mode)) {
      result = new LogicalArrayVector.Builder(x.length());
    } else if ("numeric".equals(mode)) {
      result = new DoubleArrayVector.Builder(x.length());
    } else if ("list".equals(mode)) {
      result = new ListVector.Builder();
    } else if ("raw".equals(mode)) {
      result = new RawVector.Builder();
    } else {
      throw new EvalException("invalid 'mode' argument");
    }

    for (PairList.Node node : x.nodes()) {
      if (node.hasTag()) {
        names.add(node.getTag().getPrintName());
      } else {
        names.addNA();
      }
      result.add(node.getValue());
    }
    result.setAttribute(Symbols.NAMES.getPrintName(),
        names.build(result.length()));
    return result.build();
  }
View Full Code Here

public class Tools {


  public static StringVector Rmd5(@Current Context context, StringVector paths) throws IOException, NoSuchAlgorithmException {
    StringVector.Builder result = new StringVector.Builder();
    NamesBuilder names = NamesBuilder.withInitialCapacity(paths.length());
    for(String path : paths) {
      result.add(hashFile(context, path));
    }
    result.setAttribute(Symbols.NAMES, names.build());
    return result.build();
  }
View Full Code Here

    super("expression");
  }

  @Override
  public SEXP apply(Context context, Environment rho, FunctionCall call, PairList args) {
    NamesBuilder names = NamesBuilder.withInitialLength(0);
    List<SEXP> expressions = Lists.newArrayList();
    for(PairList.Node node : args.nodes()) {
      names.add(node.getName());
      expressions.add(node.getValue());
    }
    AttributeMap.Builder attributes = AttributeMap.builder();
    if(names.haveNames()) {
      attributes.setNames((StringVector)names.build());
    }
    return new ExpressionVector(expressions, attributes.build());
  }
View Full Code Here

TOP

Related Classes of org.renjin.util.NamesBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.