| 1 |
package fun; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
|
| 5 |
import shared.Parse; |
| 6 |
import shared.Parser; |
| 7 |
import shared.PreParser; |
| 8 |
import shared.Shared; |
| 9 |
import shared.Timer; |
| 10 |
import shared.Tools; |
| 11 |
import tracker.ReadStats; |
| 12 |
|
| 13 |
public class Calc { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public static void main(String[] args){ |
| 24 |
Timer t=new Timer(); |
| 25 |
Calc x=new Calc(args); |
| 26 |
x.process(t); |
| 27 |
|
| 28 |
|
| 29 |
Shared.closeStream(x.outstream); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public Calc(String[] args){ |
| 37 |
|
| 38 |
{ |
| 39 |
PreParser pp=new PreParser(args, getClass(), false); |
| 40 |
args=pp.args; |
| 41 |
outstream=pp.outstream; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
Parser parser=new Parser(); |
| 46 |
|
| 47 |
|
| 48 |
for(int i=0; i<args.length; i++){ |
| 49 |
String arg=args[i]; |
| 50 |
|
| 51 |
|
| 52 |
String[] split=arg.split("="); |
| 53 |
String a=split[0].toLowerCase(); |
| 54 |
String b=split.length>1 ? split[1] : null; |
| 55 |
|
| 56 |
if(a.equals("verbose")){ |
| 57 |
verbose=Parse.parseBoolean(b); |
| 58 |
}else if(a.equals("parse_flag_goes_here")){ |
| 59 |
long fake_variable=Parse.parseKMG(b); |
| 60 |
|
| 61 |
}else if(a.equals("num") || a.equals("numstats")){ |
| 62 |
numStats=Integer.parseInt(b); |
| 63 |
}else if(parser.parse(arg, a, b)){ |
| 64 |
|
| 65 |
}else{ |
| 66 |
outstream.println("Unknown parameter "+args[i]); |
| 67 |
assert(false) : "Unknown parameter "+args[i]; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
{ |
| 72 |
|
| 73 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 74 |
append=ReadStats.append=parser.append; |
| 75 |
|
| 76 |
out1=parser.out1; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
void process(Timer t){ |
| 87 |
|
| 88 |
|
| 89 |
processInner(numStats); |
| 90 |
|
| 91 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 92 |
|
| 93 |
|
| 94 |
{ |
| 95 |
t.stop(); |
| 96 |
outstream.println("Time: \t"+t); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
if(errorState){ |
| 101 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
void processInner(int numStats){ |
| 107 |
int bits=numStats*5; |
| 108 |
final int iters=1<<bits; |
| 109 |
final int buckets=1+31*numStats; |
| 110 |
int[] counts=new int[buckets]; |
| 111 |
for(int i=0; i<iters; i++){ |
| 112 |
counts[sum(i)]++; |
| 113 |
} |
| 114 |
int[] cumulative=new int[buckets]; |
| 115 |
cumulative[0]=counts[0]; |
| 116 |
for(int i=1; i<buckets; i++){ |
| 117 |
cumulative[i]=cumulative[i-1]+counts[i]; |
| 118 |
} |
| 119 |
|
| 120 |
final double mult=100.0/iters; |
| 121 |
for(int i=0; i<buckets; i++){ |
| 122 |
String s=(Tools.format("%d\t%.4f%%\n", i, cumulative[i]*mult)); |
| 123 |
System.out.print(s); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
int sum(int stats){ |
| 128 |
int sum=0; |
| 129 |
while(stats>0){ |
| 130 |
sum+=(stats&0x1F); |
| 131 |
stats>>>=5; |
| 132 |
} |
| 133 |
return sum; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private String out1="stdout.txt"; |
| 146 |
|
| 147 |
private int numStats=6; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
private PrintStream outstream=System.err; |
| 155 |
|
| 156 |
public static boolean verbose=false; |
| 157 |
|
| 158 |
public boolean errorState=false; |
| 159 |
|
| 160 |
private boolean overwrite=true; |
| 161 |
|
| 162 |
private boolean append=false; |
| 163 |
|
| 164 |
} |