mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-26 16:45:21 +00:00
Compare commits
10 Commits
thread-saf
...
promotion-
| Author | SHA1 | Date | |
|---|---|---|---|
| 566831246c | |||
| ad8a0a9b2a | |||
| e430e738cf | |||
| f6e326e0f1 | |||
| 07581557c7 | |||
| 14ac9c67f4 | |||
| 0ff071e212 | |||
| 88e3bb7109 | |||
| 540e5d6099 | |||
| c9e93d87a2 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -24,7 +24,9 @@ hs_err_pid*
|
|||||||
# Custom Stuff
|
# Custom Stuff
|
||||||
# Gradle
|
# Gradle
|
||||||
.gradle/*
|
.gradle/*
|
||||||
build/*
|
**/build/*
|
||||||
|
**/out/**
|
||||||
|
**/.DS_Store
|
||||||
|
|
||||||
# IntelliJ
|
# IntelliJ
|
||||||
.idea/*
|
.idea/*
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package org.nwapw.abacus.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception thrown when a promotion fails.
|
||||||
|
*/
|
||||||
|
public class PromotionException extends AbacusException {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new PromotionException with the default message
|
||||||
|
* and no additional information.
|
||||||
|
*/
|
||||||
|
public PromotionException() {
|
||||||
|
this("");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new PromotionException with the given additional message.
|
||||||
|
* @param message the additional message to include with the error.
|
||||||
|
*/
|
||||||
|
public PromotionException(String message) {
|
||||||
|
super("Failed to promote number instances.", message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ import org.nwapw.abacus.exception.ComputationInterruptedException;
|
|||||||
/**
|
/**
|
||||||
* An interface used to represent a number.
|
* An interface used to represent a number.
|
||||||
*/
|
*/
|
||||||
public abstract class NumberInterface {
|
public abstract class NumberInterface implements Comparable<NumberInterface> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the thread was interrupted and
|
* Check if the thread was interrupted and
|
||||||
@@ -158,14 +158,6 @@ public abstract class NumberInterface {
|
|||||||
return intPowInternal(exponent);
|
return intPowInternal(exponent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Compares this number to another.
|
|
||||||
*
|
|
||||||
* @param number the number to compare to.
|
|
||||||
* @return same as Integer.compare();
|
|
||||||
*/
|
|
||||||
public abstract int compareTo(NumberInterface number);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Same as Math.signum().
|
* Same as Math.signum().
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ class Abacus(val configuration: Configuration) {
|
|||||||
pluginManager.reload()
|
pluginManager.reload()
|
||||||
with(mutableContext) {
|
with(mutableContext) {
|
||||||
numberImplementation = pluginManager.numberImplementationFor(configuration.numberImplementation)
|
numberImplementation = pluginManager.numberImplementationFor(configuration.numberImplementation)
|
||||||
|
?: StandardPlugin.IMPLEMENTATION_NAIVE
|
||||||
clearVariables()
|
clearVariables()
|
||||||
clearDefinitions()
|
clearDefinitions()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.nwapw.abacus.number
|
package org.nwapw.abacus.number
|
||||||
|
|
||||||
import org.nwapw.abacus.Abacus
|
import org.nwapw.abacus.Abacus
|
||||||
|
import org.nwapw.abacus.exception.PromotionException
|
||||||
import org.nwapw.abacus.plugin.NumberImplementation
|
import org.nwapw.abacus.plugin.NumberImplementation
|
||||||
import org.nwapw.abacus.plugin.PluginListener
|
import org.nwapw.abacus.plugin.PluginListener
|
||||||
import org.nwapw.abacus.plugin.PluginManager
|
import org.nwapw.abacus.plugin.PluginManager
|
||||||
@@ -45,14 +46,14 @@ class PromotionManager(val abacus: Abacus) : PluginListener {
|
|||||||
* @param numbers the numbers to promote.
|
* @param numbers the numbers to promote.
|
||||||
* @return the resulting promotion result.
|
* @return the resulting promotion result.
|
||||||
*/
|
*/
|
||||||
fun promote(vararg numbers: NumberInterface): PromotionResult? {
|
fun promote(vararg numbers: NumberInterface): PromotionResult {
|
||||||
val pluginManager = abacus.pluginManager
|
val pluginManager = abacus.pluginManager
|
||||||
val implementations = numbers.map { pluginManager.interfaceImplementationFor(it.javaClass) }
|
val implementations = numbers.map { pluginManager.interfaceImplementationFor(it.javaClass) }
|
||||||
val highestPriority = implementations.sortedBy { it.priority }.last()
|
val highestPriority = implementations.sortedBy { it.priority }.last()
|
||||||
return PromotionResult(items = numbers.map {
|
return PromotionResult(items = numbers.map {
|
||||||
if(it.javaClass == highestPriority.implementation) it
|
if(it.javaClass == highestPriority.implementation) it
|
||||||
else computePaths[pluginManager.interfaceImplementationFor(it.javaClass) to highestPriority]
|
else computePaths[pluginManager.interfaceImplementationFor(it.javaClass) to highestPriority]
|
||||||
?.promote(it) ?: return null
|
?.promote(it) ?: throw PromotionException()
|
||||||
}.toTypedArray(), promotedTo = highestPriority)
|
}.toTypedArray(), promotedTo = highestPriority)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,15 +36,13 @@ class NumberReducer(val abacus: Abacus, context: EvaluationContext) : Reducer<Nu
|
|||||||
is NumberBinaryNode -> {
|
is NumberBinaryNode -> {
|
||||||
val left = children[0] as NumberInterface
|
val left = children[0] as NumberInterface
|
||||||
val right = children[1] as NumberInterface
|
val right = children[1] as NumberInterface
|
||||||
val promotionResult = promotionManager.promote(left, right) ?:
|
val promotionResult = promotionManager.promote(left, right)
|
||||||
throw EvaluationException("promotion failed.")
|
|
||||||
context.numberImplementation = promotionResult.promotedTo
|
context.numberImplementation = promotionResult.promotedTo
|
||||||
abacus.pluginManager.operatorFor(treeNode.operation).apply(context, *promotionResult.items)
|
abacus.pluginManager.operatorFor(treeNode.operation).apply(context, *promotionResult.items)
|
||||||
}
|
}
|
||||||
is FunctionNode -> {
|
is FunctionNode -> {
|
||||||
val promotionResult = promotionManager
|
val promotionResult = promotionManager
|
||||||
.promote(*children.map { it as NumberInterface }.toTypedArray()) ?:
|
.promote(*children.map { it as NumberInterface }.toTypedArray())
|
||||||
throw EvaluationException("promotion failed.")
|
|
||||||
context.numberImplementation = promotionResult.promotedTo
|
context.numberImplementation = promotionResult.promotedTo
|
||||||
abacus.pluginManager.functionFor(treeNode.callTo).apply(context, *promotionResult.items)
|
abacus.pluginManager.functionFor(treeNode.callTo).apply(context, *promotionResult.items)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user