// MARK: - file splitting/sorting/output to temp files public static int FileSplit(string path, Int32 FileSize, Int32 ListSize) { var FileToProcess = new StreamReader(path); List <string> strSlice = new List <string>(); int counter = 0; string tempPath = ""; if (ListSize < FileSize) { counter = FileSize / ListSize; for (int i = 0; i < counter; i++) { tempPath = WorkPath + "\\splits\\out_slice" + i + ".txt"; for (int j = 0; j < ListSize; j++) { strSlice.Add(FileToProcess.ReadLine()); } // MARK: - random equitable file: using Quick Sort strSlice = SortingMethods.TextRecordSortedInStrings(strSlice); // MARK: - nearly sorted file: using Insertion Sort //strSlice = SortingMethods.TRSortedtoStringsByInserts(strSlice); // MARK: - filling temp file with sorted list CreateFileFromListInRAM(tempPath, strSlice, true); strSlice.Clear(); } if (FileSize % ListSize != 0) { tempPath = WorkPath + "\\splits\\out_slice" + counter + ".txt"; while (FileToProcess.Peek() != -1) { strSlice.Add(FileToProcess.ReadLine()); } strSlice = SortingMethods.TextRecordSortedInStrings(strSlice); CreateFileFromListInRAM(tempPath, strSlice, false); strSlice.Clear(); } return(counter + 1); } else if (ListSize >= FileSize) { return(1); } else { return(-1); } }
public static void Main() { // MARK: - working paths creating FileManager.CreateWorkingDirs(FileManager.WorkPath); // MARK: - creating file of known size string FileName = FileManager.WorkPath + "\\out_small.txt"; FileManager.CreateFileFromListsByAppending(); // MARK: - timer starts... var sWatch = System.Diagnostics.Stopwatch.StartNew(); // MARK: - file splitting, in case of slice sizes // MARK: - if slice size is less than file size, we go with split/external sort/merge procedure if (slicesize < fileSize) { int counter = FileManager.FileSplit(FileName, fileSize, slicesize); Console.WriteLine("slices count: {0}", counter); FileManager.MergeByQueues(FileManager.WorkPath + "\\splits\\", FileName, FileManager.WorkPath + "\\out_merged_sorted.txt"); } // MARK: - else we just sort file in RAM else { // MARK: - reading all strings to array string[] stringbuf = File.ReadAllLines(FileName); // MARK: - sorting array List <string> trSorted = SortingMethods.TextRecordSortedInStrings(stringbuf.ToList <string>()); // MARK: - writing array to output file FileName = FileManager.WorkPath + "\\out_small_sorted.txt"; FileManager.CreateFileFromListInRAM(FileName, trSorted, true); } // MARK: - timer stops. sWatch.Stop(); Console.WriteLine("time spent: {0}", sWatch.Elapsed); Console.WriteLine("strings in file: {0}", FileManager.FileSize); Console.ReadKey(); }
public static void MergeSortedFile(string tempPath, int slicesCount, string inPath, string outPath) { //List<string> Tammy = new List<string>(); //List<string> Squirrel = new List<string>(); string tempfileName = ""; StreamReader[] sr = new StreamReader[slicesCount]; //var threads=new Thread[slicesCount]; //int[] threadEmpty=new int[slicesCount]; StreamWriter swout = new StreamWriter(outPath, true); int flag; List <string> cutFirst = new List <string>(); for (int k = 0; k < FileSizeinStrings(inPath); k++) { for (int i = 0; i < slicesCount; i++) { if (File.Exists(tempfileName = tempPath + i + ".txt")) { tempfileName = tempPath + i + ".txt"; Console.WriteLine("размер временного файла: {0}", FileSizeinStrings(tempfileName)); if (sr[i] == null) { //threadEmpty[i] = i; sr[i] = new StreamReader(tempfileName); } //else if (sr[i] != null) { //if ((threads[i] == null)) //{ //var state = new StateObject<StreamReader>(sr[i], new object()); //threads[i] = new Thread(ThreadProc); //threads[i].Start(state); //} if (FileSizeinStrings(tempfileName) > 1) { //sr[threadEmpty[i]] = new StreamReader(tempfileName); TammyGlobal.Add(sr[i].ReadLine()); } else if (FileSizeinStrings(tempfileName) == 1) { TammyGlobal.Add(sr[i].ReadLine()); sr[i].Close(); File.Delete(tempfileName); } else if (FileSizeinStrings(tempfileName) == -1) { sr[i].Close(); File.Delete(tempfileName); } } } } flag = TammyGlobal.IndexOf(SortingMethods.TextRecordSortedInStrings(TammyGlobal).FirstOrDefault()); swout.WriteLine(SortingMethods.TextRecordSortedInStrings(TammyGlobal).FirstOrDefault()); Console.WriteLine("индекс минимальной записи: {0}", flag); tempfileName = tempPath + flag + ".txt"; TammyGlobal.Clear(); if (File.Exists(tempfileName)) { if (FileSizeinStrings(tempfileName) > 1) { //threads[flag].Join(); sr[flag].Close(); cutFirst = File.ReadAllLines(tempfileName).ToList <string>(); File.Delete(tempfileName); cutFirst.RemoveAt(0); File.WriteAllLines(tempfileName, cutFirst); cutFirst.Clear(); sr[flag] = new StreamReader(tempfileName); } else if (FileSizeinStrings(tempfileName) <= 1) { File.Delete(tempfileName); } } } swout.Close(); }