public static class ScalarStringSelection {
public static RArray genericUpdate(RArray base, String index, RAny value, boolean subset, ASTNode ast) {
RArray typedBase;
Object rawValue;
int[] dimensions = base.dimensions();
Names names = base.names();
if (value instanceof RList) { // FIXME: this code gets copied around a few times, could it be refactored without a performance penalty?
if (base instanceof RList) {
typedBase = base;
} else {
typedBase = base.asList();
dimensions = null;
}
RAny v = subset ? ((RList) value).getRAny(0) : value;
v.ref();
rawValue = v;
} else if (base instanceof RList) {
typedBase = base;
rawValue = value;
value.ref();
} else if (base instanceof RRaw) {
if (value instanceof RRaw) {
typedBase = base;
rawValue = ((RRaw) value).get(0);
} else {
throw RError.getSubassignTypeFix(ast, value.typeOf(), base.typeOf());
}
} else if (value instanceof RRaw) {
throw RError.getSubassignTypeFix(ast, value.typeOf(), base.typeOf());
} else if (base instanceof RString || value instanceof RString) {
typedBase = base.asString();
rawValue = value.asString().get(0);
} else if (base instanceof RComplex || value instanceof RComplex) {
typedBase = base.asComplex();
rawValue = value.asComplex().get(0);
} else if (base instanceof RDouble || value instanceof RDouble) {
typedBase = base.asDouble();
rawValue = value.asDouble().get(0);
} else if (base instanceof RInt || value instanceof RInt) {
typedBase = base.asInt();
rawValue = value.asInt().get(0);
} else {
assert Utils.check(base instanceof RLogical || base instanceof RNull);
assert Utils.check(value instanceof RLogical);
typedBase = base.asLogical();
rawValue = ((RLogical) value).get(0);
}
int bsize = base.size();
int pos = -1;
RSymbol symbol = RSymbol.getSymbol(index);
if (names != null) {
pos = names.map(symbol);
}
if (pos != -1) {
// updating
if (base == typedBase && !base.isShared()) {
base.set(pos, rawValue);