From 7ec3aea64c84c85ed6ba57965f3fb7fcc39266d3 Mon Sep 17 00:00:00 2001 From: zeroflag Date: Sun, 8 Aug 2021 11:46:20 +0200 Subject: [PATCH] switched to a faster stack --- src/main/java/com/vectron/fcl/FclStack.java | 9 +++--- src/main/java/com/vectron/fcl/Juggler.java | 30 ------------------- src/main/java/com/vectron/fcl/LStack.java | 33 +++++++++++++++++++++ src/test/java/com/vectron/fcl/FclTest.java | 3 ++ 4 files changed, 40 insertions(+), 35 deletions(-) create mode 100644 src/main/java/com/vectron/fcl/LStack.java diff --git a/src/main/java/com/vectron/fcl/FclStack.java b/src/main/java/com/vectron/fcl/FclStack.java index 878192e..e56716e 100644 --- a/src/main/java/com/vectron/fcl/FclStack.java +++ b/src/main/java/com/vectron/fcl/FclStack.java @@ -9,13 +9,13 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Stack; public class FclStack { private static final Gson gson; - private final Stack stack = new Stack<>(); + private final LStack stack = new LStack(); static { FclTypeAdapter typeAdapter = new FclTypeAdapter(); @@ -40,7 +40,7 @@ public class FclStack { } public boolean empty() { - return stack.empty(); + return stack.size() == 0; } public void clean() { @@ -81,8 +81,7 @@ public class FclStack { stream = fileStore.open(fileName(id)); Obj[] loaded = gson.fromJson(new BufferedReader(new InputStreamReader(stream)), Obj[].class); stack.clear(); - for (Obj each : loaded) - stack.add(each); + stack.addAll(Arrays.asList(loaded)); } catch (IOException e) { throw new RuntimeException(e); } finally { diff --git a/src/main/java/com/vectron/fcl/Juggler.java b/src/main/java/com/vectron/fcl/Juggler.java index 06f7d9b..9cf3fd8 100644 --- a/src/main/java/com/vectron/fcl/Juggler.java +++ b/src/main/java/com/vectron/fcl/Juggler.java @@ -6,9 +6,7 @@ import com.vectron.fcl.types.Obj; import com.vectron.fcl.types.Str; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; -import java.util.EmptyStackException; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -330,33 +328,5 @@ public class Juggler { } return false; } - - public final class LStack extends ArrayList { - public LStack() { - super(10); - } - - public LStack(final Collection collection) { - super(collection); - } - - public void push(Obj item) { - add(item); - } - - public Obj pop() { - Obj top = peek(); - remove(size() - 1); - return top; - } - - public Obj peek() { - int size = size(); - if (size == 0) { - throw new EmptyStackException(); - } - return get(size - 1); - } - } } diff --git a/src/main/java/com/vectron/fcl/LStack.java b/src/main/java/com/vectron/fcl/LStack.java new file mode 100644 index 0000000..a0885dc --- /dev/null +++ b/src/main/java/com/vectron/fcl/LStack.java @@ -0,0 +1,33 @@ +package com.vectron.fcl; + +import com.vectron.fcl.types.Obj; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.EmptyStackException; + +public final class LStack extends ArrayList { + public LStack() { + super(10); + } + + public LStack(final Collection collection) { + super(collection); + } + + public void push(Obj item) { + add(item); + } + + public Obj pop() { + Obj top = peek(); + remove(size() - 1); + return top; + } + + public Obj peek() { + int size = size(); + if (size == 0) throw new EmptyStackException(); + return get(size - 1); + } +} diff --git a/src/test/java/com/vectron/fcl/FclTest.java b/src/test/java/com/vectron/fcl/FclTest.java index 4c508a8..fab21c6 100644 --- a/src/test/java/com/vectron/fcl/FclTest.java +++ b/src/test/java/com/vectron/fcl/FclTest.java @@ -45,6 +45,9 @@ public class FclTest { private void resetForth() throws IOException { fcl = new Fcl(new FclStack(), 524288, transcript); + fcl.addPrimitive("exchange", () -> {}, false); + fcl.addPrimitive("aux>", () -> {}, false); + fcl.addPrimitive(">aux", () -> {}, false); load("core.forth"); load("ops.forth"); load("locals.forth");