Format newly written code.

This commit is contained in:
Danila Fedorin 2017-08-26 12:19:34 -07:00
parent 385a64eace
commit fbc12ec41c
8 changed files with 78 additions and 77 deletions

View File

@ -70,7 +70,7 @@ public abstract class Plugin {
* To be used in load(). Registers a tree value function abstract class * To be used in load(). Registers a tree value function abstract class
* with the plugin internally, which makes it accessible to the plugin manager. * with the plugin internally, which makes it accessible to the plugin manager.
* *
* @param name the name to register by. * @param name the name to register by.
* @param toRegister the tree value function implementation. * @param toRegister the tree value function implementation.
*/ */
protected final void registerTreeValueFunction(String name, TreeValueFunction toRegister) { protected final void registerTreeValueFunction(String name, TreeValueFunction toRegister) {
@ -94,10 +94,10 @@ public abstract class Plugin {
* with the plugin internally, which makes it accessible * with the plugin internally, which makes it accessible
* to the plugin manager. * to the plugin manager.
* *
* @param name the name of the tree value operator. * @param name the name of the tree value operator.
* @param operator the tree value operator to register. * @param operator the tree value operator to register.
*/ */
protected final void registerTreeValueOperator(String name, TreeValueOperator operator){ protected final void registerTreeValueOperator(String name, TreeValueOperator operator) {
manager.registerTreeValueOperator(name, operator); manager.registerTreeValueOperator(name, operator);
} }
@ -142,7 +142,7 @@ public abstract class Plugin {
* @param name the name for which to search. * @param name the name for which to search.
* @return the resulting tree value function, or null if none was found for that name. * @return the resulting tree value function, or null if none was found for that name.
*/ */
protected final TreeValueFunction treeValueFunctionFor(String name){ protected final TreeValueFunction treeValueFunctionFor(String name) {
return manager.treeValueFunctionFor(name); return manager.treeValueFunctionFor(name);
} }

View File

@ -100,7 +100,7 @@ public class PluginManager {
/** /**
* Registers a tree value function under the given name. * Registers a tree value function under the given name.
* *
* @param name the name of the function. * @param name the name of the function.
* @param function the function to register. * @param function the function to register.
*/ */
public void registerTreeValueFunction(String name, TreeValueFunction function) { public void registerTreeValueFunction(String name, TreeValueFunction function) {
@ -120,10 +120,10 @@ public class PluginManager {
/** /**
* Registers a tree value operator under the given name. * Registers a tree value operator under the given name.
* *
* @param name the name of the tree value operator. * @param name the name of the tree value operator.
* @param operator the tree value operator to register. * @param operator the tree value operator to register.
*/ */
public void registerTreeValueOperator(String name, TreeValueOperator operator){ public void registerTreeValueOperator(String name, TreeValueOperator operator) {
registeredTreeValueOperators.put(name, operator); registeredTreeValueOperators.put(name, operator);
} }
@ -163,7 +163,7 @@ public class PluginManager {
* @param name the name of the function. * @param name the name of the function.
* @return the function, or null if it was not found. * @return the function, or null if it was not found.
*/ */
public TreeValueFunction treeValueFunctionFor(String name){ public TreeValueFunction treeValueFunctionFor(String name) {
return registeredTreeValueFunctions.get(name); return registeredTreeValueFunctions.get(name);
} }
@ -374,7 +374,7 @@ public class PluginManager {
* *
* @return the set of all tree value operators that were loaded. * @return the set of all tree value operators that were loaded.
*/ */
public Set<String> getAllTreeValueOperators(){ public Set<String> getAllTreeValueOperators() {
return registeredTreeValueOperators.keySet(); return registeredTreeValueOperators.keySet();
} }

View File

@ -80,21 +80,6 @@ public class StandardPlugin extends Plugin {
return product; return product;
} }
}; };
/**
* The combination operator.
*/
public static final NumberOperator OP_NCR = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) {
@Override
public boolean matchesParams(NumberInterface[] params) {
return params.length == 2 && params[0].fractionalPart().signum() == 0
&& params[1].fractionalPart().signum() == 0;
}
@Override
public NumberInterface applyInternal(NumberInterface[] params) {
return OP_NPR.apply(params).divide(OP_FACTORIAL.apply(params[1]));
}
};
/** /**
* The implementation for double-based naive numbers. * The implementation for double-based naive numbers.
*/ */
@ -109,6 +94,20 @@ public class StandardPlugin extends Plugin {
return new NaiveNumber(Math.PI); return new NaiveNumber(Math.PI);
} }
}; };
/**
* The square root function.
*/
public static final NumberFunction FUNCTION_SQRT = new NumberFunction() {
@Override
public boolean matchesParams(NumberInterface[] params) {
return params.length == 1;
}
@Override
public NumberInterface applyInternal(NumberInterface[] params) {
return OP_CARET.apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass())));
}
};
/** /**
* The implementation for the infinite-precision BigDecimal. * The implementation for the infinite-precision BigDecimal.
*/ */
@ -225,6 +224,21 @@ public class StandardPlugin extends Plugin {
return total; return total;
} }
}; };
/**
* The combination operator.
*/
public static final NumberOperator OP_NCR = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) {
@Override
public boolean matchesParams(NumberInterface[] params) {
return params.length == 2 && params[0].fractionalPart().signum() == 0
&& params[1].fractionalPart().signum() == 0;
}
@Override
public NumberInterface applyInternal(NumberInterface[] params) {
return OP_NPR.apply(params).divide(OP_FACTORIAL.apply(params[1]));
}
};
/** /**
* The absolute value function, abs(-3) = 3 * The absolute value function, abs(-3) = 3
*/ */
@ -333,50 +347,6 @@ public class StandardPlugin extends Plugin {
return fromInt(params[0].getClass(), (int) Math.round(Math.random() * params[0].floor().intValue())); return fromInt(params[0].getClass(), (int) Math.round(Math.random() * params[0].floor().intValue()));
} }
}; };
/**
* The caret / pow operator, ^
*/
public static final NumberOperator OP_CARET = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2) {
@Override
public boolean matchesParams(NumberInterface[] params) {
NumberInterface zero = fromInt(params[0].getClass(), 0);
return params.length == 2
&& !(params[0].compareTo(zero) == 0
&& params[1].compareTo(zero) == 0)
&& !(params[0].signum() == -1 && params[1].fractionalPart().compareTo(zero) != 0);
}
@Override
public NumberInterface applyInternal(NumberInterface[] params) {
NumberInterface zero = fromInt(params[0].getClass(), 0);
if (params[0].compareTo(zero) == 0)
return zero;
else if (params[1].compareTo(zero) == 0)
return fromInt(params[0].getClass(), 1);
//Detect integer bases:
if (params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[0].getClass(), Integer.MAX_VALUE)) < 0
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[1].getClass(), 1)) >= 0) {
NumberInterface[] newParams = {params[0], params[1].fractionalPart()};
return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(newParams));
}
return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1]));
}
};
/**
* The square root function.
*/
public static final NumberFunction FUNCTION_SQRT = new NumberFunction() {
@Override
public boolean matchesParams(NumberInterface[] params) {
return params.length == 1;
}
@Override
public NumberInterface applyInternal(NumberInterface[] params) {
return OP_CARET.apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass())));
}
};
private static final HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> FACTORIAL_LISTS = new HashMap<>(); private static final HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> FACTORIAL_LISTS = new HashMap<>();
/** /**
* The exponential function, exp(1) = e^1 = 2.71... * The exponential function, exp(1) = e^1 = 2.71...
@ -415,6 +385,36 @@ public class StandardPlugin extends Plugin {
} }
} }
}; };
/**
* The caret / pow operator, ^
*/
public static final NumberOperator OP_CARET = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2) {
@Override
public boolean matchesParams(NumberInterface[] params) {
NumberInterface zero = fromInt(params[0].getClass(), 0);
return params.length == 2
&& !(params[0].compareTo(zero) == 0
&& params[1].compareTo(zero) == 0)
&& !(params[0].signum() == -1 && params[1].fractionalPart().compareTo(zero) != 0);
}
@Override
public NumberInterface applyInternal(NumberInterface[] params) {
NumberInterface zero = fromInt(params[0].getClass(), 0);
if (params[0].compareTo(zero) == 0)
return zero;
else if (params[1].compareTo(zero) == 0)
return fromInt(params[0].getClass(), 1);
//Detect integer bases:
if (params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[0].getClass(), Integer.MAX_VALUE)) < 0
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[1].getClass(), 1)) >= 0) {
NumberInterface[] newParams = {params[0], params[1].fractionalPart()};
return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(newParams));
}
return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1]));
}
};
/** /**
* The sine function (the argument is interpreted in radians). * The sine function (the argument is interpreted in radians).
*/ */

View File

@ -10,4 +10,4 @@ package org.nwapw.abacus.function
* @param precedence the precedence of this operator, used for order of operations. * @param precedence the precedence of this operator, used for order of operations.
*/ */
open class Operator(val associativity: OperatorAssociativity, val type: OperatorType, open class Operator(val associativity: OperatorAssociativity, val type: OperatorType,
val precedence: Int) val precedence: Int)

View File

@ -8,7 +8,7 @@ package org.nwapw.abacus.function.applicable
* @param <T> the type of the parameters passed to this applicable. * @param <T> the type of the parameters passed to this applicable.
* @param <O> the return type of the applicable. * @param <O> the return type of the applicable.
*/ */
interface Applicable<in T: Any, out O: Any> { interface Applicable<in T : Any, out O : Any> {
/** /**
* Checks if the given applicable can be used with the given parameters. * Checks if the given applicable can be used with the given parameters.
@ -16,6 +16,7 @@ interface Applicable<in T: Any, out O: Any> {
* @return whether the array can be used with applyInternal. * @return whether the array can be used with applyInternal.
*/ */
fun matchesParams(params: Array<out T>): Boolean fun matchesParams(params: Array<out T>): Boolean
/** /**
* Applies the applicable object to the given parameters, * Applies the applicable object to the given parameters,
* without checking for compatibility. * without checking for compatibility.
@ -32,7 +33,7 @@ interface Applicable<in T: Any, out O: Any> {
* @return the result of the operation, or null if parameters do not match. * @return the result of the operation, or null if parameters do not match.
*/ */
fun apply(vararg params: T): O? { fun apply(vararg params: T): O? {
if(!matchesParams(params)) return null if (!matchesParams(params)) return null
return applyInternal(params) return applyInternal(params)
} }

View File

@ -11,7 +11,7 @@ import org.nwapw.abacus.tree.Reducer
* @param <O> the return type of the application. * @param <O> the return type of the application.
* @param <R> the required type of the reducer. * @param <R> the required type of the reducer.
*/ */
interface ReducerApplicable<in T: Any, out O: Any, in R: Any> { interface ReducerApplicable<in T : Any, out O : Any, in R : Any> {
/** /**
* Checks if this applicable can be applied to the * Checks if this applicable can be applied to the
@ -19,6 +19,7 @@ interface ReducerApplicable<in T: Any, out O: Any, in R: Any> {
* @param params the parameters to check. * @param params the parameters to check.
*/ */
fun matchesParams(params: Array<out T>): Boolean fun matchesParams(params: Array<out T>): Boolean
/** /**
* Applies this applicable to the given arguments, and reducer. * Applies this applicable to the given arguments, and reducer.
* @param reducer the reducer to use in the application. * @param reducer the reducer to use in the application.
@ -26,6 +27,7 @@ interface ReducerApplicable<in T: Any, out O: Any, in R: Any> {
* @return the result of the application. * @return the result of the application.
*/ */
fun applyWithReducerInternal(reducer: Reducer<R>, params: Array<out T>): O? fun applyWithReducerInternal(reducer: Reducer<R>, params: Array<out T>): O?
/** /**
* Applies this applicable to the given arguments, and reducer, * Applies this applicable to the given arguments, and reducer,
* if the arguments and reducer are compatible with this applicable. * if the arguments and reducer are compatible with this applicable.
@ -34,7 +36,7 @@ interface ReducerApplicable<in T: Any, out O: Any, in R: Any> {
* @return the result of the application, or null if the arguments are incompatible. * @return the result of the application, or null if the arguments are incompatible.
*/ */
fun applyWithReducer(reducer: Reducer<R>, vararg params: T): O? { fun applyWithReducer(reducer: Reducer<R>, vararg params: T): O? {
if(!matchesParams(params)) return null if (!matchesParams(params)) return null
return applyWithReducerInternal(reducer, params) return applyWithReducerInternal(reducer, params)
} }

View File

@ -19,9 +19,9 @@ abstract class CallNode(val callTo: String) : TreeNode() {
val buffer = StringBuffer() val buffer = StringBuffer()
buffer.append(callTo) buffer.append(callTo)
buffer.append("(") buffer.append("(")
for(i in 0 until children.size){ for (i in 0 until children.size) {
buffer.append(children[i].toString()) buffer.append(children[i].toString())
buffer.append(if(i != children.size - 1) ", " else ")") buffer.append(if (i != children.size - 1) ", " else ")")
} }
return buffer.toString() return buffer.toString()
} }

View File

@ -1,7 +1,5 @@
package org.nwapw.abacus.tree package org.nwapw.abacus.tree
import org.nwapw.abacus.number.NumberInterface
/** /**
* A tree node that holds a single number value. * A tree node that holds a single number value.
* *