Beispiel #1
0
        public static ForwardIterator <T> Search <T>(ForwardIterator <T> begin, ForwardIterator <T> end,
                                                     ForwardIterator <T> searchBegin, ForwardIterator <T> searchEnd,
                                                     Functional.BinaryPredicate <T> func)
        {
            if (begin.Equals(end))
            {
                return(null);
            }

            if (searchBegin.Equals(searchEnd))
            {
                return(null);
            }

            begin       = IteratorUtil.Clone(begin);
            searchBegin = IteratorUtil.Clone(searchBegin);
            T firstSearchElement = searchBegin.Read();

            if (IteratorUtil.IsOneElementRange(searchBegin, searchEnd))
            {
                return(FindIf(begin, end, Functional.Bind2ndPred(func, firstSearchElement)));
            }

            while (!begin.Equals(end))
            {
                begin = FindIf(begin, end, Functional.Bind2ndPred(func, firstSearchElement));
                if (begin == null)
                {
                    return(null);
                }

                ForwardIterator <T> iter = IteratorUtil.Clone(begin);
                iter.MoveNext();
                if (iter.Equals(end))
                {
                    return(null);
                }

                ForwardIterator <T> searchIter = IteratorUtil.Clone(searchBegin);
                searchIter.MoveNext();

                while (func(iter.Read(), searchIter.Read()))
                {
                    searchIter.MoveNext();
                    if (searchIter.Equals(searchEnd))
                    {
                        return(begin);
                    }

                    iter.MoveNext();
                    if (iter.Equals(end))
                    {
                        return(null);
                    }
                }

                begin.MoveNext();
            }
            return(null);
        }
Beispiel #2
0
        public static ForwardIterator <T> FindEnd <T>(ForwardIterator <T> begin, ForwardIterator <T> end,
                                                      ForwardIterator <T> searchBegin, ForwardIterator <T> searchEnd,
                                                      Functional.BinaryPredicate <T> func)
        {
            if (begin.Equals(end))
            {
                return(null);
            }

            if (searchBegin.Equals(searchEnd))
            {
                return(null);
            }

            ForwardIterator <T> oldResult = null;
            ForwardIterator <T> result    = begin;

            while (true)
            {
                result = Search(result, end, searchBegin, searchEnd, func);
                if (result == null)
                {
                    return(oldResult);
                }
                else
                {
                    oldResult = IteratorUtil.Clone(result);
                    result.MoveNext();
                }
            }
        }
Beispiel #3
0
        [Test] public void Not_Predicate()
        {
            Functional.UnaryPredicate <int> func1 = Functional.Not1Pred(Functional.Bind1stPred(Functional.LessThan <int>, 29));
            Assert.IsTrue(func1(12));  // !(29 < 12) == false;
            Assert.IsFalse(func1(30)); // !(29 < 30) == true;
            Assert.IsTrue(func1(29));  // !(29 < 29) == false;

            Functional.BinaryPredicate <int> func2 = Functional.Not2Pred <int>(Functional.LessThan <int>);
            Assert.IsFalse(func2(12, 29)); // !(29 < 12) == false;
            Assert.IsTrue(func2(30, 29));  // !(29 < 30) == true;
            Assert.IsTrue(func2(29, 29));  // !(29 < 29) == false;
        }
Beispiel #4
0
 public static ForwardIterator <T> FindFirstOf <T>(ForwardIterator <T> begin, ForwardIterator <T> end,
                                                   ForwardIterator <T> searchBegin, ForwardIterator <T> searchEnd,
                                                   Functional.BinaryPredicate <T> func)
 {
     begin = IteratorUtil.Clone(begin);
     for (; !begin.Equals(end); begin.MoveNext())
     {
         for (ForwardIterator <T> searchIter = IteratorUtil.Clone(searchBegin);
              !searchIter.Equals(searchEnd); searchIter.MoveNext())
         {
             if (func(begin.Read(), searchIter.Read()))
             {
                 return(begin);
             }
         }
     }
     return(null);
 }
Beispiel #5
0
        public static bool NextPermutation <T>(BidirectionalIterator <T> begin, BidirectionalIterator <T> end,
                                               Functional.BinaryPredicate <T> func)
        {
            if (begin.Equals(end))
            {
                return(false);
            }

            if (IteratorUtil.IsOneElementRange(begin, end))
            {
                return(false);
            }

            BidirectionalIterator <T> i = IteratorUtil.Clone(end);

            i.MovePrev();

            for (;;)
            {
                BidirectionalIterator <T> ii = IteratorUtil.Clone(i);
                i.MovePrev();

                T i_value = i.Read();
                if (func(i_value, ii.Read()))
                {
                    BidirectionalIterator <T> j = IteratorUtil.Clone(end);
                    for (j.MovePrev(); !func(i_value, j.Read()); j.MovePrev())
                    {
                        ;
                    }
                    IteratorUtil.Swap(i, j);
                    Reverse(ii, end);
                    return(true);
                }
                if (i.Equals(begin))
                {
                    Reverse(begin, end);
                    return(false);
                }
            }
        }
Beispiel #6
0
        public static T MinElement <T>(IEnumerable <T> enumerable, Functional.BinaryPredicate <T> func)
        {
            IEnumerator <T> enumerator = enumerable.GetEnumerator();

            if (!enumerator.MoveNext())
            {
                return(default(T));
            }

            T min = enumerator.Current;

            while (enumerator.MoveNext())
            {
                T    temp = enumerator.Current;
                bool less = func(temp, min);
                if (less)
                {
                    min = temp;
                }
            }

            return(min);
        }
Beispiel #7
0
        public static T MaxElement <T>(IEnumerable <T> enumerable, Functional.BinaryPredicate <T> func)
        {
            IEnumerator <T> enumerator = enumerable.GetEnumerator();

            if (!enumerator.MoveNext())
            {
                return(default(T));
            }

            T max = enumerator.Current;

            while (enumerator.MoveNext())
            {
                T temp = enumerator.Current;
                // TODO: review this and compare with comments from Josuttis
                bool more = func(temp, max);
                if (more)
                {
                    max = temp;
                }
            }

            return(max);
        }
Beispiel #8
0
 public static ListIterator <T> Unique <T>(IList <T> list, Functional.BinaryPredicate <T> op)
 {
     return(Unique(IteratorUtil.Begin(list), IteratorUtil.End(list), op));
 }
Beispiel #9
0
 public static ListIterator <T> Unique <T>(ListIterator <T> begin, ListIterator <T> end, Functional.BinaryPredicate <T> op)
 {
     return(Unique((ForwardIterator <T>)begin, (ForwardIterator <T>)end, op) as ListIterator <T>);
 }
Beispiel #10
0
 public static RandomAccessIterator <T> Unique <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, Functional.BinaryPredicate <T> op)
 {
     return(Unique((ForwardIterator <T>)begin, (ForwardIterator <T>)end, op) as RandomAccessIterator <T>);
 }
Beispiel #11
0
 public static ListIterator <T> AdjacentFind <T>(ListIterator <T> begin, ListIterator <T> end, Functional.BinaryPredicate <T> op)
 {
     return((ListIterator <T>)AdjacentFind((InputIterator <T>)begin, (InputIterator <T>)end, op));
 }
Beispiel #12
0
        public static OutputIterator <T> UniqueCopy <T>(InputIterator <T> begin, InputIterator <T> end, OutputIterator <T> dest, Functional.BinaryPredicate <T> func)
        {
            if (begin.Equals(end))
            {
                return(null);
            }

            begin = IteratorUtil.Clone(begin);
            dest  = IteratorUtil.Clone(dest);
            T t = begin.Read();

            dest.Write(t);
            dest.MoveNext();
            begin.MoveNext();
            for (; !begin.Equals(end); begin.MoveNext())
            {
                T next = begin.Read();

                if (!func(t, next))
                {
                    t = next;
                    dest.Write(next);
                    dest.MoveNext();
                }
            }
            return(dest);
        }
Beispiel #13
0
 public static ListIterator <T> UniqueCopy <T>(IList <T> source, ListIterator <T> dest, Functional.BinaryPredicate <T> func)
 {
     return(UniqueCopy(source, (OutputIterator <T>)dest, func) as ListIterator <T>);
 }
Beispiel #14
0
 public static void StableSort <T>(IList <T> list, Functional.BinaryPredicate <T> func)
 {
     Sort(IteratorUtil.Begin(list), IteratorUtil.End(list), func);
 }
Beispiel #15
0
 public static ListIterator <T> MinElement <T>(ListIterator <T> begin, ListIterator <T> end, Functional.BinaryPredicate <T> func)
 {
     return((ListIterator <T>)MinElement((ForwardIterator <T>)begin, (ForwardIterator <T>)end, func));
 }
Beispiel #16
0
 public static RandomAccessIterator <T> MinElement <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, Functional.BinaryPredicate <T> func)
 {
     return((RandomAccessIterator <T>)MinElement((ForwardIterator <T>)begin, (ForwardIterator <T>)end, func));
 }
Beispiel #17
0
        public static ForwardIterator <T> MaxElement <T>(ForwardIterator <T> begin, ForwardIterator <T> end, Functional.BinaryPredicate <T> func)
        {
            if (begin.Equals(end))
            {
                return(null);
            }

            begin = IteratorUtil.Clone(begin);
            ForwardIterator <T> maxIter = IteratorUtil.Clone(begin);
            T max = maxIter.Read();

            begin.MoveNext();
            for (; !begin.Equals(end); begin.MoveNext())
            {
                T    temp = begin.Read();
                bool more = func(temp, max);
                if (more)
                {
                    max     = temp;
                    maxIter = IteratorUtil.Clone(begin);
                }
            }

            return(maxIter);
        }
Beispiel #18
0
        public static InputIterator <T> AdjacentFind <T>(InputIterator <T> begin, InputIterator <T> end, Functional.BinaryPredicate <T> op)
        {
            if (begin.Equals(end))
            {
                return(null);
            }

            begin = IteratorUtil.Clone(begin);
            InputIterator <T> next = IteratorUtil.Clone(begin);

            next.MoveNext();
            T t1 = begin.Read();
            T t2;

            for (; !next.Equals(end); begin.MoveNext(), next.MoveNext(), t1 = t2)
            {
                t2 = next.Read();
                if (op(t1, t2))
                {
                    return(IteratorUtil.Clone(begin));
                }
            }
            return(null);
        }
Beispiel #19
0
        public static void Sort <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, Functional.BinaryPredicate <T> func)
        {
            if (begin.Equals(end))
            {
                return;
            }

            T[] array = new T[end.Position - begin.Position];
            Copy(begin, end, array);
            Array.Sort(array, new Functional.BinaryPredicateComparison <T>(func).Compare);
            Copy(array, begin);
        }
Beispiel #20
0
 public static void StableSort <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, Functional.BinaryPredicate <T> func)
 {
     Sort(begin, end, func);
 }
Beispiel #21
0
 public static ListIterator <T> MinElement <T>(IList <T> list, Functional.BinaryPredicate <T> func)
 {
     return(MinElement(IteratorUtil.Begin(list), IteratorUtil.End(list), func));
 }
Beispiel #22
0
 public static ListIterator <T> UniqueCopy <T>(InputIterator <T> begin, InputIterator <T> end, ListIterator <T> dest, Functional.BinaryPredicate <T> func)
 {
     return(UniqueCopy(begin, end, (OutputIterator <T>)dest, func) as ListIterator <T>);
 }
Beispiel #23
0
        public static void Merge <T>(InputIterator <T> begin1, InputIterator <T> end1,
                                     InputIterator <T> begin2, InputIterator <T> end2, OutputIterator <T> dest, Functional.BinaryPredicate <T> func)
        {
            T t1, t2;

            begin1 = IteratorUtil.Clone(begin1);
            begin2 = IteratorUtil.Clone(begin2);
            dest   = IteratorUtil.Clone(dest);
            for (; !begin1.Equals(end1) && !begin2.Equals(end2); dest.MoveNext())
            {
                t1 = begin1.Read();
                t2 = begin2.Read();
                bool less = func(t1, t2);

                if (less)
                {
                    dest.Write(t1);
                    begin1.MoveNext();
                }
                else
                {
                    dest.Write(t2);
                    begin2.MoveNext();
                }
            }

            Copy(begin1, end1, dest);
            Copy(begin2, end2, dest);
        }
Beispiel #24
0
 public static ListIterator <T> UniqueCopy <T>(IList <T> source, IList <T> dest, Functional.BinaryPredicate <T> func)
 {
     return(UniqueCopy(IteratorUtil.Begin(source), IteratorUtil.End(source), IteratorUtil.Begin(dest), func));
 }
Beispiel #25
0
        public static ForwardIterator <T> MinElement <T>(ForwardIterator <T> begin, ForwardIterator <T> end, Functional.BinaryPredicate <T> func)
        {
            if (begin.Equals(end))
            {
                return(null);
            }

            begin = IteratorUtil.Clone(begin);
            ForwardIterator <T> minIter = IteratorUtil.Clone(begin);
            T min = minIter.Read();

            begin.MoveNext();
            for (; !begin.Equals(end); begin.MoveNext())
            {
                T    temp = begin.Read();
                bool less = func(temp, min);
                if (less)
                {
                    min     = temp;
                    minIter = IteratorUtil.Clone(begin);
                }
            }

            return(minIter);
        }
Beispiel #26
0
        public static ForwardIterator <T> Unique <T>(ForwardIterator <T> begin, ForwardIterator <T> end, Functional.BinaryPredicate <T> op)
        {
            if (begin.Equals(end))
            {
                return(null);
            }

            ForwardIterator <T> firstMatch = AdjacentFind(begin, end, op) as ForwardIterator <T>;

            if (firstMatch != null)
            {
                return(UniqueCopy(firstMatch, end, firstMatch, op) as ForwardIterator <T>);
            }
            return(null);
        }
Beispiel #27
0
 public static RandomAccessIterator <T> AdjacentFind <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, Functional.BinaryPredicate <T> op)
 {
     return((RandomAccessIterator <T>)AdjacentFind((InputIterator <T>)begin, (InputIterator <T>)end, op));
 }