Created
September 6, 2011 14:41
-
-
Save akbertram/1197723 to your computer and use it in GitHub Desktop.
Wrapper for + function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package r.base.primitives; | |
import r.lang.*; | |
import r.jvmi.wrapper.WrapperRuntime; | |
import r.jvmi.wrapper.ArgumentException; | |
import r.jvmi.wrapper.ArgumentIterator; | |
import r.lang.exception.EvalException; | |
import static r.jvmi.wrapper.WrapperRuntime.*; | |
public class R$primitive$_$43$_ extends BuiltinFunction { | |
public R$primitive$_$43$_() { super("+"); } | |
@Override | |
public EvalResult apply(Context context, Environment rho, FunctionCall call, PairList args) { | |
try {; | |
ArgumentIterator argIt = new ArgumentIterator(context, rho, args); | |
SEXP s0 = argIt.next().evalToExp(context,rho); | |
r.lang.Vector arg0; | |
arg0 = convertToVector(s0); | |
if(argIt.hasNext()) { | |
SEXP s1 = argIt.next().evalToExp(context,rho); | |
r.lang.Vector arg1; | |
arg1 = convertToVector(s1); | |
// **** public static double r.base.Ops.plus(double,double) | |
// component lengths | |
int arg0_length = arg0.length(); | |
int arg1_length = arg1.length(); | |
// compute longest vector | |
Vector longest = Null.INSTANCE; | |
int cycles = 0; | |
if(arg0_length > cycles) { | |
cycles = arg0_length; | |
longest = arg0; | |
} | |
if(arg1_length > cycles) { | |
cycles = arg1_length; | |
longest = arg1; | |
} | |
// initialize counters | |
int arg0_i = 0; | |
int arg1_i = 0; | |
r.lang.DoubleVector.Builder result = new r.lang.DoubleVector.Builder(cycles); | |
int resultIndex = 0; | |
for(int i=0;i!=cycles;++i) { | |
if(arg0.isElementNA(arg0_i) || arg1.isElementNA(arg1_i)) { | |
result.setNA(i); | |
} else { | |
double arg0_element = arg0.getElementAsDouble(arg0_i); | |
double arg1_element = arg1.getElementAsDouble(arg1_i); | |
result.set(i, r.base.Ops.plus(arg0_element, arg1_element)); | |
} | |
arg0_i++; | |
if(arg0_i >= arg0_length) arg0_i = 0; ; | |
arg1_i++; | |
if(arg1_i >= arg1_length) arg1_i = 0; ; | |
} | |
if(cycles > 0) { | |
result.copyAttributesFrom(longest); | |
} | |
return new EvalResult(result.build()); | |
} else { | |
// **** public static double r.base.Ops.plus(double) | |
int cycles = arg0.length(); | |
r.lang.DoubleVector.Builder result = new r.lang.DoubleVector.Builder(cycles); | |
int resultIndex = 0; | |
for(int i=0;i!=cycles;++i) { | |
if(arg0.isElementNA(i)) { | |
result.setNA(i); | |
} else { | |
double arg0_element = arg0.getElementAsDouble(i); | |
result.set(i, r.base.Ops.plus(arg0_element)); | |
} | |
} | |
if(cycles > 0) { | |
result.copyAttributesFrom(arg0); | |
} | |
return new EvalResult(result.build()); | |
} | |
} catch (r.jvmi.wrapper.ArgumentException e) { | |
throw new EvalException("Invalid argument. Expected:\n\t+(double, double)\n\t+(double)"); | |
} catch (java.lang.RuntimeException e) { | |
throw e; | |
} catch (java.lang.Exception e) { | |
throw new r.lang.exception.EvalException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment