Beispiel #1
0
        public static bool PrevPermutation <T>(
            this ArrayIterator <T> first,
            ArrayIterator <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);
                }
            }
        }
Beispiel #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);
 }
Beispiel #3
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);
        }
Beispiel #4
0
        public static ArrayIterator <T> UniqueCopy <T>(
            this ArrayIterator <T> first,
            ArrayIterator <T> last,
            ArrayIterator <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);
        }
Beispiel #5
0
        public static ArrayIterator <T> Search <T>(
            this ArrayIterator <T> first1,
            ArrayIterator <T> last1,
            ArrayIterator <T> first2,
            ArrayIterator <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);
        }
Beispiel #6
0
        public static bool IsPermutation <T>(
            this ArrayIterator <T> first1,
            ArrayIterator <T> last1,
            ArrayIterator <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);
        }
Beispiel #7
0
 public static void MinMaxElement <T>(
     this ArrayIterator <T> first,
     ArrayIterator <T> last,
     Func <T, T, bool> comp,
     out ArrayIterator <T> min,
     out ArrayIterator <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;
         }
     }
 }
Beispiel #8
0
 public static ArrayIterator <T> Merge <T>(
     this ArrayIterator <T> first1,
     ArrayIterator <T> last1,
     ArrayIterator <T> first2,
     ArrayIterator <T> last2,
     ArrayIterator <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();
     }
 }
Beispiel #9
0
        public static ArrayIterator <T> MaxElement <T>(
            this ArrayIterator <T> first,
            ArrayIterator <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);
        }
Beispiel #10
0
        public static ArrayIterator <T> IsSortedUntil <T>(
            this ArrayIterator <T> first,
            ArrayIterator <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);
        }
Beispiel #11
0
 public static bool Includes <T>(
     this ArrayIterator <T> first1,
     ArrayIterator <T> last1,
     ArrayIterator <T> first2,
     ArrayIterator <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);
 }
Beispiel #12
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;
                }
            }
        }
Beispiel #13
0
 public static bool LexicographicalCompare <T>(
     this ArrayIterator <T> first1,
     ArrayIterator <T> last1,
     ArrayIterator <T> first2,
     ArrayIterator <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));
 }