@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();
}