From 86438bf6188b4af7236c5f0367c8a223af498dd4 Mon Sep 17 00:00:00 2001 From: XDrake99 Date: Fri, 3 Mar 2017 23:06:43 +0100 Subject: [PATCH] Some other updates to the new internal structure --- .classpath | 5 + src/org/warp/picalculator/Error.java | 10 +- src/org/warp/picalculator/Errors.java | 2 +- src/org/warp/picalculator/TestGPU.java | 53 +++- src/org/warp/picalculator/Utils.java | 112 +++++-- .../warp/picalculator/gui/DisplayManager.java | 6 +- .../picalculator/gui/expression/Block.java | 47 +++ .../gui/expression/BlockChar.java | 41 +++ .../gui/expression/BlockContainer.java | 193 ++++++++++++ .../gui/expression/BlockDivision.java | 70 +++++ .../picalculator/gui/expression/Caret.java | 30 ++ .../gui/expression/CaretState.java | 7 + .../GraphicalElement.java | 8 +- .../gui/graphicengine/Renderer.java | 6 + .../gui/graphicengine/cpu/CPUEngine.java | 20 +- .../gui/graphicengine/cpu/CPUFont.java | 4 +- .../gui/graphicengine/cpu/CPUSkin.java | 2 +- .../gui/graphicengine/cpu/SwingWindow.java | 45 +-- .../graphicengine/gpu/DeallocationHelper.java | 10 +- .../gui/graphicengine/gpu/GPUFont.java | 10 +- .../gui/graphicengine/gpu/GPURenderer.java | 20 +- src/org/warp/picalculator/gui/math/Block.java | 11 - .../warp/picalculator/gui/math/Container.java | 112 ------- .../gui/screens/MathInputScreen.java | 24 +- .../picalculator/math/FunctionDynamic.java | 14 +- .../picalculator/math/FunctionOperator.java | 14 +- .../picalculator/math/FunctionSingle.java | 14 +- .../warp/picalculator/math/MathContext.java | 34 ++- .../math/MathematicalSymbols.java | 77 +++-- .../warp/picalculator/math/SolveMethod.java | 4 +- .../picalculator/math/functions/Division.java | 8 +- .../math/functions/EmptyNumber.java | 4 +- .../math/functions/Expression.java | 42 +-- .../picalculator/math/functions/Joke.java | 4 +- .../math/functions/Multiplication.java | 6 +- .../picalculator/math/functions/Negative.java | 62 +--- .../picalculator/math/functions/Number.java | 4 +- .../picalculator/math/functions/Power.java | 6 +- .../picalculator/math/functions/Root.java | 78 +---- .../math/functions/RootSquare.java | 67 +--- .../math/functions/Subtraction.java | 24 +- .../warp/picalculator/math/functions/Sum.java | 6 +- .../math/functions/SumSubtraction.java | 68 +---- .../math/functions/Undefined.java | 64 ++-- .../picalculator/math/functions/Variable.java | 4 +- .../math/functions/equations/Equation.java | 26 +- .../functions/equations/EquationsSystem.java | 72 +---- .../equations/EquationsSystemPart.java | 61 +--- .../functions/trigonometry/ArcCosine.java | 20 +- .../math/functions/trigonometry/ArcSine.java | 20 +- .../functions/trigonometry/ArcTangent.java | 20 +- .../math/functions/trigonometry/Cosine.java | 20 +- .../math/functions/trigonometry/Sine.java | 31 +- .../math/functions/trigonometry/Tangent.java | 22 +- .../picalculator/math/parser/InputParser.java | 289 ++++++++++++++++++ .../math/parser/features/FeatureChar.java | 13 + .../math/parser/features/FeatureDivision.java | 9 + .../parser/features/FeatureDoubleImpl.java | 33 ++ .../features/FeatureMultiplication.java | 9 + .../math/parser/features/FeatureNumber.java | 27 ++ .../parser/features/FeatureSingleImpl.java | 21 ++ .../math/parser/features/FeatureSum.java | 9 + .../parser/features/interfaces/Feature.java | 5 + .../features/interfaces/FeatureBasic.java | 4 + .../features/interfaces/FeatureDouble.java | 8 + .../features/interfaces/FeatureMultiple.java | 10 + .../features/interfaces/FeatureSingle.java | 6 + .../picalculator/math/rules/ExpandRule1.java | 6 +- .../picalculator/math/rules/ExpandRule5.java | 6 +- .../math/rules/ExponentRule1.java | 6 +- .../math/rules/ExponentRule15.java | 6 +- .../math/rules/ExponentRule16.java | 6 +- .../math/rules/ExponentRule17.java | 6 +- .../math/rules/ExponentRule2.java | 6 +- .../math/rules/ExponentRule3.java | 6 +- .../math/rules/ExponentRule4.java | 6 +- .../math/rules/ExponentRule9.java | 6 +- .../math/rules/FractionsRule1.java | 6 +- .../math/rules/FractionsRule11.java | 6 +- .../math/rules/FractionsRule12.java | 6 +- .../math/rules/FractionsRule14.java | 6 +- .../math/rules/FractionsRule2.java | 6 +- .../math/rules/FractionsRule3.java | 6 +- .../math/rules/FractionsRule4.java | 6 +- .../math/rules/FractionsRule5.java | 6 +- .../picalculator/math/rules/NumberRule1.java | 6 +- .../picalculator/math/rules/NumberRule2.java | 6 +- .../picalculator/math/rules/NumberRule3.java | 6 +- .../picalculator/math/rules/NumberRule4.java | 6 +- .../picalculator/math/rules/NumberRule5.java | 6 +- .../picalculator/math/rules/NumberRule6.java | 6 +- .../picalculator/math/rules/NumberRule7.java | 6 +- .../picalculator/math/rules/SyntaxRule1.java | 6 +- .../picalculator/math/rules/SyntaxRule2.java | 6 +- .../math/rules/UndefinedRule1.java | 6 +- .../math/rules/UndefinedRule2.java | 6 +- .../math/rules/VariableRule1.java | 6 +- .../math/rules/VariableRule2.java | 6 +- .../math/rules/VariableRule3.java | 6 +- .../math/rules/methods/DivisionRule1.java | 16 +- .../rules/methods/MultiplicationMethod1.java | 14 +- .../math/rules/methods/SumMethod1.java | 14 +- 102 files changed, 1467 insertions(+), 956 deletions(-) create mode 100644 src/org/warp/picalculator/gui/expression/Block.java create mode 100644 src/org/warp/picalculator/gui/expression/BlockChar.java create mode 100644 src/org/warp/picalculator/gui/expression/BlockContainer.java create mode 100644 src/org/warp/picalculator/gui/expression/BlockDivision.java create mode 100644 src/org/warp/picalculator/gui/expression/Caret.java create mode 100644 src/org/warp/picalculator/gui/expression/CaretState.java rename src/org/warp/picalculator/gui/{math => expression}/GraphicalElement.java (71%) delete mode 100644 src/org/warp/picalculator/gui/math/Block.java delete mode 100644 src/org/warp/picalculator/gui/math/Container.java create mode 100644 src/org/warp/picalculator/math/parser/InputParser.java create mode 100644 src/org/warp/picalculator/math/parser/features/FeatureChar.java create mode 100644 src/org/warp/picalculator/math/parser/features/FeatureDivision.java create mode 100644 src/org/warp/picalculator/math/parser/features/FeatureDoubleImpl.java create mode 100644 src/org/warp/picalculator/math/parser/features/FeatureMultiplication.java create mode 100644 src/org/warp/picalculator/math/parser/features/FeatureNumber.java create mode 100644 src/org/warp/picalculator/math/parser/features/FeatureSingleImpl.java create mode 100644 src/org/warp/picalculator/math/parser/features/FeatureSum.java create mode 100644 src/org/warp/picalculator/math/parser/features/interfaces/Feature.java create mode 100644 src/org/warp/picalculator/math/parser/features/interfaces/FeatureBasic.java create mode 100644 src/org/warp/picalculator/math/parser/features/interfaces/FeatureDouble.java create mode 100644 src/org/warp/picalculator/math/parser/features/interfaces/FeatureMultiple.java create mode 100644 src/org/warp/picalculator/math/parser/features/interfaces/FeatureSingle.java diff --git a/.classpath b/.classpath index f382bba6..1ebddf1e 100644 --- a/.classpath +++ b/.classpath @@ -29,5 +29,10 @@ + + + + + diff --git a/src/org/warp/picalculator/Error.java b/src/org/warp/picalculator/Error.java index 6c9091cf..8ac6a768 100644 --- a/src/org/warp/picalculator/Error.java +++ b/src/org/warp/picalculator/Error.java @@ -7,8 +7,14 @@ public class Error extends java.lang.Throwable { */ private static final long serialVersionUID = -1014947815755694651L; - public Error(Errors ErrorID) { - id = ErrorID; + public Error(Errors errorID) { + super(errorID.toString()); + id = errorID; + } + + public Error(Errors errorID, String errorMessage) { + super(errorID.toString() + ": " + errorMessage); + id = errorID; } public Errors id = Errors.ERROR; diff --git a/src/org/warp/picalculator/Errors.java b/src/org/warp/picalculator/Errors.java index a15dec96..a3df6442 100644 --- a/src/org/warp/picalculator/Errors.java +++ b/src/org/warp/picalculator/Errors.java @@ -1,5 +1,5 @@ package org.warp.picalculator; public enum Errors { - ERROR, DIVISION_BY_ZERO, UNBALANCED_BRACKETS, NOT_IMPLEMENTED, NEGATIVE_PARAMETER, NUMBER_TOO_LARGE, NUMBER_TOO_SMALL, CONVERSION_ERROR, SYNTAX_ERROR, NOT_AN_EQUATION, TIMEOUT + ERROR, DIVISION_BY_ZERO, UNBALANCED_STACK, NOT_IMPLEMENTED, NEGATIVE_PARAMETER, NUMBER_TOO_LARGE, NUMBER_TOO_SMALL, CONVERSION_ERROR, SYNTAX_ERROR, NOT_AN_EQUATION, TIMEOUT, MISSING_ARGUMENTS } diff --git a/src/org/warp/picalculator/TestGPU.java b/src/org/warp/picalculator/TestGPU.java index 62b1f656..8ea9b711 100644 --- a/src/org/warp/picalculator/TestGPU.java +++ b/src/org/warp/picalculator/TestGPU.java @@ -3,25 +3,36 @@ package org.warp.picalculator; import java.io.IOException; import org.warp.picalculator.gui.graphicengine.GraphicEngine; +import org.warp.picalculator.gui.graphicengine.Renderer; import org.warp.picalculator.gui.graphicengine.RenderingLoop; +import org.warp.picalculator.gui.expression.BlockChar; +import org.warp.picalculator.gui.expression.BlockContainer; +import org.warp.picalculator.gui.expression.BlockDivision; +import org.warp.picalculator.gui.expression.Caret; +import org.warp.picalculator.gui.expression.CaretState; import org.warp.picalculator.gui.graphicengine.BinaryFont; import org.warp.picalculator.gui.graphicengine.Skin; +import org.warp.picalculator.gui.graphicengine.cpu.CPUEngine; import org.warp.picalculator.gui.graphicengine.gpu.GPUEngine; import org.warp.picalculator.gui.graphicengine.gpu.GPURenderer; import org.warp.picalculator.gui.screens.KeyboardDebugScreen; import org.warp.picalculator.gui.screens.MarioScreen; +import org.warp.picalculator.math.MathContext; +import org.warp.picalculator.math.MathematicalSymbols; +import org.warp.picalculator.math.functions.Expression; +import org.warp.picalculator.math.parser.InputParser; import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; public class TestGPU { - public static final GPUEngine d = new GPUEngine(); + public static final GraphicEngine d = new GPUEngine(); - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException, Error { Utils.debugOn = true; Utils.debugThirdScreen = false; d.create(); @@ -34,17 +45,40 @@ public class TestGPU { private BinaryFont exampleFont; private final Skin exampleSkin; - private final GPURenderer r; + private final Renderer r; private final GraphicEngine d; - - public Scene(GraphicEngine d) throws IOException { + + private final BlockContainer c; + + public Scene(GraphicEngine d) throws IOException, Error { this.d = d; - r = (GPURenderer) d.getRenderer(); + r = d.getRenderer(); exampleFont = d.loadFont("ex"); exampleSkin = d.loadSkin("skin.png"); + BlockContainer.initializeFonts(d.loadFont("ex"), d.loadFont("big")); + + + //New expression framework test + c = new BlockContainer(false, 0, 200); + BlockDivision bd = new BlockDivision(); + c.addBlock(bd); + bd.getUpperContainer().addBlock(new BlockChar('5')); + bd.getUpperContainer().addBlock(new BlockChar(MathematicalSymbols.MULTIPLICATION)); + bd.getUpperContainer().addBlock(new BlockChar('2')); + bd.getLowerContainer().addBlock(new BlockChar('2')); + bd.recomputeDimensions(); + c.addBlock(new BlockChar(MathematicalSymbols.MULTIPLICATION)); + c.addBlock(new BlockChar('2')); + c.addBlock(new BlockChar('2')); + c.addBlock(new BlockChar('b')); + c.recomputeDimensions(); + + Expression expr = InputParser.parseInput(new MathContext(), c); + System.out.println(expr.toString()); + d.start(this); // fonts = new RAWFont[1]; @@ -76,13 +110,16 @@ public class TestGPU { r.glFillRect(2, 2, 160, 160, 0, 0, 16, 16); exampleFont.use(d); r.glColor3f(1, 0, 0); - r.glDrawStringLeft(10, 170, "Prova! 123456789"); + r.glDrawStringLeft(10, 170, "Prova! 123456789 222"); //MSAA TEST r.glDrawStringLeft(10f, 190.5f, "Test MSAA"); exampleSkin.use(d); r.glColor3f(1.0f, 1.0f, 1.0f); r.glFillRect(162, 2.5f, 160, 160, 0, 0, 16, 16); + + //New expression framework test + c.draw(d, r, 10, 220, new Caret(CaretState.VISIBLE_ON, 10)); } } diff --git a/src/org/warp/picalculator/Utils.java b/src/org/warp/picalculator/Utils.java index 6ae106c7..ac026f5a 100644 --- a/src/org/warp/picalculator/Utils.java +++ b/src/org/warp/picalculator/Utils.java @@ -15,7 +15,7 @@ import java.lang.reflect.Modifier; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.List; import org.nevec.rjm.BigDecimalMath; @@ -78,6 +78,17 @@ public class Utils { return contains; } + public static boolean isInArray(char ch, char[] a) { + boolean contains = false; + for (final char c : a) { + if (c == ch) { + contains = true; + break; + } + } + return contains; + } + private static final String[] regexNormalSymbols = new String[] { "\\", ".", "[", "]", "{", "}", "(", ")", "*", "+", "-", "?", "^", "$", "|" }; public static String ArrayToRegex(String[] array) { @@ -107,6 +118,33 @@ public class Utils { return regex; } + public static String ArrayToRegex(char[] array) { + String regex = null; + for (final char symbol : array) { + boolean contained = false; + for (final String smb : regexNormalSymbols) { + if ((smb).equals(symbol+"")) { + contained = true; + break; + } + } + if (contained) { + if (regex != null) { + regex += "|\\" + symbol; + } else { + regex = "\\" + symbol; + } + } else { + if (regex != null) { + regex += "|" + symbol; + } else { + regex = symbol+""; + } + } + } + return regex; + } + public static String[] concat(String[] a, String[] b) { final int aLen = a.length; final int bLen = b.length; @@ -116,6 +154,15 @@ public class Utils { return c; } + public static char[] concat(char[] a, char[] b) { + final int aLen = a.length; + final int bLen = b.length; + final char[] c = new char[aLen + bLen]; + System.arraycopy(a, 0, c, 0, aLen); + System.arraycopy(b, 0, c, aLen, bLen); + return c; + } + public static String[] add(String[] a, String b) { final int aLen = a.length; final String[] c = new String[aLen + 1]; @@ -124,7 +171,15 @@ public class Utils { return c; } - public static boolean areThereOnlySettedUpFunctionsSumsEquationsAndSystems(ArrayList fl) { + public static char[] add(char[] a, char b) { + final int aLen = a.length; + final char[] c = new char[aLen + 1]; + System.arraycopy(a, 0, c, 0, aLen); + c[aLen] = b; + return c; + } + + public static boolean areThereOnlySettedUpFunctionsSumsEquationsAndSystems(ObjectArrayList fl) { for (int i = 0; i < fl.size(); i++) { if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Subtraction || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) { if (fl.get(i) instanceof FunctionSingle) { @@ -143,7 +198,7 @@ public class Utils { return true; } - public static boolean areThereOnlySettedUpFunctionsSumsMultiplicationsEquationsAndSystems(ArrayList fl) { + public static boolean areThereOnlySettedUpFunctionsSumsMultiplicationsEquationsAndSystems(ObjectArrayList fl) { for (int i = 0; i < fl.size(); i++) { if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Multiplication || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Subtraction || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) { if (fl.get(i) instanceof FunctionSingle) { @@ -162,7 +217,7 @@ public class Utils { return true; } - public static boolean areThereOnlySettedUpFunctionsEquationsAndSystems(ArrayList fl) { + public static boolean areThereOnlySettedUpFunctionsEquationsAndSystems(ObjectArrayList fl) { for (int i = 0; i < fl.size(); i++) { if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) { if (fl.get(i) instanceof FunctionSingle) { @@ -181,7 +236,7 @@ public class Utils { return true; } - public static boolean areThereOnlySettedUpFunctionsAndSystems(ArrayList fl) { + public static boolean areThereOnlySettedUpFunctionsAndSystems(ObjectArrayList fl) { for (int i = 0; i < fl.size(); i++) { if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) { if (fl.get(i) instanceof FunctionSingle) { @@ -200,7 +255,7 @@ public class Utils { return true; } - public static boolean areThereOnlyEmptySNFunctions(ArrayList fl) { + public static boolean areThereOnlyEmptySNFunctions(ObjectArrayList fl) { for (int i = 0; i < fl.size(); i++) { if (fl.get(i) instanceof FunctionSingle) { if (((FunctionSingle) fl.get(i)).getParameter() == null) { @@ -211,7 +266,7 @@ public class Utils { return false; } - public static boolean areThereOnlyEmptyNSNFunctions(ArrayList fl) { + public static boolean areThereOnlyEmptyNSNFunctions(ObjectArrayList fl) { for (int i = 0; i < fl.size(); i++) { if (fl.get(i) instanceof FunctionOperator && !(fl.get(i) instanceof Sum) && !(fl.get(i) instanceof SumSubtraction) && !(fl.get(i) instanceof Subtraction) && !(fl.get(i) instanceof Multiplication) && !(fl.get(i) instanceof Division)) { if (((FunctionOperator) fl.get(i)).getParameter1() == null && ((FunctionOperator) fl.get(i)).getParameter2() == null) { @@ -222,7 +277,7 @@ public class Utils { return false; } - public static boolean areThereEmptyMultiplications(ArrayList fl) { + public static boolean areThereEmptyMultiplications(ObjectArrayList fl) { for (int i = 0; i < fl.size(); i++) { if (fl.get(i) instanceof Multiplication || fl.get(i) instanceof Division) { if (((FunctionOperator) fl.get(i)).getParameter1() == null && ((FunctionOperator) fl.get(i)).getParameter2() == null) { @@ -233,7 +288,7 @@ public class Utils { return false; } - public static boolean areThereEmptySums(ArrayList fl) { + public static boolean areThereEmptySums(ObjectArrayList fl) { for (int i = 0; i < fl.size(); i++) { if (fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Subtraction) { if (((FunctionOperator) fl.get(i)).getParameter1() == null && ((FunctionOperator) fl.get(i)).getParameter2() == null) { @@ -244,7 +299,7 @@ public class Utils { return false; } - public static boolean areThereEmptySystems(ArrayList fl) { + public static boolean areThereEmptySystems(ObjectArrayList fl) { for (int i = 0; i < fl.size(); i++) { if (fl.get(i) instanceof EquationsSystemPart) { if (((EquationsSystemPart) fl.get(i)).getParameter() == null) { @@ -255,7 +310,7 @@ public class Utils { return false; } - public static boolean areThereOtherSettedUpFunctions(ArrayList fl) { + public static boolean areThereOtherSettedUpFunctions(ObjectArrayList fl) { for (int i = 0; i < fl.size(); i++) { if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Expression || fl.get(i) instanceof FunctionSingle || fl.get(i) instanceof Multiplication || fl.get(i) instanceof Division)) { if (fl.get(i) instanceof FunctionSingle) { @@ -338,7 +393,7 @@ public class Utils { return BigDecimalMath.divideRound(new BigDecimal(r.numer()).setScale(Utils.scale, Utils.scaleMode), new BigDecimal(r.denom()).setScale(Utils.scale, Utils.scaleMode)); } - public static boolean equalsVariables(ArrayList variables, ArrayList variables2) { + public static boolean equalsVariables(ObjectArrayList variables, ObjectArrayList variables2) { if (variables.size() != variables2.size()) { return false; } else { @@ -351,21 +406,22 @@ public class Utils { } } + @Deprecated public static void writeSquareRoot(Function var, int x, int y, boolean small) { - var.setSmall(small); - final int w1 = var.getWidth(); - final int h1 = var.getHeight(); - final int wsegno = 5; - final int hsegno = h1 + 2; - - var.draw(x + wsegno, y + (hsegno - h1), null, null); - - DisplayManager.renderer.glDrawLine(x + 1, y + hsegno - 3, x + 1, y + hsegno - 3); - DisplayManager.renderer.glDrawLine(x + 2, y + hsegno - 2, x + 2, y + hsegno - 2); - DisplayManager.renderer.glDrawLine(x + 3, y + hsegno - 1, x + 3, y + hsegno - 1); - DisplayManager.renderer.glDrawLine(x + 3, y + (hsegno - 1) / 2 + 1, x + 3, y + hsegno - 1); - DisplayManager.renderer.glDrawLine(x + 4, y, x + 4, y + (hsegno - 1) / 2); - DisplayManager.renderer.glDrawLine(x + 4, y, x + 4 + 1 + w1 + 1, y); +// var.setSmall(small); +// final int w1 = var.getWidth(); +// final int h1 = var.getHeight(); +// final int wsegno = 5; +// final int hsegno = h1 + 2; +// +// var.draw(x + wsegno, y + (hsegno - h1), null, null); +// +// DisplayManager.renderer.glDrawLine(x + 1, y + hsegno - 3, x + 1, y + hsegno - 3); +// DisplayManager.renderer.glDrawLine(x + 2, y + hsegno - 2, x + 2, y + hsegno - 2); +// DisplayManager.renderer.glDrawLine(x + 3, y + hsegno - 1, x + 3, y + hsegno - 1); +// DisplayManager.renderer.glDrawLine(x + 3, y + (hsegno - 1) / 2 + 1, x + 3, y + hsegno - 1); +// DisplayManager.renderer.glDrawLine(x + 4, y, x + 4, y + (hsegno - 1) / 2); +// DisplayManager.renderer.glDrawLine(x + 4, y, x + 4 + 1 + w1 + 1, y); } public static final int getFontHeight() { @@ -482,7 +538,7 @@ public class Utils { } - public static Function[][] joinFunctionsResults(ArrayList> ln) { + public static Function[][] joinFunctionsResults(ObjectArrayList> ln) { final int[] sizes = new int[ln.size()]; for (int i = 0; i < ln.size(); i++) { sizes[i] = ln.get(i).size(); @@ -579,7 +635,7 @@ public class Utils { if (displayName.endsWith("MemorySize")) { mb = true; } - ArrayList arr = new ArrayList<>(); + ObjectArrayList arr = new ObjectArrayList<>(); arr.add("getFreePhysicalMemorySize"); arr.add("getProcessCpuLoad"); arr.add("getSystemCpuLoad"); diff --git a/src/org/warp/picalculator/gui/DisplayManager.java b/src/org/warp/picalculator/gui/DisplayManager.java index 6ca9cf38..8b91a077 100644 --- a/src/org/warp/picalculator/gui/DisplayManager.java +++ b/src/org/warp/picalculator/gui/DisplayManager.java @@ -5,7 +5,7 @@ import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Main; import org.warp.picalculator.Utils; @@ -413,7 +413,7 @@ public final class DisplayManager implements RenderingLoop { // if (displayName.endsWith("MemorySize")) { // mb = true; // } -// ArrayList arr = new ArrayList<>(); +// ObjectArrayList arr = new ObjectArrayList<>(); // arr.add("getFreePhysicalMemorySize"); // arr.add("getProcessCpuLoad"); // arr.add("getSystemCpuLoad"); @@ -449,7 +449,7 @@ public final class DisplayManager implements RenderingLoop { workThread.setName("Work thread"); workThread.start(); - engine.start(this); + engine.start(getDrawable()); engine.waitUntilExit(); } catch (final Exception ex) { diff --git a/src/org/warp/picalculator/gui/expression/Block.java b/src/org/warp/picalculator/gui/expression/Block.java new file mode 100644 index 00000000..85967e38 --- /dev/null +++ b/src/org/warp/picalculator/gui/expression/Block.java @@ -0,0 +1,47 @@ +package org.warp.picalculator.gui.expression; + +import org.warp.picalculator.gui.graphicengine.GraphicEngine; +import org.warp.picalculator.gui.graphicengine.Renderer; +import org.warp.picalculator.math.parser.features.interfaces.Feature; + +public abstract class Block implements GraphicalElement { + + protected boolean small; + protected int width; + protected int height; + protected int line; + + /** + * + * @param r Graphic Renderer class. + * @param x Position relative to the window. + * @param y Position relative to the window. + * @param small + */ + public abstract void draw(GraphicEngine ge, Renderer r, int x, int y, Caret caret); + + @Override + public abstract void recomputeDimensions(); + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + + @Override + public int getLine() { + return line; + } + + public boolean isSmall() { + return small; + } + + public abstract void setSmall(boolean small); + +} diff --git a/src/org/warp/picalculator/gui/expression/BlockChar.java b/src/org/warp/picalculator/gui/expression/BlockChar.java new file mode 100644 index 00000000..18c87c50 --- /dev/null +++ b/src/org/warp/picalculator/gui/expression/BlockChar.java @@ -0,0 +1,41 @@ +package org.warp.picalculator.gui.expression; + +import org.warp.picalculator.gui.graphicengine.GraphicEngine; +import org.warp.picalculator.gui.graphicengine.Renderer; +import org.warp.picalculator.math.parser.features.FeatureChar; +import org.warp.picalculator.math.parser.features.interfaces.Feature; + +public class BlockChar extends Block { + + private final char ch; + + public BlockChar(char ch) { + this.ch = ch; + recomputeDimensions(); + } + + @Override + public void draw(GraphicEngine ge, Renderer r, int x, int y, Caret caret) { + BlockContainer.getDefaultFont(small).use(ge); + r.glColor(BlockContainer.getDefaultColor()); + r.glDrawCharLeft(x, y, ch); + } + + @Override + public void recomputeDimensions() { + width = BlockContainer.getDefaultCharWidth(small); + height = BlockContainer.getDefaultCharHeight(small); + line = height/2; + } + + @Override + public void setSmall(boolean small) { + this.small = small; + recomputeDimensions(); + } + + public char getChar() { + return ch; + } + +} diff --git a/src/org/warp/picalculator/gui/expression/BlockContainer.java b/src/org/warp/picalculator/gui/expression/BlockContainer.java new file mode 100644 index 00000000..51235657 --- /dev/null +++ b/src/org/warp/picalculator/gui/expression/BlockContainer.java @@ -0,0 +1,193 @@ +package org.warp.picalculator.gui.expression; + +import java.io.IOException; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +import org.warp.picalculator.device.graphicengine.Display; +import org.warp.picalculator.gui.graphicengine.BinaryFont; +import org.warp.picalculator.gui.graphicengine.GraphicEngine; +import org.warp.picalculator.gui.graphicengine.Renderer; +import org.warp.picalculator.gui.graphicengine.gpu.GPUFont; + +public class BlockContainer implements GraphicalElement { + + private final int minWidth; + private final int minHeight; + private final ObjectArrayList content; + private boolean small; + private int width; + private int height; + private int line; + + public BlockContainer() { + this(false, BlockContainer.getDefaultCharWidth(true), BlockContainer.getDefaultCharHeight(true)); + } + + public BlockContainer(boolean small) { + this(small, BlockContainer.getDefaultCharWidth(true), BlockContainer.getDefaultCharHeight(true)); + } + + public BlockContainer(boolean small, int minWidth, int minHeight) { + this(small, minWidth, minHeight, new ObjectArrayList<>()); + } + + public BlockContainer(boolean small, int minWidth, int minHeight, ObjectArrayList content) { + this.small = small; + this.minWidth = minWidth; + this.minHeight = minHeight; + for (Block b: content) { + b.setSmall(small); + } + this.content = content; + recomputeDimensions(); + } + + public void addBlock(Block b) { + b.setSmall(small); + content.add(b); + recomputeDimensions(); + } + + public void removeBlock(Block b) { + content.remove(b); + recomputeDimensions(); + } + + public void removeAt(int i) { + content.remove(i); + recomputeDimensions(); + } + + public Block getBlockAt(int i) { + return content.get(i); + } + + public void clear() { + content.clear(); + recomputeDimensions(); + } + + /** + * + * @param ge Graphic Engine class. + * @param r Graphic Renderer class of ge. + * @param x Position relative to the window. + * @param y Position relative to the window. + * @param caret Position of the caret. + */ + public void draw(GraphicEngine ge, Renderer r, int x, int y, Caret caret) { + int paddingX = 0; + + if (caret.getRemaining() == 0) { + if (content.size() > 0) { + BlockContainer.drawCaret(ge, r, caret, x, y, content.get(0).getHeight()); + } else { + BlockContainer.drawCaret(ge, r, caret, x, y, height); + } + } + + for (Block b : content) { + caret.skip(1); + b.draw(ge, r, x+paddingX, y+line-b.getLine(), caret); + paddingX += b.getWidth(); + if (caret.getRemaining() == 0) BlockContainer.drawCaret(ge, r, caret, x + paddingX, y+line-b.getLine(), b.height); + } + caret.skip(1); + } + + @Override + public void recomputeDimensions() { + int l = 0; //Line + int w = 0; //Width + int h2 = 0; //Height under the line. h = h2 + l + int h = 0; //Height + + for (Block b : content) { + w += b.getWidth(); + final int bl = b.getLine(); + final int bh = b.getHeight(); + final int bh2 = bh - bl; + if (bl > l) { + l = bl; + } + if (bh2 > h2) { + h2 = bh2; + } + } + + h = h2 + l; + + line = l; + if (w > minWidth) { + width = w; + } else { + width = minWidth; + } + if (h > minHeight) { + height = h; + } else { + height = minHeight; + } + } + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + + @Override + public int getLine() { + return line; + } + + private static final BinaryFont[] defFonts = new BinaryFont[2]; + private static final int[] defFontSizes = new int[4]; + private static final int defColor = 0xFF000000; + + public static void initializeFonts(BinaryFont big, BinaryFont small) { + defFonts[0] = big; + defFonts[1] = small; + defFontSizes[0] = big.getCharacterWidth(); + defFontSizes[1] = big.getCharacterHeight(); + defFontSizes[2] = small.getCharacterWidth(); + defFontSizes[3] = small.getCharacterHeight(); + } + + public static BinaryFont getDefaultFont(boolean small) { + return defFonts[small?1:0]; + } + + + public static int getDefaultColor() { + return defColor; + } + + public static int getDefaultCharWidth(boolean b) { + return defFontSizes[b?2:0]; + } + + public static int getDefaultCharHeight(boolean b) { + return defFontSizes[b?3:1]; + } + + public static void drawCaret(GraphicEngine ge, Renderer r, Caret caret, int x, int y, int height) { + r.glColor(getDefaultColor()); + r.glDrawLine(x, y, x, y-1+height); + r.glDrawLine(x+1, y, x+1, y-1+height); + } + + public void setSmall(boolean small) { + this.small = small; + recomputeDimensions(); + } + + public ObjectArrayList getContent() { + return content.clone(); + } + +} \ No newline at end of file diff --git a/src/org/warp/picalculator/gui/expression/BlockDivision.java b/src/org/warp/picalculator/gui/expression/BlockDivision.java new file mode 100644 index 00000000..c73805d5 --- /dev/null +++ b/src/org/warp/picalculator/gui/expression/BlockDivision.java @@ -0,0 +1,70 @@ +package org.warp.picalculator.gui.expression; + +import org.warp.picalculator.Main; +import org.warp.picalculator.gui.graphicengine.GraphicEngine; +import org.warp.picalculator.gui.graphicengine.Renderer; +import org.warp.picalculator.math.parser.features.interfaces.Feature; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +public class BlockDivision extends Block { + + private final BlockContainer containerUp; + private final BlockContainer containerDown; + + private int paddingLeftUpper; + private int paddingLeftLower; + private int h1; + + public BlockDivision() { + this.containerUp = new BlockContainer(false); + this.containerDown = new BlockContainer(false); + recomputeDimensions(); + } + + @Override + public void draw(GraphicEngine ge, Renderer r, int x, int y, Caret caret) { + BlockContainer.getDefaultFont(small).use(ge); + r.glColor(BlockContainer.getDefaultColor()); + containerUp.draw(ge, r, x+1+paddingLeftUpper, y, caret); + r.glDrawLine(x, y+h1+1, x+width, y+h1+1); + containerDown.draw(ge, r, x+1+paddingLeftLower, y + h1+3, caret); + } + + @Override + public void recomputeDimensions() { + final int w1 = containerUp.getWidth(); + final int w2 = containerDown.getWidth(); + final int h1 = containerUp.getHeight(); + final int h2 = containerDown.getHeight(); + width = (w1>w2?w1:w2) + 2; + height = h1+3+h2; + line = h1+1; + this.h1 = h1; + if (w1 != w2) { + if (w1 > w2) { + paddingLeftUpper = 0; + paddingLeftLower = (w1 - w2) / 2; + } else { + paddingLeftUpper = (w2 - w1) / 2; + paddingLeftLower = 0; + } + } + } + + @Override + public void setSmall(boolean small) { + this.small = small; + this.containerUp.setSmall(small); + this.containerDown.setSmall(small); + recomputeDimensions(); + } + + public BlockContainer getUpperContainer() { + return containerUp; + } + + public BlockContainer getLowerContainer() { + return containerDown; + } +} diff --git a/src/org/warp/picalculator/gui/expression/Caret.java b/src/org/warp/picalculator/gui/expression/Caret.java new file mode 100644 index 00000000..5bbdca50 --- /dev/null +++ b/src/org/warp/picalculator/gui/expression/Caret.java @@ -0,0 +1,30 @@ +package org.warp.picalculator.gui.expression; + +public class Caret { + + private int pos; + private int remaining; + private CaretState state; + + public Caret(CaretState state, int pos) { + this.state = state; + this.pos = pos; + this.remaining = pos; + } + + public void skip(int i) { + remaining-=i; + } + + public int getPosition() { + return pos; + } + + public int getRemaining() { + return remaining; + } + + public CaretState getState() { + return state; + } +} diff --git a/src/org/warp/picalculator/gui/expression/CaretState.java b/src/org/warp/picalculator/gui/expression/CaretState.java new file mode 100644 index 00000000..a5d63be0 --- /dev/null +++ b/src/org/warp/picalculator/gui/expression/CaretState.java @@ -0,0 +1,7 @@ +package org.warp.picalculator.gui.expression; + +public enum CaretState { + VISIBLE_ON, + VISIBLE_OFF, + HIDDEN +} diff --git a/src/org/warp/picalculator/gui/math/GraphicalElement.java b/src/org/warp/picalculator/gui/expression/GraphicalElement.java similarity index 71% rename from src/org/warp/picalculator/gui/math/GraphicalElement.java rename to src/org/warp/picalculator/gui/expression/GraphicalElement.java index 6c5a152e..4d6ab338 100644 --- a/src/org/warp/picalculator/gui/math/GraphicalElement.java +++ b/src/org/warp/picalculator/gui/expression/GraphicalElement.java @@ -1,4 +1,4 @@ -package org.warp.picalculator.gui.math; +package org.warp.picalculator.gui.expression; public interface GraphicalElement { @@ -24,10 +24,4 @@ public interface GraphicalElement { * @return Position of the vertical alignment line of the element, relative to itself. */ public int getLine(); - - /** - * Used to compute the position of the caret. - * @return Length (in characters) of the element. - */ - public int getLength(); } diff --git a/src/org/warp/picalculator/gui/graphicengine/Renderer.java b/src/org/warp/picalculator/gui/graphicengine/Renderer.java index 027ed361..d3f1984c 100644 --- a/src/org/warp/picalculator/gui/graphicengine/Renderer.java +++ b/src/org/warp/picalculator/gui/graphicengine/Renderer.java @@ -29,6 +29,12 @@ public interface Renderer { public void glFillColor(float x, float y, float width, float height); + public void glDrawCharLeft(int x, int y, char ch); + + public void glDrawCharCenter(int x, int y, char ch); + + public void glDrawCharRight(int x, int y, char ch); + public void glDrawStringLeft(float x, float y, String text); public void glDrawStringCenter(float x, float y, String text); diff --git a/src/org/warp/picalculator/gui/graphicengine/cpu/CPUEngine.java b/src/org/warp/picalculator/gui/graphicengine/cpu/CPUEngine.java index 5c4ce9cc..293a1fc2 100644 --- a/src/org/warp/picalculator/gui/graphicengine/cpu/CPUEngine.java +++ b/src/org/warp/picalculator/gui/graphicengine/cpu/CPUEngine.java @@ -45,10 +45,10 @@ public class CPUEngine implements GraphicEngine { g = new BufferedImage(ww, wh, BufferedImage.TYPE_INT_ARGB); INSTANCE.wasResized = false; } - + @Override public void create() { - INSTANCE = new SwingWindow(this, DisplayManager.getDrawable()); + INSTANCE = new SwingWindow(this); setResizable(Utils.debugOn & !Utils.debugThirdScreen); setDisplayMode(Main.screenSize[0], Main.screenSize[1]); INSTANCE.setVisible(true); @@ -86,6 +86,7 @@ public class CPUEngine implements GraphicEngine { @Override public void start(RenderingLoop d) { + INSTANCE.setRenderingLoop(d); Thread th = new Thread(() -> { try { double extratime = 0; @@ -409,6 +410,21 @@ public class CPUEngine implements GraphicEngine { currentSkin = null; } + @Override + public void glDrawCharLeft(int x, int y, char ch) { + glDrawStringLeft(x, y, ch+""); + } + + @Override + public void glDrawCharCenter(int x, int y, char ch) { + glDrawStringCenter(x, y, ch+""); + } + + @Override + public void glDrawCharRight(int x, int y, char ch) { + glDrawStringRight(x, y, ch+""); + } + } @Override diff --git a/src/org/warp/picalculator/gui/graphicengine/cpu/CPUFont.java b/src/org/warp/picalculator/gui/graphicengine/cpu/CPUFont.java index 77510ccb..70bd632a 100644 --- a/src/org/warp/picalculator/gui/graphicengine/cpu/CPUFont.java +++ b/src/org/warp/picalculator/gui/graphicengine/cpu/CPUFont.java @@ -161,9 +161,9 @@ public class CPUFont implements BinaryFont { @Override public int getStringWidth(String text) { - final int w = (charW + 1) * text.length(); + final int w = (charW) * text.length(); if (text.length() > 0) { - return w - 1; + return w; } else { return 0; } diff --git a/src/org/warp/picalculator/gui/graphicengine/cpu/CPUSkin.java b/src/org/warp/picalculator/gui/graphicengine/cpu/CPUSkin.java index ab6d24e2..3a5b5598 100644 --- a/src/org/warp/picalculator/gui/graphicengine/cpu/CPUSkin.java +++ b/src/org/warp/picalculator/gui/graphicengine/cpu/CPUSkin.java @@ -21,7 +21,7 @@ public class CPUSkin implements Skin { @Override public void load(String file) throws IOException { - final BufferedImage img = ImageIO.read(Main.instance.getClass().getResource("/"+file)); + final BufferedImage img = ImageIO.read(this.getClass().getResource("/"+file)); skinData = getMatrixOfImage(img); skinSize = new int[] { img.getWidth(), img.getHeight() }; } diff --git a/src/org/warp/picalculator/gui/graphicengine/cpu/SwingWindow.java b/src/org/warp/picalculator/gui/graphicengine/cpu/SwingWindow.java index 126a56bd..ac29e302 100644 --- a/src/org/warp/picalculator/gui/graphicengine/cpu/SwingWindow.java +++ b/src/org/warp/picalculator/gui/graphicengine/cpu/SwingWindow.java @@ -24,12 +24,11 @@ import org.warp.picalculator.gui.graphicengine.RenderingLoop; public class SwingWindow extends JFrame { private static final long serialVersionUID = 2945898937634075491L; public CustomCanvas c; - private static RenderingLoop d; + private RenderingLoop renderingLoop; public boolean wasResized = false; private final CPUEngine display; - public SwingWindow(CPUEngine disp, RenderingLoop d) { - SwingWindow.d = d; + public SwingWindow(CPUEngine disp) { display = disp; c = new CustomCanvas(); c.setDoubleBuffered(false); @@ -222,8 +221,12 @@ public class SwingWindow extends JFrame { public int getHeight() { return c.getHeight(); } + + public void setRenderingLoop(RenderingLoop renderingLoop) { + this.renderingLoop = renderingLoop; + } -// private static ArrayList mediaValori = new ArrayList(); +// private static ObjectArrayList mediaValori = new ObjectArrayList(); public class CustomCanvas extends JPanel { @@ -235,22 +238,24 @@ public class SwingWindow extends JFrame { @Override public void paintComponent(Graphics g) { // long time1 = System.nanoTime(); - d.refresh(); - - final int[] a = ((DataBufferInt) display.g.getRaster().getDataBuffer()).getData(); -// System.arraycopy(canvas2d, 0, a, 0, canvas2d.length); - CPUEngine.canvas2d = a; - g.clearRect(0, 0, display.size[0], display.size[1]); - g.drawImage(display.g, 0, 0, null); -// long time2 = System.nanoTime(); -// double timeDelta = ((double)(time2-time1))/1000000000d; -// double mediaAttuale = timeDelta; -// mediaValori.add(mediaAttuale); -// double somma = 0; -// for (Double val : mediaValori) { -// somma+=val; -// } -// System.out.println(somma/((double)mediaValori.size())); + if (renderingLoop != null) { + renderingLoop.refresh(); + + final int[] a = ((DataBufferInt) display.g.getRaster().getDataBuffer()).getData(); + // System.arraycopy(canvas2d, 0, a, 0, canvas2d.length); + CPUEngine.canvas2d = a; + g.clearRect(0, 0, display.size[0], display.size[1]); + g.drawImage(display.g, 0, 0, null); + // long time2 = System.nanoTime(); + // double timeDelta = ((double)(time2-time1))/1000000000d; + // double mediaAttuale = timeDelta; + // mediaValori.add(mediaAttuale); + // double somma = 0; + // for (Double val : mediaValori) { + // somma+=val; + // } + // System.out.println(somma/((double)mediaValori.size())); + } } } } diff --git a/src/org/warp/picalculator/gui/graphicengine/gpu/DeallocationHelper.java b/src/org/warp/picalculator/gui/graphicengine/gpu/DeallocationHelper.java index 232f29c4..80b53179 100644 --- a/src/org/warp/picalculator/gui/graphicengine/gpu/DeallocationHelper.java +++ b/src/org/warp/picalculator/gui/graphicengine/gpu/DeallocationHelper.java @@ -24,7 +24,7 @@ import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.nio.LongBuffer; import java.nio.ShortBuffer; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -267,7 +267,7 @@ public class DeallocationHelper { */ public DeallocationHelper(final boolean ignoreClassesAndFieldsHints) { super(); - final List buffersToDelete = new ArrayList<>(); + final List buffersToDelete = new ObjectArrayList<>(); /** * builds the map used to determine the names of the fields containing * the direct byte buffers. The direct read only buffers and the sliced @@ -342,7 +342,7 @@ public class DeallocationHelper { } // checks if these classes are in the class library if (!attachmentOrByteBufferFieldNameMap.isEmpty()) { - final List classnamesToRemove = new ArrayList<>(); + final List classnamesToRemove = new ObjectArrayList<>(); for (final String classname : attachmentOrByteBufferFieldNameMap.keySet()) { try { Class.forName(classname); @@ -365,7 +365,7 @@ public class DeallocationHelper { final Class bufferClass = Class.forName(classname); Field bufferField = null; Class bufferIntermediaryClass = bufferClass; - final List> intermediaryClassWithoutBufferList = new ArrayList<>(); + final List> intermediaryClassWithoutBufferList = new ObjectArrayList<>(); while (bufferIntermediaryClass != null) { try { bufferField = bufferIntermediaryClass.getDeclaredField(fieldname); @@ -454,7 +454,7 @@ public class DeallocationHelper { final LongBuffer littleEndianReadWriteDirectLongBuffer = ByteBuffer.allocateDirect(1).order(ByteOrder.LITTLE_ENDIAN).asLongBuffer(); final ShortBuffer littleEndianReadOnlyDirectShortBuffer = ByteBuffer.allocateDirect(1).order(ByteOrder.LITTLE_ENDIAN).asReadOnlyBuffer().asShortBuffer(); final ShortBuffer littleEndianReadWriteDirectShortBuffer = ByteBuffer.allocateDirect(1).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); - final List buffers = new ArrayList<>(); + final List buffers = new ObjectArrayList<>(); buffers.add(slicedBigEndianReadOnlyDirectByteBuffer); buffers.add(slicedBigEndianReadWriteDirectByteBuffer); buffers.add(bigEndianReadOnlyDirectCharBuffer); diff --git a/src/org/warp/picalculator/gui/graphicengine/gpu/GPUFont.java b/src/org/warp/picalculator/gui/graphicengine/gpu/GPUFont.java index 18733825..522192ba 100644 --- a/src/org/warp/picalculator/gui/graphicengine/gpu/GPUFont.java +++ b/src/org/warp/picalculator/gui/graphicengine/gpu/GPUFont.java @@ -50,10 +50,14 @@ public class GPUFont implements BinaryFont { final int[] indexes = new int[l]; final char[] chars = txt.toCharArray(); for (int i = 0; i < l; i++) { - indexes[i] = (chars[i] & 0xFFFF) - minCharIndex; + indexes[i] = getCharIndex(chars[i]); } return indexes; } + + public int getCharIndex(char ch) { + return (ch & 0xFFFF) - minCharIndex; + } private void genTexture(boolean[][] chars) { final double totalChars = maxCharIndex - minCharIndex; @@ -120,9 +124,9 @@ public class GPUFont implements BinaryFont { @Override public int getStringWidth(String text) { - final int w = (charW + 1) * text.length(); + final int w = (charW) * text.length(); if (text.length() > 0) { - return w - 1; + return w; } else { return 0; } diff --git a/src/org/warp/picalculator/gui/graphicengine/gpu/GPURenderer.java b/src/org/warp/picalculator/gui/graphicengine/gpu/GPURenderer.java index f388cf82..da347e7b 100644 --- a/src/org/warp/picalculator/gui/graphicengine/gpu/GPURenderer.java +++ b/src/org/warp/picalculator/gui/graphicengine/gpu/GPURenderer.java @@ -151,7 +151,7 @@ public class GPURenderer implements Renderer { for (int currentCharIndex = 0; currentCharIndex < txtLen; currentCharIndex++) { tableIndexX = txtArray[currentCharIndex] % currentFont.memoryWidthOfEachColumn; tableIndexY = (txtArray[currentCharIndex] - tableIndexX) / currentFont.memoryWidthOfEachColumn; - glFillRect(x + ((float)currentCharIndex) * ((float)(currentFont.charW + 1)), y, currentFont.charW, currentFont.charH, tableIndexX*currentFont.charW, tableIndexY*currentFont.charH, currentFont.charW, currentFont.charH); + glFillRect(x + ((float)currentCharIndex) * ((float)(currentFont.charW)), y, currentFont.charW, currentFont.charH, tableIndexX*currentFont.charW, tableIndexY*currentFont.charH, currentFont.charW, currentFont.charH); } } @@ -165,6 +165,24 @@ public class GPURenderer implements Renderer { glDrawStringLeft(x - currentFont.getStringWidth(text), y, text); } + @Override + public void glDrawCharLeft(int x, int y, char ch) { + int index = currentFont.getCharIndex(ch); + int tableIndexX = index % currentFont.memoryWidthOfEachColumn; + int tableIndexY = (index - tableIndexX) / currentFont.memoryWidthOfEachColumn; + glFillRect(x, y, currentFont.charW, currentFont.charH, tableIndexX*currentFont.charW, tableIndexY*currentFont.charH, currentFont.charW, currentFont.charH); + } + + @Override + public void glDrawCharCenter(int x, int y, char ch) { + glDrawCharLeft(x - (currentFont.charW / 2), y, ch); + } + + @Override + public void glDrawCharRight(int x, int y, char ch) { + glDrawCharLeft(x - currentFont.charW, y, ch); + } + @Override public BinaryFont getCurrentFont() { return currentFont; diff --git a/src/org/warp/picalculator/gui/math/Block.java b/src/org/warp/picalculator/gui/math/Block.java deleted file mode 100644 index c33f1316..00000000 --- a/src/org/warp/picalculator/gui/math/Block.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.warp.picalculator.gui.math; - -public abstract class Block { - /** - * - * @param x Position relative to the window. - * @param y Position relative to the window. - * @param c Parent container. - */ - public abstract void draw(int x, int y, Container c); -} diff --git a/src/org/warp/picalculator/gui/math/Container.java b/src/org/warp/picalculator/gui/math/Container.java deleted file mode 100644 index 4880de8d..00000000 --- a/src/org/warp/picalculator/gui/math/Container.java +++ /dev/null @@ -1,112 +0,0 @@ -package org.warp.picalculator.gui.math; - -import java.util.ArrayList; - -import org.warp.picalculator.device.graphicengine.Display; -import org.warp.picalculator.gui.graphicengine.GraphicEngine; -import org.warp.picalculator.gui.graphicengine.Renderer; - -public class Container implements GraphicalElement { - - private final int minWidth; - private final int minHeight; - private final ArrayList content; - private int width; - private int height; - private int line; - - public Container(int minWidth, int minHeight) { - this.minWidth = minWidth; - this.minHeight = minHeight; - this.content = new ArrayList<>(); - } - - public Container(int minWidth, int minHeight, ArrayList content) { - this.minWidth = minWidth; - this.minHeight = minHeight; - this.content = content; - } - - public void addBlock(Block b) { - content.add(b); - recomputeDimensions(); - } - - public void removeBlock(Block b) { - content.remove(b); - recomputeDimensions(); - } - - public void removeAt(int i) { - content.remove(i); - recomputeDimensions(); - } - - public Block getBlockAt(int i) { - return content.get(i); - } - - public void clear() { - content.clear(); - recomputeDimensions(); - } - - /** - * - * @param x Position relative to the window. - * @param y Position relative to the window. - * @param small size of the element. - * @param caretPos remaining positions of the caret. - * @return caretPos - currentElementLength - */ - public int draw(GraphicEngine g, int x, int y, boolean small, int caretPos) { - Renderer r = g.getRenderer(); - return caretPos; - - } - - @Override - public void recomputeDimensions() { - int w = 0; - int h = 0; - int l = 0; - - for (Block b : content) { - w += b.getWidth(); - } - - if (w > minWidth) { - width = w; - } else { - width = minWidth; - } - if (h > minHeight) { - height = h; - } else { - height = minHeight; - } - line = l; - } - - @Override - public int getWidth() { - return width; - } - - @Override - public int getHeight() { - return height; - } - - @Override - public int getLine() { - return line; - } - - @Override - public int getLength() { - // TODO Auto-generated method stub - return 0; - } - -} \ No newline at end of file diff --git a/src/org/warp/picalculator/gui/screens/MathInputScreen.java b/src/org/warp/picalculator/gui/screens/MathInputScreen.java index 6fe471a9..ba0b652a 100644 --- a/src/org/warp/picalculator/gui/screens/MathInputScreen.java +++ b/src/org/warp/picalculator/gui/screens/MathInputScreen.java @@ -2,7 +2,7 @@ package org.warp.picalculator.gui.screens; import java.io.PrintWriter; import java.io.StringWriter; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashSet; @@ -457,7 +457,7 @@ public class MathInputScreen extends Screen { newExpression = ""; firstStep = false; if (calc.f != null) { - calc.f = new ArrayList<>(); + calc.f = new ObjectArrayList<>(); } return true; } @@ -525,7 +525,7 @@ public class MathInputScreen extends Screen { } } - private ArrayList solveExpression(ArrayList f22) { + private ObjectArrayList solveExpression(ObjectArrayList f22) { try { try { return calc.solveExpression(f22); @@ -550,8 +550,8 @@ public class MathInputScreen extends Screen { try { try { showVariablesDialog(); - ArrayList results = new ArrayList<>(); - final ArrayList partialResults = new ArrayList<>(); + ObjectArrayList results = new ObjectArrayList<>(); + final ObjectArrayList partialResults = new ObjectArrayList<>(); for (final Function f : calc.f2) { if (f instanceof Equation) { DisplayManager.INSTANCE.setScreen(new SolveEquationScreen(this)); @@ -565,7 +565,7 @@ public class MathInputScreen extends Screen { partialResults.add(itm); } } - results = new ArrayList<>(partialResults); + results = new ObjectArrayList<>(partialResults); partialResults.clear(); } } @@ -582,7 +582,7 @@ public class MathInputScreen extends Screen { results.addAll(hs); calc.f2 = results; for (final Function rf : calc.f2) { - rf.recomputeDimensions(); + rf.recomputeDimensions(null); } } Utils.debug.println(calc.f2.toString()); @@ -612,7 +612,7 @@ public class MathInputScreen extends Screen { } } - final ArrayList results = solveExpression(calc.f); + final ObjectArrayList results = solveExpression(calc.f); if (results.size() == 0) { calc.resultsCount = 0; } else { @@ -625,7 +625,7 @@ public class MathInputScreen extends Screen { results.addAll(hs); calc.f2 = results; for (final Function rf : calc.f2) { - rf.recomputeDimensions(); + rf.recomputeDimensions(null); } } } catch (final Exception ex) { @@ -685,7 +685,7 @@ public class MathInputScreen extends Screen { public void showVariablesDialog(final Runnable runnable) { final Thread ct = new Thread(() -> { - final ArrayList knownVarsInFunctions = getKnownVariables(calc.f.toArray(new Function[calc.f.size()])); + final ObjectArrayList knownVarsInFunctions = getKnownVariables(calc.f.toArray(new Function[calc.f.size()])); for (final VariableValue f : calc.variablesValues) { if (knownVarsInFunctions.contains(f.v)) { knownVarsInFunctions.remove(f.v); @@ -727,8 +727,8 @@ public class MathInputScreen extends Screen { ct.start(); } - private ArrayList getKnownVariables(Function[] fncs) { - final ArrayList res = new ArrayList<>(); + private ObjectArrayList getKnownVariables(Function[] fncs) { + final ObjectArrayList res = new ObjectArrayList<>(); for (final Function f : fncs) { if (f instanceof FunctionOperator) { res.addAll(getKnownVariables(new Function[] { ((FunctionOperator) f).getParameter1(), ((FunctionOperator) f).getParameter2() })); diff --git a/src/org/warp/picalculator/math/FunctionDynamic.java b/src/org/warp/picalculator/math/FunctionDynamic.java index b4c8f51d..186ca418 100644 --- a/src/org/warp/picalculator/math/FunctionDynamic.java +++ b/src/org/warp/picalculator/math/FunctionDynamic.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.Arrays; import java.util.List; @@ -103,7 +103,7 @@ public abstract class FunctionDynamic implements Function { protected abstract boolean isSolvable(); @Override - public final ArrayList simplify() throws Error { + public final ObjectArrayList simplify() throws Error { boolean solved = true; Function[] fncs = getParameters(); for (Function f : fncs) { @@ -112,14 +112,14 @@ public abstract class FunctionDynamic implements Function { break; } } - ArrayList result = solved ? solve() : null; + ObjectArrayList result = solved ? solve() : null; if (result == null || result.isEmpty()) { - result = new ArrayList<>(); + result = new ObjectArrayList<>(); - final ArrayList> ln = new ArrayList<>(); + final ObjectArrayList> ln = new ObjectArrayList<>(); for (int i = 0; i < fncs.length; i++) { - ArrayList l = new ArrayList<>(); + ObjectArrayList l = new ObjectArrayList<>(); if (fncs[i].isSimplified()) { l.add(fncs[i]); } else { @@ -143,7 +143,7 @@ public abstract class FunctionDynamic implements Function { * @return The solved function. * @throws Error Errors during computation, like a/0 or similar. */ - protected abstract ArrayList solve() throws Error; + protected abstract ObjectArrayList solve() throws Error; @Override public MathContext getMathContext() { diff --git a/src/org/warp/picalculator/math/FunctionOperator.java b/src/org/warp/picalculator/math/FunctionOperator.java index aff12093..84368607 100644 --- a/src/org/warp/picalculator/math/FunctionOperator.java +++ b/src/org/warp/picalculator/math/FunctionOperator.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.Utils; @@ -114,15 +114,15 @@ public abstract class FunctionOperator implements Function { protected abstract boolean isSolvable(); @Override - public final ArrayList simplify() throws Error { + public final ObjectArrayList simplify() throws Error { final boolean solved = parameter1.isSimplified() & parameter2.isSimplified(); - ArrayList result = solved ? solve() : null;; + ObjectArrayList result = solved ? solve() : null;; if (result == null || result.isEmpty()) { - result = new ArrayList<>(); + result = new ObjectArrayList<>(); - final ArrayList l1 = new ArrayList<>(); - final ArrayList l2 = new ArrayList<>(); + final ObjectArrayList l1 = new ObjectArrayList<>(); + final ObjectArrayList l2 = new ObjectArrayList<>(); if (parameter1.isSimplified()) { l1.add(parameter1); } else { @@ -149,7 +149,7 @@ public abstract class FunctionOperator implements Function { * @return The solved function. * @throws Error Errors during computation, like a/0 or similar. */ - protected abstract ArrayList solve() throws Error; + protected abstract ObjectArrayList solve() throws Error; @Override public abstract FunctionOperator clone(); diff --git a/src/org/warp/picalculator/math/FunctionSingle.java b/src/org/warp/picalculator/math/FunctionSingle.java index e9ec6d52..0eb9678d 100644 --- a/src/org/warp/picalculator/math/FunctionSingle.java +++ b/src/org/warp/picalculator/math/FunctionSingle.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; @@ -26,7 +26,7 @@ public abstract class FunctionSingle implements Function { parameter = value; } - private final MathContext mathContext; + protected final MathContext mathContext; /** * Function parameter.
@@ -76,14 +76,14 @@ public abstract class FunctionSingle implements Function { } @Override - public final ArrayList simplify() throws Error { + public final ObjectArrayList simplify() throws Error { final boolean simplified = parameter.isSimplified(); - ArrayList result = simplified ? solve() : null; + ObjectArrayList result = simplified ? solve() : null; if (result == null || result.isEmpty()) { - result = new ArrayList<>(); + result = new ObjectArrayList<>(); - final ArrayList l1 = new ArrayList<>(); + final ObjectArrayList l1 = new ObjectArrayList<>(); if (parameter.isSimplified()) { l1.add(parameter); } else { @@ -104,7 +104,7 @@ public abstract class FunctionSingle implements Function { * @return The solved function. * @throws Error Errors during computation, like a/0 or similar. */ - protected abstract ArrayList solve() throws Error; + protected abstract ObjectArrayList solve() throws Error; @Override public boolean isSimplified() { diff --git a/src/org/warp/picalculator/math/MathContext.java b/src/org/warp/picalculator/math/MathContext.java index eb335446..b3644c7f 100644 --- a/src/org/warp/picalculator/math/MathContext.java +++ b/src/org/warp/picalculator/math/MathContext.java @@ -1,7 +1,6 @@ package org.warp.picalculator.math; import java.math.BigInteger; -import java.util.ArrayList; import java.util.List; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; @@ -12,19 +11,22 @@ import org.warp.picalculator.math.functions.Variable.VariableValue; import org.warp.picalculator.math.functions.equations.Equation; import org.warp.picalculator.math.functions.equations.EquationsSystem; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + + public class MathContext { public AngleMode angleMode = AngleMode.DEG; public boolean exactMode = false; - public ArrayList f; - public ArrayList f2; - public ArrayList variablesValues; + public ObjectArrayList f; + public ObjectArrayList f2; + public ObjectArrayList variablesValues; public int resultsCount; public MathContext() { - f = new ArrayList<>(); - f2 = new ArrayList<>(); - variablesValues = new ArrayList<>(); + f = new ObjectArrayList<>(); + f2 = new ObjectArrayList<>(); + variablesValues = new ObjectArrayList<>(); resultsCount = 0; } @@ -57,9 +59,9 @@ public class MathContext { } } - public ArrayList solveExpression(ArrayList input) throws Error { - ArrayList results = new ArrayList<>(); - final ArrayList partialResults = new ArrayList<>(); + public ObjectArrayList solveExpression(ObjectArrayList input) throws Error { + ObjectArrayList results = new ObjectArrayList<>(); + final ObjectArrayList partialResults = new ObjectArrayList<>(); for (final Function f : input) { if (f instanceof Equation) { throw new IllegalArgumentException("Not an expression!"); @@ -79,7 +81,7 @@ public class MathContext { partialResults.add(itm); } } - results = new ArrayList<>(partialResults); + results = new ObjectArrayList<>(partialResults); partialResults.clear(); } } @@ -93,15 +95,15 @@ public class MathContext { public void init() { if (f == null & f2 == null) { - f = new ArrayList<>(); - f2 = new ArrayList<>(); - variablesValues = new ArrayList<>(); + f = new ObjectArrayList<>(); + f2 = new ObjectArrayList<>(); + variablesValues = new ObjectArrayList<>(); resultsCount = 0; } } public void parseInputString(String eqn) throws Error { - final ArrayList fncs = new ArrayList<>(); + final ObjectArrayList fncs = new ObjectArrayList<>(); if (eqn.length() > 0) { try { fncs.add(parseString(eqn.replace("sqrt", MathematicalSymbols.SQUARE_ROOT).replace("^", MathematicalSymbols.POWER))); @@ -115,7 +117,7 @@ public class MathContext { /*public void solve(EquationScreen equationScreen, char letter) throws Error { if (Calculator.currentSession == 0 && Calculator.sessions[0] instanceof EquationScreen) { EquationScreen es = (EquationScreen) Calculator.sessions[0]; - ArrayList f = es.f; + ObjectArrayList f = es.f; if (f instanceof Equation) { List results = ((Equation)f).solve(letter); Collections.reverse(results); diff --git a/src/org/warp/picalculator/math/MathematicalSymbols.java b/src/org/warp/picalculator/math/MathematicalSymbols.java index 1ff4e60a..61ead678 100644 --- a/src/org/warp/picalculator/math/MathematicalSymbols.java +++ b/src/org/warp/picalculator/math/MathematicalSymbols.java @@ -5,60 +5,53 @@ import static org.warp.picalculator.Utils.concat; import org.warp.picalculator.Utils; public class MathematicalSymbols { - public static final String SUM = "+"; - public static final String SUM_SUBTRACTION = "±"; - public static final String SUBTRACTION = "−"; - public static final String MINUS = "-"; - public static final String MULTIPLICATION = "*"; - public static final String DIVISION = "/"; - public static final String NTH_ROOT = "√"; - public static final String SQUARE_ROOT = "Ⓐ"; - public static final String PARENTHESIS_OPEN = "("; - public static final String PARENTHESIS_CLOSE = ")"; - public static final String POWER = "Ⓑ"; - public static final String EQUATION = "="; - public static final String SYSTEM = "{"; - public static final String SINE = "Ⓒ"; - public static final String COSINE = "Ⓓ"; - public static final String TANGENT = "Ⓔ"; - public static final String ARC_SINE = "Ⓕ"; - public static final String ARC_COSINE = "Ⓖ"; - public static final String ARC_TANGENT = "Ⓗ"; - public static final String PI = "π"; + public static final char SUM = '+'; + public static final char SUM_SUBTRACTION = '±'; + public static final char SUBTRACTION = '−'; + public static final char MINUS = '-'; + public static final char MULTIPLICATION = '*'; + public static final char DIVISION = '/'; + public static final char NTH_ROOT = '√'; + public static final char SQUARE_ROOT = 'Ⓐ'; + public static final char PARENTHESIS_OPEN = '('; + public static final char PARENTHESIS_CLOSE = ')'; + public static final char POWER = 'Ⓑ'; + public static final char EQUATION = '='; + public static final char SYSTEM = '{'; + public static final char SINE = 'Ⓒ'; + public static final char COSINE = 'Ⓓ'; + public static final char TANGENT = 'Ⓔ'; + public static final char ARC_SINE = 'Ⓕ'; + public static final char ARC_COSINE = 'Ⓖ'; + public static final char ARC_TANGENT = 'Ⓗ'; + public static final char PI = 'π'; - public static final String[] functions() { - return concat(functionsNSN(), functionsSN()); - } - public static final String[] functionsNSN() { - return new String[] { NTH_ROOT, POWER }; - } + public static final char[] functionsNSN = new char[] { NTH_ROOT, POWER }; - public static final String[] functionsSN() { - return new String[] { SQUARE_ROOT, MINUS, SINE, COSINE, TANGENT, ARC_SINE, ARC_COSINE, ARC_TANGENT }; - } + public static final char[] functionsSN = new char[] { SQUARE_ROOT, MINUS, SINE, COSINE, TANGENT, ARC_SINE, ARC_COSINE, ARC_TANGENT }; - public static final String[] signums(boolean withMultiplication) { - String[] ret = new String[] { SUM, SUM_SUBTRACTION, SUBTRACTION, DIVISION }; + public static final char[] functions = concat(functionsNSN, functionsSN); + + private static final char[] signumsWithoutMultiplication = new char[] { SUM, SUM_SUBTRACTION, SUBTRACTION, DIVISION }; + private static final char[] signumsWithMultiplication = Utils.add(signumsWithoutMultiplication, MULTIPLICATION); + + public static final char[] signums(boolean withMultiplication) { if (withMultiplication) { - ret = Utils.add(ret, MULTIPLICATION); + return signumsWithMultiplication; } - return ret; + return signumsWithoutMultiplication; } - public static final String[] parentheses() { - return new String[] { PARENTHESIS_OPEN, PARENTHESIS_CLOSE }; - } + public static final char[] parentheses = new char[] { PARENTHESIS_OPEN, PARENTHESIS_CLOSE }; - public static String[] variables() { - return new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "ⓧ", "Ⓨ", "Z", PI }; - } + public static final char[] variables = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'ⓧ', 'Ⓨ', 'Z', PI }; - public static String[] genericSyntax() { - return new String[] { SYSTEM, EQUATION }; - } + public static final char[] genericSyntax = new char[] { SYSTEM, EQUATION }; public static String getGraphicRepresentation(String string) { return string.replace("Ⓑ", "^").replace("Ⓒ", "SIN").replace("Ⓓ", "COS").replace("Ⓔ", "TAN").replace("Ⓕ", "ASIN").replace("Ⓖ", "ACOS").replace("Ⓗ", "ATAN"); } + + public static final char[] numbers = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; } diff --git a/src/org/warp/picalculator/math/SolveMethod.java b/src/org/warp/picalculator/math/SolveMethod.java index a075b139..b79ec8ba 100644 --- a/src/org/warp/picalculator/math/SolveMethod.java +++ b/src/org/warp/picalculator/math/SolveMethod.java @@ -1,11 +1,11 @@ package org.warp.picalculator.math; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.math.functions.equations.Equation; public interface SolveMethod { public static final SolveMethod[] techniques = new SolveMethod[] {}; - public abstract ArrayList solve(Equation equation); + public abstract ObjectArrayList solve(Equation equation); } diff --git a/src/org/warp/picalculator/math/functions/Division.java b/src/org/warp/picalculator/math/functions/Division.java index 549f41ff..f0b44106 100644 --- a/src/org/warp/picalculator/math/functions/Division.java +++ b/src/org/warp/picalculator/math/functions/Division.java @@ -1,12 +1,12 @@ package org.warp.picalculator.math.functions; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.Utils; import org.warp.picalculator.gui.DisplayManager; +import org.warp.picalculator.gui.expression.GraphicalElement; import org.warp.picalculator.gui.graphicengine.cpu.CPUEngine; -import org.warp.picalculator.gui.math.GraphicalElement; import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.Function; import org.warp.picalculator.math.FunctionOperator; @@ -61,10 +61,10 @@ public class Division extends FunctionOperator { } @Override - public ArrayList solve() throws Error { + public ObjectArrayList solve() throws Error { Function variable1 = getParameter1(); Function variable2 = getParameter2(); - ArrayList result = new ArrayList<>(); + ObjectArrayList result = new ObjectArrayList<>(); if (FractionsRule1.compare(this)) { result = FractionsRule1.execute(this); } else if (FractionsRule2.compare(this)) { diff --git a/src/org/warp/picalculator/math/functions/EmptyNumber.java b/src/org/warp/picalculator/math/functions/EmptyNumber.java index f9ab9ce7..805802bf 100644 --- a/src/org/warp/picalculator/math/functions/EmptyNumber.java +++ b/src/org/warp/picalculator/math/functions/EmptyNumber.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.List; import org.warp.picalculator.Error; @@ -20,7 +20,7 @@ public class EmptyNumber implements Function { @Override - public ArrayList simplify() throws Error { + public ObjectArrayList simplify() throws Error { // TODO Auto-generated method stub return null; } diff --git a/src/org/warp/picalculator/math/functions/Expression.java b/src/org/warp/picalculator/math/functions/Expression.java index b82fb8f7..6aecf16c 100644 --- a/src/org/warp/picalculator/math/functions/Expression.java +++ b/src/org/warp/picalculator/math/functions/Expression.java @@ -4,7 +4,7 @@ import static org.warp.picalculator.Utils.ArrayToRegex; import static org.warp.picalculator.Utils.concat; import java.math.BigDecimal; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -141,7 +141,7 @@ public class Expression extends FunctionDynamic { } // Rimuovi i + in eccesso - pattern = Pattern.compile("[" + ArrayToRegex(Utils.add(concat(MathematicalSymbols.signums(true), MathematicalSymbols.functions()), "(")) + "]\\+[^" + ArrayToRegex(concat(concat(MathematicalSymbols.signums(true), MathematicalSymbols.functions()), new String[] { "(", ")" })) + "]+?[" + ArrayToRegex(concat(MathematicalSymbols.signums(true), MathematicalSymbols.functions())) + "]|[" + ArrayToRegex(concat(MathematicalSymbols.signums(true), MathematicalSymbols.functions())) + "]+?\\+[^" + ArrayToRegex(concat(concat(MathematicalSymbols.signums(true), MathematicalSymbols.functions()), new String[] { "(", ")" })) + "]"); + pattern = Pattern.compile("[" + ArrayToRegex(Utils.add(concat(MathematicalSymbols.signums(true), MathematicalSymbols.functions), '(')) + "]\\+[^" + ArrayToRegex(concat(concat(MathematicalSymbols.signums(true), MathematicalSymbols.functions), new char[] { '(', ')' })) + "]+?[" + ArrayToRegex(concat(MathematicalSymbols.signums(true), MathematicalSymbols.functions)) + "]|[" + ArrayToRegex(concat(MathematicalSymbols.signums(true), MathematicalSymbols.functions)) + "]+?\\+[^" + ArrayToRegex(concat(concat(MathematicalSymbols.signums(true), MathematicalSymbols.functions), new char[] { '(', ')' })) + "]"); matcher = pattern.matcher(processExpression); symbolsChanged = false; while (matcher.find()) { @@ -152,14 +152,14 @@ public class Expression extends FunctionDynamic { } // Correggi i segni - in − - processExpression = processExpression.replace("-", MathematicalSymbols.SUBTRACTION); + processExpression = processExpression.replace('-', MathematicalSymbols.SUBTRACTION); // Correggi i segni − dopo di espressioni o funzioni SN in - - pattern = Pattern.compile("[" + Utils.ArrayToRegex(concat(concat(MathematicalSymbols.functions(), new String[] { MathematicalSymbols.PARENTHESIS_OPEN }), MathematicalSymbols.signums(true))) + "]" + MathematicalSymbols.SUBTRACTION); + pattern = Pattern.compile("[" + Utils.ArrayToRegex(concat(concat(MathematicalSymbols.functions, new char[] { MathematicalSymbols.PARENTHESIS_OPEN }), MathematicalSymbols.signums(true))) + "]" + MathematicalSymbols.SUBTRACTION); matcher = pattern.matcher(processExpression); while (matcher.find()) { symbolsChanged = true; - final String correzione = MathematicalSymbols.MINUS; + final char correzione = MathematicalSymbols.MINUS; processExpression = processExpression.substring(0, matcher.start(0) + 1) + correzione + processExpression.substring(matcher.start(0) + 2, processExpression.length()); matcher = pattern.matcher(processExpression); } @@ -196,11 +196,11 @@ public class Expression extends FunctionDynamic { String beforeexp = processExpression.substring(0, matcher.start(0)); final String newexp = matcher.group(0).substring(1, matcher.group(0).length() - 1); String afterexp = processExpression.substring(matcher.start(0) + matcher.group(0).length(), processExpression.length()); - if (Pattern.compile("[^\\-" + Utils.ArrayToRegex(Utils.add(concat(MathematicalSymbols.functions(), concat(MathematicalSymbols.signums(true), MathematicalSymbols.genericSyntax())), "(")) + "]$").matcher(beforeexp).find()) { + if (Pattern.compile("[^\\-" + Utils.ArrayToRegex(Utils.add(concat(MathematicalSymbols.functions, concat(MathematicalSymbols.signums(true), MathematicalSymbols.genericSyntax)), '(')) + "]$").matcher(beforeexp).find()) { // Se la stringa precedente finisce con un numero beforeexp += MathematicalSymbols.MULTIPLICATION; } - if (Pattern.compile("^[^\\-" + Utils.ArrayToRegex(Utils.add(concat(MathematicalSymbols.functions(), concat(MathematicalSymbols.signums(true), MathematicalSymbols.genericSyntax())), ")")) + "]").matcher(afterexp).find()) { + if (Pattern.compile("^[^\\-" + Utils.ArrayToRegex(Utils.add(concat(MathematicalSymbols.functions, concat(MathematicalSymbols.signums(true), MathematicalSymbols.genericSyntax)), ')')) + "]").matcher(afterexp).find()) { // Se la stringa successiva inizia con un numero afterexp = MathematicalSymbols.MULTIPLICATION + afterexp; } @@ -222,10 +222,10 @@ public class Expression extends FunctionDynamic { Expression imputRawParenthesis = new Expression(root); imputRawParenthesis = (Expression) imputRawParenthesis.setParameters(new Function[] {}); String tmp = ""; - final String[] functions = concat(concat(concat(concat(MathematicalSymbols.functions(), MathematicalSymbols.parentheses()), MathematicalSymbols.signums(true)), MathematicalSymbols.variables()), MathematicalSymbols.genericSyntax()); + final char[] functions = concat(concat(concat(concat(MathematicalSymbols.functions, MathematicalSymbols.parentheses), MathematicalSymbols.signums(true)), MathematicalSymbols.variables), MathematicalSymbols.genericSyntax); for (int i = 0; i < processExpression.length(); i++) { // Per ogni carattere cerca se è un numero o una funzione: - final String charI = processExpression.charAt(i) + ""; + final char charI = processExpression.charAt(i); if (Utils.isInArray(charI, functions)) { // Finds the type of function fron the following list @@ -291,14 +291,14 @@ public class Expression extends FunctionDynamic { } else if (jumps > 0) { jumps -= 1; } else if (jumps < 0) { - throw new Error(Errors.UNBALANCED_BRACKETS); + throw new Error(Errors.UNBALANCED_STACK); } } else if ((processExpression.charAt(i2) + "").equals(MathematicalSymbols.PARENTHESIS_OPEN)) { jumps += 1; } } if (endIndex == -1 || endIndex < startIndex) { - throw new Error(Errors.UNBALANCED_BRACKETS); + throw new Error(Errors.UNBALANCED_STACK); } startIndex += 1; i = startIndex; @@ -311,11 +311,11 @@ public class Expression extends FunctionDynamic { f = new Expression(root, tmpExpr, debugSpaces, false); break; default: - if (Utils.isInArray(charI, MathematicalSymbols.variables())) { + if (Utils.isInArray(charI, MathematicalSymbols.variables)) { f = new Variable(root, charI, Variable.V_TYPE.UNKNOWN); } else { - if (charI == "(" || charI == ")") { - throw new Error(Errors.UNBALANCED_BRACKETS); + if (charI == '(' || charI == ')') { + throw new Error(Errors.UNBALANCED_STACK); } else { System.err.println("Unexpected character while parsing expression: " + charI); throw new Error(Errors.SYNTAX_ERROR); @@ -368,7 +368,7 @@ public class Expression extends FunctionDynamic { tmp = ""; } else { try { - if (charI.equals("-") == false && charI.equals(".") == false) { + if (charI != '-' && charI != '.') { new BigDecimal(tmp + charI); } // Se il carattere è un numero intero, un segno @@ -415,7 +415,7 @@ public class Expression extends FunctionDynamic { Utils.debug.println(debugSpaces + "•Pushing classes..."); final Function[] oldFunctionsArray = imputRawParenthesis.getParameters(); - final ArrayList oldFunctionsList = new ArrayList<>(); + final ObjectArrayList oldFunctionsList = new ObjectArrayList<>(); for (int i = 0; i < oldFunctionsArray.length; i++) { Function funzione = oldFunctionsArray[i]; if (funzione != null) { @@ -570,8 +570,8 @@ public class Expression extends FunctionDynamic { } @Override - public ArrayList solve() throws Error { - final ArrayList ret = new ArrayList<>(); + public ObjectArrayList solve() throws Error { + final ObjectArrayList ret = new ObjectArrayList<>(); if (getParametersLength() == 1) { if (getParameter(0).isSimplified() || !parenthesisNeeded()) { ret.add(getParameter(0)); @@ -629,7 +629,11 @@ public class Expression extends FunctionDynamic { String s = "("; if (functions.length > 0) { for (Function f : functions) { - s+=f.toString()+","; + if (f == null) { + s+="[null],"; + } else { + s+=f.toString()+","; + } } s = s.substring(0, s.length()-1); } diff --git a/src/org/warp/picalculator/math/functions/Joke.java b/src/org/warp/picalculator/math/functions/Joke.java index 312125f3..b828b4be 100644 --- a/src/org/warp/picalculator/math/functions/Joke.java +++ b/src/org/warp/picalculator/math/functions/Joke.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.List; import org.warp.picalculator.Error; @@ -27,7 +27,7 @@ public class Joke implements Function { } @Override - public ArrayList simplify() throws Error { + public ObjectArrayList simplify() throws Error { return null; } diff --git a/src/org/warp/picalculator/math/functions/Multiplication.java b/src/org/warp/picalculator/math/functions/Multiplication.java index 8dffd225..283f89d6 100644 --- a/src/org/warp/picalculator/math/functions/Multiplication.java +++ b/src/org/warp/picalculator/math/functions/Multiplication.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -62,8 +62,8 @@ public class Multiplication extends FunctionOperator { } @Override - public ArrayList solve() throws Error { - ArrayList result = new ArrayList<>(); + public ObjectArrayList solve() throws Error { + ObjectArrayList result = new ObjectArrayList<>(); if (SyntaxRule1.compare(this)) { result = SyntaxRule1.execute(this); } else if (NumberRule1.compare(this)) { diff --git a/src/org/warp/picalculator/math/functions/Negative.java b/src/org/warp/picalculator/math/functions/Negative.java index d4207281..0605820f 100644 --- a/src/org/warp/picalculator/math/functions/Negative.java +++ b/src/org/warp/picalculator/math/functions/Negative.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; @@ -20,29 +20,9 @@ public class Negative extends FunctionSingle { super(root, value); } - @Override - public Function NewInstance(MathContext root, Function value) { - return new Negative(root, value); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.MINUS; - } - - @Override - public void recomputeDimensions() { - variable.setSmall(small); - variable.recomputeDimensions(); - - height = getParameter().getHeight(); - width = Utils.getFont(small).getCharacterWidth() /* Width of - */ + getParameter().getWidth(); - line = getParameter().getLine(); - } - @Override protected boolean isSolvable() { - if (variable instanceof Number) { + if (parameter instanceof Number) { return true; } if (ExpandRule1.compare(this)) { @@ -55,16 +35,16 @@ public class Negative extends FunctionSingle { } @Override - public ArrayList solve() throws Error { - if (variable == null) { + public ObjectArrayList solve() throws Error { + if (parameter == null) { throw new Error(Errors.SYNTAX_ERROR); } - ArrayList result = new ArrayList<>(); + ObjectArrayList result = new ObjectArrayList<>(); if (ExpandRule1.compare(this)) { result = ExpandRule1.execute(this); } else if (ExpandRule5.compare(this)) { result = ExpandRule5.execute(this); - } else if (variable.isSimplified()) { + } else if (parameter.isSimplified()) { try { final Number var = (Number) getParameter(); result.add(var.multiply(new Number(mathContext, "-1"))); @@ -76,11 +56,11 @@ public class Negative extends FunctionSingle { throw new Error(Errors.NUMBER_TOO_SMALL); } } else { - final ArrayList l1 = new ArrayList<>(); - if (variable.isSimplified()) { - l1.add(variable); + final ObjectArrayList l1 = new ObjectArrayList<>(); + if (parameter.isSimplified()) { + l1.add(parameter); } else { - l1.addAll(variable.simplify()); + l1.addAll(parameter.simplify()); } for (final Function f : l1) { @@ -90,26 +70,16 @@ public class Negative extends FunctionSingle { return result; } - @Override - public int getWidth() { - return width; - } - - @Override - public int getHeight() { - return height; - } - - @Override - public int getLine() { - return line; - } - @Override public boolean equals(Object o) { if (o instanceof Negative) { - return ((Negative) o).variable.equals(variable); + return ((Negative) o).getParameter().equals(parameter); } return false; } + + @Override + public Negative clone() { + return new Negative(mathContext, parameter); + } } diff --git a/src/org/warp/picalculator/math/functions/Number.java b/src/org/warp/picalculator/math/functions/Number.java index f8bc20e3..2159920a 100644 --- a/src/org/warp/picalculator/math/functions/Number.java +++ b/src/org/warp/picalculator/math/functions/Number.java @@ -2,7 +2,7 @@ package org.warp.picalculator.math.functions; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.LinkedList; import java.util.List; @@ -121,7 +121,7 @@ public class Number implements Function { @Override public List simplify() throws Error { - final List result = new ArrayList<>(); + final List result = new ObjectArrayList<>(); if (root.exactMode) { Number divisor = new Number(root, BigInteger.TEN.pow(getNumberOfDecimalPlaces())); Number numb = new Number(root, term.multiply(divisor.term)); diff --git a/src/org/warp/picalculator/math/functions/Power.java b/src/org/warp/picalculator/math/functions/Power.java index 8a758107..b4b158ee 100644 --- a/src/org/warp/picalculator/math/functions/Power.java +++ b/src/org/warp/picalculator/math/functions/Power.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -55,8 +55,8 @@ public class Power extends FunctionOperator { } @Override - public ArrayList solve() throws Error { - final ArrayList result = new ArrayList<>(); + public ObjectArrayList solve() throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); if (UndefinedRule1.compare(this)) { result.addAll(UndefinedRule1.execute(this)); } else if (ExponentRule1.compare(this)) { diff --git a/src/org/warp/picalculator/math/functions/Root.java b/src/org/warp/picalculator/math/functions/Root.java index 57159435..6cafb7ed 100644 --- a/src/org/warp/picalculator/math/functions/Root.java +++ b/src/org/warp/picalculator/math/functions/Root.java @@ -2,7 +2,7 @@ package org.warp.picalculator.math.functions; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.gui.DisplayManager; @@ -17,29 +17,6 @@ public class Root extends FunctionOperator { super(root, value1, value2); } - @Override - protected Function NewInstance(MathContext root, Function value1, Function value2) { - return new Root(root, value1, value2); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.NTH_ROOT; - } - - @Override - public void generateGraphics() { - parameter1.setSmall(true); - parameter1.recomputeDimensions(); - - parameter2.setSmall(small); - parameter2.recomputeDimensions(); - - width = 1 + parameter1.getWidth() + 2 + parameter2.getWidth() + 2; - height = parameter1.getHeight() + parameter2.getHeight() - 2; - line = parameter1.getHeight() + parameter2.getLine() - 2; - } - @Override protected boolean isSolvable() { if (parameter1 instanceof Number & parameter2 instanceof Number) { @@ -65,8 +42,8 @@ public class Root extends FunctionOperator { } @Override - public ArrayList solve() throws Error { - final ArrayList result = new ArrayList<>(); + public ObjectArrayList solve() throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); if (mathContext.exactMode) { if (parameter1 instanceof Number && ((Number) parameter1).equals(new Number(mathContext, 2))) { result.add(new RootSquare(mathContext, parameter2)); @@ -83,52 +60,19 @@ public class Root extends FunctionOperator { } return result; } - - @Override - public void draw(int x, int y, boolean small, int caretPos) { -// glColor3f(0, 255, 0); -// glFillRect(x,y,width,height); -// glColor3f(0, 0, 0); - - final int w1 = getVariable2().getWidth(); - final int h1 = getVariable2().getHeight(); - final int w2 = getParameter1().getWidth(); - final int h2 = getParameter1().getHeight(); - final int height = getHeight(); - final int hh = (int) Math.ceil((double) h1 / 2); - - getParameter1().draw(x + 1, y, null, null); - getVariable2().draw(x + 1 + w2 + 2, y + h2 - 2, null, null); - - DisplayManager.renderer.glDrawLine(x + 1 + w2 - 2, y + height - 2, x + 1 + w2 - 2, y + height - 2); - DisplayManager.renderer.glDrawLine(x + 1 + w2 - 1, y + height - 1, x + 1 + w2 - 1, y + height - 1); - DisplayManager.renderer.glDrawLine(x + 1 + w2 - 0, y + height - 0, x + 1 + w2 - 0, y + height - 0); - DisplayManager.renderer.glDrawLine(x + 1 + w2, y + height - 1 - hh, x + 1 + w2, y + height - 1); - DisplayManager.renderer.glDrawLine(x + 1 + w2 + 1, y + height - 2 - h1, x + 1 + w2 + 1, y + height - 1 - hh - 1); - DisplayManager.renderer.glDrawLine(x + 1 + w2 + 1, y + height - h1 - 2, x + 1 + w2 + 2 + w1 + 1, y + height - h1 - 2); - } - - @Override - public int getWidth() { - return width; - } - - @Override - public int getHeight() { - return height; - } - - @Override - public int getLine() { - return line; - } - + @Override public boolean equals(Object o) { if (o instanceof Root) { final FunctionOperator f = (FunctionOperator) o; - return parameter1.equals(f.parameter1) && parameter2.equals(f.parameter2); + return parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2()); } return false; } + + @Override + public Root clone() { + return new Root(mathContext, parameter1, parameter2); + } + } diff --git a/src/org/warp/picalculator/math/functions/RootSquare.java b/src/org/warp/picalculator/math/functions/RootSquare.java index 1896fe50..aadc44b5 100644 --- a/src/org/warp/picalculator/math/functions/RootSquare.java +++ b/src/org/warp/picalculator/math/functions/RootSquare.java @@ -1,7 +1,7 @@ package org.warp.picalculator.math.functions; import java.math.BigInteger; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.Utils; @@ -16,38 +16,18 @@ public class RootSquare extends FunctionSingle { super(root, value); } - @Override - public Function NewInstance(MathContext root, Function value) { - return new RootSquare(root, value); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.SQUARE_ROOT; - } - - @Override - public void recomputeDimensions() { - variable.setSmall(small); - variable.recomputeDimensions(); - - height = getParameter().getHeight() + 2; - width = 1 + 4 + getParameter().getWidth() + 1; - line = getParameter().getLine() + 2; - } - @Override protected boolean isSolvable() { - if (variable instanceof Number) { + if (parameter instanceof Number) { if (mathContext.exactMode == false) { return true; } try { Number exponent = new Number(mathContext, BigInteger.ONE); exponent = exponent.divide(new Number(mathContext, 2)); - final Number resultVal = ((Number) variable).pow(exponent); + final Number resultVal = ((Number) parameter).pow(exponent); final Number originalVariable = resultVal.pow(new Number(mathContext, 2)); - if (originalVariable.equals(variable)) { + if (originalVariable.equals(parameter)) { return true; } } catch (Exception | Error ex) { @@ -58,50 +38,31 @@ public class RootSquare extends FunctionSingle { } @Override - public ArrayList solve() throws Error { - final ArrayList result = new ArrayList<>(); + public ObjectArrayList solve() throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); if (mathContext.exactMode) { Number exponent = new Number(mathContext, BigInteger.ONE); exponent = exponent.divide(new Number(mathContext, 2)); - result.add(((Number) variable).pow(exponent)); + result.add(((Number) parameter).pow(exponent)); } else { final Number exp = new Number(mathContext, 2); - final Number numb = (Number) variable; + final Number numb = (Number) parameter; result.add(numb.pow(new Number(mathContext, 1).divide(exp))); } return result; } - @Override - public int draw(int x, int y, boolean small, int caretPos) { -// glColor3f(0, 255, 0); -// glFillRect(x,y,width,height); -// glColor3f(0, 0, 0); - - Utils.writeSquareRoot(getParameter(), x, y, small); - } - - @Override - public int getWidth() { - return width; - } - - @Override - public int getHeight() { - return height; - } - - @Override - public int getLine() { - return line; - } - @Override public boolean equals(Object o) { if (o instanceof RootSquare) { - return ((RootSquare) o).variable.equals(variable); + return ((RootSquare) o).getParameter().equals(parameter); } return false; } + + @Override + public RootSquare clone() { + return new RootSquare(mathContext, parameter); + } } diff --git a/src/org/warp/picalculator/math/functions/Subtraction.java b/src/org/warp/picalculator/math/functions/Subtraction.java index 6ec25c9e..d5e08774 100644 --- a/src/org/warp/picalculator/math/functions/Subtraction.java +++ b/src/org/warp/picalculator/math/functions/Subtraction.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -22,16 +22,6 @@ public class Subtraction extends FunctionOperator { super(root, value1, value2); } - @Override - protected Function NewInstance(MathContext root, Function value1, Function value2) { - return new Subtraction(root, value1, value2); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.SUBTRACTION; - } - @Override protected boolean isSolvable() { if (parameter1 instanceof Number & parameter2 instanceof Number) { @@ -65,8 +55,8 @@ public class Subtraction extends FunctionOperator { } @Override - public ArrayList solve() throws Error { - ArrayList result = new ArrayList<>(); + public ObjectArrayList solve() throws Error { + ObjectArrayList result = new ObjectArrayList<>(); if (VariableRule1.compare(this)) { result = VariableRule1.execute(this); } else if (VariableRule2.compare(this)) { @@ -93,8 +83,14 @@ public class Subtraction extends FunctionOperator { public boolean equals(Object o) { if (o instanceof Subtraction) { final FunctionOperator f = (FunctionOperator) o; - return parameter1.equals(f.parameter1) && parameter2.equals(f.parameter2); + return parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2()); } return false; } + + @Override + public Subtraction clone() { + return new Subtraction(mathContext, parameter1, parameter2); + } + } \ No newline at end of file diff --git a/src/org/warp/picalculator/math/functions/Sum.java b/src/org/warp/picalculator/math/functions/Sum.java index 525cd99b..749e2d9e 100644 --- a/src/org/warp/picalculator/math/functions/Sum.java +++ b/src/org/warp/picalculator/math/functions/Sum.java @@ -1,7 +1,7 @@ package org.warp.picalculator.math.functions; import java.math.BigDecimal; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; @@ -59,11 +59,11 @@ public class Sum extends FunctionOperator { } @Override - public ArrayList solve() throws Error { + public ObjectArrayList solve() throws Error { if (parameter1 == null || parameter2 == null) { throw new Error(Errors.SYNTAX_ERROR); } - ArrayList result = new ArrayList<>(); + ObjectArrayList result = new ObjectArrayList<>(); if (SyntaxRule2.compare(this)) { result = SyntaxRule2.execute(this); } else if (VariableRule1.compare(this)) { diff --git a/src/org/warp/picalculator/math/functions/SumSubtraction.java b/src/org/warp/picalculator/math/functions/SumSubtraction.java index dd017aa2..ab3d171b 100644 --- a/src/org/warp/picalculator/math/functions/SumSubtraction.java +++ b/src/org/warp/picalculator/math/functions/SumSubtraction.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; @@ -22,16 +22,6 @@ public class SumSubtraction extends FunctionOperator { super(root, value1, value2); } - @Override - protected Function NewInstance(MathContext root, Function value1, Function value2) { - return new SumSubtraction(root, value1, value2); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.SUM_SUBTRACTION; - } - @Override protected boolean isSolvable() { if (parameter1 instanceof Number & parameter2 instanceof Number) { @@ -53,11 +43,11 @@ public class SumSubtraction extends FunctionOperator { } @Override - public ArrayList solve() throws Error { + public ObjectArrayList solve() throws Error { if (parameter1 == null || parameter2 == null) { throw new Error(Errors.SYNTAX_ERROR); } - ArrayList result = new ArrayList<>(); + ObjectArrayList result = new ObjectArrayList<>(); if (NumberRule3.compare(this)) { result = NumberRule3.execute(this); } else if (ExpandRule1.compare(this)) { @@ -73,56 +63,18 @@ public class SumSubtraction extends FunctionOperator { return result; } - @Override - public void generateGraphics() { - parameter1.setSmall(small); - parameter1.recomputeDimensions(); - - parameter2.setSmall(small); - parameter2.recomputeDimensions(); - - width = calcWidth(); - height = calcHeight(); - line = calcLine(); - } - - @Override - public void draw(int x, int y, boolean small, int caretPos) { -// glColor3f(127, 127-50+new Random().nextInt(50), 255); -// glFillRect(x,y,width,height); -// glColor3f(0, 0, 0); - - final int ln = getLine(); - int dx = 0; - parameter1.draw(dx + x, ln - parameter1.getLine() + y, null, null); - dx += parameter1.getWidth(); - Utils.getFont(small).use(DisplayManager.engine); - dx += 1; - DisplayManager.renderer.glDrawStringLeft(dx + x, ln - Utils.getFontHeight(small) / 2 + y, getSymbol()); - dx += Utils.getFont(small).getStringWidth(getSymbol()); - parameter2.draw(dx + x, ln - parameter2.getLine() + y, null, null); - } - - @Override - public int getWidth() { - return width; - } - - @Override - protected int calcWidth() { - int dx = 0; - dx += parameter1.getWidth(); - dx += 1; - dx += Utils.getFont(small).getStringWidth(getSymbol()); - return dx += parameter2.getWidth(); - } - @Override public boolean equals(Object o) { if (o instanceof SumSubtraction) { final FunctionOperator f = (FunctionOperator) o; - return parameter1.equals(f.parameter1) && parameter2.equals(f.parameter2); + return parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2()); } return false; } + + @Override + public SumSubtraction clone() { + return new SumSubtraction(mathContext, parameter1, parameter2); + } + } diff --git a/src/org/warp/picalculator/math/functions/Undefined.java b/src/org/warp/picalculator/math/functions/Undefined.java index 93e5741f..e68d742f 100644 --- a/src/org/warp/picalculator/math/functions/Undefined.java +++ b/src/org/warp/picalculator/math/functions/Undefined.java @@ -16,63 +16,22 @@ public class Undefined implements Function { public Undefined(MathContext root) { this.root = root; } - + @Override - public String getSymbol() { - return "undefined"; - } - - @Override - public List solveOneStep() throws Error { + public List simplify() throws Error { return null; } @Override - public boolean isSolved() { + public boolean isSimplified() { return true; } - private int width, height, line; - private boolean small; - @Override - public void generateGraphics() { - width = Utils.getFont(small).getStringWidth("UNDEFINED"); - height = Utils.getFontHeight(small); - line = height / 2; - } - - @Override - public void draw(int x, int y) { - Utils.getFont(small).use(DisplayManager.engine); - DisplayManager.renderer.glDrawStringLeft(x, y, "UNDEFINED"); - } - - @Override - public int getWidth() { - return width; - } - - @Override - public int getHeight() { - return height; - } - - @Override - public int getLine() { - return line; - } - - @Override - public MathContext getRoot() { + public MathContext getMathContext() { return root; } - @Override - public void setSmall(boolean small) { - this.small = small; - } - @Override public boolean equals(Object o) { if (o instanceof Undefined) { @@ -81,4 +40,19 @@ public class Undefined implements Function { return false; } + @Override + public Undefined clone() { + return new Undefined(root); + } + + @Override + public Function setParameter(int index, Function var) throws IndexOutOfBoundsException { + throw new IndexOutOfBoundsException(); + } + + @Override + public Function getParameter(int index) throws IndexOutOfBoundsException { + throw new IndexOutOfBoundsException(); + } + } diff --git a/src/org/warp/picalculator/math/functions/Variable.java b/src/org/warp/picalculator/math/functions/Variable.java index 8d346eb6..665e0098 100644 --- a/src/org/warp/picalculator/math/functions/Variable.java +++ b/src/org/warp/picalculator/math/functions/Variable.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.List; import org.warp.picalculator.Error; @@ -65,7 +65,7 @@ public class Variable implements Function { @Override public List simplify() throws Error { - final List result = new ArrayList<>(); + final List result = new ObjectArrayList<>(); result.add(this); return result; } diff --git a/src/org/warp/picalculator/math/functions/equations/Equation.java b/src/org/warp/picalculator/math/functions/equations/Equation.java index 10e0bc6f..b213b8c3 100644 --- a/src/org/warp/picalculator/math/functions/equations/Equation.java +++ b/src/org/warp/picalculator/math/functions/equations/Equation.java @@ -1,7 +1,7 @@ package org.warp.picalculator.math.functions.equations; import java.math.BigDecimal; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -23,17 +23,7 @@ public class Equation extends FunctionOperator { public Equation(MathContext root, Function value1, Function value2) { super(root, value1, value2); } - - @Override - protected Function NewInstance(MathContext root, Function value1, Function value2) { - return new Equation(root, value1, value2); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.EQUATION; - } - + @Override protected boolean isSolvable() { if (parameter1 instanceof Number & parameter2 instanceof Number) { @@ -43,11 +33,11 @@ public class Equation extends FunctionOperator { } @Override - public ArrayList solve() throws Error { + public ObjectArrayList solve() throws Error { if (parameter1 == null || parameter2 == null) { throw new Error(Errors.SYNTAX_ERROR); } - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); if (parameter1.isSimplified() & parameter2.isSimplified()) { if (((Number) parameter2).getTerm().compareTo(new BigDecimal(0)) == 0) { result.add(this); @@ -63,18 +53,18 @@ public class Equation extends FunctionOperator { public List solve(char variableCharacter) { @SuppressWarnings("unused") - final ArrayList e; + final ObjectArrayList e; //TODO: WORK IN PROGRESS. //TODO: Finire. Fare in modo che risolva i passaggi fino a che non ce ne sono più return null; } //WORK IN PROGRESS - public ArrayList solveStep(char charIncognita) { - ArrayList result = new ArrayList<>(); + public ObjectArrayList solveStep(char charIncognita) { + ObjectArrayList result = new ObjectArrayList<>(); result.add(clone()); for (final SolveMethod t : SolveMethod.techniques) { - final ArrayList newResults = new ArrayList<>(); + final ObjectArrayList newResults = new ObjectArrayList<>(); final int sz = result.size(); for (int n = 0; n < sz; n++) { newResults.addAll(t.solve(result.get(n))); diff --git a/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java b/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java index af33e4ac..2cb972d9 100644 --- a/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java +++ b/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions.equations; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.List; import org.warp.picalculator.Error; @@ -26,11 +26,6 @@ public class EquationsSystem extends FunctionDynamic { super(root, value); } - @Override - public String getSymbol() { - return null; - } - @Override protected boolean isSolvable() { if (functions.length >= 1) { @@ -40,8 +35,8 @@ public class EquationsSystem extends FunctionDynamic { } @Override - public List solveOneStep() throws Error { - final List ret = new ArrayList<>(); + public ObjectArrayList solve() throws Error { + final ObjectArrayList ret = new ObjectArrayList<>(); if (functions.length == 1) { if (functions[0].isSimplified()) { ret.add(functions[0]); @@ -71,63 +66,8 @@ public class EquationsSystem extends FunctionDynamic { } @Override - public void generateGraphics() { - for (final Function f : functions) { - f.setSmall(false); - f.recomputeDimensions(); - } - - width = 0; - for (final Function f : functions) { - if (f.getWidth() > width) { - width = f.getWidth(); - } - } - width += 5; - - height = 3; - for (final Function f : functions) { - height += f.getHeight() + spacing; - } - height = height - spacing + 2; - - line = height / 2; - } - - @Override - public void draw(int x, int y) { - - final int h = getHeight() - 1; - final int marginTop = 3; - final int marginBottom = (h - 3 - 2) / 2 + marginTop; - final int spazioSopra = h - marginBottom; - int dy = marginTop; - for (final Function f : functions) { - f.draw(x + 5, y + dy, null, null); - dy += f.getHeight() + spacing; - } - - DisplayManager.renderer.glDrawLine(x + 2, y + 0, x + 3, y + 0); - DisplayManager.renderer.glDrawLine(x + 1, y + 1, x + 1, y + marginBottom / 2); - DisplayManager.renderer.glDrawLine(x + 2, y + marginBottom / 2 + 1, x + 2, y + marginBottom - 1); - DisplayManager.renderer.glDrawLine(x + 0, y + marginBottom, x + 1, y + marginBottom); - DisplayManager.renderer.glDrawLine(x + 2, y + marginBottom + 1, x + 2, y + marginBottom + spazioSopra / 2 - 1); - DisplayManager.renderer.glDrawLine(x + 1, y + marginBottom + spazioSopra / 2, x + 1, y + h - 1); - DisplayManager.renderer.glDrawLine(x + 2, y + h, x + 3, y + h); - } - - @Override - public int getWidth() { - return width; - } - - @Override - public int getHeight() { - return height; - } - - @Override - public int getLine() { - return line; + public EquationsSystem clone() { + return new EquationsSystem(root, functions); } + } diff --git a/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java b/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java index 649bbc12..e69a1cb5 100644 --- a/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java +++ b/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions.equations; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.List; import org.warp.picalculator.Error; @@ -17,58 +17,7 @@ public class EquationsSystemPart extends FunctionSingle { } @Override - protected Function NewInstance(MathContext root, Function value) { - return new EquationsSystemPart(root, (Equation) value); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.SYSTEM; - } - - @Override - public void recomputeDimensions() { - variable.setSmall(false); - variable.recomputeDimensions(); - - width = 5 + getParameter().getWidth(); - height = 3 + getParameter().getHeight() + 2; - line = 3 + getParameter().getLine(); - } - - @Override - public int draw(int x, int y, boolean small, int caretPos) { - final int h = getHeight() - 1; - final int paddingTop = 3; - final int spazioSotto = (h - 3 - 2) / 2 + paddingTop; - final int spazioSopra = h - spazioSotto; - variable.draw(x + 5, y + paddingTop, null, null); - DisplayManager.renderer.glDrawLine(x + 2, y + 0, x + 3, y + 0); - DisplayManager.renderer.glDrawLine(x + 1, y + 1, x + 1, y + spazioSotto / 2); - DisplayManager.renderer.glDrawLine(x + 2, y + spazioSotto / 2 + 1, x + 2, y + spazioSotto - 1); - DisplayManager.renderer.glDrawLine(x + 0, y + spazioSotto, x + 1, y + spazioSotto); - DisplayManager.renderer.glDrawLine(x + 2, y + spazioSotto + 1, x + 2, y + spazioSotto + spazioSopra / 2 - 1); - DisplayManager.renderer.glDrawLine(x + 1, y + spazioSotto + spazioSopra / 2, x + 1, y + h - 1); - DisplayManager.renderer.glDrawLine(x + 2, y + h, x + 3, y + h); - } - - @Override - public int getWidth() { - return width; - } - - @Override - public int getHeight() { - return height; - } - - @Override - public int getLine() { - return line; - } - - @Override - protected ArrayList solve() throws Error { + protected ObjectArrayList solve() throws Error { // TODO Auto-generated method stub return null; } @@ -84,4 +33,10 @@ public class EquationsSystemPart extends FunctionSingle { // TODO Auto-generated method stub return false; } + + @Override + public EquationsSystemPart clone() { + return new EquationsSystemPart(mathContext, (Equation) parameter); + } + } diff --git a/src/org/warp/picalculator/math/functions/trigonometry/ArcCosine.java b/src/org/warp/picalculator/math/functions/trigonometry/ArcCosine.java index c984e039..5f0f0eb3 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/ArcCosine.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/ArcCosine.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions.trigonometry; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -15,17 +15,7 @@ public class ArcCosine extends FunctionSingle { } @Override - public Function NewInstance(MathContext root, Function value) { - return new ArcCosine(root, value); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.getGraphicRepresentation(MathematicalSymbols.ARC_COSINE); - } - - @Override - public ArrayList solve() throws Error { + public ObjectArrayList solve() throws Error { // TODO Auto-generated method stub return null; } @@ -42,4 +32,10 @@ public class ArcCosine extends FunctionSingle { return false; } + @Override + public FunctionSingle clone() { + // TODO Auto-generated method stub + return null; + } + } diff --git a/src/org/warp/picalculator/math/functions/trigonometry/ArcSine.java b/src/org/warp/picalculator/math/functions/trigonometry/ArcSine.java index 45a7d66e..99d1dea4 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/ArcSine.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/ArcSine.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions.trigonometry; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -15,17 +15,7 @@ public class ArcSine extends FunctionSingle { } @Override - public Function NewInstance(MathContext root, Function value) { - return new ArcSine(root, value); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.getGraphicRepresentation(MathematicalSymbols.ARC_SINE); - } - - @Override - public ArrayList solve() throws Error { + public ObjectArrayList solve() throws Error { // TODO Auto-generated method stub return null; } @@ -42,4 +32,10 @@ public class ArcSine extends FunctionSingle { return false; } + @Override + public FunctionSingle clone() { + // TODO Auto-generated method stub + return null; + } + } diff --git a/src/org/warp/picalculator/math/functions/trigonometry/ArcTangent.java b/src/org/warp/picalculator/math/functions/trigonometry/ArcTangent.java index bb9fb487..e7ba0e9f 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/ArcTangent.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/ArcTangent.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions.trigonometry; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -15,17 +15,7 @@ public class ArcTangent extends FunctionSingle { } @Override - public Function NewInstance(MathContext root, Function value) { - return new ArcTangent(root, value); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.getGraphicRepresentation(MathematicalSymbols.ARC_TANGENT); - } - - @Override - public ArrayList solve() throws Error { + public ObjectArrayList solve() throws Error { // TODO Auto-generated method stub return null; } @@ -42,4 +32,10 @@ public class ArcTangent extends FunctionSingle { return false; } + @Override + public FunctionSingle clone() { + // TODO Auto-generated method stub + return null; + } + } diff --git a/src/org/warp/picalculator/math/functions/trigonometry/Cosine.java b/src/org/warp/picalculator/math/functions/trigonometry/Cosine.java index 21a5cbd5..26a2c2d2 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/Cosine.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/Cosine.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions.trigonometry; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -15,17 +15,7 @@ public class Cosine extends FunctionSingle { } @Override - public Function NewInstance(MathContext root, Function value) { - return new Cosine(root, value); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.getGraphicRepresentation(MathematicalSymbols.COSINE); - } - - @Override - public ArrayList solve() throws Error { + public ObjectArrayList solve() throws Error { // TODO Auto-generated method stub return null; } @@ -42,4 +32,10 @@ public class Cosine extends FunctionSingle { return false; } + @Override + public FunctionSingle clone() { + // TODO Auto-generated method stub + return null; + } + } diff --git a/src/org/warp/picalculator/math/functions/trigonometry/Sine.java b/src/org/warp/picalculator/math/functions/trigonometry/Sine.java index 0ba6dc02..1b677ab5 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/Sine.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/Sine.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions.trigonometry; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.nevec.rjm.BigDecimalMath; import org.warp.picalculator.Error; @@ -16,20 +16,10 @@ public class Sine extends FunctionSingle { public Sine(MathContext root, Function value) { super(root, value); } - - @Override - public Function NewInstance(MathContext root, Function value) { - return new Sine(root, value); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.getGraphicRepresentation(MathematicalSymbols.SINE); - } - + @Override protected boolean isSolvable() { - if (variable instanceof Number) { + if (parameter instanceof Number) { if (mathContext.exactMode == false) { return true; } @@ -41,11 +31,11 @@ public class Sine extends FunctionSingle { } @Override - public ArrayList solve() throws Error { - final ArrayList results = new ArrayList<>(); - if (variable instanceof Number) { + public ObjectArrayList solve() throws Error { + final ObjectArrayList results = new ObjectArrayList<>(); + if (parameter instanceof Number) { if (mathContext.exactMode == false) { - results.add(new Number(mathContext, BigDecimalMath.sin(((Number) variable).getTerm()))); + results.add(new Number(mathContext, BigDecimalMath.sin(((Number) parameter).getTerm()))); } } return results; @@ -55,11 +45,16 @@ public class Sine extends FunctionSingle { public boolean equals(Object o) { if (o instanceof Sine) { final FunctionSingle f = (FunctionSingle) o; - if (variable.equals(f.getParameter())) { + if (parameter.equals(f.getParameter())) { return true; } } return false; } + @Override + public Sine clone() { + return new Sine(mathContext, parameter); + } + } diff --git a/src/org/warp/picalculator/math/functions/trigonometry/Tangent.java b/src/org/warp/picalculator/math/functions/trigonometry/Tangent.java index 6e029ceb..00dc62f9 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/Tangent.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/Tangent.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions.trigonometry; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -13,19 +13,9 @@ public class Tangent extends FunctionSingle { public Tangent(MathContext root, Function value) { super(root, value); } - + @Override - public Function NewInstance(MathContext root, Function value) { - return new Tangent(root, value); - } - - @Override - public String getSymbol() { - return MathematicalSymbols.getGraphicRepresentation(MathematicalSymbols.TANGENT); - } - - @Override - public ArrayList solve() throws Error { + public ObjectArrayList solve() throws Error { // TODO Auto-generated method stub return null; } @@ -42,4 +32,10 @@ public class Tangent extends FunctionSingle { return false; } + @Override + public FunctionSingle clone() { + // TODO Auto-generated method stub + return null; + } + } diff --git a/src/org/warp/picalculator/math/parser/InputParser.java b/src/org/warp/picalculator/math/parser/InputParser.java new file mode 100644 index 00000000..b536e865 --- /dev/null +++ b/src/org/warp/picalculator/math/parser/InputParser.java @@ -0,0 +1,289 @@ +package org.warp.picalculator.math.parser; + +import org.warp.picalculator.Error; +import org.warp.picalculator.Errors; +import org.warp.picalculator.gui.expression.Block; +import org.warp.picalculator.gui.expression.BlockChar; +import org.warp.picalculator.gui.expression.BlockContainer; +import org.warp.picalculator.gui.expression.BlockDivision; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +import org.warp.picalculator.math.MathematicalSymbols; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Expression; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.parser.features.FeatureChar; +import org.warp.picalculator.math.parser.features.FeatureDivision; +import org.warp.picalculator.math.parser.features.FeatureMultiplication; +import org.warp.picalculator.math.parser.features.FeatureNumber; +import org.warp.picalculator.math.parser.features.FeatureSum; +import org.warp.picalculator.math.parser.features.interfaces.Feature; +import org.warp.picalculator.math.parser.features.interfaces.FeatureDouble; + +import com.sun.org.apache.xpath.internal.functions.Function2Args; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import it.unimi.dsi.fastutil.objects.ObjectListIterator; + +public class InputParser { + public static Expression parseInput(MathContext context, BlockContainer root) throws Error { + Expression result; + + Function resultFunction = parseContainer(context, root); + + result = new Expression(context, resultFunction); + return result; + } + + private static Function parseContainer(final MathContext context, final BlockContainer container) throws Error { + final ObjectArrayList blocks = container.getContent(); + final ObjectArrayList blockFeatures = new ObjectArrayList<>(); + + for (final Block block : blocks) { + Feature blockFeature = parseBlock(context, block); + blockFeatures.add(blockFeature); + } + + Function result = joinFeatures(context, blockFeatures); + return result; + } + + private static Feature parseBlock(final MathContext context, final Block block) throws Error { + + Feature result; + + if (block instanceof BlockChar) { + result = new FeatureChar(((BlockChar) block).getChar()); + } else if (block instanceof BlockDivision) { + BlockDivision bd = (BlockDivision) block; + Function upper = parseContainer(context, bd.getUpperContainer()); + Function lower = parseContainer(context, bd.getLowerContainer()); + result = new FeatureDivision(upper, lower); + } else { + throw new Error(Errors.SYNTAX_ERROR); + } + + return result; + } + + private static Function joinFeatures(final MathContext context, ObjectArrayList features) throws Error { + + features = fixFeatures(context, features); + + ObjectArrayList process = makeFunctions(context, features); + + fixStack(context, process); + + if (process.size() > 1) { + throw new Error(Errors.UNBALANCED_STACK, "The stack is unbalanced. Not all the functions are nested correctly"); + } + + return process.get(0); + } + + private static void fixStack(MathContext context, ObjectArrayList process) throws Error { + + //Phase 1 + ObjectListIterator stackIterator = process.listIterator(process.size()); + Function lastElement = null; + while (stackIterator.hasPrevious()) { + Function f = stackIterator.previous(); + + if (f instanceof FunctionSingle) { + if (((FunctionSingle) f).getParameter() == null) { + if (lastElement == null) { + throw new Error(Errors.MISSING_ARGUMENTS, "There is a function at the end without any argument specified."); + } else { + ((FunctionSingle) f).setParameter(lastElement); + process.remove(stackIterator.nextIndex()); + } + } + } + lastElement = f; + } + + //Phase 2 + stackIterator = process.iterator(); + while (stackIterator.hasNext()) { + Function f = stackIterator.next(); + + if (f instanceof Multiplication || f instanceof Division) { + + } + } + + //Phase 3 + stackIterator = process.iterator(); + while (stackIterator.hasNext()) { + Function f = stackIterator.next(); + + if (f instanceof Sum || f instanceof Subtraction || f instanceof SumSubtraction) { + + } + } + + //Phase 4 + stackIterator = process.iterator(); + while (stackIterator.hasNext()) { + Function f = stackIterator.next(); + + if (f instanceof Function2Args) { + + } + } + } + + private static ObjectArrayList makeFunctions(MathContext context, ObjectArrayList features) throws Error { + ObjectArrayList process = new ObjectArrayList<>(); + + for (Feature f : features) { + if (f instanceof FeatureDivision) { + process.add(new Division(context, (Function) ((FeatureDouble) f).getChild1(), (Function) ((FeatureDouble) f).getChild2())); + } else if (f instanceof FeatureMultiplication) { + process.add(new Multiplication(context, (Function) ((FeatureDouble) f).getChild1(), (Function) ((FeatureDouble) f).getChild2())); + } else if (f instanceof FeatureNumber) { + process.add(new Number(context, ((FeatureNumber) f).getNumberString())); + } else if (f instanceof FeatureChar) { + //All the char features must have been changed already before + throw new Error(Errors.SYNTAX_ERROR, "\"" + f.getClass().getSimpleName().toString() + "\" can't be converted into a Function!"); + } else { + throw new Error(Errors.SYNTAX_ERROR, "\"" + f.getClass().getSimpleName().toString() + "\" can't be converted into a Function!"); + } + } + return process; + } + + private static ObjectArrayList fixFeatures(final MathContext context, ObjectArrayList features) throws Error { + + features = fixMinuses(context, features); + + features = makeNumbers(context, features); + + features = convertFunctionChars(context, features); + + return features; + } + + /** + * Create function features from char features + * @param context + * @param features + * @return + */ + private static ObjectArrayList convertFunctionChars(MathContext context, ObjectArrayList features) throws Error { + ObjectArrayList process = new ObjectArrayList<>(); + + for (Feature f : features) { + if (f instanceof FeatureChar) { + final char featureChar = ((FeatureChar) f).ch; + Feature result = null; + switch (featureChar) { + case MathematicalSymbols.SUM: + result = new FeatureSum(null, null); + break; + case MathematicalSymbols.MULTIPLICATION: + result = new FeatureMultiplication(null, null); + break; + } + + if (result == null) { + throw new Error(Errors.SYNTAX_ERROR, "Char " + featureChar + " isn't a known feature"); + } + + process.add(result); + } else { + process.add(f); + } + } + + return process; + } + + /** + * Make numbers [-][1][2][+][-][3] => [-12] + * @param context + * @param features + * @return + */ + private static ObjectArrayList makeNumbers(MathContext context, ObjectArrayList features) { + ObjectArrayList process = new ObjectArrayList<>(); + + FeatureNumber numberBuffer = null; + for (Feature f : features) { + if (f instanceof FeatureChar) { + final FeatureChar bcf = (FeatureChar) f; + final char[] numbers = MathematicalSymbols.numbers; + boolean isNumber = false; + for (char n : numbers) { + if (bcf.ch == n) { + isNumber = true; + break; + } + } + if (bcf.ch == '-') { + isNumber = true; + } + if (isNumber) { + if (numberBuffer == null) { + numberBuffer = new FeatureNumber(bcf.ch); + process.add(numberBuffer); + } else { + numberBuffer.append(bcf.ch); + } + } else { + if (numberBuffer != null) { + numberBuffer = null; + } + process.add(f); + } + } else { + process.add(f); + } + } + + return process; + } + + /** + * Fix minuses [-][1][2][+][-][3][-][2] => [-][12][+][-][3][—][2] + * @param context + * @param features + * @return + * @throws Error + */ + private static ObjectArrayList fixMinuses(final MathContext context, ObjectArrayList features) throws Error { + ObjectArrayList process = new ObjectArrayList<>(); + Feature lastFeature = null; + for (Feature f : features) { + if (f instanceof FeatureChar && ((FeatureChar)f).ch == MathematicalSymbols.SUBTRACTION) { + boolean isNegativeOfNumber = false; + if (lastFeature == null) { + isNegativeOfNumber = true; + } else if (lastFeature instanceof FeatureChar) { + FeatureChar lcf = (FeatureChar) lastFeature; + final char[] operators = MathematicalSymbols.functionsNSN; + for (char operator : operators) { + if (lcf.ch == operator) { + isNegativeOfNumber = true; + break; + } + } + } + if (isNegativeOfNumber) { + process.add(new FeatureChar(MathematicalSymbols.MINUS)); + } else { + process.add(new FeatureChar(MathematicalSymbols.SUBTRACTION)); + } + } else { + process.add(f); + } + lastFeature = f; + } + return process; + } +} diff --git a/src/org/warp/picalculator/math/parser/features/FeatureChar.java b/src/org/warp/picalculator/math/parser/features/FeatureChar.java new file mode 100644 index 00000000..3797e3c8 --- /dev/null +++ b/src/org/warp/picalculator/math/parser/features/FeatureChar.java @@ -0,0 +1,13 @@ +package org.warp.picalculator.math.parser.features; + +import org.warp.picalculator.math.parser.features.interfaces.Feature; + +public class FeatureChar implements Feature { + + public final char ch; + + public FeatureChar(char ch) { + this.ch = ch; + } + +} diff --git a/src/org/warp/picalculator/math/parser/features/FeatureDivision.java b/src/org/warp/picalculator/math/parser/features/FeatureDivision.java new file mode 100644 index 00000000..4ae989f7 --- /dev/null +++ b/src/org/warp/picalculator/math/parser/features/FeatureDivision.java @@ -0,0 +1,9 @@ +package org.warp.picalculator.math.parser.features; + +public class FeatureDivision extends FeatureDoubleImpl { + + public FeatureDivision(Object child1, Object child2) { + super(child1, child2); + } + +} diff --git a/src/org/warp/picalculator/math/parser/features/FeatureDoubleImpl.java b/src/org/warp/picalculator/math/parser/features/FeatureDoubleImpl.java new file mode 100644 index 00000000..86afd892 --- /dev/null +++ b/src/org/warp/picalculator/math/parser/features/FeatureDoubleImpl.java @@ -0,0 +1,33 @@ +package org.warp.picalculator.math.parser.features; + +import org.warp.picalculator.math.parser.features.interfaces.FeatureDouble; + +public abstract class FeatureDoubleImpl implements FeatureDouble { + private Object child_1; + private Object child_2; + + public FeatureDoubleImpl(Object child1, Object child2) { + child_1 = child1; + child_2 = child2; + } + + @Override + public Object getChild1() { + return child_1; + } + + @Override + public void setChild1(Object obj) { + child_1 = obj; + } + + @Override + public Object getChild2() { + return child_2; + } + + @Override + public void setChild2(Object obj) { + child_2 = obj; + } +} diff --git a/src/org/warp/picalculator/math/parser/features/FeatureMultiplication.java b/src/org/warp/picalculator/math/parser/features/FeatureMultiplication.java new file mode 100644 index 00000000..cd6d22e6 --- /dev/null +++ b/src/org/warp/picalculator/math/parser/features/FeatureMultiplication.java @@ -0,0 +1,9 @@ +package org.warp.picalculator.math.parser.features; + +public class FeatureMultiplication extends FeatureDoubleImpl { + + public FeatureMultiplication(Object child1, Object child2) { + super(child1, child2); + } + +} diff --git a/src/org/warp/picalculator/math/parser/features/FeatureNumber.java b/src/org/warp/picalculator/math/parser/features/FeatureNumber.java new file mode 100644 index 00000000..4e8be142 --- /dev/null +++ b/src/org/warp/picalculator/math/parser/features/FeatureNumber.java @@ -0,0 +1,27 @@ +package org.warp.picalculator.math.parser.features; + +import org.warp.picalculator.math.parser.features.interfaces.FeatureBasic; + +public class FeatureNumber implements FeatureBasic { + private String numberString; + + public FeatureNumber(char c){ + numberString = c+""; + } + + public FeatureNumber(String s) { + numberString = s; + } + + public FeatureNumber() { + numberString = ""; + } + + public String getNumberString() { + return numberString; + } + + public void append(char ch) { + numberString+=ch; + } +} diff --git a/src/org/warp/picalculator/math/parser/features/FeatureSingleImpl.java b/src/org/warp/picalculator/math/parser/features/FeatureSingleImpl.java new file mode 100644 index 00000000..e3ea1312 --- /dev/null +++ b/src/org/warp/picalculator/math/parser/features/FeatureSingleImpl.java @@ -0,0 +1,21 @@ +package org.warp.picalculator.math.parser.features; + +import org.warp.picalculator.math.parser.features.interfaces.FeatureSingle; + +public abstract class FeatureSingleImpl implements FeatureSingle { + private Object child; + + public FeatureSingleImpl(Object child) { + this.child = child; + } + + @Override + public Object getChild() { + return child; + } + + @Override + public void setChild(Object obj) { + child = obj; + } +} diff --git a/src/org/warp/picalculator/math/parser/features/FeatureSum.java b/src/org/warp/picalculator/math/parser/features/FeatureSum.java new file mode 100644 index 00000000..d5817080 --- /dev/null +++ b/src/org/warp/picalculator/math/parser/features/FeatureSum.java @@ -0,0 +1,9 @@ +package org.warp.picalculator.math.parser.features; + +public class FeatureSum extends FeatureDoubleImpl { + + public FeatureSum(Object child1, Object child2) { + super(child1, child2); + } + +} diff --git a/src/org/warp/picalculator/math/parser/features/interfaces/Feature.java b/src/org/warp/picalculator/math/parser/features/interfaces/Feature.java new file mode 100644 index 00000000..fa361356 --- /dev/null +++ b/src/org/warp/picalculator/math/parser/features/interfaces/Feature.java @@ -0,0 +1,5 @@ +package org.warp.picalculator.math.parser.features.interfaces; + +public abstract interface Feature { + +} diff --git a/src/org/warp/picalculator/math/parser/features/interfaces/FeatureBasic.java b/src/org/warp/picalculator/math/parser/features/interfaces/FeatureBasic.java new file mode 100644 index 00000000..cc19c77e --- /dev/null +++ b/src/org/warp/picalculator/math/parser/features/interfaces/FeatureBasic.java @@ -0,0 +1,4 @@ +package org.warp.picalculator.math.parser.features.interfaces; + +public interface FeatureBasic extends Feature { +} diff --git a/src/org/warp/picalculator/math/parser/features/interfaces/FeatureDouble.java b/src/org/warp/picalculator/math/parser/features/interfaces/FeatureDouble.java new file mode 100644 index 00000000..2c871f4e --- /dev/null +++ b/src/org/warp/picalculator/math/parser/features/interfaces/FeatureDouble.java @@ -0,0 +1,8 @@ +package org.warp.picalculator.math.parser.features.interfaces; + +public interface FeatureDouble extends Feature { + public Object getChild1(); + public void setChild1(Object obj); + public Object getChild2(); + public void setChild2(Object obj); +} diff --git a/src/org/warp/picalculator/math/parser/features/interfaces/FeatureMultiple.java b/src/org/warp/picalculator/math/parser/features/interfaces/FeatureMultiple.java new file mode 100644 index 00000000..71f47049 --- /dev/null +++ b/src/org/warp/picalculator/math/parser/features/interfaces/FeatureMultiple.java @@ -0,0 +1,10 @@ +package org.warp.picalculator.math.parser.features.interfaces; + +public interface FeatureMultiple extends Feature { + public Object[] getChildren(); + public Object getChild(int index); + public int getChildCount(); + public void setChild(int index, Object obj); + public void setChildren(Object[] objs); + public void addChild(Object obj); +} diff --git a/src/org/warp/picalculator/math/parser/features/interfaces/FeatureSingle.java b/src/org/warp/picalculator/math/parser/features/interfaces/FeatureSingle.java new file mode 100644 index 00000000..6c5e46b7 --- /dev/null +++ b/src/org/warp/picalculator/math/parser/features/interfaces/FeatureSingle.java @@ -0,0 +1,6 @@ +package org.warp.picalculator.math.parser.features.interfaces; + +public interface FeatureSingle extends Feature { + public Object getChild(); + public void setChild(Object obj); +} diff --git a/src/org/warp/picalculator/math/rules/ExpandRule1.java b/src/org/warp/picalculator/math/rules/ExpandRule1.java index 852c9610..67aae966 100644 --- a/src/org/warp/picalculator/math/rules/ExpandRule1.java +++ b/src/org/warp/picalculator/math/rules/ExpandRule1.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -55,8 +55,8 @@ public class ExpandRule1 { return false; } - public static ArrayList execute(Function f) throws Error { - final ArrayList result = new ArrayList<>(); + public static ObjectArrayList execute(Function f) throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); final MathContext root = f.getMathContext(); Expression expr = null; diff --git a/src/org/warp/picalculator/math/rules/ExpandRule5.java b/src/org/warp/picalculator/math/rules/ExpandRule5.java index 81e402f6..45492269 100644 --- a/src/org/warp/picalculator/math/rules/ExpandRule5.java +++ b/src/org/warp/picalculator/math/rules/ExpandRule5.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; @@ -34,8 +34,8 @@ public class ExpandRule5 { return false; } - public static ArrayList execute(Function f) throws Error { - final ArrayList result = new ArrayList<>(); + public static ObjectArrayList execute(Function f) throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); final Function a = null; if (f instanceof Negative) { diff --git a/src/org/warp/picalculator/math/rules/ExponentRule1.java b/src/org/warp/picalculator/math/rules/ExponentRule1.java index 7b06b7e8..b0ee7216 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule1.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule1.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -26,9 +26,9 @@ public class ExponentRule1 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); result.add(new Number(root, 1)); return result; } diff --git a/src/org/warp/picalculator/math/rules/ExponentRule15.java b/src/org/warp/picalculator/math/rules/ExponentRule15.java index dabe4789..c03b6492 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule15.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule15.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -27,9 +27,9 @@ public class ExponentRule15 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final Multiplication fnc = (Multiplication) f; final Function a = fnc.getParameter1(); final Expression expr = new Expression(root, a); diff --git a/src/org/warp/picalculator/math/rules/ExponentRule16.java b/src/org/warp/picalculator/math/rules/ExponentRule16.java index aac92f59..a71405ae 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule16.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule16.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -32,9 +32,9 @@ public class ExponentRule16 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final Multiplication fnc = (Multiplication) f; if (fnc.getParameter1() instanceof Power && fnc.getParameter2() instanceof Power) { result.add(new Power(root, ((Power)fnc.getParameter1()).getParameter1(), new Sum(root, new Expression(root, ((Power)fnc.getParameter1()).getParameter2()), new Expression(root, ((Power)fnc.getParameter2()).getParameter2())))); diff --git a/src/org/warp/picalculator/math/rules/ExponentRule17.java b/src/org/warp/picalculator/math/rules/ExponentRule17.java index bfc98a27..34d3981e 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule17.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule17.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -30,9 +30,9 @@ public class ExponentRule17 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final Multiplication fnc = (Multiplication) f; final Function a = fnc.getParameter1(); final Expression expr = new Expression(root, a); diff --git a/src/org/warp/picalculator/math/rules/ExponentRule2.java b/src/org/warp/picalculator/math/rules/ExponentRule2.java index 0e0c9709..92aae1ce 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule2.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule2.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; @@ -24,8 +24,8 @@ public class ExponentRule2 { return false; } - public static ArrayList execute(Function f) throws Error { - final ArrayList result = new ArrayList<>(); + public static ObjectArrayList execute(Function f) throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); result.add(((Power) f).getParameter1()); return result; } diff --git a/src/org/warp/picalculator/math/rules/ExponentRule3.java b/src/org/warp/picalculator/math/rules/ExponentRule3.java index 7f7e9999..be61a545 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule3.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule3.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; @@ -24,8 +24,8 @@ public class ExponentRule3 { return false; } - public static ArrayList execute(Function f) throws Error { - final ArrayList result = new ArrayList<>(); + public static ObjectArrayList execute(Function f) throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); result.add(new Number(f.getMathContext(), 1)); return result; } diff --git a/src/org/warp/picalculator/math/rules/ExponentRule4.java b/src/org/warp/picalculator/math/rules/ExponentRule4.java index bf9756da..82f13f8a 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule4.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule4.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -27,9 +27,9 @@ public class ExponentRule4 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final Power fnc = (Power) f; final Expression expr = (Expression) fnc.getParameter1(); final Multiplication mult = (Multiplication) expr.getParameter(0); diff --git a/src/org/warp/picalculator/math/rules/ExponentRule9.java b/src/org/warp/picalculator/math/rules/ExponentRule9.java index 12ab6957..188fa867 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule9.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule9.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -27,9 +27,9 @@ public class ExponentRule9 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final Power powC = (Power) f; final Power powB = (Power) powC.getParameter1(); final Power p = new Power(root, powB.getParameter1(), new Multiplication(root, new Expression(root, powB.getParameter2()), new Expression(root, powC.getParameter2()))); diff --git a/src/org/warp/picalculator/math/rules/FractionsRule1.java b/src/org/warp/picalculator/math/rules/FractionsRule1.java index d635a7fc..f594da57 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule1.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule1.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -35,8 +35,8 @@ public class FractionsRule1 { return false; } - public static ArrayList execute(Function f) throws Error { - final ArrayList result = new ArrayList<>(); + public static ObjectArrayList execute(Function f) throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); result.add(new Number(f.getMathContext(), 0)); return result; } diff --git a/src/org/warp/picalculator/math/rules/FractionsRule11.java b/src/org/warp/picalculator/math/rules/FractionsRule11.java index 0e380a3d..5c888d62 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule11.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule11.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; @@ -32,8 +32,8 @@ public class FractionsRule11 { return new Multiplication(fnc.getMathContext(), a, c).isSimplified() == false; } - public static ArrayList execute(Function f) throws Error { - final ArrayList result = new ArrayList<>(); + public static ObjectArrayList execute(Function f) throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); final Division fnc = (Division) f; Function a; Function b; diff --git a/src/org/warp/picalculator/math/rules/FractionsRule12.java b/src/org/warp/picalculator/math/rules/FractionsRule12.java index 91db82da..0b5a2792 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule12.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule12.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; @@ -30,8 +30,8 @@ public class FractionsRule12 { return false; } - public static ArrayList execute(Function f) throws Error { - final ArrayList result = new ArrayList<>(); + public static ObjectArrayList execute(Function f) throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); final Division fnc = (Division) f; Function a; Function b; diff --git a/src/org/warp/picalculator/math/rules/FractionsRule14.java b/src/org/warp/picalculator/math/rules/FractionsRule14.java index 86132e59..40ac75e1 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule14.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule14.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; @@ -47,8 +47,8 @@ public class FractionsRule14 { return false; } - public static ArrayList execute(Function f) throws Error { - final ArrayList result = new ArrayList<>(); + public static ObjectArrayList execute(Function f) throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); final Multiplication fnc = (Multiplication) f; Function a; Function b; diff --git a/src/org/warp/picalculator/math/rules/FractionsRule2.java b/src/org/warp/picalculator/math/rules/FractionsRule2.java index d4e2b95e..97c4336f 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule2.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule2.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; @@ -27,8 +27,8 @@ public class FractionsRule2 { return false; } - public static ArrayList execute(Function f) throws Error { - final ArrayList result = new ArrayList<>(); + public static ObjectArrayList execute(Function f) throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); final Division fnc = (Division) f; result.add(fnc.getParameter1()); return result; diff --git a/src/org/warp/picalculator/math/rules/FractionsRule3.java b/src/org/warp/picalculator/math/rules/FractionsRule3.java index 22b6b5d2..5ab080c8 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule3.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule3.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; @@ -24,8 +24,8 @@ public class FractionsRule3 { return false; } - public static ArrayList execute(Function f) throws Error { - final ArrayList result = new ArrayList<>(); + public static ObjectArrayList execute(Function f) throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); result.add(new Number(f.getMathContext(), 1)); return result; } diff --git a/src/org/warp/picalculator/math/rules/FractionsRule4.java b/src/org/warp/picalculator/math/rules/FractionsRule4.java index a5e37fdb..153f3b43 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule4.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule4.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; @@ -28,8 +28,8 @@ public class FractionsRule4 { return false; } - public static ArrayList execute(Function f) throws Error { - final ArrayList result = new ArrayList<>(); + public static ObjectArrayList execute(Function f) throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); final Power fnc = (Power) f; final Function a = ((Division) fnc.getParameter1()).getParameter1(); final Function b = ((Division) fnc.getParameter1()).getParameter2(); diff --git a/src/org/warp/picalculator/math/rules/FractionsRule5.java b/src/org/warp/picalculator/math/rules/FractionsRule5.java index b6992173..66384e90 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule5.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule5.java @@ -1,7 +1,7 @@ package org.warp.picalculator.math.rules; import java.math.BigDecimal; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -30,9 +30,9 @@ public class FractionsRule5 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final Power fnc = (Power) f; final Function a = ((Division) fnc.getParameter1()).getParameter1(); final Function b = ((Division) fnc.getParameter1()).getParameter2(); diff --git a/src/org/warp/picalculator/math/rules/NumberRule1.java b/src/org/warp/picalculator/math/rules/NumberRule1.java index cb8832ff..3cfd9545 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule1.java +++ b/src/org/warp/picalculator/math/rules/NumberRule1.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -35,8 +35,8 @@ public class NumberRule1 { return false; } - public static ArrayList execute(Function f) throws Error { - final ArrayList result = new ArrayList<>(); + public static ObjectArrayList execute(Function f) throws Error { + final ObjectArrayList result = new ObjectArrayList<>(); result.add(new Number(f.getMathContext(), "0")); return result; } diff --git a/src/org/warp/picalculator/math/rules/NumberRule2.java b/src/org/warp/picalculator/math/rules/NumberRule2.java index 5259c96d..22ba9aab 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule2.java +++ b/src/org/warp/picalculator/math/rules/NumberRule2.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -35,9 +35,9 @@ public class NumberRule2 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); Function a = null; boolean aFound = false; final Multiplication mult = (Multiplication) f; diff --git a/src/org/warp/picalculator/math/rules/NumberRule3.java b/src/org/warp/picalculator/math/rules/NumberRule3.java index 519999cf..6cef02a0 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule3.java +++ b/src/org/warp/picalculator/math/rules/NumberRule3.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -46,9 +46,9 @@ public class NumberRule3 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); if (f instanceof SumSubtraction) { final Multiplication mul = new Multiplication(root, new Number(root, 2), f); result.add(mul); diff --git a/src/org/warp/picalculator/math/rules/NumberRule4.java b/src/org/warp/picalculator/math/rules/NumberRule4.java index 18ea5f37..f71ef091 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule4.java +++ b/src/org/warp/picalculator/math/rules/NumberRule4.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -25,9 +25,9 @@ public class NumberRule4 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final SumSubtraction ss = (SumSubtraction) f; result.add(new Sum(root, ss.getParameter1(), ss.getParameter2())); result.add(new Subtraction(root, ss.getParameter1(), ss.getParameter2())); diff --git a/src/org/warp/picalculator/math/rules/NumberRule5.java b/src/org/warp/picalculator/math/rules/NumberRule5.java index 448e8d49..08b90768 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule5.java +++ b/src/org/warp/picalculator/math/rules/NumberRule5.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -31,9 +31,9 @@ public class NumberRule5 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final FunctionOperator fnc = (FunctionOperator) f; Function a = fnc.getParameter1(); if (a.equals(new Number(root, 0))) { diff --git a/src/org/warp/picalculator/math/rules/NumberRule6.java b/src/org/warp/picalculator/math/rules/NumberRule6.java index 13fdf5a6..52c90e40 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule6.java +++ b/src/org/warp/picalculator/math/rules/NumberRule6.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -36,9 +36,9 @@ public class NumberRule6 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); Function a = null; boolean aFound = false; final Multiplication mult = (Multiplication) f; diff --git a/src/org/warp/picalculator/math/rules/NumberRule7.java b/src/org/warp/picalculator/math/rules/NumberRule7.java index 670a1d4e..a7855991 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule7.java +++ b/src/org/warp/picalculator/math/rules/NumberRule7.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -22,9 +22,9 @@ public class NumberRule7 { return f.getParameter1().equals(f.getParameter2()); } - public static ArrayList execute(Sum f) throws Error { + public static ObjectArrayList execute(Sum f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final Multiplication mult = new Multiplication(root, new Number(root, 2), f.getParameter1()); result.add(mult); return result; diff --git a/src/org/warp/picalculator/math/rules/SyntaxRule1.java b/src/org/warp/picalculator/math/rules/SyntaxRule1.java index 0766c77a..e592f55b 100644 --- a/src/org/warp/picalculator/math/rules/SyntaxRule1.java +++ b/src/org/warp/picalculator/math/rules/SyntaxRule1.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -28,9 +28,9 @@ public class SyntaxRule1 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); FunctionOperator mOut = (FunctionOperator) f; final Function a = ((FunctionOperator) mOut.getParameter1()).getParameter1(); final Function b = ((FunctionOperator) mOut.getParameter1()).getParameter2(); diff --git a/src/org/warp/picalculator/math/rules/SyntaxRule2.java b/src/org/warp/picalculator/math/rules/SyntaxRule2.java index d09896ca..64950df7 100644 --- a/src/org/warp/picalculator/math/rules/SyntaxRule2.java +++ b/src/org/warp/picalculator/math/rules/SyntaxRule2.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -30,9 +30,9 @@ public class SyntaxRule2 { return false; } - public static ArrayList execute(Sum f) throws Error { + public static ObjectArrayList execute(Sum f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final Function a = f.getParameter1(); Function b, c; if (f.getParameter2() instanceof Sum) { diff --git a/src/org/warp/picalculator/math/rules/UndefinedRule1.java b/src/org/warp/picalculator/math/rules/UndefinedRule1.java index 094a4b4e..e5bceb24 100644 --- a/src/org/warp/picalculator/math/rules/UndefinedRule1.java +++ b/src/org/warp/picalculator/math/rules/UndefinedRule1.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -27,9 +27,9 @@ public class UndefinedRule1 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); result.add(new Undefined(root)); return result; } diff --git a/src/org/warp/picalculator/math/rules/UndefinedRule2.java b/src/org/warp/picalculator/math/rules/UndefinedRule2.java index ec964f10..4094c543 100644 --- a/src/org/warp/picalculator/math/rules/UndefinedRule2.java +++ b/src/org/warp/picalculator/math/rules/UndefinedRule2.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -30,9 +30,9 @@ public class UndefinedRule2 { return false; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { final MathContext root = f.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); result.add(new Undefined(root)); return result; } diff --git a/src/org/warp/picalculator/math/rules/VariableRule1.java b/src/org/warp/picalculator/math/rules/VariableRule1.java index c93757e7..7360f2c3 100644 --- a/src/org/warp/picalculator/math/rules/VariableRule1.java +++ b/src/org/warp/picalculator/math/rules/VariableRule1.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -31,9 +31,9 @@ public class VariableRule1 { return false; } - public static ArrayList execute(FunctionOperator fnc) throws Error { + public static ObjectArrayList execute(FunctionOperator fnc) throws Error { final MathContext root = fnc.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final Multiplication m1 = (Multiplication) fnc.getParameter1(); final Multiplication m2 = (Multiplication) fnc.getParameter2(); final Function a = m1.getParameter1(); diff --git a/src/org/warp/picalculator/math/rules/VariableRule2.java b/src/org/warp/picalculator/math/rules/VariableRule2.java index 2f9ed328..f22123a6 100644 --- a/src/org/warp/picalculator/math/rules/VariableRule2.java +++ b/src/org/warp/picalculator/math/rules/VariableRule2.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -31,9 +31,9 @@ public class VariableRule2 { return false; } - public static ArrayList execute(FunctionOperator fnc) throws Error { + public static ObjectArrayList execute(FunctionOperator fnc) throws Error { final MathContext root = fnc.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final Multiplication m1 = (Multiplication) fnc.getParameter1(); final Function a = m1.getParameter1(); final Function x = fnc.getParameter2(); diff --git a/src/org/warp/picalculator/math/rules/VariableRule3.java b/src/org/warp/picalculator/math/rules/VariableRule3.java index de5a8d2f..cd3cc36f 100644 --- a/src/org/warp/picalculator/math/rules/VariableRule3.java +++ b/src/org/warp/picalculator/math/rules/VariableRule3.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -31,9 +31,9 @@ public class VariableRule3 { return false; } - public static ArrayList execute(FunctionOperator fnc) throws Error { + public static ObjectArrayList execute(FunctionOperator fnc) throws Error { final MathContext root = fnc.getMathContext(); - final ArrayList result = new ArrayList<>(); + final ObjectArrayList result = new ObjectArrayList<>(); final Multiplication m2 = (Multiplication) fnc.getParameter2(); final Function a = m2.getParameter1(); final Function x = fnc.getParameter1(); diff --git a/src/org/warp/picalculator/math/rules/methods/DivisionRule1.java b/src/org/warp/picalculator/math/rules/methods/DivisionRule1.java index 577d22ee..f538d8c4 100644 --- a/src/org/warp/picalculator/math/rules/methods/DivisionRule1.java +++ b/src/org/warp/picalculator/math/rules/methods/DivisionRule1.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules.methods; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.functions.Multiplication; @@ -22,10 +22,10 @@ public class DivisionRule1 { return f.getParameter1().isSimplified() && f.getParameter2().isSimplified() && !(f.getParameter1() instanceof Number && f.getParameter2() instanceof Number) && getFirstWorkingDivisionCouple(getDivisionElements(f)) != null; } - public static ArrayList execute(Division f) throws Error { + public static ObjectArrayList execute(Division f) throws Error { final MathContext root = f.getMathContext(); Function result; - final ArrayList elements = getDivisionElements(f); + final ObjectArrayList elements = getDivisionElements(f); final int[] workingElementCouple = getFirstWorkingDivisionCouple(elements); final Function elem1 = elements.get(workingElementCouple[0]); final Function elem2 = elements.get(workingElementCouple[1]); @@ -42,13 +42,13 @@ public class DivisionRule1 { result = prec; - final ArrayList results = new ArrayList<>(); + final ObjectArrayList results = new ObjectArrayList<>(); results.add(result); return results; } - private static ArrayList getDivisionElements(Division division) { - final ArrayList elementsNumerator = new ArrayList<>(); + private static ObjectArrayList getDivisionElements(Division division) { + final ObjectArrayList elementsNumerator = new ObjectArrayList<>(); Function numMult = division.getParameter1(); while (numMult instanceof Multiplication) { elementsNumerator.add(((Multiplication) numMult).getParameter1()); @@ -56,7 +56,7 @@ public class DivisionRule1 { } elementsNumerator.add(numMult); - final ArrayList elementsDenominator = new ArrayList<>(); + final ObjectArrayList elementsDenominator = new ObjectArrayList<>(); Function denomMult = division.getParameter1(); while (denomMult instanceof Multiplication) { elementsDenominator.add(((Multiplication) denomMult).getParameter1()); @@ -67,7 +67,7 @@ public class DivisionRule1 { return elements; } - private static int[] getFirstWorkingDivisionCouple(ArrayList elements) { + private static int[] getFirstWorkingDivisionCouple(ObjectArrayList elements) { final int size = elements.size(); Function a; Function b; diff --git a/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java b/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java index 21d11cb8..39d1796f 100644 --- a/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java +++ b/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.rules.methods; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -21,10 +21,10 @@ public class MultiplicationMethod1 { return ((Multiplication) f).getParameter1().isSimplified() && ((Multiplication) f).getParameter2().isSimplified() && !(((Multiplication) f).getParameter1() instanceof Number && ((Multiplication) f).getParameter2() instanceof Number) && getFirstWorkingMultiplicationCouple(getMultiplicationElements(f)) != null; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { Function result; final MathContext root = f.getMathContext(); - final ArrayList elements = getMultiplicationElements(f); + final ObjectArrayList elements = getMultiplicationElements(f); final int[] workingElementCouple = getFirstWorkingMultiplicationCouple(elements); final Function elem1 = elements.get(workingElementCouple[0]); final Function elem2 = elements.get(workingElementCouple[1]); @@ -41,13 +41,13 @@ public class MultiplicationMethod1 { result = prec; - final ArrayList results = new ArrayList<>(); + final ObjectArrayList results = new ObjectArrayList<>(); results.add(result); return results; } - private static ArrayList getMultiplicationElements(Function mult) { - final ArrayList elements = new ArrayList<>(); + private static ObjectArrayList getMultiplicationElements(Function mult) { + final ObjectArrayList elements = new ObjectArrayList<>(); while (mult instanceof Multiplication) { elements.add(((Multiplication) mult).getParameter1()); mult = ((Multiplication) mult).getParameter2(); @@ -56,7 +56,7 @@ public class MultiplicationMethod1 { return elements; } - private static int[] getFirstWorkingMultiplicationCouple(ArrayList elements) { + private static int[] getFirstWorkingMultiplicationCouple(ObjectArrayList elements) { final int size = elements.size(); Function a; Function b; diff --git a/src/org/warp/picalculator/math/rules/methods/SumMethod1.java b/src/org/warp/picalculator/math/rules/methods/SumMethod1.java index 042b719c..8d362101 100644 --- a/src/org/warp/picalculator/math/rules/methods/SumMethod1.java +++ b/src/org/warp/picalculator/math/rules/methods/SumMethod1.java @@ -1,7 +1,7 @@ package org.warp.picalculator.math.rules.methods; import java.math.BigDecimal; -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.MathContext; @@ -26,10 +26,10 @@ public class SumMethod1 { return (f instanceof Sum || f instanceof Subtraction) && ((FunctionOperator) f).getParameter1().isSimplified() && ((FunctionOperator) f).getParameter2().isSimplified() && !(((FunctionOperator) f).getParameter1() instanceof Number && ((FunctionOperator) f).getParameter2() instanceof Number) && getFirstWorkingSumCouple(root, getSumElements(f)) != null; } - public static ArrayList execute(Function f) throws Error { + public static ObjectArrayList execute(Function f) throws Error { Function result; final MathContext root = f.getMathContext(); - final ArrayList elements = getSumElements(f); + final ObjectArrayList elements = getSumElements(f); final int[] workingElementCouple = getFirstWorkingSumCouple(root, elements); final Function elem1 = elements.get(workingElementCouple[0]); final Function elem2 = elements.get(workingElementCouple[1]); @@ -54,14 +54,14 @@ public class SumMethod1 { result = prec; - final ArrayList results = new ArrayList<>(); + final ObjectArrayList results = new ObjectArrayList<>(); results.add(result); return results; } - private static ArrayList getSumElements(Function sum) { + private static ObjectArrayList getSumElements(Function sum) { final MathContext root = sum.getMathContext(); - final ArrayList elements = new ArrayList<>(); + final ObjectArrayList elements = new ObjectArrayList<>(); while (sum instanceof Sum || sum instanceof Subtraction) { if (sum instanceof Sum) { elements.add(((FunctionOperator) sum).getParameter2()); @@ -74,7 +74,7 @@ public class SumMethod1 { return elements; } - private static int[] getFirstWorkingSumCouple(MathContext root, ArrayList elements) { + private static int[] getFirstWorkingSumCouple(MathContext root, ObjectArrayList elements) { final int size = elements.size(); Function a; Function b;