Exemplo n.º 1
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;
        }
Exemplo n.º 2
0
        [Test] public void Bind_Predicate()
        {
            Functional.UnaryPredicate <int> func1 = Functional.Bind1stPred(Functional.LessThan <int>, 29);
            Assert.IsFalse(func1(12)); // 29 < 12 == false;
            Assert.IsTrue(func1(30));  // 29 < 30 == true;
            Assert.IsFalse(func1(29)); // 29 < 29 == false;

            Functional.UnaryPredicate <int> func2 = Functional.Bind2ndPred(Functional.LessThan <int>, 29);
            Assert.IsTrue(func2(12));  // 12 < 29 == true;
            Assert.IsFalse(func2(30)); // 30 < 12 == false;
            Assert.IsFalse(func2(29)); // 29 < 29 == false;
        }
Exemplo n.º 3
0
        public static int CountIf <T>(IEnumerable <T> enumerable, Functional.UnaryPredicate <T> func)
        {
            int count = 0;

            foreach (T t in enumerable)
            {
                if (func(t))
                {
                    ++count;
                }
            }

            return(count);
        }
Exemplo n.º 4
0
        public static IEnumerator <T> FindIf <T>(IEnumerable <T> enumerable, Functional.UnaryPredicate <T> func)
        {
            IEnumerator <T> enumerator = enumerable.GetEnumerator();

            while (enumerator.MoveNext())
            {
                T t = enumerator.Current;
                if (func(t))
                {
                    return(enumerator);
                }
            }
            return(null);
        }
Exemplo n.º 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);
        }
Exemplo n.º 6
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);
         }
     }
 }
Exemplo n.º 7
0
 public static ListIterator <T> CopyIf <T>(InputIterator <T> begin, InputIterator <T> end, ListIterator <T> dest, Functional.UnaryPredicate <T> func)
 {
     return(CopyIf(begin, end, (OutputIterator <T>)dest, func) as ListIterator <T>);
 }
Exemplo n.º 8
0
 public static ForwardIterator <T> RemoveCopyIf <T>(IList <T> source, ForwardIterator <T> dest, Functional.UnaryPredicate <T> func)
 {
     return(RemoveCopyIf(source, (OutputIterator <T>)dest, func) as ForwardIterator <T>);
 }
Exemplo n.º 9
0
 public static ListIterator <T> RemoveCopyIf <T>(IList <T> source, IList <T> dest, Functional.UnaryPredicate <T> func)
 {
     return(RemoveCopyIf(IteratorUtil.Begin(source), IteratorUtil.End(source), IteratorUtil.Begin(dest), func));
 }
Exemplo n.º 10
0
 public static void ReplaceCopyIf <T>(IList <T> list, IList <T> dest, Functional.UnaryPredicate <T> func, T newValue)
 {
     ReplaceCopyIf(IteratorUtil.Begin(list), IteratorUtil.End(list), IteratorUtil.Begin(dest), func, newValue);
 }
Exemplo n.º 11
0
 public static RandomAccessIterator <T> RemoveCopyIf <T>(IList <T> source, RandomAccessIterator <T> dest, Functional.UnaryPredicate <T> func)
 {
     return(RemoveCopyIf(source, (OutputIterator <T>)dest, func) as RandomAccessIterator <T>);
 }
Exemplo n.º 12
0
 public static RandomAccessIterator <T> FindIf <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, Functional.UnaryPredicate <T> func)
 {
     return((RandomAccessIterator <T>)FindIf((InputIterator <T>)begin, (InputIterator <T>)end, func));
 }
Exemplo n.º 13
0
 public static ListIterator <T> FindIf <T>(IList <T> list, Functional.UnaryPredicate <T> func)
 {
     return(FindIf(IteratorUtil.Begin(list), IteratorUtil.End(list), func));
 }
Exemplo n.º 14
0
        public static int CountIf <T>(InputIterator <T> begin, InputIterator <T> end, Functional.UnaryPredicate <T> func)
        {
            return(CountIf(IteratorUtil.CreateEnumerator(begin, end), func));

#if NEVER
            int count = 0;
            while (!begin.Equals(end))
            {
                if (func(begin.Read()))
                {
                    ++count;
                }
                begin.MoveNext();
            }

            return(count);
#endif
        }
Exemplo n.º 15
0
 public static void ReplaceIf <T>(IList <T> list, T newValue, Functional.UnaryPredicate <T> func)
     where T : IEquatable <T>
 {
     ReplaceIf(IteratorUtil.Begin(list), IteratorUtil.End(list), newValue, func);
 }
Exemplo n.º 16
0
 public static ListIterator <T> RemoveIf <T>(ListIterator <T> begin, ListIterator <T> end, Functional.UnaryPredicate <T> func)
 {
     return(RemoveIf((ForwardIterator <T>)begin, (ForwardIterator <T>)end, func) as ListIterator <T>);
 }
Exemplo n.º 17
0
 public static RandomAccessIterator <T> RemoveIf <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, Functional.UnaryPredicate <T> func)
 {
     return(RemoveIf((ForwardIterator <T>)begin, (ForwardIterator <T>)end, func) as RandomAccessIterator <T>);
 }
Exemplo n.º 18
0
        public static ForwardIterator <T> RemoveIf <T>(ForwardIterator <T> begin, ForwardIterator <T> end, Functional.UnaryPredicate <T> func)
        {
            ForwardIterator <T> iter = FindIf(begin, end, func);

            if (iter == null)
            {
                return(null);
            }

            return(RemoveCopyIf(iter, end, iter, func) as ForwardIterator <T>);
        }
Exemplo n.º 19
0
 public static ListIterator <T> CopyIf <T>(IList <T> source, ListIterator <T> dest, Functional.UnaryPredicate <T> func)
 {
     return(CopyIf(source, (OutputIterator <T>)dest, func) as ListIterator <T>);
 }
Exemplo n.º 20
0
 public static OutputIterator <T> CopyIf <T>(IList <T> source, OutputIterator <T> dest, Functional.UnaryPredicate <T> func)
 {
     return(CopyIf(IteratorUtil.Begin(source), IteratorUtil.End(source), dest, func));
 }
Exemplo n.º 21
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);
 }
Exemplo n.º 22
0
 public static OutputIterator <T> RemoveCopyIf <T>(InputIterator <T> begin, InputIterator <T> end, OutputIterator <T> dest, Functional.UnaryPredicate <T> func)
 {
     for (begin = IteratorUtil.Clone(begin), dest = IteratorUtil.Clone(dest); !begin.Equals(end); begin.MoveNext())
     {
         T t = begin.Read();
         if (!func(t))
         {
             dest.Write(t);
             dest.MoveNext();
         }
     }
     return(dest);
 }
Exemplo n.º 23
0
 public static ListIterator <T> FindIf <T>(ListIterator <T> begin, ListIterator <T> end, Functional.UnaryPredicate <T> func)
 {
     return((ListIterator <T>)FindIf((InputIterator <T>)begin, (InputIterator <T>)end, func));
 }
Exemplo n.º 24
0
 public static RandomAccessIterator <T> RemoveCopyIf <T>(InputIterator <T> begin, InputIterator <T> end, RandomAccessIterator <T> dest, Functional.UnaryPredicate <T> func)
 {
     return(RemoveCopyIf(begin, end, (OutputIterator <T>)dest, func) as RandomAccessIterator <T>);
 }
Exemplo n.º 25
0
 public static ForwardIterator <T> RemoveCopyIf <T>(InputIterator <T> begin, InputIterator <T> end, ForwardIterator <T> dest, Functional.UnaryPredicate <T> func)
 {
     return(RemoveCopyIf(begin, end, (OutputIterator <T>)dest, func) as ForwardIterator <T>);
 }
Exemplo n.º 26
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);
         }
     }
 }