switched to a faster stack

This commit is contained in:
zeroflag 2021-08-08 11:46:20 +02:00
parent d77add81df
commit 7ec3aea64c
4 changed files with 40 additions and 35 deletions

View file

@ -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<Obj> 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 {

View file

@ -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<Obj> {
public LStack() {
super(10);
}
public LStack(final Collection<Obj> 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);
}
}
}

View file

@ -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<Obj> {
public LStack() {
super(10);
}
public LStack(final Collection<Obj> 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);
}
}

View file

@ -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");