mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 16:15:19 +00:00
Compare commits
20 Commits
time-limit
...
kotlin
| Author | SHA1 | Date | |
|---|---|---|---|
| ff7d90967e | |||
| 355a91d690 | |||
| 5f0fba15eb | |||
| 3bdc0e2ae5 | |||
| e54b5cdd66 | |||
| fd87cb66a3 | |||
| 200f4c7288 | |||
|
|
1cd544e712 | ||
|
|
a8c70a6bbe | ||
| f28e915c9a | |||
| 7a0863380a | |||
|
|
8a9df051cf | ||
|
|
4eda15b3fb | ||
| c9e0d4f8d3 | |||
| 213d7af10b | |||
| 585a3839c1 | |||
|
|
d0ccb8b625 | ||
|
|
7d5efa1fe6 | ||
| 35254d3e99 | |||
| 44f018060d |
@@ -1,3 +1,8 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
id 'application'
|
||||||
|
id 'org.jetbrains.kotlin.jvm' version '1.1.3'
|
||||||
|
}
|
||||||
apply plugin: 'java'
|
apply plugin: 'java'
|
||||||
apply plugin: 'application'
|
apply plugin: 'application'
|
||||||
|
|
||||||
@@ -7,6 +12,7 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile 'com.moandjiezana.toml:toml4j:0.7.1'
|
compile 'com.moandjiezana.toml:toml4j:0.7.1'
|
||||||
|
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8"
|
||||||
testCompile 'junit:junit:4.12'
|
testCompile 'junit:junit:4.12'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,76 +0,0 @@
|
|||||||
package org.nwapw.abacus.function;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A class that represents a single infix operator.
|
|
||||||
*/
|
|
||||||
public class Operator {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The associativity of the operator.
|
|
||||||
*/
|
|
||||||
private OperatorAssociativity associativity;
|
|
||||||
/**
|
|
||||||
* The type of this operator.
|
|
||||||
*/
|
|
||||||
private OperatorType type;
|
|
||||||
/**
|
|
||||||
* The precedence of the operator.
|
|
||||||
*/
|
|
||||||
private int precedence;
|
|
||||||
/**
|
|
||||||
* The function that is called by this operator.
|
|
||||||
*/
|
|
||||||
private Function function;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new operator with the given parameters.
|
|
||||||
*
|
|
||||||
* @param associativity the associativity of the operator.
|
|
||||||
* @param operatorType the type of this operator, like binary infix or unary postfix.
|
|
||||||
* @param precedence the precedence of the operator.
|
|
||||||
* @param function the function that the operator calls.
|
|
||||||
*/
|
|
||||||
public Operator(OperatorAssociativity associativity, OperatorType operatorType, int precedence, Function function) {
|
|
||||||
this.associativity = associativity;
|
|
||||||
this.type = operatorType;
|
|
||||||
this.precedence = precedence;
|
|
||||||
this.function = function;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the operator's associativity.
|
|
||||||
*
|
|
||||||
* @return the associativity.
|
|
||||||
*/
|
|
||||||
public OperatorAssociativity getAssociativity() {
|
|
||||||
return associativity;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the operator's type.
|
|
||||||
*
|
|
||||||
* @return the type.
|
|
||||||
*/
|
|
||||||
public OperatorType getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the operator's precedence.
|
|
||||||
*
|
|
||||||
* @return the precedence.
|
|
||||||
*/
|
|
||||||
public int getPrecedence() {
|
|
||||||
return precedence;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the operator's function.
|
|
||||||
*
|
|
||||||
* @return the function.
|
|
||||||
*/
|
|
||||||
public Function getFunction() {
|
|
||||||
return function;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -5,6 +5,7 @@ import javafx.beans.value.ChangeListener;
|
|||||||
import javafx.beans.value.ObservableValue;
|
import javafx.beans.value.ObservableValue;
|
||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
|
import javafx.collections.transformation.FilteredList;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.control.*;
|
import javafx.scene.control.*;
|
||||||
import javafx.scene.control.cell.CheckBoxListCell;
|
import javafx.scene.control.cell.CheckBoxListCell;
|
||||||
@@ -72,6 +73,8 @@ public class AbacusController implements PluginListener {
|
|||||||
@FXML
|
@FXML
|
||||||
private Tab settingsTab;
|
private Tab settingsTab;
|
||||||
@FXML
|
@FXML
|
||||||
|
private Tab functionListTab;
|
||||||
|
@FXML
|
||||||
private TableView<HistoryModel> historyTable;
|
private TableView<HistoryModel> historyTable;
|
||||||
@FXML
|
@FXML
|
||||||
private TableColumn<HistoryModel, String> inputColumn;
|
private TableColumn<HistoryModel, String> inputColumn;
|
||||||
@@ -93,6 +96,10 @@ public class AbacusController implements PluginListener {
|
|||||||
private ListView<ToggleablePlugin> enabledPluginView;
|
private ListView<ToggleablePlugin> enabledPluginView;
|
||||||
@FXML
|
@FXML
|
||||||
private TextField computationLimitField;
|
private TextField computationLimitField;
|
||||||
|
@FXML
|
||||||
|
private ListView<String> functionListView;
|
||||||
|
@FXML
|
||||||
|
private TextField functionListSearchField;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of history entries, created by the users.
|
* The list of history entries, created by the users.
|
||||||
@@ -110,6 +117,14 @@ public class AbacusController implements PluginListener {
|
|||||||
* and, when reloaded, get added to the plugin manager's black list.
|
* and, when reloaded, get added to the plugin manager's black list.
|
||||||
*/
|
*/
|
||||||
private ObservableList<ToggleablePlugin> enabledPlugins;
|
private ObservableList<ToggleablePlugin> enabledPlugins;
|
||||||
|
/**
|
||||||
|
* The list of functions that are registered in the calculator.
|
||||||
|
*/
|
||||||
|
private ObservableList<String> functionList;
|
||||||
|
/**
|
||||||
|
* The filtered list displayed to the user.
|
||||||
|
*/
|
||||||
|
private FilteredList<String> functionFilter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The abacus instance used for changing the plugin configuration.
|
* The abacus instance used for changing the plugin configuration.
|
||||||
@@ -213,6 +228,11 @@ public class AbacusController implements PluginListener {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
functionList = FXCollections.observableArrayList();
|
||||||
|
functionFilter = new FilteredList<>(functionList, (s) -> true);
|
||||||
|
functionListView.setItems(functionFilter);
|
||||||
|
functionListSearchField.textProperty().addListener((observable, oldValue, newValue) ->
|
||||||
|
functionFilter.setPredicate((newValue.length() == 0) ? ((s) -> true) : ((s) -> s.contains(newValue))));
|
||||||
historyData = FXCollections.observableArrayList();
|
historyData = FXCollections.observableArrayList();
|
||||||
historyTable.setItems(historyData);
|
historyTable.setItems(historyData);
|
||||||
numberImplementationOptions = FXCollections.observableArrayList();
|
numberImplementationOptions = FXCollections.observableArrayList();
|
||||||
@@ -327,10 +347,13 @@ public class AbacusController implements PluginListener {
|
|||||||
plugin.enabledProperty().addListener(e -> changesMade = true);
|
plugin.enabledProperty().addListener(e -> changesMade = true);
|
||||||
enabledPlugins.add(plugin);
|
enabledPlugins.add(plugin);
|
||||||
}
|
}
|
||||||
|
functionList.addAll(manager.getAllFunctions());
|
||||||
|
functionList.sort(String::compareTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUnload(PluginManager manager) {
|
public void onUnload(PluginManager manager) {
|
||||||
|
functionList.clear();
|
||||||
enabledPlugins.clear();
|
enabledPlugins.clear();
|
||||||
numberImplementationOptions.clear();
|
numberImplementationOptions.clear();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ public abstract class NumberInterface {
|
|||||||
/**
|
/**
|
||||||
* Returns the least integer greater than or equal to the number.
|
* Returns the least integer greater than or equal to the number.
|
||||||
*
|
*
|
||||||
* @return the least integer >= the number, if int can hold the value.
|
* @return the least integer greater or equal to the number, if int can hold the value.
|
||||||
*/
|
*/
|
||||||
protected abstract NumberInterface ceilingInternal();
|
protected abstract NumberInterface ceilingInternal();
|
||||||
|
|
||||||
@@ -182,7 +182,7 @@ public abstract class NumberInterface {
|
|||||||
* Also, checks if the thread has been interrupted, and if so, throws
|
* Also, checks if the thread has been interrupted, and if so, throws
|
||||||
* an exception.
|
* an exception.
|
||||||
*
|
*
|
||||||
* @return the least integer bigger or equal to the number, if int can hold the value.
|
* @return the least integer bigger or equal to the number.
|
||||||
*/
|
*/
|
||||||
public final NumberInterface ceiling(){
|
public final NumberInterface ceiling(){
|
||||||
checkInterrupted();
|
checkInterrupted();
|
||||||
@@ -192,7 +192,7 @@ public abstract class NumberInterface {
|
|||||||
/**
|
/**
|
||||||
* Return the greatest integer less than or equal to the number.
|
* Return the greatest integer less than or equal to the number.
|
||||||
*
|
*
|
||||||
* @return the greatest integer smaller or equal the number, if int can hold the value.
|
* @return the greatest integer smaller or equal the number.
|
||||||
*/
|
*/
|
||||||
protected abstract NumberInterface floorInternal();
|
protected abstract NumberInterface floorInternal();
|
||||||
|
|
||||||
@@ -201,7 +201,7 @@ public abstract class NumberInterface {
|
|||||||
* Also, checks if the thread has been interrupted, and if so, throws
|
* Also, checks if the thread has been interrupted, and if so, throws
|
||||||
* an exception.
|
* an exception.
|
||||||
*
|
*
|
||||||
* @return the greatest int >= the number, if int can hold the value.
|
* @return the greatest int smaller than or equal to the number.
|
||||||
*/
|
*/
|
||||||
public final NumberInterface floor(){
|
public final NumberInterface floor(){
|
||||||
checkInterrupted();
|
checkInterrupted();
|
||||||
@@ -216,7 +216,7 @@ public abstract class NumberInterface {
|
|||||||
protected abstract NumberInterface fractionalPartInternal();
|
protected abstract NumberInterface fractionalPartInternal();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the fractional part of the number.
|
* Returns the fractional part of the number, specifically x - floor(x).
|
||||||
* Also, checks if the thread has been interrupted,
|
* Also, checks if the thread has been interrupted,
|
||||||
* and if so, throws an exception.
|
* and if so, throws an exception.
|
||||||
* @return the fractional part of the number.
|
* @return the fractional part of the number.
|
||||||
|
|||||||
@@ -113,19 +113,18 @@ public class PreciseNumber extends NumberInterface {
|
|||||||
String str = value.toPlainString();
|
String str = value.toPlainString();
|
||||||
int decimalIndex = str.indexOf('.');
|
int decimalIndex = str.indexOf('.');
|
||||||
if (decimalIndex != -1) {
|
if (decimalIndex != -1) {
|
||||||
return new PreciseNumber(str.substring(0, decimalIndex));
|
NumberInterface floor = new PreciseNumber(str.substring(0, decimalIndex));
|
||||||
|
if(signum() == -1){
|
||||||
|
floor = floor.subtract(ONE);
|
||||||
|
}
|
||||||
|
return floor;
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface fractionalPartInternal() {
|
public NumberInterface fractionalPartInternal() {
|
||||||
String str = value.toPlainString();
|
return this.subtractInternal(floorInternal());
|
||||||
int decimalIndex = str.indexOf('.');
|
|
||||||
if (decimalIndex != -1) {
|
|
||||||
return new PreciseNumber(str.substring(decimalIndex + 1));
|
|
||||||
}
|
|
||||||
return ZERO;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -17,18 +17,6 @@ import java.util.Set;
|
|||||||
*/
|
*/
|
||||||
public abstract class Plugin {
|
public abstract class Plugin {
|
||||||
|
|
||||||
/**
|
|
||||||
* A hash map of functions mapped to their string names.
|
|
||||||
*/
|
|
||||||
private Map<String, Function> functions;
|
|
||||||
/**
|
|
||||||
* A hash map of operators mapped to their string names.
|
|
||||||
*/
|
|
||||||
private Map<String, Operator> operators;
|
|
||||||
/**
|
|
||||||
* The map of the number implementations this plugin provides.
|
|
||||||
*/
|
|
||||||
private Map<String, NumberImplementation> numberImplementations;
|
|
||||||
/**
|
/**
|
||||||
* The plugin manager in which to search for functions
|
* The plugin manager in which to search for functions
|
||||||
* not inside this package,
|
* not inside this package,
|
||||||
@@ -49,69 +37,9 @@ public abstract class Plugin {
|
|||||||
*/
|
*/
|
||||||
public Plugin(PluginManager manager) {
|
public Plugin(PluginManager manager) {
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
functions = new HashMap<>();
|
|
||||||
operators = new HashMap<>();
|
|
||||||
numberImplementations = new HashMap<>();
|
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the list of functions provided by this plugin.
|
|
||||||
*
|
|
||||||
* @return the list of registered functions.
|
|
||||||
*/
|
|
||||||
public final Set<String> providedFunctions() {
|
|
||||||
return functions.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the list of functions provided by this plugin.
|
|
||||||
*
|
|
||||||
* @return the list of registered functions.
|
|
||||||
*/
|
|
||||||
public final Set<String> providedOperators() {
|
|
||||||
return operators.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the list of number implementations provided by this plugin.
|
|
||||||
*
|
|
||||||
* @return the list of registered number implementations.
|
|
||||||
*/
|
|
||||||
public final Set<String> providedNumberImplementations() {
|
|
||||||
return numberImplementations.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a function under the given function name.
|
|
||||||
*
|
|
||||||
* @param functionName the name of the function to get
|
|
||||||
* @return the function, or null if this plugin doesn't provide it.
|
|
||||||
*/
|
|
||||||
public final Function getFunction(String functionName) {
|
|
||||||
return functions.get(functionName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets an operator under the given operator name.
|
|
||||||
*
|
|
||||||
* @param operatorName the name of the operator to get.
|
|
||||||
* @return the operator, or null if this plugin doesn't provide it.
|
|
||||||
*/
|
|
||||||
public final Operator getOperator(String operatorName) {
|
|
||||||
return operators.get(operatorName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the number implementation under the given name.
|
|
||||||
*
|
|
||||||
* @param name the name of the number implementation to look up.
|
|
||||||
* @return the number implementation associated with that name, or null if the plugin doesn't provide it.
|
|
||||||
*/
|
|
||||||
public final NumberImplementation getNumberImplementation(String name) {
|
|
||||||
return numberImplementations.get(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables the function, loading the necessary instances
|
* Enables the function, loading the necessary instances
|
||||||
* of functions.
|
* of functions.
|
||||||
@@ -129,8 +57,6 @@ public abstract class Plugin {
|
|||||||
public final void disable() {
|
public final void disable() {
|
||||||
if (!enabled) return;
|
if (!enabled) return;
|
||||||
onDisable();
|
onDisable();
|
||||||
functions.clear();
|
|
||||||
operators.clear();
|
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +68,7 @@ public abstract class Plugin {
|
|||||||
* @param toRegister the function implementation.
|
* @param toRegister the function implementation.
|
||||||
*/
|
*/
|
||||||
protected final void registerFunction(String name, Function toRegister) {
|
protected final void registerFunction(String name, Function toRegister) {
|
||||||
functions.put(name, toRegister);
|
manager.registerFunction(name, toRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -154,7 +80,7 @@ public abstract class Plugin {
|
|||||||
* @param operator the operator to register.
|
* @param operator the operator to register.
|
||||||
*/
|
*/
|
||||||
protected final void registerOperator(String name, Operator operator) {
|
protected final void registerOperator(String name, Operator operator) {
|
||||||
operators.put(name, operator);
|
manager.registerOperator(name, operator);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -165,7 +91,7 @@ public abstract class Plugin {
|
|||||||
* @param implementation the actual implementation class to register.
|
* @param implementation the actual implementation class to register.
|
||||||
*/
|
*/
|
||||||
protected final void registerNumberImplementation(String name, NumberImplementation implementation) {
|
protected final void registerNumberImplementation(String name, NumberImplementation implementation) {
|
||||||
numberImplementations.put(name, implementation);
|
manager.registerNumberImplementation(name, implementation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -213,7 +139,7 @@ public abstract class Plugin {
|
|||||||
* @param forClass the class to which to find the pi instance.
|
* @param forClass the class to which to find the pi instance.
|
||||||
* @return the pi value for the given class.
|
* @return the pi value for the given class.
|
||||||
*/
|
*/
|
||||||
protected final NumberInterface getPi(Class<? extends NumberInterface> forClass) {
|
protected final NumberInterface piFor(Class<? extends NumberInterface> forClass) {
|
||||||
return manager.piFor(forClass);
|
return manager.piFor(forClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,20 +23,17 @@ public class PluginManager {
|
|||||||
*/
|
*/
|
||||||
private Set<Plugin> plugins;
|
private Set<Plugin> plugins;
|
||||||
/**
|
/**
|
||||||
* List of functions that have been cached,
|
* The map of functions registered by the plugins.
|
||||||
* that is, found in a plugin and returned.
|
|
||||||
*/
|
*/
|
||||||
private Map<String, Function> cachedFunctions;
|
private Map<String, Function> registeredFunctions;
|
||||||
/**
|
/**
|
||||||
* List of operators that have been cached,
|
* The map of operators registered by the plugins
|
||||||
* that is, found in a plugin and returned.
|
|
||||||
*/
|
*/
|
||||||
private Map<String, Operator> cachedOperators;
|
private Map<String, Operator> registeredOperators;
|
||||||
/**
|
/**
|
||||||
* The list of number implementations that have
|
* The map of number implementations registered by the plugins.
|
||||||
* been cached, that is, found in a plugin and returned.
|
|
||||||
*/
|
*/
|
||||||
private Map<String, NumberImplementation> cachedNumberImplementations;
|
private Map<String, NumberImplementation> registeredNumberImplementations;
|
||||||
/**
|
/**
|
||||||
* The list of number implementations that have been
|
* The list of number implementations that have been
|
||||||
* found by their implementation class.
|
* found by their implementation class.
|
||||||
@@ -46,18 +43,6 @@ public class PluginManager {
|
|||||||
* The pi values for each implementation class that have already been computer.
|
* The pi values for each implementation class that have already been computer.
|
||||||
*/
|
*/
|
||||||
private Map<Class<? extends NumberInterface>, NumberInterface> cachedPi;
|
private Map<Class<? extends NumberInterface>, NumberInterface> cachedPi;
|
||||||
/**
|
|
||||||
* List of all functions loaded by the plugins.
|
|
||||||
*/
|
|
||||||
private Set<String> allFunctions;
|
|
||||||
/**
|
|
||||||
* List of all operators loaded by the plugins.
|
|
||||||
*/
|
|
||||||
private Set<String> allOperators;
|
|
||||||
/**
|
|
||||||
* List of all the number implementations loaded by the plugins.
|
|
||||||
*/
|
|
||||||
private Set<String> allNumberImplementations;
|
|
||||||
/**
|
/**
|
||||||
* The list of plugin listeners attached to this instance.
|
* The list of plugin listeners attached to this instance.
|
||||||
*/
|
*/
|
||||||
@@ -77,79 +62,66 @@ public class PluginManager {
|
|||||||
this.abacus = abacus;
|
this.abacus = abacus;
|
||||||
loadedPluginClasses = new HashSet<>();
|
loadedPluginClasses = new HashSet<>();
|
||||||
plugins = new HashSet<>();
|
plugins = new HashSet<>();
|
||||||
cachedFunctions = new HashMap<>();
|
registeredFunctions = new HashMap<>();
|
||||||
cachedOperators = new HashMap<>();
|
registeredOperators = new HashMap<>();
|
||||||
cachedNumberImplementations = new HashMap<>();
|
registeredNumberImplementations = new HashMap<>();
|
||||||
cachedInterfaceImplementations = new HashMap<>();
|
cachedInterfaceImplementations = new HashMap<>();
|
||||||
cachedPi = new HashMap<>();
|
cachedPi = new HashMap<>();
|
||||||
allFunctions = new HashSet<>();
|
|
||||||
allOperators = new HashSet<>();
|
|
||||||
allNumberImplementations = new HashSet<>();
|
|
||||||
listeners = new HashSet<>();
|
listeners = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches the plugin list for a certain value, retrieving the Plugin's
|
* Registers a function under the given name.
|
||||||
* list of items of the type using the setFunction and getting the value
|
* @param name the name of the function.
|
||||||
* of it is available via getFunction. If the value is contained
|
* @param function the function to register.
|
||||||
* in the cache, it returns the cached value instead.
|
|
||||||
*
|
|
||||||
* @param plugins the plugin list to search.
|
|
||||||
* @param cache the cache to use
|
|
||||||
* @param setFunction the function to retrieve a set of available T's from the plugin
|
|
||||||
* @param getFunction the function to get the T value under the given name
|
|
||||||
* @param name the name to search for
|
|
||||||
* @param <T> the type of element being search
|
|
||||||
* @param <K> the type of key that the cache is indexed by.
|
|
||||||
* @return the retrieved element, or null if it was not found.
|
|
||||||
*/
|
*/
|
||||||
private static <T, K> T searchCached(Collection<Plugin> plugins, Map<K, T> cache,
|
public void registerFunction(String name, Function function){
|
||||||
java.util.function.Function<Plugin, Set<K>> setFunction,
|
registeredFunctions.put(name, function);
|
||||||
java.util.function.BiFunction<Plugin, K, T> getFunction,
|
|
||||||
K name) {
|
|
||||||
if (cache.containsKey(name)) return cache.get(name);
|
|
||||||
|
|
||||||
T loadedValue = null;
|
|
||||||
for (Plugin plugin : plugins) {
|
|
||||||
if (setFunction.apply(plugin).contains(name)) {
|
|
||||||
loadedValue = getFunction.apply(plugin, name);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cache.put(name, loadedValue);
|
|
||||||
return loadedValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a function under the given name.
|
* Registers an operator under the given name.
|
||||||
*
|
|
||||||
* @param name the name of the function
|
|
||||||
* @return the function under the given name.
|
|
||||||
*/
|
|
||||||
public Function functionFor(String name) {
|
|
||||||
return searchCached(plugins, cachedFunctions, Plugin::providedFunctions, Plugin::getFunction, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets an operator under the given name.
|
|
||||||
*
|
|
||||||
* @param name the name of the operator.
|
* @param name the name of the operator.
|
||||||
* @return the operator under the given name.
|
* @param operator the operator to register.
|
||||||
*/
|
*/
|
||||||
public Operator operatorFor(String name) {
|
public void registerOperator(String name, Operator operator){
|
||||||
return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name);
|
registeredOperators.put(name, operator);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number implementation under the given name.
|
* Registers a number implementation under the given name.
|
||||||
*
|
* @param name the name of the number implementation.
|
||||||
* @param name the name of the implementation.
|
* @param implementation the number implementation to register.
|
||||||
* @return the implementation.
|
|
||||||
*/
|
*/
|
||||||
public NumberImplementation numberImplementationFor(String name) {
|
public void registerNumberImplementation(String name, NumberImplementation implementation){
|
||||||
return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations,
|
registeredNumberImplementations.put(name, implementation);
|
||||||
Plugin::getNumberImplementation, name);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the function registered under the given name.
|
||||||
|
* @param name the name of the function.
|
||||||
|
* @return the function, or null if it was not found.
|
||||||
|
*/
|
||||||
|
public Function functionFor(String name){
|
||||||
|
return registeredFunctions.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the operator registered under the given name.
|
||||||
|
* @param name the name of the operator.
|
||||||
|
* @return the operator, or null if it was not found.
|
||||||
|
*/
|
||||||
|
public Operator operatorFor(String name){
|
||||||
|
return registeredOperators.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the number implementation registered under the given name.
|
||||||
|
* @param name the name of the number implementation.
|
||||||
|
* @return the number implementation, or null if it was not found.
|
||||||
|
*/
|
||||||
|
public NumberImplementation numberImplementationFor(String name){
|
||||||
|
return registeredNumberImplementations.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -161,14 +133,11 @@ public class PluginManager {
|
|||||||
public NumberImplementation interfaceImplementationFor(Class<? extends NumberInterface> name) {
|
public NumberImplementation interfaceImplementationFor(Class<? extends NumberInterface> name) {
|
||||||
if (cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name);
|
if (cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name);
|
||||||
NumberImplementation toReturn = null;
|
NumberImplementation toReturn = null;
|
||||||
outside:
|
for(String key : registeredNumberImplementations.keySet()){
|
||||||
for (Plugin plugin : plugins) {
|
NumberImplementation implementation = registeredNumberImplementations.get(key);
|
||||||
for (String implementationName : plugin.providedNumberImplementations()) {
|
if(implementation.getImplementation() == name) {
|
||||||
NumberImplementation implementation = plugin.getNumberImplementation(implementationName);
|
toReturn = implementation;
|
||||||
if (implementation.getImplementation().equals(name)) {
|
break;
|
||||||
toReturn = implementation;
|
|
||||||
break outside;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cachedInterfaceImplementations.put(name, toReturn);
|
cachedInterfaceImplementations.put(name, toReturn);
|
||||||
@@ -227,12 +196,6 @@ public class PluginManager {
|
|||||||
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
||||||
plugin.enable();
|
plugin.enable();
|
||||||
}
|
}
|
||||||
for (Plugin plugin : plugins) {
|
|
||||||
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
|
||||||
allFunctions.addAll(plugin.providedFunctions());
|
|
||||||
allOperators.addAll(plugin.providedOperators());
|
|
||||||
allNumberImplementations.addAll(plugin.providedNumberImplementations());
|
|
||||||
}
|
|
||||||
listeners.forEach(e -> e.onLoad(this));
|
listeners.forEach(e -> e.onLoad(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,14 +209,11 @@ public class PluginManager {
|
|||||||
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
||||||
plugin.disable();
|
plugin.disable();
|
||||||
}
|
}
|
||||||
cachedFunctions.clear();
|
registeredFunctions.clear();
|
||||||
cachedOperators.clear();
|
registeredOperators.clear();
|
||||||
cachedNumberImplementations.clear();
|
registeredNumberImplementations.clear();
|
||||||
cachedInterfaceImplementations.clear();
|
cachedInterfaceImplementations.clear();
|
||||||
cachedPi.clear();
|
cachedPi.clear();
|
||||||
allFunctions.clear();
|
|
||||||
allOperators.clear();
|
|
||||||
allNumberImplementations.clear();
|
|
||||||
listeners.forEach(e -> e.onUnload(this));
|
listeners.forEach(e -> e.onUnload(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,7 +231,7 @@ public class PluginManager {
|
|||||||
* @return the set of all functions that were loaded.
|
* @return the set of all functions that were loaded.
|
||||||
*/
|
*/
|
||||||
public Set<String> getAllFunctions() {
|
public Set<String> getAllFunctions() {
|
||||||
return allFunctions;
|
return registeredFunctions.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -280,7 +240,7 @@ public class PluginManager {
|
|||||||
* @return the set of all operators that were loaded.
|
* @return the set of all operators that were loaded.
|
||||||
*/
|
*/
|
||||||
public Set<String> getAllOperators() {
|
public Set<String> getAllOperators() {
|
||||||
return allOperators;
|
return registeredOperators.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -289,7 +249,7 @@ public class PluginManager {
|
|||||||
* @return the set of all implementations that were loaded.
|
* @return the set of all implementations that were loaded.
|
||||||
*/
|
*/
|
||||||
public Set<String> getAllNumberImplementations() {
|
public Set<String> getAllNumberImplementations() {
|
||||||
return allNumberImplementations;
|
return registeredNumberImplementations.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -18,6 +18,11 @@ import java.util.function.BiFunction;
|
|||||||
*/
|
*/
|
||||||
public class StandardPlugin extends Plugin {
|
public class StandardPlugin extends Plugin {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores objects of NumberInterface with integer values for reuse.
|
||||||
|
*/
|
||||||
|
private final static HashMap<Class<? extends NumberInterface>, HashMap<Integer, NumberInterface>> integerValues = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The addition operator, +
|
* The addition operator, +
|
||||||
*/
|
*/
|
||||||
@@ -112,7 +117,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
if (params[0].signum() == 0) {
|
if (params[0].signum() == 0) {
|
||||||
return (new NaiveNumber(1)).promoteTo(params[0].getClass());
|
return fromInt(params[0].getClass(), 1);
|
||||||
}
|
}
|
||||||
NumberInterface factorial = params[0];
|
NumberInterface factorial = params[0];
|
||||||
NumberInterface multiplier = params[0];
|
NumberInterface multiplier = params[0];
|
||||||
@@ -157,14 +162,14 @@ public class StandardPlugin extends Plugin {
|
|||||||
int powersOf2 = 0;
|
int powersOf2 = 0;
|
||||||
while (FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo(new NaiveNumber(0.1).promoteTo(param.getClass())) >= 0) {
|
while (FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo(new NaiveNumber(0.1).promoteTo(param.getClass())) >= 0) {
|
||||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() == 1) {
|
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() == 1) {
|
||||||
param = param.divide(new NaiveNumber(2).promoteTo(param.getClass()));
|
param = param.divide(fromInt(param.getClass(), 2));
|
||||||
powersOf2++;
|
powersOf2++;
|
||||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
||||||
break;
|
break;
|
||||||
//No infinite loop for you.
|
//No infinite loop for you.
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
param = param.multiply(new NaiveNumber(2).promoteTo(param.getClass()));
|
param = param.multiply(fromInt(param.getClass(), 2));
|
||||||
powersOf2--;
|
powersOf2--;
|
||||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != -1) {
|
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != -1) {
|
||||||
break;
|
break;
|
||||||
@@ -203,17 +208,17 @@ public class StandardPlugin extends Plugin {
|
|||||||
*/
|
*/
|
||||||
private NumberInterface getLog2(NumberInterface number) {
|
private NumberInterface getLog2(NumberInterface number) {
|
||||||
NumberInterface maxError = getMaxError(number);
|
NumberInterface maxError = getMaxError(number);
|
||||||
//NumberInterface errorBound = (new NaiveNumber(1)).promoteTo(number.getClass());
|
//NumberInterface errorBound = fromInt(number.getClass(), 1);
|
||||||
//We'll use the series \sigma_{n >= 1) ((1/3^n + 1/4^n) * 1/n)
|
//We'll use the series \sigma_{n >= 1) ((1/3^n + 1/4^n) * 1/n)
|
||||||
//In the following, a=1/3^n, b=1/4^n, c = 1/n.
|
//In the following, a=1/3^n, b=1/4^n, c = 1/n.
|
||||||
//a is also an error bound.
|
//a is also an error bound.
|
||||||
NumberInterface a = (new NaiveNumber(1)).promoteTo(number.getClass()), b = a, c = a;
|
NumberInterface a = fromInt(number.getClass(), 1), b = a, c = a;
|
||||||
NumberInterface sum = NaiveNumber.ZERO.promoteTo(number.getClass());
|
NumberInterface sum = NaiveNumber.ZERO.promoteTo(number.getClass());
|
||||||
int n = 0;
|
int n = 0;
|
||||||
while (a.compareTo(maxError) >= 1) {
|
while (a.compareTo(maxError) >= 1) {
|
||||||
n++;
|
n++;
|
||||||
a = a.divide((new NaiveNumber(3)).promoteTo(number.getClass()));
|
a = a.divide(fromInt(number.getClass(), 3));
|
||||||
b = b.divide((new NaiveNumber(4)).promoteTo(number.getClass()));
|
b = b.divide(fromInt(number.getClass(), 4));
|
||||||
c = NaiveNumber.ONE.promoteTo(number.getClass()).divide((new NaiveNumber(n)).promoteTo(number.getClass()));
|
c = NaiveNumber.ONE.promoteTo(number.getClass()).divide((new NaiveNumber(n)).promoteTo(number.getClass()));
|
||||||
sum = sum.add(a.add(b).multiply(c));
|
sum = sum.add(a.add(b).multiply(c));
|
||||||
}
|
}
|
||||||
@@ -311,7 +316,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
//right and left refer to lhs and rhs in the above inequality.
|
//right and left refer to lhs and rhs in the above inequality.
|
||||||
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
||||||
NumberInterface nextNumerator = params[0];
|
NumberInterface nextNumerator = params[0];
|
||||||
NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling().intValue())), right = maxError;
|
NumberInterface left = params[0].multiply(fromInt(params[0].getClass(), 3).intPow(params[0].ceiling().intValue())), right = maxError;
|
||||||
do {
|
do {
|
||||||
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n + 1)));
|
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n + 1)));
|
||||||
n++;
|
n++;
|
||||||
@@ -335,7 +340,8 @@ public class StandardPlugin extends Plugin {
|
|||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length == 2
|
return params.length == 2
|
||||||
&& !(params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0
|
&& !(params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0
|
||||||
&& params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) == 0);
|
&& params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) == 0)
|
||||||
|
&& !(params[0].signum() == -1 && params[1].fractionalPart().compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -344,6 +350,13 @@ public class StandardPlugin extends Plugin {
|
|||||||
return NaiveNumber.ZERO.promoteTo(params[0].getClass());
|
return NaiveNumber.ZERO.promoteTo(params[0].getClass());
|
||||||
else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
|
else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
|
||||||
return NaiveNumber.ONE.promoteTo(params[1].getClass());
|
return NaiveNumber.ONE.promoteTo(params[1].getClass());
|
||||||
|
//Detect integer bases:
|
||||||
|
if(params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0
|
||||||
|
&& FUNCTION_ABS.apply(params[0]).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]));
|
return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1]));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -358,13 +371,13 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
NumberInterface pi = getPi(params[0].getClass());
|
NumberInterface pi = piFor(params[0].getClass());
|
||||||
NumberInterface twoPi = pi.multiply(new NaiveNumber(2).promoteTo(pi.getClass()));
|
NumberInterface twoPi = pi.multiply(fromInt(pi.getClass(), 2));
|
||||||
NumberInterface theta = getSmallAngle(params[0], pi);
|
NumberInterface theta = getSmallAngle(params[0], pi);
|
||||||
//System.out.println(theta);
|
//System.out.println(theta);
|
||||||
if (theta.compareTo(pi.multiply(new NaiveNumber(1.5).promoteTo(twoPi.getClass()))) >= 0) {
|
if (theta.compareTo(pi.multiply(new NaiveNumber(1.5).promoteTo(twoPi.getClass()))) >= 0) {
|
||||||
theta = theta.subtract(twoPi);
|
theta = theta.subtract(twoPi);
|
||||||
} else if (theta.compareTo(pi.divide(new NaiveNumber(2).promoteTo(pi.getClass()))) > 0) {
|
} else if (theta.compareTo(pi.divide(fromInt(pi.getClass(), 2))) > 0) {
|
||||||
theta = pi.subtract(theta);
|
theta = pi.subtract(theta);
|
||||||
}
|
}
|
||||||
//System.out.println(theta);
|
//System.out.println(theta);
|
||||||
@@ -382,7 +395,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return functionSin.apply(getPi(params[0].getClass()).divide(new NaiveNumber(2).promoteTo(params[0].getClass()))
|
return functionSin.apply(piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2))
|
||||||
.subtract(params[0]));
|
.subtract(params[0]));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -470,7 +483,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
* @return the maximum error.
|
* @return the maximum error.
|
||||||
*/
|
*/
|
||||||
private static NumberInterface getMaxError(NumberInterface number) {
|
private static NumberInterface getMaxError(NumberInterface number) {
|
||||||
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.getMaxPrecision());
|
return fromInt(number.getClass(), 10).intPow(-number.getMaxPrecision());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -531,6 +544,22 @@ public class StandardPlugin extends Plugin {
|
|||||||
return theta;
|
return theta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a number of class numType with value n.
|
||||||
|
* @param numType class of number to return.
|
||||||
|
* @param n value of returned number.
|
||||||
|
* @return numClass instance with value n.
|
||||||
|
*/
|
||||||
|
private static NumberInterface fromInt(Class<? extends NumberInterface> numType, int n){
|
||||||
|
if(!integerValues.containsKey(numType)){
|
||||||
|
integerValues.put(numType, new HashMap<>());
|
||||||
|
}
|
||||||
|
if(!integerValues.get(numType).containsKey(n)){
|
||||||
|
integerValues.get(numType).put(n, new NaiveNumber(n).promoteTo(numType));
|
||||||
|
}
|
||||||
|
return integerValues.get(numType).get(n);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);
|
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);
|
||||||
|
|||||||
@@ -1,108 +0,0 @@
|
|||||||
package org.nwapw.abacus.tree;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A tree node that represents an operation being applied to two operands.
|
|
||||||
*/
|
|
||||||
public class BinaryNode extends TreeNode {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The operation being applied.
|
|
||||||
*/
|
|
||||||
private String operation;
|
|
||||||
/**
|
|
||||||
* The left node of the operation.
|
|
||||||
*/
|
|
||||||
private TreeNode left;
|
|
||||||
/**
|
|
||||||
* The right node of the operation.
|
|
||||||
*/
|
|
||||||
private TreeNode right;
|
|
||||||
|
|
||||||
private BinaryNode() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new operation node with the given operation
|
|
||||||
* and no child nodes.
|
|
||||||
*
|
|
||||||
* @param operation the operation.
|
|
||||||
*/
|
|
||||||
public BinaryNode(String operation) {
|
|
||||||
this(operation, null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new operation node with the given operation
|
|
||||||
* and child nodes.
|
|
||||||
*
|
|
||||||
* @param operation the operation.
|
|
||||||
* @param left the left node of the expression.
|
|
||||||
* @param right the right node of the expression.
|
|
||||||
*/
|
|
||||||
public BinaryNode(String operation, TreeNode left, TreeNode right) {
|
|
||||||
this.operation = operation;
|
|
||||||
this.left = left;
|
|
||||||
this.right = right;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the operation in this node.
|
|
||||||
*
|
|
||||||
* @return the operation in this node.
|
|
||||||
*/
|
|
||||||
public String getOperation() {
|
|
||||||
return operation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the left sub-expression of this node.
|
|
||||||
*
|
|
||||||
* @return the left node.
|
|
||||||
*/
|
|
||||||
public TreeNode getLeft() {
|
|
||||||
return left;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the left sub-expression of this node.
|
|
||||||
*
|
|
||||||
* @param left the sub-expression to apply.
|
|
||||||
*/
|
|
||||||
public void setLeft(TreeNode left) {
|
|
||||||
this.left = left;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the right sub-expression of this node.
|
|
||||||
*
|
|
||||||
* @return the right node.
|
|
||||||
*/
|
|
||||||
public TreeNode getRight() {
|
|
||||||
return right;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the right sub-expression of this node.
|
|
||||||
*
|
|
||||||
* @param right the sub-expression to apply.
|
|
||||||
*/
|
|
||||||
public void setRight(TreeNode right) {
|
|
||||||
this.right = right;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
|
||||||
T leftReduce = left.reduce(reducer);
|
|
||||||
T rightReduce = right.reduce(reducer);
|
|
||||||
if (leftReduce == null || rightReduce == null) return null;
|
|
||||||
return reducer.reduceNode(this, leftReduce, rightReduce);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
String leftString = left != null ? left.toString() : "null";
|
|
||||||
String rightString = right != null ? right.toString() : "null";
|
|
||||||
|
|
||||||
return "(" + leftString + operation + rightString + ")";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
package org.nwapw.abacus.tree;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A node that represents a function call.
|
|
||||||
*/
|
|
||||||
public class FunctionNode extends TreeNode {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The name of the function being called
|
|
||||||
*/
|
|
||||||
private String function;
|
|
||||||
/**
|
|
||||||
* The list of arguments to the function.
|
|
||||||
*/
|
|
||||||
private List<TreeNode> children;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a function node with no function.
|
|
||||||
*/
|
|
||||||
private FunctionNode() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new function node with the given function name.
|
|
||||||
*
|
|
||||||
* @param function the function name.
|
|
||||||
*/
|
|
||||||
public FunctionNode(String function) {
|
|
||||||
this.function = function;
|
|
||||||
children = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the function name for this node.
|
|
||||||
*
|
|
||||||
* @return the function name.
|
|
||||||
*/
|
|
||||||
public String getFunction() {
|
|
||||||
return function;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a child to the end of this node's child list.
|
|
||||||
*
|
|
||||||
* @param node the child to add.
|
|
||||||
*/
|
|
||||||
public void appendChild(TreeNode node) {
|
|
||||||
children.add(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a new child to the beginning of this node's child list.
|
|
||||||
*
|
|
||||||
* @param node the node to add.
|
|
||||||
*/
|
|
||||||
public void prependChild(TreeNode node) {
|
|
||||||
children.add(0, node);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
|
||||||
Object[] reducedChildren = new Object[children.size()];
|
|
||||||
for (int i = 0; i < reducedChildren.length; i++) {
|
|
||||||
reducedChildren[i] = children.get(i).reduce(reducer);
|
|
||||||
if (reducedChildren[i] == null) return null;
|
|
||||||
}
|
|
||||||
return reducer.reduceNode(this, reducedChildren);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
StringBuilder buffer = new StringBuilder();
|
|
||||||
buffer.append(function);
|
|
||||||
buffer.append("(");
|
|
||||||
for (int i = 0; i < children.size(); i++) {
|
|
||||||
buffer.append(children.get(i));
|
|
||||||
buffer.append(i == children.size() - 1 ? "" : ", ");
|
|
||||||
}
|
|
||||||
buffer.append(")");
|
|
||||||
return buffer.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package org.nwapw.abacus.tree;
|
|
||||||
|
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A node implementation that represents a single number.
|
|
||||||
*/
|
|
||||||
public class NumberNode extends TreeNode {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The number that is represented by this number node.
|
|
||||||
*/
|
|
||||||
private NumberInterface number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a number node with no number.
|
|
||||||
*/
|
|
||||||
public NumberNode() {
|
|
||||||
number = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new number node with the given double value.
|
|
||||||
*
|
|
||||||
* @param newNumber the number for which to create a number node.
|
|
||||||
*/
|
|
||||||
public NumberNode(NumberInterface newNumber) {
|
|
||||||
this.number = newNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the number value of this node.
|
|
||||||
*
|
|
||||||
* @return the number value of this node.
|
|
||||||
*/
|
|
||||||
public NumberInterface getNumber() {
|
|
||||||
return number;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
|
||||||
return reducer.reduceNode(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return number != null ? number.toString() : "null";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package org.nwapw.abacus.tree;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An abstract class that represents an expression tree node.
|
|
||||||
*/
|
|
||||||
public abstract class TreeNode {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The function that reduces a tree to a single vale.
|
|
||||||
*
|
|
||||||
* @param reducer the reducer used to reduce the tree.
|
|
||||||
* @param <T> the type the reducer produces.
|
|
||||||
* @return the result of the reduction, or null on error.
|
|
||||||
*/
|
|
||||||
public abstract <T> T reduce(Reducer<T> reducer);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
package org.nwapw.abacus.tree;
|
|
||||||
|
|
||||||
public class UnaryNode extends TreeNode {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The operation this node will apply.
|
|
||||||
*/
|
|
||||||
private String operation;
|
|
||||||
/**
|
|
||||||
* The tree node to apply the operation to.
|
|
||||||
*/
|
|
||||||
private TreeNode applyTo;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new node with the given operation and no child.
|
|
||||||
*
|
|
||||||
* @param operation the operation for this node.
|
|
||||||
*/
|
|
||||||
public UnaryNode(String operation) {
|
|
||||||
this(operation, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new node with the given operation and child.
|
|
||||||
*
|
|
||||||
* @param operation the operation for this node.
|
|
||||||
* @param applyTo the node to apply the function to.
|
|
||||||
*/
|
|
||||||
public UnaryNode(String operation, TreeNode applyTo) {
|
|
||||||
this.operation = operation;
|
|
||||||
this.applyTo = applyTo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
|
||||||
Object reducedChild = applyTo.reduce(reducer);
|
|
||||||
if (reducedChild == null) return null;
|
|
||||||
return reducer.reduceNode(this, reducedChild);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the operation of this node.
|
|
||||||
*
|
|
||||||
* @return the operation this node performs.
|
|
||||||
*/
|
|
||||||
public String getOperation() {
|
|
||||||
return operation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the node to which this node's operation applies.
|
|
||||||
*
|
|
||||||
* @return the tree node to which the operation will be applied.
|
|
||||||
*/
|
|
||||||
public TreeNode getApplyTo() {
|
|
||||||
return applyTo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "(" + (applyTo == null ? "null" : applyTo.toString()) + ")" + operation;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
14
src/main/kotlin/org/nwapw/abacus/function/Operator.kt
Normal file
14
src/main/kotlin/org/nwapw/abacus/function/Operator.kt
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package org.nwapw.abacus.function
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A single operator that can be used by Abacus.
|
||||||
|
*
|
||||||
|
* This is a data class that holds the information about a single operator, such as a plus or minus.
|
||||||
|
*
|
||||||
|
* @param associativity the associativity of this operator, used for order of operations;.
|
||||||
|
* @param type the type of this operator, used for parsing (infix / prefix / postfix and binary / unary)
|
||||||
|
* @param precedence the precedence of this operator, used for order of operations.
|
||||||
|
* @param function the function this operator applies to its arguments.
|
||||||
|
*/
|
||||||
|
data class Operator(val associativity: OperatorAssociativity, val type: OperatorType,
|
||||||
|
val precedence: Int, val function: Function)
|
||||||
26
src/main/kotlin/org/nwapw/abacus/tree/BinaryNode.kt
Normal file
26
src/main/kotlin/org/nwapw/abacus/tree/BinaryNode.kt
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package org.nwapw.abacus.tree
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A tree node that holds a binary operation.
|
||||||
|
*
|
||||||
|
* This node represents any binary operation, such as binary infix or binary postfix. The only
|
||||||
|
* currently implemented into Abacus is binary infix, but that has more to do with the parser than
|
||||||
|
* this class, which doesn't care about the order that its operation and nodes were found in text.
|
||||||
|
*
|
||||||
|
* @param operation the operation this node performs on its children.
|
||||||
|
* @param left the left node.
|
||||||
|
* @param right the right node.
|
||||||
|
*/
|
||||||
|
data class BinaryNode(val operation: String, val left: TreeNode? = null, val right: TreeNode?) : TreeNode() {
|
||||||
|
|
||||||
|
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||||
|
val leftReduce = left?.reduce(reducer) ?: return null
|
||||||
|
val rightReduce = right?.reduce(reducer) ?: return null
|
||||||
|
return reducer.reduceNode(this, leftReduce, rightReduce)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return "(" + (left?.toString() ?: "null") + operation + (right?.toString() ?: "null") + ")"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
52
src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt
Normal file
52
src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package org.nwapw.abacus.tree
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A tree node that holds a function call.
|
||||||
|
*
|
||||||
|
* The function call node can hold any number of children, and passes the to the appropriate reducer,
|
||||||
|
* but that is its sole purpose.
|
||||||
|
*
|
||||||
|
* @param function the function string.
|
||||||
|
*/
|
||||||
|
data class FunctionNode(val function: String) : TreeNode() {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of function parameters added to this node.
|
||||||
|
*/
|
||||||
|
val children: MutableList<TreeNode> = mutableListOf()
|
||||||
|
|
||||||
|
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||||
|
val children = Array<Any?>(children.size, { children[it].reduce(reducer) ?: return null; })
|
||||||
|
return reducer.reduceNode(this, *children)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
val buffer = StringBuffer()
|
||||||
|
buffer.append(function)
|
||||||
|
buffer.append('(')
|
||||||
|
for (i in 0 until children.size) {
|
||||||
|
buffer.append(children[i].toString())
|
||||||
|
buffer.append(if (i == children.size - 1) ")" else ",")
|
||||||
|
}
|
||||||
|
return buffer.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends a child to this node's list of children.
|
||||||
|
*
|
||||||
|
* @node the node to append.
|
||||||
|
*/
|
||||||
|
fun appendChild(node: TreeNode){
|
||||||
|
children.add(node)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepends a child to this node's list of children.
|
||||||
|
*
|
||||||
|
* @node the node to prepend.
|
||||||
|
*/
|
||||||
|
fun prependChild(node: TreeNode){
|
||||||
|
children.add(0, node)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
23
src/main/kotlin/org/nwapw/abacus/tree/NumberNode.kt
Normal file
23
src/main/kotlin/org/nwapw/abacus/tree/NumberNode.kt
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package org.nwapw.abacus.tree
|
||||||
|
|
||||||
|
import org.nwapw.abacus.number.NumberInterface
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A tree node that holds a single number value.
|
||||||
|
*
|
||||||
|
* This is a tree node that holds a single NumberInterface, which represents any number,
|
||||||
|
* and is not defined during compile time.
|
||||||
|
*
|
||||||
|
* @number the number value of this node.
|
||||||
|
*/
|
||||||
|
data class NumberNode(val number: NumberInterface) : TreeNode() {
|
||||||
|
|
||||||
|
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||||
|
return reducer.reduceNode(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return number.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
10
src/main/kotlin/org/nwapw/abacus/tree/TreeNode.kt
Normal file
10
src/main/kotlin/org/nwapw/abacus/tree/TreeNode.kt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package org.nwapw.abacus.tree
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A tree node.
|
||||||
|
*/
|
||||||
|
abstract class TreeNode {
|
||||||
|
|
||||||
|
abstract fun <T: Any> reduce(reducer: Reducer<T>) : T?
|
||||||
|
|
||||||
|
}
|
||||||
23
src/main/kotlin/org/nwapw/abacus/tree/UnaryNode.kt
Normal file
23
src/main/kotlin/org/nwapw/abacus/tree/UnaryNode.kt
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package org.nwapw.abacus.tree
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A tree node that holds a unary operation.
|
||||||
|
*
|
||||||
|
* This node holds a single operator applied to a single parameter, and does not care
|
||||||
|
* whether the operation was found before or after the parameter in the text.
|
||||||
|
*
|
||||||
|
* @param operation the operation applied to the given node.
|
||||||
|
* @param applyTo the node to which the operation will be applied.
|
||||||
|
*/
|
||||||
|
data class UnaryNode(val operation: String, val applyTo: TreeNode? = null) : TreeNode() {
|
||||||
|
|
||||||
|
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||||
|
val reducedChild = applyTo?.reduce(reducer) ?: return null
|
||||||
|
return reducer.reduceNode(this, reducedChild)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return "(" + (applyTo?.toString() ?: "null") + ")" + operation
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -60,6 +60,15 @@
|
|||||||
</FlowPane>
|
</FlowPane>
|
||||||
</GridPane>
|
</GridPane>
|
||||||
</Tab>
|
</Tab>
|
||||||
|
<Tab fx:id="functionListTab" text="Functions" closable="false">
|
||||||
|
<VBox spacing="10">
|
||||||
|
<padding>
|
||||||
|
<Insets left="10" right="10" top="10" bottom="10"/>
|
||||||
|
</padding>
|
||||||
|
<TextField fx:id="functionListSearchField" maxWidth="Infinity"/>
|
||||||
|
<ListView maxWidth="Infinity" fx:id="functionListView"/>
|
||||||
|
</VBox>
|
||||||
|
</Tab>
|
||||||
</TabPane>
|
</TabPane>
|
||||||
</center>
|
</center>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user