Exemplo n.º 1
0
        public static bool PrevPermutation <T>(
            this ListIterator <T> first,
            ListIterator <T> last,
            Func <T, T, bool> comp
            )
        {
            var i = last;

            if (first.IsEqual(last) || first.IsEqual(i = i.GetPrev()))
            {
                return(false);
            }
            while (true)
            {
                var ip1 = i;
                if (comp(ip1.GetCurrent(), (i = i.GetPrev()).GetCurrent()))
                {
                    var j = last;
                    while (comp((j = j.GetPrev()).GetCurrent(), i.GetCurrent()) == false)
                    {
                    }
                    Swap(i, j);
                    Reverse(ip1, last);
                    return(true);
                }
                if (i.IsEqual(first))
                {
                    Reverse(first, last);
                    return(false);
                }
            }
        }
Exemplo n.º 2
0
 public static ListIterator <T> Partition <T>(
     this ListIterator <T> first,
     ListIterator <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 Sort <T>(
            this ListIterator <T> first,
            ListIterator <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.º 4
0
        public static ListIterator <T> UniqueCopy <T>(
            this ListIterator <T> first,
            ListIterator <T> last,
            ListIterator <T> result,
            Func <T, T, bool> pred
            )
        {
            if (first.IsEqual(last))
            {
                return(last);
            }

            result.SetCurrent(first.GetCurrent());
            while ((first = first.GetNext()).NotEqual(last))
            {
                var val = first.GetCurrent();
                if (pred(result.GetCurrent(), val) == false)
                {
                    result = result.GetNext();
                    result.SetCurrent(val);
                }
            }
            result = result.GetNext();
            return(result);
        }
Exemplo n.º 5
0
        public static ListIterator <T> Search <T>(
            this ListIterator <T> first1,
            ListIterator <T> last1,
            ListIterator <T> first2,
            ListIterator <T> last2,
            Func <T, T, bool> pred
            )
        {
            if (first2.IsEqual(last2))
            {
                return(first1);
            }

            while (first1.NotEqual(last1))
            {
                var it1 = first1;
                var it2 = first2;
                while (pred(it1.GetCurrent(), it2.GetCurrent()))
                {
                    it1 = it1.GetNext();
                    it2 = it2.GetNext();
                    if (it2.IsEqual(last2))
                    {
                        return(first1);
                    }
                    if (it1.IsEqual(last1))
                    {
                        return(last1);
                    }
                }
                first1 = first1.GetNext();
            }
            return(last1);
        }
Exemplo n.º 6
0
        public static bool IsPermutation <T>(
            this ListIterator <T> first1,
            ListIterator <T> last1,
            ListIterator <T> first2,
            Func <T, T, bool> pred
            )
        {
            first1.Mismatch(last1, first2, pred, out first1, out first2);
            if (first1.IsEqual(last1))
            {
                return(true);
            }
            var last2 = first2;

            last2 = last2.GetAdvanced(first1.Distance(last1));
            for (var it1 = first1; it1.NotEqual(last1); it1 = it1.GetNext())
            {
                if (first1.Find(it1, it1.GetCurrent(), pred).IsEqual(it1))
                {
                    var n = first2.Count(last2, it1.GetCurrent(), pred);
                    if (n == 0 || it1.Count(last1, it1.GetCurrent(), pred) != n)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 7
0
 public static void MinMaxElement <T>(
     this ListIterator <T> first,
     ListIterator <T> last,
     Func <T, T, bool> comp,
     out ListIterator <T> min,
     out ListIterator <T> max
     )
 {
     if (first.IsEqual(last))
     {
         min = last;
         max = last;
     }
     min = first;
     max = first;
     while ((first = first.GetNext()).NotEqual(last))
     {
         if (comp(first.GetCurrent(), min.GetCurrent()))
         {
             min = first;
         }
         if (comp(max.GetCurrent(), first.GetCurrent()))
         {
             max = first;
         }
     }
 }
Exemplo n.º 8
0
 public static ListIterator <T> Merge <T>(
     this ListIterator <T> first1,
     ListIterator <T> last1,
     ListIterator <T> first2,
     ListIterator <T> last2,
     ListIterator <T> result,
     Func <T, T, bool> comp
     )
 {
     while (true)
     {
         if (first1.IsEqual(last1))
         {
             return(first2.Copy(last2, result));
         }
         if (first2.IsEqual(last2))
         {
             return(first1.Copy(last1, result));
         }
         if (comp(first2.GetCurrent(), first1.GetCurrent()))
         {
             result.SetCurrent(first2.GetCurrent());
             first2 = first2.GetNext();
         }
         else
         {
             result.SetCurrent(first1.GetCurrent());
             first1 = first1.GetNext();
         }
         result = result.GetNext();
     }
 }
Exemplo n.º 9
0
        public static ListIterator <T> MaxElement <T>(
            this ListIterator <T> first,
            ListIterator <T> last,
            Func <T, T, bool> comp
            )
        {
            if (first.IsEqual(last))
            {
                return(last);
            }
            var largest = first;

            while ((first = first.GetNext()).NotEqual(last))
            {
                if (comp(largest.GetCurrent(), first.GetCurrent()))
                {
                    largest = first;
                }
            }
            return(largest);
        }
Exemplo n.º 10
0
        public static ListIterator <T> IsSortedUntil <T>(
            this ListIterator <T> first,
            ListIterator <T> last,
            Func <T, T, bool> comp
            )
        {
            if (first.IsEqual(last))
            {
                return(first);
            }
            var next = first;

            while ((next = next.GetNext()).NotEqual(last))
            {
                if (comp(next.GetCurrent(), first.GetCurrent()))
                {
                    return(next);
                }
                first = first.GetNext();
            }
            return(last);
        }
Exemplo n.º 11
0
 public static bool Includes <T>(
     this ListIterator <T> first1,
     ListIterator <T> last1,
     ListIterator <T> first2,
     ListIterator <T> last2,
     Func <T, T, bool> comp
     )
 {
     while (first2.NotEqual(last2))
     {
         if ((first1.IsEqual(last1)) || comp(first2.GetCurrent(), first1.GetCurrent()))
         {
             return(false);
         }
         if (comp(first1.GetCurrent(), first2.GetCurrent()) == false)
         {
             first2 = first2.GetNext();
         }
         first1 = first1.GetNext();
     }
     return(true);
 }
Exemplo n.º 12
0
        public static void Rotate <T>(
            this ListIterator <T> first,
            ListIterator <T> middle,
            ListIterator <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;
                }
            }
        }
Exemplo n.º 13
0
 public static bool LexicographicalCompare <T>(
     this ListIterator <T> first1,
     ListIterator <T> last1,
     ListIterator <T> first2,
     ListIterator <T> last2,
     Func <T, T, bool> comp
     )
 {
     while (first1.NotEqual(last1))
     {
         if (first2.IsEqual(last2) || comp(first2.GetCurrent(), first1.GetCurrent()))
         {
             return(false);
         }
         else if (comp(first1.GetCurrent(), first2.GetCurrent()))
         {
             return(true);
         }
         first1 = first1.GetNext();
         first2 = first2.GetNext();
     }
     return(first2.NotEqual(last2));
 }