jpayne@68
|
1 package fun;
|
jpayne@68
|
2
|
jpayne@68
|
3 import java.io.PrintStream;
|
jpayne@68
|
4 import java.util.Locale;
|
jpayne@68
|
5
|
jpayne@68
|
6 import shared.Parse;
|
jpayne@68
|
7 import shared.Parser;
|
jpayne@68
|
8 import shared.PreParser;
|
jpayne@68
|
9 import shared.ReadStats;
|
jpayne@68
|
10 import shared.Shared;
|
jpayne@68
|
11 import shared.Timer;
|
jpayne@68
|
12
|
jpayne@68
|
13 public class Calc {
|
jpayne@68
|
14
|
jpayne@68
|
15 /*--------------------------------------------------------------*/
|
jpayne@68
|
16 /*---------------- Initialization ----------------*/
|
jpayne@68
|
17 /*--------------------------------------------------------------*/
|
jpayne@68
|
18
|
jpayne@68
|
19 /**
|
jpayne@68
|
20 * Code entrance from the command line.
|
jpayne@68
|
21 * @param args Command line arguments
|
jpayne@68
|
22 */
|
jpayne@68
|
23 public static void main(String[] args){
|
jpayne@68
|
24 Timer t=new Timer();
|
jpayne@68
|
25 Calc x=new Calc(args);
|
jpayne@68
|
26 x.process(t);
|
jpayne@68
|
27
|
jpayne@68
|
28 //Close the print stream if it was redirected
|
jpayne@68
|
29 Shared.closeStream(x.outstream);
|
jpayne@68
|
30 }
|
jpayne@68
|
31
|
jpayne@68
|
32 /**
|
jpayne@68
|
33 * Constructor.
|
jpayne@68
|
34 * @param args Command line arguments
|
jpayne@68
|
35 */
|
jpayne@68
|
36 public Calc(String[] args){
|
jpayne@68
|
37
|
jpayne@68
|
38 {//Preparse block for help, config files, and outstream
|
jpayne@68
|
39 PreParser pp=new PreParser(args, getClass(), false);
|
jpayne@68
|
40 args=pp.args;
|
jpayne@68
|
41 outstream=pp.outstream;
|
jpayne@68
|
42 }
|
jpayne@68
|
43
|
jpayne@68
|
44 //Create a parser object
|
jpayne@68
|
45 Parser parser=new Parser();
|
jpayne@68
|
46
|
jpayne@68
|
47 //Parse each argument
|
jpayne@68
|
48 for(int i=0; i<args.length; i++){
|
jpayne@68
|
49 String arg=args[i];
|
jpayne@68
|
50
|
jpayne@68
|
51 //Break arguments into their constituent parts, in the form of "a=b"
|
jpayne@68
|
52 String[] split=arg.split("=");
|
jpayne@68
|
53 String a=split[0].toLowerCase();
|
jpayne@68
|
54 String b=split.length>1 ? split[1] : null;
|
jpayne@68
|
55
|
jpayne@68
|
56 if(a.equals("verbose")){
|
jpayne@68
|
57 verbose=Parse.parseBoolean(b);
|
jpayne@68
|
58 }else if(a.equals("parse_flag_goes_here")){
|
jpayne@68
|
59 long fake_variable=Parse.parseKMG(b);
|
jpayne@68
|
60 //Set a variable here
|
jpayne@68
|
61 }else if(a.equals("num") || a.equals("numstats")){
|
jpayne@68
|
62 numStats=Integer.parseInt(b);
|
jpayne@68
|
63 }else if(parser.parse(arg, a, b)){//Parse standard flags in the parser
|
jpayne@68
|
64 //do nothing
|
jpayne@68
|
65 }else{
|
jpayne@68
|
66 outstream.println("Unknown parameter "+args[i]);
|
jpayne@68
|
67 assert(false) : "Unknown parameter "+args[i];
|
jpayne@68
|
68 }
|
jpayne@68
|
69 }
|
jpayne@68
|
70
|
jpayne@68
|
71 {//Process parser fields
|
jpayne@68
|
72
|
jpayne@68
|
73 overwrite=ReadStats.overwrite=parser.overwrite;
|
jpayne@68
|
74 append=ReadStats.append=parser.append;
|
jpayne@68
|
75
|
jpayne@68
|
76 out1=parser.out1;
|
jpayne@68
|
77 }
|
jpayne@68
|
78 }
|
jpayne@68
|
79
|
jpayne@68
|
80
|
jpayne@68
|
81 /*--------------------------------------------------------------*/
|
jpayne@68
|
82 /*---------------- Outer Methods ----------------*/
|
jpayne@68
|
83 /*--------------------------------------------------------------*/
|
jpayne@68
|
84
|
jpayne@68
|
85 /** Create read streams and process all data */
|
jpayne@68
|
86 void process(Timer t){
|
jpayne@68
|
87
|
jpayne@68
|
88 //Process the read stream
|
jpayne@68
|
89 processInner(numStats);
|
jpayne@68
|
90
|
jpayne@68
|
91 if(verbose){outstream.println("Finished; closing streams.");}
|
jpayne@68
|
92
|
jpayne@68
|
93 //Report timing and results
|
jpayne@68
|
94 {
|
jpayne@68
|
95 t.stop();
|
jpayne@68
|
96 outstream.println("Time: \t"+t);
|
jpayne@68
|
97 }
|
jpayne@68
|
98
|
jpayne@68
|
99 //Throw an exception of there was an error in a thread
|
jpayne@68
|
100 if(errorState){
|
jpayne@68
|
101 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
|
jpayne@68
|
102 }
|
jpayne@68
|
103 }
|
jpayne@68
|
104
|
jpayne@68
|
105 /** Iterate through the reads */
|
jpayne@68
|
106 void processInner(int numStats){
|
jpayne@68
|
107 int bits=numStats*5;
|
jpayne@68
|
108 final int iters=1<<bits;
|
jpayne@68
|
109 final int buckets=1+31*numStats;
|
jpayne@68
|
110 int[] counts=new int[buckets];
|
jpayne@68
|
111 for(int i=0; i<iters; i++){
|
jpayne@68
|
112 counts[sum(i)]++;
|
jpayne@68
|
113 }
|
jpayne@68
|
114 int[] cumulative=new int[buckets];
|
jpayne@68
|
115 cumulative[0]=counts[0];
|
jpayne@68
|
116 for(int i=1; i<buckets; i++){
|
jpayne@68
|
117 cumulative[i]=cumulative[i-1]+counts[i];
|
jpayne@68
|
118 }
|
jpayne@68
|
119 //StringBuilder sb=new StringBuilder();
|
jpayne@68
|
120 final double mult=100.0/iters;
|
jpayne@68
|
121 for(int i=0; i<buckets; i++){
|
jpayne@68
|
122 String s=(String.format(Locale.ROOT, "%d\t%.4f%%\n", i, cumulative[i]*mult));
|
jpayne@68
|
123 System.out.print(s);
|
jpayne@68
|
124 }
|
jpayne@68
|
125 }
|
jpayne@68
|
126
|
jpayne@68
|
127 int sum(int stats){
|
jpayne@68
|
128 int sum=0;
|
jpayne@68
|
129 while(stats>0){
|
jpayne@68
|
130 sum+=(stats&0x1F);
|
jpayne@68
|
131 stats>>>=5;
|
jpayne@68
|
132 }
|
jpayne@68
|
133 return sum;
|
jpayne@68
|
134 }
|
jpayne@68
|
135
|
jpayne@68
|
136 /*--------------------------------------------------------------*/
|
jpayne@68
|
137 /*---------------- Inner Methods ----------------*/
|
jpayne@68
|
138 /*--------------------------------------------------------------*/
|
jpayne@68
|
139
|
jpayne@68
|
140 /*--------------------------------------------------------------*/
|
jpayne@68
|
141 /*---------------- Fields ----------------*/
|
jpayne@68
|
142 /*--------------------------------------------------------------*/
|
jpayne@68
|
143
|
jpayne@68
|
144 /** Primary output file path */
|
jpayne@68
|
145 private String out1="stdout.txt";
|
jpayne@68
|
146
|
jpayne@68
|
147 private int numStats=6;
|
jpayne@68
|
148
|
jpayne@68
|
149 /*--------------------------------------------------------------*/
|
jpayne@68
|
150 /*---------------- Common Fields ----------------*/
|
jpayne@68
|
151 /*--------------------------------------------------------------*/
|
jpayne@68
|
152
|
jpayne@68
|
153 /** Print status messages to this output stream */
|
jpayne@68
|
154 private PrintStream outstream=System.err;
|
jpayne@68
|
155 /** Print verbose messages */
|
jpayne@68
|
156 public static boolean verbose=false;
|
jpayne@68
|
157 /** True if an error was encountered */
|
jpayne@68
|
158 public boolean errorState=false;
|
jpayne@68
|
159 /** Overwrite existing output files */
|
jpayne@68
|
160 private boolean overwrite=false;
|
jpayne@68
|
161 /** Append to existing output files */
|
jpayne@68
|
162 private boolean append=false;
|
jpayne@68
|
163
|
jpayne@68
|
164 }
|