コード例 #1
0
        [Test] public void RemoveIf_ForwardIterator()
        {
            ForwardIterator <int> begin = IteratorUtil.Begin(src);
            ForwardIterator <int> end   = IteratorUtil.End(src);

            ForwardIterator <int> result = Algorithm.RemoveIf(begin, end, Is29);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, (begin as ListIterator <int>).Position);
            Assert.AreEqual(src.Length, IteratorUtil.Distance(begin, end));
            Assert.AreEqual(src.Length - 4, IteratorUtil.Distance(begin, result));
        }
コード例 #2
0
        [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());
        }
コード例 #3
0
        [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());
        }
コード例 #4
0
ファイル: UniqueCopyTest_Int.cs プロジェクト: ggeurts/nhive
        [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());
        }
コード例 #5
0
ファイル: UniqueCopyTest_Int.cs プロジェクト: ggeurts/nhive
        [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());
        }
コード例 #6
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());
        }
コード例 #7
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());
        }
コード例 #8
0
        public void SwapRangeTest()
        {
            using (ForwardIterator <int> inputIterator1 = new ForwardIterator <int>(new int[] { 1, 2 }))
            {
                using (ForwardIterator <int> inputIterator2 = new ForwardIterator <int>(new int[] { 3, 4 }))
                {
                    Algorithm.SwapRange(inputIterator1, inputIterator2);

                    Assert.IsTrue(inputIterator1[0] == 3 && inputIterator1[1] == 4 &&
                                  inputIterator2[0] == 1 && inputIterator2[1] == 2);
                }
            }
        }
コード例 #9
0
ファイル: Algorithm.Remove.cs プロジェクト: ggeurts/nhive
        public static ForwardIterator <T> Remove <T>(ForwardIterator <T> begin, ForwardIterator <T> end, T value)
            where T : IEquatable <T>
        {
            ForwardIterator <T> iter = Find(begin, end, value);

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

            //ForwardIterator<T> dest = IteratorUtil.Clone(iter);
            return(RemoveCopy(iter, end, iter, value));
        }
コード例 #10
0
ファイル: UniqueCopyTest_Int.cs プロジェクト: ggeurts/nhive
        [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());
        }
コード例 #11
0
ファイル: Algorithm.SearchN.cs プロジェクト: ggeurts/nhive
        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);
        }
コード例 #12
0
ファイル: Algorithm.SearchN.cs プロジェクト: ggeurts/nhive
        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);
        }
コード例 #13
0
        [Test] public void Search_Iterator()
        {
            int[] array  = Constants.TEST_INT_ARRAY;
            int[] search = new int[3];

            Algorithm.CopyN(array, 5, 3, search, 0);

            ForwardIterator <int> result = Algorithm.Search(IteratorUtil.Begin(array), IteratorUtil.End(array),
                                                            IteratorUtil.Begin(search), IteratorUtil.End(search));

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(typeof(ListIterator <int>), result);
            Assert.AreEqual(5, IteratorUtil.Distance(IteratorUtil.Begin(array), result));
            Assert.AreEqual(array[5], result.Read());
        }
コード例 #14
0
        public void ReplaceTest()
        {
            int[] data = { 1, 2, 3 };

            using (ForwardIterator <int> inputIterator = new ForwardIterator <int>(data))
            {
                Algorithm.Replace(inputIterator, 3, 2);

                bool isCorrectData = (inputIterator[0] == 1 &&
                                      inputIterator[1] == 2 &&
                                      inputIterator[2] == 2);

                Assert.IsTrue(isCorrectData);
            }
        }
コード例 #15
0
        public void FillTest()
        {
            using (ForwardIterator <int> inputIterator = new ForwardIterator <int>(3))
            {
                Algorithm.Fill(inputIterator, 5);

                Assert.IsTrue(inputIterator.Count() == 3);

                bool isCorrectData = (inputIterator[0] == 5 &&
                                      inputIterator[1] == 5 &&
                                      inputIterator[2] == 5);

                Assert.IsTrue(isCorrectData);
            }
        }
コード例 #16
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);
        }
コード例 #17
0
        public static ForwardIterator <T> Unique <T>(ForwardIterator <T> begin, ForwardIterator <T> end, IEqualityComparer <T> comp)
        {
            if (begin.Equals(end))
            {
                return(null);
            }

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

            if (firstMatch != null)
            {
                return(UniqueCopy(firstMatch, end, firstMatch, comp) as ForwardIterator <T>);
            }
            return(null);
        }
コード例 #18
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);
 }
コード例 #19
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);
 }
コード例 #20
0
        public void ReplaceIfTest()
        {
            int[] data = { 1, 2, 3 };

            using (ForwardIterator <int> inputIterator = new ForwardIterator <int>(data))
            {
                Algorithm.ReplaceIf(inputIterator, delegate(int x)
                {
                    return(x == 3);
                }, 2);

                bool isCorrectData = (inputIterator[0] == 1 &&
                                      inputIterator[1] == 2 &&
                                      inputIterator[2] == 2);

                Assert.IsTrue(isCorrectData);
            }
        }
コード例 #21
0
        public void RandomShuffle()
        {
            int[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16 };

            using (IForwardIterator <int> inputIterator = new ForwardIterator <int>(data))
            {
                Algorithm.RandomShuffle(inputIterator);

                inputIterator.Begin();

                bool isSorted = Algorithm.IsSorted(inputIterator);

                //we have random the data , therefore it should not be sorted
                //the chance of returning the sorted data is very small

                Assert.IsFalse(isSorted);
            }
        }
コード例 #22
0
        public void GenerateNTest()
        {
            using (ForwardIterator <int> inputIterator = new ForwardIterator <int>(3))
            {
                Algorithm.GenerateN(inputIterator, 1, delegate
                {
                    return(2);
                });

                Assert.IsTrue(inputIterator.Count() == 3);

                bool isCorrectData = (inputIterator[0] == 2 &&
                                      inputIterator[1] == 0 &&
                                      inputIterator[2] == 0);

                Assert.IsTrue(isCorrectData);
            }
        }
コード例 #23
0
        [Test] public void AdjacentFind_ForwardIterator()
        {
            int[] array = Constants.TEST_INT_ARRAY;

            ForwardIterator <int> begin = IteratorUtil.Begin(array);
            ForwardIterator <int> end   = IteratorUtil.End(array);
            ForwardIterator <int> iter  = Algorithm.AdjacentFind(begin, end);

            Assert.AreEqual(6, IteratorUtil.Distance(begin, iter));
            Assert.AreEqual(array[6], iter.Read());

            iter = Algorithm.AdjacentFind(IteratorUtil.Begin(array), IteratorUtil.End(array), EqualityComparer <int> .Default);
            Assert.AreEqual(6, IteratorUtil.Distance(begin, iter));
            Assert.AreEqual(array[6], iter.Read());

            iter = Algorithm.AdjacentFind(IteratorUtil.Begin(array), IteratorUtil.End(array), AreEqual);
            Assert.AreEqual(6, IteratorUtil.Distance(begin, iter));
            Assert.AreEqual(array[6], iter.Read());
        }
コード例 #24
0
ファイル: MinElementTest.cs プロジェクト: ggeurts/nhive
        [Test] public void MinElement_IntIteratorPredicate()
        {
            int[] array             = Constants.TEST_INT_ARRAY;
            ListIterator <int> iter = Algorithm.MinElement(IteratorUtil.Begin(array), IteratorUtil.End(array), LessThanInt);

            Assert.AreEqual(-17, iter.Read());
            Assert.AreEqual(4, iter.Position);

            RandomAccessIterator <int> beginR = IteratorUtil.Begin(array);
            RandomAccessIterator <int> endR   = IteratorUtil.End(array);
            RandomAccessIterator <int> iterR  = Algorithm.MinElement(beginR, endR, LessThanInt);

            Assert.AreEqual(4, ((ListIterator <int>)iterR).Position);

            ForwardIterator <int> beginF = IteratorUtil.Begin(array);
            ForwardIterator <int> endF   = IteratorUtil.End(array);
            ForwardIterator <int> iterF  = Algorithm.MinElement(beginF, endF, LessThanInt);

            Assert.AreEqual(4, ((ListIterator <int>)iterF).Position);
        }
コード例 #25
0
ファイル: MaxElementTest.cs プロジェクト: ggeurts/nhive
        [Test] public void MaxElement_IntIteratorPredicate()
        {
            int[] array             = Constants.TEST_INT_ARRAY;
            ListIterator <int> iter = Algorithm.MaxElement(IteratorUtil.Begin(array), IteratorUtil.End(array), Functional.Compare);

            Assert.AreEqual(100, iter.Read());
            Assert.AreEqual(10, iter.Position);

            RandomAccessIterator <int> beginR = IteratorUtil.Begin(array);
            RandomAccessIterator <int> endR   = IteratorUtil.End(array);
            RandomAccessIterator <int> iterR  = Algorithm.MaxElement(beginR, endR, Functional.Compare);

            Assert.AreEqual(10, ((ListIterator <int>)iterR).Position);

            ForwardIterator <int> beginF = IteratorUtil.Begin(array);
            ForwardIterator <int> endF   = IteratorUtil.End(array);
            ForwardIterator <int> iterF  = Algorithm.MaxElement(beginF, endF, Functional.Compare);

            Assert.AreEqual(10, ((ListIterator <int>)iterF).Position);
        }
コード例 #26
0
        public void PartitionTest()
        {
            int[] data = { 0, 1, 2, 3 };

            UnaryPredicate <int> isEven = delegate(int x)
            {
                return(x % 2 == 0);
            };

            using (ForwardIterator <int> inputIterator = new ForwardIterator <int>(data))
            {
                Algorithm.Partition(inputIterator, isEven);

                bool isCorrectData = (inputIterator[0] == 0 &&
                                      inputIterator[1] == 2 &&
                                      inputIterator[2] == 1 &&
                                      inputIterator[3] == 3);

                Assert.IsTrue(isCorrectData);
            }
        }
コード例 #27
0
        public void PartitionCopyTest()
        {
            int[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            IList <int> trueList = new List <int>();

            IList <int> falseList = new List <int>();

            UnaryPredicate <int> isEven = delegate(int x)
            {
                return(x % 2 == 0);
            };

            using (ForwardIterator <int> inputIterator = new ForwardIterator <int>(data))
            {
                using (IOutputIterator <int> trueOutputIterator = new BackInsertIterator <int>(trueList))
                {
                    using (IOutputIterator <int> falseOutputIterator = new BackInsertIterator <int>(falseList))
                    {
                        Algorithm.PartitionCopy(inputIterator, trueOutputIterator,
                                                falseOutputIterator, isEven);

                        bool isEvenCorrect = (trueList[0] == 0 &&
                                              trueList[1] == 2 &&
                                              trueList[2] == 4 &&
                                              trueList[3] == 6 &&
                                              trueList[4] == 8);

                        bool isOddCorrect = (falseList[0] == 1 &&
                                             falseList[1] == 3 &&
                                             falseList[2] == 5 &&
                                             falseList[3] == 7 &&
                                             falseList[4] == 9);

                        Assert.IsTrue(isEvenCorrect && isOddCorrect);
                    }
                }
            }
        }
コード例 #28
0
ファイル: Algorithm.RotateCopy.cs プロジェクト: ggeurts/nhive
        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
        }
コード例 #29
0
        public void UniqueCopyTest()
        {
            string[] data = new string[]
            {
                "hello",
                "world",
                "road",
                "road",
                "cool",
                "job",
                "road"
            };

            int expectedCount = 6;

            int actualCount = 0;

            IList <string> lst = new List <string>();

            using (ForwardIterator <string> inputIterator = new ForwardIterator <string>(data))
            {
                using (IOutputIterator <string> outputIterator = new BackInsertIterator <string>(lst))
                {
                    Algorithm.UniqueCopy(inputIterator, outputIterator);

                    actualCount = lst.Count();

                    Assert.IsTrue(expectedCount == actualCount);

                    bool isCorrectData = (lst[0] == "hello" &&
                                          lst[1] == "world" &&
                                          lst[2] == "world" &&
                                          lst[3] == "cool" &&
                                          lst[4] == "job" &&
                                          lst[5] == "road");
                }
            }
        }
コード例 #30
0
        [Test] public void Mismatch_ForwardIterator()
        {
            array1[7]++;
            ListIterator <int> cmpIter = IteratorUtil.Begin(array2);

            cmpIter.MoveNext();
            cmpIter.MoveNext();

            ForwardIterator <int> beginF = IteratorUtil.Begin(array1);
            ForwardIterator <int> endF   = IteratorUtil.End(array1);
            ForwardIterator <int> cmpF   = cmpIter;
            Pair <ForwardIterator <int>, ForwardIterator <int> > pF = Algorithm.Mismatch(beginF, endF, cmpF);

            Assert.AreEqual(array1[7], pF.First.Read());
            Assert.IsInstanceOfType(typeof(ListIterator <int>), pF.First);
            Assert.AreEqual(array2[9], pF.Second.Read());
            Assert.IsInstanceOfType(typeof(ListIterator <int>), pF.Second);

            pF = Algorithm.Mismatch(beginF, endF, cmpF, EqualityComparer <int> .Default);
            Assert.AreEqual(array1[7], pF.First.Read());
            Assert.IsInstanceOfType(typeof(ListIterator <int>), pF.First);
            Assert.AreEqual(array2[9], pF.Second.Read());
            Assert.IsInstanceOfType(typeof(ListIterator <int>), pF.Second);
        }