Exemple #1
0
        public void Generate(int amount, int max)
        {
            int record;

            df.OpenSaveStream("MainFile.txt");
            for (int i = 0; i < amount; i++)
            {
                record = random.Next(1, max);
                df.SaveRecord(record, "MainFile.txt");
            }
            df.FlushRecordBuffer("MainFile.txt");
            df.CloseStream("MainFile.txt");
            df.ZeroWriteOp();
        }
        public static void MergeRuns(DiskFunctionality df, string in1, string in2, string outTape)  //merge 1 series from each input
        {
            int previous1Record = -1, record1 = df.ReadRecord(in1);
            int previous2Record = -1, record2 = df.ReadRecord(in2);

            while (true)
            {
                if (record1 != -1 && record2 != -1)
                {
                    if (Compare(record1, record2))
                    {
                        if (Compare(previous1Record, record1))
                        {
                            df.SaveRecord(record1, outTape);
                            previous1Record = record1;
                            record1         = df.ReadRecord(in1);
                        }
                        else
                        {
                            df.ReturnRecord(in1);
                            while (true)
                            {
                                if (Compare(previous2Record, record2))
                                {
                                    df.SaveRecord(record2, outTape);
                                    previous2Record = record2;
                                    record2         = df.ReadRecord(in2);
                                    if (record2 == -1)
                                    {
                                        return;
                                    }
                                }
                                else
                                {
                                    df.ReturnRecord(in2);
                                    return;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (Compare(previous2Record, record2))
                        {
                            df.SaveRecord(record2, outTape);
                            previous2Record = record2;
                            record2         = df.ReadRecord(in2);
                        }
                        else
                        {
                            df.ReturnRecord(in2);
                            while (true)
                            {
                                if (Compare(previous1Record, record1))
                                {
                                    df.SaveRecord(record1, outTape);
                                    previous1Record = record1;
                                    record1         = df.ReadRecord(in1);
                                    if (record1 == -1)
                                    {
                                        return;
                                    }
                                }
                                else
                                {
                                    df.ReturnRecord(in1);
                                    return;
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (record2 == -1)
                    {
                        while (true)
                        {
                            if (Compare(previous1Record, record1))
                            {
                                df.SaveRecord(record1, outTape);
                                previous1Record = record1;
                                record1         = df.ReadRecord(in1);
                                if (record1 == -1)
                                {
                                    return;
                                }
                            }
                            else
                            {
                                df.ReturnRecord(in1);
                                return;
                            }
                        }
                    }
                    else if (record1 == -1)
                    {
                        while (true)
                        {
                            if (Compare(previous2Record, record2))
                            {
                                df.SaveRecord(record2, outTape);
                                previous2Record = record2;
                                record2         = df.ReadRecord(in2);
                                if (record2 == -1)
                                {
                                    return;
                                }
                            }
                            else
                            {
                                df.ReturnRecord(in2);
                                return;
                            }
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }
        public static RunInfo DivideBetweenTapes(DiskFunctionality df, string main, string tape1, string tape2)
        {
            int record = -1, previousRecord;
            int whichTape = 1, tape1Runs = 0, tape2Runs = 0, whichFibonacci = 0;
            int fibbNumber = GetFibonacciNumber(whichFibonacci);

            df.OpenReadStream(main);
            df.OpenSaveStream(tape1);
            df.OpenSaveStream(tape2);
            while (true)
            {
                previousRecord = record;
                record         = df.ReadRecord(main);
                if (record == -1)
                {
                    break;
                }
                if (whichTape == 1)
                {
                    if (Compare(previousRecord, record))
                    {
                        df.SaveRecord(record, tape1);
                    }
                    else
                    {
                        tape1Runs++;
                        if (tape1Runs == fibbNumber)
                        {
                            df.SaveRecord(record, tape2);
                            whichTape = 2;
                            whichFibonacci++;
                            fibbNumber = GetFibonacciNumber(whichFibonacci);
                        }
                        else
                        {
                            df.SaveRecord(record, tape1);
                        }
                    }
                }
                else
                {
                    if (Compare(previousRecord, record))
                    {
                        df.SaveRecord(record, tape2);
                    }
                    else
                    {
                        tape2Runs++;
                        if (tape2Runs == fibbNumber)
                        {
                            df.SaveRecord(record, tape1);
                            whichTape = 1;
                            whichFibonacci++;
                            fibbNumber = GetFibonacciNumber(whichFibonacci);
                        }
                        else
                        {
                            df.SaveRecord(record, tape2);
                        }
                    }
                }
            }
            if (whichTape == 1)// dummy runs
            {
                tape1Runs = GetFibonacciNumber(whichFibonacci);
            }
            else
            {
                tape2Runs = GetFibonacciNumber(whichFibonacci);
            }
            df.FlushRecordBuffer(tape1);
            df.FlushRecordBuffer(tape2);
            df.CloseStream(main);
            df.CloseStream(tape1);
            df.CloseStream(tape2);
            Console.WriteLine("Divide finished. Sorting...");
            return(new RunInfo(tape1Runs, tape2Runs));
        }