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.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Stack;
public class FclStack { public class FclStack {
private static final Gson gson; private static final Gson gson;
private final Stack<Obj> stack = new Stack<>(); private final LStack stack = new LStack();
static { static {
FclTypeAdapter typeAdapter = new FclTypeAdapter(); FclTypeAdapter typeAdapter = new FclTypeAdapter();
@ -40,7 +40,7 @@ public class FclStack {
} }
public boolean empty() { public boolean empty() {
return stack.empty(); return stack.size() == 0;
} }
public void clean() { public void clean() {
@ -81,8 +81,7 @@ public class FclStack {
stream = fileStore.open(fileName(id)); stream = fileStore.open(fileName(id));
Obj[] loaded = gson.fromJson(new BufferedReader(new InputStreamReader(stream)), Obj[].class); Obj[] loaded = gson.fromJson(new BufferedReader(new InputStreamReader(stream)), Obj[].class);
stack.clear(); stack.clear();
for (Obj each : loaded) stack.addAll(Arrays.asList(loaded));
stack.add(each);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {

View file

@ -6,9 +6,7 @@ import com.vectron.fcl.types.Obj;
import com.vectron.fcl.types.Str; import com.vectron.fcl.types.Str;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.EmptyStackException;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -330,33 +328,5 @@ public class Juggler {
} }
return false; 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 { private void resetForth() throws IOException {
fcl = new Fcl(new FclStack(), 524288, transcript); fcl = new Fcl(new FclStack(), 524288, transcript);
fcl.addPrimitive("exchange", () -> {}, false);
fcl.addPrimitive("aux>", () -> {}, false);
fcl.addPrimitive(">aux", () -> {}, false);
load("core.forth"); load("core.forth");
load("ops.forth"); load("ops.forth");
load("locals.forth"); load("locals.forth");