00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 namespace Antlr.Runtime.Misc
00036 {
00037 using System;
00038 using FileInfo = System.IO.FileInfo;
00039 using DirectoryInfo = System.IO.DirectoryInfo;
00040 using StreamWriter = System.IO.StreamWriter;
00041 using Path = System.IO.Path;
00042 using IOException = System.IO.IOException;
00043
00050 public class Stats
00051 {
00065 public static double Stddev(int[] X)
00066 {
00067 int m = X.Length;
00068 if (m <= 1)
00069 {
00070 return 0;
00071 }
00072 double xbar = Avg(X);
00073 double s2 = 0.0;
00074 for (int i = 0; i < m; i++)
00075 {
00076 s2 += (X[i] - xbar) * (X[i] - xbar);
00077 }
00078 s2 = s2 / (m - 1);
00079 return Math.Sqrt(s2);
00080 }
00081
00083 public static double Avg(int[] X)
00084 {
00085 double xbar = 0.0;
00086 int m = X.Length;
00087 if (m == 0)
00088 {
00089 return 0;
00090 }
00091 for (int i = 0; i < m; i++)
00092 {
00093 xbar += X[i];
00094 }
00095 if (xbar >= 0.0)
00096 {
00097 return xbar / m;
00098 }
00099 return 0.0;
00100 }
00101
00102 public static int Min(int[] X)
00103 {
00104 int min = Int32.MaxValue;
00105 int m = X.Length;
00106 if (m == 0)
00107 {
00108 return 0;
00109 }
00110 for (int i = 0; i < m; i++)
00111 {
00112 if (X[i] < min)
00113 {
00114 min = X[i];
00115 }
00116 }
00117 return min;
00118 }
00119
00120 public static int Max(int[] X)
00121 {
00122 int max = Int32.MinValue;
00123 int m = X.Length;
00124 if (m == 0)
00125 {
00126 return 0;
00127 }
00128 for (int i = 0; i < m; i++)
00129 {
00130 if (X[i] > max)
00131 {
00132 max = X[i];
00133 }
00134 }
00135 return max;
00136 }
00137
00138 public static int Sum(int[] X)
00139 {
00140 int s = 0;
00141 int m = X.Length;
00142 if (m == 0)
00143 {
00144 return 0;
00145 }
00146 for (int i = 0; i < m; i++)
00147 {
00148 s += X[i];
00149 }
00150 return s;
00151 }
00152
00153 public static void WriteReport(string filename, string data)
00154 {
00155 string absoluteFilename = GetAbsoluteFileName(filename);
00156 FileInfo f = new FileInfo(absoluteFilename);
00157 f.Directory.Create();
00158
00159 try
00160 {
00161 StreamWriter w = new StreamWriter(f.FullName, true);
00162 w.WriteLine(data);
00163 w.Close();
00164 }
00165 catch (IOException ioe)
00166 {
00167 ErrorManager.InternalError("can't write stats to " + absoluteFilename,
00168 ioe);
00169 }
00170 }
00171
00172 public static string GetAbsoluteFileName(string filename)
00173 {
00174 return Path.Combine(
00175 Path.Combine(Environment.CurrentDirectory, Constants.ANTLRWORKS_DIR),
00176 filename);
00177 }
00178 }
00179 }