/// <summary> /// This method performs the BurrowsWheelerTransform algorithm on the input SuffixArray. /// </summary> /// <returns></returns> public BurrowsWheelerTransformBin(SuffixArrayBin suffixArray) { textLength = suffixArray.InputText.Length; List<BitArray> bwtTemp = (suffixArray.Suffixes.Select( i => new { i, lenSuffix = i.Length }).Select( @t => @t.i.Append(suffixArray.InputText.Substring(0, textLength - @t.lenSuffix)) ) ).ToList(); bwtTemp.Sort( (BitArray bA, BitArray bB) => { int order = 0; for (int i = 0; i < Math.Max(bA.Length, bB.Length); i++) { if (i >= bA.Length) { order = -1; break; } if (i >= bB.Length) { order = 1; break; } if (!bA[i] && bB[i]) { order = -1; break; } if (bA[i] && !bB[i]) { order = 1; break; } } return order; } ); BitArray F = new BitArray(bwtTemp.Count); BitArray L = new BitArray(bwtTemp.Count); for (int i = 0; i < bwtTemp.Count; i++) { F[i] = bwtTemp[i][0]; L[i] = bwtTemp[i][bwtTemp[i].Length - 1]; } waveletTree = new WaveletTreeBin(L); WaveletTreeBin waveletTreeF = new WaveletTreeBin(F); C = new Dictionary<bool, int>(2); if (F.HasZeros()) C[false] = waveletTreeF.Select(false, 1); else C[false] = -1; if (F.HasOnes()) C[true] = waveletTreeF.Select(true, 1); else C[true] = -1; }
public static void RunBinaryTestOnce(BitArray text, BitArray pattern, int times = 10000) { Console.WriteLine("T: " + BitArrayToString(text)); Console.WriteLine("P: " + BitArrayToString(pattern)); Console.WriteLine(); SuffixArrayBin sa = new SuffixArrayBin(text); BurrowsWheelerTransformBin bwt = new BurrowsWheelerTransformBin(sa); int answer = 0; // BWT Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < times; i++) { answer = bwt.CountOccurrences(pattern); } double time = ((double)sw.ElapsedMilliseconds) / times; Console.WriteLine("BWT:\n===="); Console.WriteLine(" Time: " + time + " ms"); Console.WriteLine(" Matches: " + answer.ToString()); Console.WriteLine(); // Brute Force Stopwatch sw2 = Stopwatch.StartNew(); for (int i = 0; i < times; i++) { answer = BruteForce.Run(text, pattern); } double time2 = ((double)sw2.ElapsedMilliseconds) / times; Console.WriteLine("BF:\n===="); Console.WriteLine(" Time: " + time2 + " ms"); Console.WriteLine(" Matches: " + answer.ToString()); Console.WriteLine(); }
public static void RunBinaryTestFiles() { const string DirectoryString = @"..\.."; var gen = new Random(); var files = Directory.EnumerateFiles(DirectoryString + @"\GenFiles\", "*").Where(u => u.Contains(@"GenFiles\B_")); StringBuilder output = new StringBuilder(); int fileNum = 0; foreach (var file in files) { var ob = new StatObj { FileName = file }; var commandLineInterpreter = new CommandInterpreter() { Encoding = Encoding.Default, Path = ob.FileName }; var fileReader = new BinaryFileReader(commandLineInterpreter); var sa = new SuffixArrayBin(fileReader.Data); var bwt = new BurrowsWheelerTransformBin(sa); for (var i = 1; i < Math.Min(20, ob.FileSize / 2); i++) { ob.PatternSize = i; BitArray pattern = fileReader.Data.Substring( (int)( (ob.FileSize - ob.PatternSize) * gen.NextDouble() ), ob.PatternSize ); // Settings and timers //=============================================================== int times = 100; Stopwatch sw; //=============================================================== // BWT //=============================================================== StatObj bwtStat = ob; bwtStat.EndTimeTicks = 0; bwtStat.Results = 0; bwtStat.RuntimeTicks = 0; bwtStat.StartTimeTicks = 0; sw = Stopwatch.StartNew(); for (int t = 0; t < times; t++) { bwtStat.Results = bwt.CountOccurrences(pattern); } sw.Stop(); bwtStat.RuntimeTicks = sw.ElapsedMilliseconds; //=============================================================== // BF //=============================================================== StatObj bfStat = ob; bfStat.EndTimeTicks = 0; bfStat.Results = 0; bfStat.RuntimeTicks = 0; bfStat.StartTimeTicks = 0; sw = Stopwatch.StartNew(); for (int t = 0; t < times; t++) { bfStat.Results = BruteForce.Run(fileReader.Data, pattern); } sw.Stop(); bfStat.RuntimeTicks = sw.ElapsedMilliseconds; //=============================================================== // Print Results //=============================================================== int m = ob.PatternSize; int n = ob.FileSize; output.AppendLine(String.Format("BWT,{0},{1},{2},{3},{4}", bwtStat.RuntimeTicks, m, n, bwtStat.Results, "Binary")); output.AppendLine(String.Format("BruteForce,{0},{1},{2},{3},{4}", bfStat.RuntimeTicks, m, n, bfStat.Results, "Binary")); //=============================================================== } fileNum++; Console.WriteLine(((((double)fileNum) / files.ToArray<string>().Length) * 100).ToString() + " %"); } using (StreamWriter writer = new StreamWriter(DirectoryString + @"\GenFiles\BinaryResults.csv")) { writer.Write(output.ToString()); } }