From f1615de202c120d50397965299427d67add2607f Mon Sep 17 00:00:00 2001 From: aspwil Date: Tue, 3 Nov 2020 07:15:42 -0700 Subject: [PATCH] 1.0.3 update Added step feature, now you can step though the balls movement one piece at a time Fixed error when an empty line is included in the code Fixed error when highest dimension was reduced to zero then that position in the array was called. Added -max tag (default 10k) for max number of step to run Added step counter to log Updated tag code to support multiple tags at the same tag. Added -i flag to list info about program instructions, steps, parse time, sim time, active memory Fixed mem leak in pos class, no longer create an extra array list with every trim call Changed the way the program exits when finished with sim --- src/ndballsim/Main.java | 10 ++++-- src/ndballsim/Pos.java | 8 +++-- src/ndballsim/Simulator.java | 67 +++++++++++++++++++++++++++++------- 3 files changed, 67 insertions(+), 18 deletions(-) diff --git a/src/ndballsim/Main.java b/src/ndballsim/Main.java index df325be..b6d7666 100644 --- a/src/ndballsim/Main.java +++ b/src/ndballsim/Main.java @@ -9,6 +9,7 @@ public class Main { public static void main(String[] args) { boolean step = false; boolean log = false; + boolean info = false; int max = 10000; String version = "V1.0.3"; String help = "NDBall Simulator " + version + "\n" @@ -20,7 +21,8 @@ public static void main(String[] args) { + " when memory cells are written to etc\n" + "-d -docs : This shows some basic documentation about how to program in NDBall\n" + "-s -step : goes through the sim one step at a time, automaticly enables log\n" - + "-m -max (num) : only runs a max number of steps for the ball (default 10k) use a negative number for unlimited steps"; + + "-m -max (num) : only runs a max number of steps for the ball (default 10k) use a negative number for unlimited steps\n" + + "-i -info : spits out info about the program after it completes"; //no insput strings given if (args.length == 0) { System.out.println(help); @@ -48,6 +50,10 @@ public static void main(String[] args) { } i++; break; + case "-i": + case "-info": + info = true; + break; case "-d": case "-docs": System.out.println("the ball starts at 0,0,0...\n" @@ -102,7 +108,7 @@ public static void main(String[] args) { + ""); break; default: - Simulator.run(args[i], max, log, step); + Simulator.run(args[i], max, log, step, info); System.exit(0); break; diff --git a/src/ndballsim/Pos.java b/src/ndballsim/Pos.java index 88ba8bc..683b9c4 100644 --- a/src/ndballsim/Pos.java +++ b/src/ndballsim/Pos.java @@ -11,6 +11,7 @@ public class Pos { //this array list stores values like this (length_in_dim_0, length_in_dim_1, length_in_dim_2...) //this is specificly private to allow for more efficent Pos classes to be able to be used with replacement of this class, withought having to reprogram everything private ArrayList list; + private int sum; //initalization public Pos(int... ints) { @@ -22,7 +23,8 @@ public Pos(int... ints) { } //set the object to the array list generated this.list = arraylist; - //remove excess zeros from the end of the + arraylist = null;//forces this to be garbge collected (jsut in case) + //remove excess zeros from the end of the line trim(); } @@ -60,14 +62,14 @@ public boolean equals(Pos checkPos) { private void trim() { //this deals with if the length is zero in all dimntions //add all the ints in the array together - int sum = 0; + sum = 0; for (int i : list) { sum += i; } //if sum = 0 if (sum == 0) { //set list to (0) - list = new ArrayList<>(); + list.clear(); list.add(0); //exit the function return; diff --git a/src/ndballsim/Simulator.java b/src/ndballsim/Simulator.java index 026bffe..66fff66 100644 --- a/src/ndballsim/Simulator.java +++ b/src/ndballsim/Simulator.java @@ -9,30 +9,38 @@ public class Simulator { private static boolean log = false; + private static long startTime; + private static long parseTime; + // ... the code being measured ... - public static void run(String file, int max, boolean doLog, boolean step) { + public static void run(String file, int max, boolean doLog, boolean step, boolean infoTag) { + long startTime = System.nanoTime();//start measuring parcer time + Instr[] instrs = Parser.parse(file);//this is the list of instructions + parseTime = System.nanoTime() - startTime;//stop measuring parcer time + //begine messuring Sim time + startTime = System.nanoTime(); log = doLog; Scanner in = new Scanner(System.in); //the scanner used for input from console String input; // this will be used to hold the input int stepsDone = 0; //how many teps we have done - - log("MAX: "+max); + + log("MAX: " + max); Pos ball = new Pos(0); //the ball itself int ballVal = 0; //the value of the ball int[] movement = new int[2]; //this represent the balls movement, its [dimention_number, ammount] so if it moving forwards in dim 4 then its [4,1] and backwards is [4,-1] - + int newVal; log("Attempting parsing"); - Instr[] instrs = Parser.parse(file);//this is the list of instructions + log("Parsing completed"); log("Starting Simulation"); while (true) { //if we are steping through once at a time if (step) { //ask for input (pauses program) - System.out.println("next step? enter yes | ctrl+c no"); + System.out.println("advance? enter|ctrl+c"); in.nextLine(); } @@ -61,8 +69,8 @@ public static void run(String file, int max, boolean doLog, boolean step) { break; //end the program case "E": - log("program ended"); - System.exit(0); + log("Program ended"); + exit(infoTag, stepsDone, instrs.length); break; //this get a input number from the console and set the balls value to it case "%": @@ -201,12 +209,11 @@ public static void run(String file, int max, boolean doLog, boolean step) { } stepsDone++; - log("Step "+stepsDone+" done"); - if(max >= 0 && stepsDone >= max){ - warn("Program terminated: reached max steps ("+max+"), to disable this use -m -1"); - System.exit(0); + log("Step " + stepsDone + " done"); + if (max >= 0 && stepsDone >= max) { + warn("Program terminated: reached max steps (" + max + "), to disable this use -m -1"); + exit(infoTag, stepsDone, instrs.length); } - } } @@ -228,4 +235,38 @@ private static void log(String str) { System.out.println("LOG: " + str); } } + + //this exits the program doing nececary exit stuff + private static void exit(boolean outputInfo, int steps, int instNum) { + if (outputInfo) { + System.out.println("\n\n***** Sim Info *****\n" + + "Parse Time: " + + ((double) parseTime / 1_000_000.0) + + "ms\n" + + "Sim Time: " + + ((double) (System.nanoTime() - startTime) / 1_000_000.0) + + "ms\n" + + "Steps: " + + steps + + "\n" + + "Num of Instr: " + + instNum + + "\n" + + memoryStats() + ); + } + System.exit(0); + } + + //memory useage statisitics + public static String memoryStats() { + System.gc(); + int kb = 1024; + // get Runtime instance + Runtime instance = Runtime.getRuntime(); + // used memory + return "Active Mem: ~" + (instance.totalMemory() - instance.freeMemory()) / kb + "KB\n"; + + } + }