internal static void Distribute(Tape src, CyclicList<Tape> tapes)
 {
     //Log.WriteDistMessage("Dist start");
     tapes.Clean();
     string line;
     using (StreamReader reader = new StreamReader(src.FilePath))
         while ((line = reader.ReadLine()) != null)
         {
             int val = int.Parse(line);
             if (tapes.IsFirstRun() || val >= tapes.LastValue)
             {
                 tapes.Add(val);
                 //Log.WriteDistMessage("Adding " + val + " to " + tapes.Index + ".", 4);
             }
             else
             {
                 tapes.ChangeToNextAndAdd(val);
                 //Log.WriteInfoMessage("NIEPOSORTOWANE!" + val);
             }
             //Log.WriteDistMessage("Adding " + val + " to " + tapes.Index + ".", 4);
         }
     Log.WriteDistMessage("A: " + tapes[0].ShowFile());
     Log.WriteDistMessage("B: " + tapes[1].ShowFile());
     //Log.WriteDistMessage("Dist end");
 }
 public static void Copy(string src, Tape dest)
 {
     using (StreamReader reader = new StreamReader(src))
     {
         string line;
         while ((line = reader.ReadLine()) != null)
         {
             dest.Append(line);
         }
     }
 }
Example #3
0
        public static void Main(string[] args)
        {
            Tape a = new Tape("a.txt");
            Tape b = new Tape("b.txt");
            var input = args.Length == 0 ? "../../../data/pentagons_01.dat" : args[0];
            Tape c = new Tape("c.txt", input);

            Log.WriteInfoMessage("Natural sort");
            CyclicList<Tape> tapes = new CyclicList<Tape>() { a, b };

            while (!SortHelper.IsSorted(c))
            {

                SortHelper.Distribute(c, tapes);
                SortHelper.Merge(c, tapes);
            }
            Log.WriteInfoMessage("End of execution");
            if (!(args.Length == 2 && args[1].Equals("TEST")))
                Console.ReadKey();
        }
 internal static bool IsSorted(Tape tape)
 {
     return tape.IsSorted();
 }
        public static void Merge(Tape dest, CyclicList<Tape> tapes)
        {
            //Log.WriteMergeMessage("Merge start");
            //Log.WriteMergeMessage("A: " + tapes[0].ShowFile(), 5);
            //Log.WriteMergeMessage("B: " + tapes[1].ShowFile(), 5);
            dest.Clean();
            tapes.Prepare();

            double a1 = (double)tapes.Current.GetNextValue();

            double b1 = -1;

            while (!tapes.Current.IsFinished)
            {
                double? nextValue = tapes.Next.GetNextValue();
                double b2;
                if (nextValue != null)
                    b2 = (double)nextValue;
                else
                    break; // przepisz resztę taśmy
                if (b2 >= b1) // czy ciągle na tym samym run-ie
                {
                    if (b2 <= a1)
                    {
                        b1 = b2;
                        dest.Append(b1);
                    }
                    else
                    {
                        dest.Append(a1);
                        tapes.MoveNext();
                        b1 = a1;
                        a1 = b2;
                    }
                }
                else // przepisz resztę run-a
                {
                    b1 = b2;
                    double a2 = a1;
                    var value = tapes.Current.GetNextValue();
                    if (value != null) a1 = (double)value;
                    else
                    {
                        dest.Append(a2);
                        tapes.MoveNext();
                        break;
                    }
                    while (a1 >= a2)
                    {
                        dest.Append(a2);
                        a2 = a1;
                        double? d = tapes.Current.GetNextValue();
                        if (d != null) a1 = (double)d;
                        else
                            break;
                    }

                    dest.Append(a2);
                    tapes.Next.StepBack();
                }
            }
            double? v;
            //dest.Append(a1);
            tapes.Current.StepBack();
            while ((v = tapes.Current.GetNextValue()) != null)
            {
                dest.Append((double)v);
            }
            //Log.WriteMergeMessage("Result:", 5);
            Log.WriteMergeMessage("C: " + dest.ShowFile());
            //Log.WriteMergeMessage("Merge end");
        }