Exemplo n.º 1
0
        public static void Sort <T>(
            this ArrayIterator <T> first,
            ArrayIterator <T> last,
            Func <T, T, bool> comp
            )
        {
            // Quicksort
            if (first.IsEqual(last))
            {
                return;
            }
            var sep = first;

            for (var i = first.GetNext(); i.NotEqual(last); i = i.GetNext())
            {
                if (comp(i.GetCurrent(), first.GetCurrent()))
                {
                    sep = sep.GetNext();
                    sep.Swap(i);
                }
            }
            first.Swap(sep);
            first.Sort(sep, comp);
            sep.GetNext().Sort(last, comp);
        }
Exemplo n.º 2
0
 public static ArrayIterator <T> Partition <T>(
     this ArrayIterator <T> first,
     ArrayIterator <T> last,
     Func <T, bool> pred
     )
 {
     while (first.NotEqual(last))
     {
         while (pred(first.GetCurrent()))
         {
             first = first.GetNext();
             if (first.IsEqual(last))
             {
                 return(first);
             }
         }
         do
         {
             last = last.GetPrev();
             if (first.IsEqual(last))
             {
                 return(first);
             }
         } while (pred(last.GetCurrent()) == false);
         first.Swap(last);
         first = first.GetNext();
     }
     return(first);
 }
Exemplo n.º 3
0
 public static void Reverse <T>(this ArrayIterator <T> first, ArrayIterator <T> last)
 {
     while ((first.NotEqual(last)) && (first.NotEqual((last = last.GetPrev()))))
     {
         first.Swap(last);
         first = first.GetNext();
     }
 }
Exemplo n.º 4
0
        public static void Rotate <T>(
            this ArrayIterator <T> first,
            ArrayIterator <T> middle,
            ArrayIterator <T> last
            )
        {
            var next = middle;

            while (first.NotEqual(last))
            {
                first.Swap(next);
                first = first.GetNext();
                next  = next.GetNext();
                if (next.IsEqual(last))
                {
                    next = middle;
                }
                else if (first.IsEqual(middle))
                {
                    middle = next;
                }
            }
        }