Esempio n. 1
0
        // 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);
            }
        }
Esempio n. 2
0
        public static void Main()
        {
            string FileName = FileManager.WorkPath + "\\" + FileManager.InputFileName;

            try
            {
                FileManager.CreateWorkingDirs(FileManager.WorkPath);

                //MARK: - uncomment if file to create. if file created, comment again, and rename file to out_small.txt.
                FileManager.CreateFileFromListsByAppending();

                // MARK: - timer starts...
                var sWatch = System.Diagnostics.Stopwatch.StartNew();

                // MARK: - if slice size is less than file size, we go with split/external sort/merge procedure
                if (slicesize < filesize)
                {
                    FileManager.FileSplit(FileName, filesize, slicesize);
                    FileManager.MergeByQueues(FileManager.WorkPath + "\\splits\\",
                                              FileName,
                                              FileManager.WorkPath + "\\out_merged_sorted.txt");
                }

                // MARK: - else we just sort file in RAM
                else
                {
                    string[]      stringbuf = File.ReadAllLines(FileName);
                    List <string> trSorted  =
                        SortingMethods.TextRecordSortedInStrings(stringbuf.ToList <string>());
                    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);
                FileManager.DeleteTemporaryDirs(FileManager.WorkPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
            }

            Console.ReadKey();
        }
Esempio n. 3
0
        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();
        }
Esempio n. 4
0
        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();
        }
Esempio n. 5
0
        // MARK: - file splitting/sorting/output to temp files
        public static void FileSplit(string path, Int32 FileSize, Int32 ListSize)
        {
            var           FileToProcess = new StreamReader(path);
            List <string> strSlice = new List <string>();
            int           FileCounter = 0;
            uint          PartSequenceSize = 0;
            uint          pivot = 0, current, left = 0;
            double        Increment    = 0;
            string        tempPath     = "";
            List <uint>   SequenceList = new List <uint>();

            if (ListSize < FileSize)
            {
                FileCounter = FileSize / ListSize;
                for (int i = 0; i < FileCounter; i++)
                {
                    tempPath = WorkPath + "\\splits\\out_slice" + i + ".txt";
                    for (int j = 0; j < ListSize; j++)
                    {
                        strSlice.Add(FileToProcess.ReadLine());
                        current = Convert.ToUInt32(strSlice[j].Substring(0, strSlice[j].IndexOf(".")));

                        if (j == 0)
                        {
                            left = current;
                        }
                        else
                        {
                            left = Convert.ToUInt32(strSlice[j - 1].Substring(0, strSlice[j - 1].IndexOf(".")));
                        }

                        if (current >= left)
                        {
                            PartSequenceSize++;
                        }
                        else
                        {
                            SequenceList.Add(PartSequenceSize);
                            PartSequenceSize = 0;
                        }
                    }

                    //MARK: - Key Formula in SwitchKEyFormula.jpg
                    if (SequenceList.Count != 0 && SequenceList.Max() > 1)
                    {
                        Increment = Math.Log(SequenceList.Max(), SequenceList.Count());
                    }

                    // MARK: - random equitable file: using Quick Sort
                    if (SequenceList.Count() != 0 &&
                        Math.Pow(ListSize - SequenceList.Max(), 2) > SequenceList.Max() * Increment)
                    {
                        strSlice = SortingMethods.TextRecordSortedInStrings(strSlice);
                    }

                    // MARK: - partly sorted file: using Insertion Sort
                    else
                    {
                        strSlice = SortingMethods.TRSortedtoStringsByInserts(strSlice);
                    }

                    CreateFileFromListInRAM(tempPath, strSlice, true);
                    SequenceList.Clear();
                    strSlice.Clear();
                }

                if (FileSize % ListSize != 0)
                {
                    tempPath = WorkPath + "\\splits\\out_slice" + FileCounter + ".txt";
                    while (FileToProcess.Peek() != -1)
                    {
                        strSlice.Add(FileToProcess.ReadLine());
                    }

                    strSlice = SortingMethods.TextRecordSortedInStrings(strSlice);
                    CreateFileFromListInRAM(tempPath, strSlice, false);
                    strSlice.Clear();
                }
            }
        }