Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public static OutputIterator <T> UniqueCopy <T>(InputIterator <T> begin, InputIterator <T> end, OutputIterator <T> dest, IEqualityComparer <T> comparer)
        {
            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 (!comparer.Equals(t, next))
                {
                    t = next;
                    dest.Write(next);
                    dest.MoveNext();
                }
            }
            return(dest);
        }
Ejemplo n.º 3
0
 public static void ForEach <T>(InputIterator <T> begin, InputIterator <T> end, Functional.UnaryVoidFunction <T> func)
 {
     for (begin = IteratorUtil.Clone(begin); !begin.Equals(end); begin.MoveNext())
     {
         func(begin.Read());
     }
 }
Ejemplo n.º 4
0
        public static void Merge <T>(InputIterator <T> begin1, InputIterator <T> end1,
                                     InputIterator <T> begin2, InputIterator <T> end2, OutputIterator <T> dest, IComparer <T> comp)
        {
            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();
                int compare = comp.Compare(t1, t2);

                if (compare == -1)
                {
                    dest.Write(t1);
                    begin1.MoveNext();
                }
                else
                {
                    dest.Write(t2);
                    begin2.MoveNext();
                }
            }

            Copy(begin1, end1, dest);
            Copy(begin2, end2, dest);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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();
                }
            }
        }
Ejemplo n.º 7
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();
                }
            }
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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);
     }
 }
Ejemplo n.º 10
0
        public static I MinElement <I, T>(I begin, I end, IComparer <T> comparer)
            where I : class, ForwardIterator <T>
        {
            if (begin.Equals(end))
            {
                return(null);
            }

            begin = IteratorUtil.Clone(begin);
            I minIter = IteratorUtil.Clone(begin);
            T min     = minIter.Read();

            begin.MoveNext();
            for (; !begin.Equals(end); begin.MoveNext())
            {
                T   temp    = begin.Read();
                int compare = comparer.Compare(temp, min);
                if (compare == -1)
                {
                    min     = temp;
                    minIter = IteratorUtil.Clone(begin);
                }
            }

            return(minIter);
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
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());
     }
 }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
0
 public static void GenerateN <T>(OutputIterator <T> begin, int count, Functional.GenFunction <T> func)
 {
     for (begin = IteratorUtil.Clone(begin); count > 0; --count, begin.MoveNext())
     {
         begin.Write(func());
     }
 }
Ejemplo n.º 15
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);
                }
            }
        }
Ejemplo n.º 16
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);
     }
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 18
0
        public static bool LexCompare <T>(InputIterator <T> begin1, InputIterator <T> end1,
                                          InputIterator <T> begin2, InputIterator <T> end2,
                                          Comparer <T> comparer)
        {
            begin1 = IteratorUtil.Clone(begin1);
            //end1   = IteratorUtil.Clone(end1);
            begin2 = IteratorUtil.Clone(begin2);
            //end2   = IteratorUtil.Clone(end2);

            for (;; begin1.MoveNext(), begin2.MoveNext())
            {
                if (begin2.Equals(end2))
                {
                    return(false);
                }
                if (begin1.Equals(end1))
                {
                    return(true);
                }

                int compare = comparer.Compare(begin1.Read(), begin2.Read());
                if (compare == -1)
                {
                    return(true);
                }
                if (compare == 1)
                {
                    return(false);
                }
            }
        }
Ejemplo n.º 19
0
 public static OutputIterator <T> FillN <T>(OutputIterator <T> begin, int count, T value)
 {
     for (begin = IteratorUtil.Clone(begin); count > 0; --count, begin.MoveNext())
     {
         begin.Write(value);
     }
     return(begin);
 }
Ejemplo n.º 20
0
 public static OutputIterator <O> Transform <I, O>(InputIterator <I> begin, InputIterator <I> end, OutputIterator <O> dest, Functional.UnaryFunction <I, O> func)
 {
     for (begin = IteratorUtil.Clone(begin), dest = IteratorUtil.Clone(dest); !begin.Equals(end); begin.MoveNext(), dest.MoveNext())
     {
         dest.Write(func(begin.Read()));
     }
     return(dest);
 }
Ejemplo n.º 21
0
 public static void CopyN <T>(InputIterator <T> begin, uint count, OutputIterator <T> target)
 {
     for (begin = IteratorUtil.Clone(begin), target = IteratorUtil.Clone(target); count > 0; --count)
     {
         target.Write(begin.Read());
         begin.MoveNext();
         target.MoveNext();
     }
 }
Ejemplo n.º 22
0
 public static OutputIterator <T> Copy <T>(IEnumerable <T> enumerable, OutputIterator <T> target)
 {
     target = IteratorUtil.Clone(target);
     foreach (T t  in enumerable)
     {
         target.Write(t);
         target.MoveNext();
     }
     return(target);
 }
Ejemplo n.º 23
0
 public static OutputIterator <T> Copy <T>(InputIterator <T> begin, InputIterator <T> end, OutputIterator <T> target)
 {
     for (begin = IteratorUtil.Clone(begin), target = IteratorUtil.Clone(target);
          !begin.Equals(end);
          begin.MoveNext(), target.MoveNext())
     {
         target.Write(begin.Read());
     }
     return(target);
 }
Ejemplo n.º 24
0
 public static void ReplaceCopyIf <T>(InputIterator <T> begin, InputIterator <T> end, OutputIterator <T> dest, Functional.UnaryPredicate <T> func, T newValue)
 {
     for (begin = IteratorUtil.Clone(begin), dest = IteratorUtil.Clone(dest); !begin.Equals(end); begin.MoveNext(), dest.MoveNext())
     {
         if (func(begin.Read()))
         {
             dest.Write(newValue);
         }
     }
 }
Ejemplo n.º 25
0
        public static T Accumulate <T>(InputIterator <T> begin, InputIterator <T> end, T initialValue, Functional.BinaryFunction <T, T, T> func)
        {
            T result = initialValue;

            for (begin = IteratorUtil.Clone(begin); !begin.Equals(end); begin.MoveNext())
            {
                result = func(result, begin.Read());
            }
            return(result);
        }
Ejemplo n.º 26
0
        public static OutputIterator <O> Transform <I, O>(IEnumerable <I> enumerable, OutputIterator <O> dest, Functional.UnaryFunction <I, O> func)
        {
            IEnumerator <I> enumerator = enumerable.GetEnumerator();

            for (dest = IteratorUtil.Clone(dest); enumerator.MoveNext(); dest.MoveNext())
            {
                dest.Write(func(enumerator.Current));
            }
            return(dest);
        }
Ejemplo n.º 27
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);
         }
     }
 }
Ejemplo n.º 28
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);
         }
     }
 }
Ejemplo n.º 29
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);
         }
     }
 }
Ejemplo n.º 30
0
 public static InputIterator <T> FindIf <T>(InputIterator <T> begin, InputIterator <T> end, Functional.UnaryPredicate <T> func)
 {
     for (begin = IteratorUtil.Clone(begin); !begin.Equals(end); begin.MoveNext())
     {
         if (func(begin.Read()))
         {
             return(begin);
         }
     }
     return(null);
 }