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);
        }
        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);
        }
        public static ForwardIterator <T> MinElement <T>(ForwardIterator <T> begin, ForwardIterator <T> end)
            where T : IComparable <T>
        {
            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();
                if (object.ReferenceEquals(temp, null))
                {
                    continue;
                }

                int compare = temp.CompareTo(min);
                if (compare == -1)
                {
                    min     = temp;
                    minIter = IteratorUtil.Clone(begin);
                }
            }

            return(minIter);
        }
Exemple #4
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);
        }
Exemple #5
0
        public static ForwardIterator <T> SearchN <T>(ForwardIterator <T> begin, ForwardIterator <T> end,
                                                      int count, Functional.UnaryPredicate <T> func)
        {
            if (begin.Equals(end) || (count <= 0))
            {
                return(null);
            }

            if (count == 1)
            {
                return(FindIf(begin, end, func));
            }

            begin = IteratorUtil.Clone(begin);
            while (!begin.Equals(end))
            {
                begin = FindIf(begin, end, func);
                if (begin == null)
                {
                    return(null);
                }

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

                int matchCount = count - 1;
                while (func(iter.Read()))
                {
                    --matchCount;
                    if (matchCount == 0)
                    {
                        return(begin);
                    }

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

                begin.MoveNext();
            }
            return(null);
        }
Exemple #6
0
        public static ForwardIterator <T> SearchN <T>(ForwardIterator <T> begin, ForwardIterator <T> end,
                                                      int count, T value, IEqualityComparer <T> comparer)
        {
            if (begin.Equals(end) || (count <= 0))
            {
                return(null);
            }

            if (count == 1)
            {
                return(Find(begin, end, value, comparer));
            }

            begin = IteratorUtil.Clone(begin);
            while (!begin.Equals(end))
            {
                begin = Find(begin, end, value, comparer);
                if (begin == null)
                {
                    return(null);
                }

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

                int matchCount = count - 1;
                while (comparer.Equals(iter.Read(), value))
                {
                    --matchCount;
                    if (matchCount == 0)
                    {
                        return(begin);
                    }

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

                begin.MoveNext();
            }
            return(null);
        }
Exemple #7
0
        public static void Rotate <T>(ForwardIterator <T> begin, ForwardIterator <T> middle, ForwardIterator <T> end)
        {
            if (begin.Equals(middle) || middle.Equals(end) || begin.Equals(end))
            {
                return;
            }

//        begin  = IteratorUtil.Clone(begin);
//        middle = IteratorUtil.Clone(middle);
            ForwardIterator <T> left  = IteratorUtil.Clone(begin);
            ForwardIterator <T> right = IteratorUtil.Clone(middle);

            while (true)
            {
                IteratorUtil.Swap(left, right);
                left.MoveNext();
                right.MoveNext();
                if (left.Equals(middle))
                {
                    if (right.Equals(end))
                    {
                        return;
                    }
                    else
                    {
                        middle = IteratorUtil.Clone(right);
                    }
                }
                else if (right.Equals(end))
                {
                    right = IteratorUtil.Clone(middle);
                }
            }
        }
Exemple #8
0
 public static void Fill <T>(ForwardIterator <T> begin, ForwardIterator <T> end, T value)
 {
     for (begin = IteratorUtil.Clone(begin); !begin.Equals(end); begin.MoveNext())
     {
         begin.Write(value);
     }
 }
Exemple #9
0
 public static void Generate <T>(ForwardIterator <T> begin, ForwardIterator <T> end, Functional.GenFunction <T> func)
 {
     for (begin = IteratorUtil.Clone(begin); !begin.Equals(end); begin.MoveNext())
     {
         begin.Write(func());
     }
 }
Exemple #10
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();
                }
            }
        }
Exemple #11
0
        public static ForwardIterator <T> FindEnd <T>(ForwardIterator <T> begin, ForwardIterator <T> end,
                                                      ForwardIterator <T> searchBegin, ForwardIterator <T> searchEnd,
                                                      IEqualityComparer <T> comparer)
        {
            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, comparer);
                if (result == null)
                {
                    return(oldResult);
                }
                else
                {
                    oldResult = IteratorUtil.Clone(result);
                    result.MoveNext();
                }
            }
        }
Exemple #12
0
 public static void Advance <T>(ForwardIterator <T> iter, uint distance)
 {
     while (distance != 0)
     {
         iter.MoveNext();
         --distance;
     }
 }
Exemple #13
0
 public static void Replace <T>(ForwardIterator <T> begin, ForwardIterator <T> end, T oldValue, T newValue, IEqualityComparer <T> comparer)
 {
     for (begin = IteratorUtil.Clone(begin); !begin.Equals(end); begin.MoveNext())
     {
         if (comparer.Equals(oldValue, begin.Read()))
         {
             begin.Write(newValue);
         }
     }
 }
Exemple #14
0
 public static void ReplaceIf <T>(ForwardIterator <T> begin, ForwardIterator <T> end, T newValue, Functional.UnaryPredicate <T> func)
     where T : IEquatable <T>
 {
     for (begin = IteratorUtil.Clone(begin); !begin.Equals(end); begin.MoveNext())
     {
         if (func(begin.Read()))
         {
             begin.Write(newValue);
         }
     }
 }
Exemple #15
0
 public static void Replace <T>(ForwardIterator <T> begin, ForwardIterator <T> end, T oldValue, T newValue)
     where T : IEquatable <T>
 {
     for (begin = IteratorUtil.Clone(begin); !begin.Equals(end); begin.MoveNext())
     {
         if (oldValue.Equals(begin.Read()))
         {
             begin.Write(newValue);
         }
     }
 }
        [Test] public void UniqueCopy_Comparer_ForwardIterator()
        {
            ForwardIterator <string> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();
            ForwardIterator <string> result = Algorithm.UniqueCopy(IteratorUtil.Begin(src), IteratorUtil.End(src), destIter, EqualityComparer <string> .Default);

            VerifyOutput();
            Assert.IsNotNull(result);
            Assert.AreEqual(6, IteratorUtil.Distance(IteratorUtil.Begin(dest), result));
            Assert.AreEqual(MARKER, result.Read());
        }
Exemple #17
0
        [Test] public void UniqueCopy_ForwardIterator()
        {
            ForwardIterator <int> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();
            ForwardIterator <int> result = Algorithm.UniqueCopy(IteratorUtil.Begin(src), IteratorUtil.End(src), destIter);

            VerifyOutput();
            Assert.IsNotNull(result);
            Assert.AreEqual(6, IteratorUtil.Distance(IteratorUtil.Begin(dest), result));
            Assert.AreEqual(Int32.MinValue, result.Read());
        }
        [Test] public void UniqueCopy_Predicate_ForwardIterator()
        {
            ForwardIterator <string> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();
            ForwardIterator <string> result = Algorithm.UniqueCopy(IteratorUtil.Begin(src), IteratorUtil.End(src), destIter, delegate(string lhs, string rhs) { return(lhs == rhs); });

            VerifyOutput();
            Assert.IsNotNull(result);
            Assert.AreEqual(6, IteratorUtil.Distance(IteratorUtil.Begin(dest), result));
            Assert.AreEqual(MARKER, result.Read());
        }
Exemple #19
0
        public static void RotateCopy <T>(ForwardIterator <T> begin, ForwardIterator <T> middle,
                                          ForwardIterator <T> end, OutputIterator <T> dest)
        {
            Copy(begin, middle, Copy(middle, end, dest));

#if NEVER
            dest = IteratorUtil.Clone(dest);
            ForwardIterator <T> src = IteratorUtil.Clone(newBegin);
            newBegin = IteratorUtil.Clone(newBegin);

            for (; !src.Equals(end); dest.MoveNext(), src.MoveNext())
            {
                dest.Write(src.Read());
            }

            src = IteratorUtil.Clone(begin);
            for (; !src.Equals(newBegin); dest.MoveNext(), src.MoveNext())
            {
                dest.Write(src.Read());
            }
#endif
        }
Exemple #20
0
        [Test] public void  UniqueCopy_List_Comparer_ForwardIterator()
        {
            ForwardIterator <int> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();

            ForwardIterator <int> result = Algorithm.UniqueCopy(src, destIter, EqualityComparer <int> .Default);

            VerifyOutput();
            Assert.IsNotNull(result);
            Assert.AreEqual(6, IteratorUtil.Distance(IteratorUtil.Begin(dest), result));
            Assert.AreEqual(Int32.MinValue, (result as ListIterator <int>).Read());
        }
Exemple #21
0
        [Test] public void  UniqueCopy_List_Predicate_ForwardIterator()
        {
            ForwardIterator <int> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();

            ForwardIterator <int> result = Algorithm.UniqueCopy(src, destIter, delegate(int lhs, int rhs) { return(lhs == rhs); });

            VerifyOutput();
            Assert.IsNotNull(result);
            Assert.AreEqual(6, IteratorUtil.Distance(IteratorUtil.Begin(dest), result));
            Assert.AreEqual(Int32.MinValue, (result as ListIterator <int>).Read());
        }
Exemple #22
0
        [Test] public void RemoveCopy_List_ForwardIterator_Comparer()
        {
            ForwardIterator <int> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();

            ForwardIterator <int> result = Algorithm.RemoveCopy(src, destIter, 29, EqualityComparer <int> .Default);

            VerifyOutput();
            Assert.IsNotNull(result);
            Assert.AreEqual(src.Length - 3, IteratorUtil.Distance(IteratorUtil.Begin(dest), result));
            Assert.AreEqual(Int32.MinValue, result.Read());
        }
Exemple #23
0
        [Test] public void RemoveCopyIf_ForwardIterator()
        {
            ForwardIterator <int> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();

            ForwardIterator <int> result = Algorithm.RemoveCopyIf(IteratorUtil.Begin(src), IteratorUtil.End(src), destIter, Is29);

            VerifyOutput();
            Assert.IsNotNull(result);
            Assert.AreEqual(src.Length - 3, IteratorUtil.Distance(IteratorUtil.Begin(dest), result));
            Assert.AreEqual(Int32.MinValue, result.Read());
        }
Exemple #24
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);
 }
Exemple #25
0
 public static ForwardIterator <T> FindFirstOf <T>(ForwardIterator <T> begin, ForwardIterator <T> end,
                                                   ForwardIterator <T> searchBegin, ForwardIterator <T> searchEnd,
                                                   IEqualityComparer <T> comparer)
 {
     begin = IteratorUtil.Clone(begin);
     for (; !begin.Equals(end); begin.MoveNext())
     {
         for (ForwardIterator <T> searchIter = IteratorUtil.Clone(searchBegin);
              !searchIter.Equals(searchEnd); searchIter.MoveNext())
         {
             if (comparer.Equals(begin.Read(), searchIter.Read()))
             {
                 return(begin);
             }
         }
     }
     return(null);
 }
Exemple #26
0
        public static ForwardIterator <T> Search <T>(ForwardIterator <T> begin, ForwardIterator <T> end,
                                                     ForwardIterator <T> searchBegin, ForwardIterator <T> searchEnd,
                                                     IEqualityComparer <T> comparer)
        {
            if (begin.Equals(end))
            {
                return(null);
            }

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

            begin       = IteratorUtil.Clone(begin);
            searchBegin = IteratorUtil.Clone(searchBegin);

            if (IteratorUtil.IsOneElementRange(searchBegin, searchEnd))
            {
                return(Find(begin, end, searchBegin.Read(), comparer));
            }

            T firstSearchElement = searchBegin.Read();

            while (!begin.Equals(end))
            {
                begin = Find(begin, end, firstSearchElement, comparer);
                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 (comparer.Equals(iter.Read(), searchIter.Read()))
                {
                    searchIter.MoveNext();
                    if (searchIter.Equals(searchEnd))
                    {
                        return(begin);
                    }

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

                begin.MoveNext();
            }
            return(null);
        }
Exemple #27
0
        public static ForwardIterator <T> Search <T>(ForwardIterator <T> begin, ForwardIterator <T> end,
                                                     ForwardIterator <T> searchBegin, ForwardIterator <T> searchEnd)
            where T : IEquatable <T>
        {
            return(Search(begin, end, searchBegin, searchEnd, EqualityComparer <T> .Default));

#if NEVER
            if (begin.Equals(end))
            {
                return(null);
            }

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

            begin       = IteratorUtil.Clone(begin);
            searchBegin = IteratorUtil.Clone(searchBegin);

            if (IteratorUtil.IsOneElementRange(searchBegin, searchEnd))
            {
                return(Find(begin, end, searchBegin.Read()));
            }

            T firstSearchElement = searchBegin.Read();
            while (!begin.Equals(end))
            {
                begin = Find(begin, end, 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 (iter.Read().Equals(searchIter.Read()))
                {
                    searchIter.MoveNext();
                    if (searchIter.Equals(searchEnd))
                    {
                        return(begin);
                    }

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

                begin.MoveNext();
            }
            return(null);
#endif
        }
Exemple #28
0
 public static void SwapRanges <T>(ForwardIterator <T> begin, ForwardIterator <T> end, ForwardIterator <T> dest)
 {
     for (begin = IteratorUtil.Clone(begin), dest = IteratorUtil.Clone(dest); !begin.Equals(end); begin.MoveNext(), dest.MoveNext())
     {
         IteratorUtil.Swap(begin, dest);
     }
 }