From 579ff78a99ec298fa630d892e31e5dec53e2bc36 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 23 Sep 2017 16:19:45 -0700 Subject: [PATCH] Move two more operators into separate classes. --- .../nwapw/abacus/plugin/StandardPlugin.java | 48 ++----------------- .../plugin/standard/OperatorFactorial.kt | 32 +++++++++++++ .../abacus/plugin/standard/OperatorNegate.kt | 17 +++++++ 3 files changed, 52 insertions(+), 45 deletions(-) create mode 100644 core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorFactorial.kt create mode 100644 core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorNegate.kt diff --git a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index db962ba..c1d8862 100755 --- a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -5,10 +5,7 @@ import org.nwapw.abacus.function.*; import org.nwapw.abacus.number.NaiveNumber; import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.number.PreciseNumber; -import org.nwapw.abacus.plugin.standard.OperatorAdd; -import org.nwapw.abacus.plugin.standard.OperatorDivide; -import org.nwapw.abacus.plugin.standard.OperatorMultiply; -import org.nwapw.abacus.plugin.standard.OperatorSubtract; +import org.nwapw.abacus.plugin.standard.*; import org.nwapw.abacus.tree.TreeNode; import org.nwapw.abacus.tree.VariableNode; @@ -65,17 +62,7 @@ public class StandardPlugin extends Plugin { /** * The negation operator, - */ - public static final NumberOperator OP_NEGATE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.UNARY_PREFIX, 0) { - @Override - public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { - return params.length == 1; - } - - @Override - public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { - return params[0].negate(); - } - }; + public static final NumberOperator OP_NEGATE = new OperatorNegate(); /** * The multiplication operator, * */ @@ -138,36 +125,7 @@ public class StandardPlugin extends Plugin { /** * The factorial operator, ! */ - public static final NumberOperator OP_FACTORIAL = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.UNARY_POSTFIX, 0) { - //private HashMap, ArrayList> storedList = new HashMap, ArrayList>(); - @Override - public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { - return params.length == 1 - && params[0].fractionalPart().compareTo(context.getInheritedNumberImplementation().instanceForString("0")) == 0 - && params[0].signum() >= 0; - } - - @Override - public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { - NumberImplementation implementation = context.getInheritedNumberImplementation(); - if (params[0].signum() == 0) { - return implementation.instanceForString("1"); - } - NumberInterface one = implementation.instanceForString("1"); - NumberInterface factorial = params[0]; - NumberInterface multiplier = params[0]; - //It is necessary to later prevent calls of factorial on anything but non-negative integers. - while ((multiplier = multiplier.subtract(one)).signum() == 1) { - factorial = factorial.multiply(multiplier); - } - return factorial; - /*if(!storedList.containsKey(params[0].getClass())){ - storedList.put(params[0].getClass(), new ArrayList()); - storedList.get(params[0].getClass()).add(NaiveNumber.ONE.promoteTo(params[0].getClass())); - storedList.get(params[0].getClass()).add(NaiveNumber.ONE.promoteTo(params[0].getClass())); - }*/ - } - }; + public static final NumberOperator OP_FACTORIAL = new OperatorFactorial(); /** * The permutation operator. */ diff --git a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorFactorial.kt b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorFactorial.kt new file mode 100644 index 0000000..4f5eab3 --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorFactorial.kt @@ -0,0 +1,32 @@ +package org.nwapw.abacus.plugin.standard + +import org.nwapw.abacus.context.MutableEvaluationContext +import org.nwapw.abacus.function.NumberOperator +import org.nwapw.abacus.function.OperatorAssociativity +import org.nwapw.abacus.function.OperatorType +import org.nwapw.abacus.number.NumberInterface + +class OperatorFactorial: NumberOperator(OperatorAssociativity.LEFT, OperatorType.UNARY_POSTFIX, 0) { + + override fun matchesParams(context: MutableEvaluationContext, params: Array) = + params.size == 1 + && params[0].fractionalPart().compareTo(context.inheritedNumberImplementation.instanceForString("0")) == 0 + && params[0].signum() >= 0 + + override fun applyInternal(context: MutableEvaluationContext, params: Array): NumberInterface { + val implementation = context.inheritedNumberImplementation + val one = implementation.instanceForString("1") + if (params[0].signum() == 0) { + return one + } + var factorial = params[0] + var multiplier = params[0] - one + //It is necessary to later prevent calls of factorial on anything but non-negative integers. + while (multiplier.signum() == 1) { + factorial *= multiplier + multiplier -= one + } + return factorial + } + +} \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorNegate.kt b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorNegate.kt new file mode 100644 index 0000000..35b40a8 --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorNegate.kt @@ -0,0 +1,17 @@ +package org.nwapw.abacus.plugin.standard + +import org.nwapw.abacus.context.MutableEvaluationContext +import org.nwapw.abacus.function.NumberOperator +import org.nwapw.abacus.function.OperatorAssociativity +import org.nwapw.abacus.function.OperatorType +import org.nwapw.abacus.number.NumberInterface + +class OperatorNegate: NumberOperator(OperatorAssociativity.LEFT, OperatorType.UNARY_PREFIX, 0) { + + override fun matchesParams(context: MutableEvaluationContext, params: Array) = + params.size == 1 + + override fun applyInternal(context: MutableEvaluationContext, params: Array) = + -params[0] + +} \ No newline at end of file