Example #1
0
        public static bool PrevPermutation <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(ii.Read(), i_value))
                {
                    BidirectionalIterator <T> j = IteratorUtil.Clone(end);
                    for (j.MovePrev(); !func(j.Read(), i_value); j.MovePrev())
                    {
                        ;
                    }
                    IteratorUtil.Swap(i, j);
                    Reverse(ii, end);
                    return(true);
                }
                if (i.Equals(begin))
                {
                    Reverse(begin, end);
                    return(false);
                }
            }
        }
Example #2
0
        [Test] public void Mismatch_ListIterator()
        {
            array1[7]++;
            ListIterator <int> cmpIter = IteratorUtil.Begin(array2);

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

            Pair <ListIterator <int>, ListIterator <int> > p = Algorithm.Mismatch(IteratorUtil.Begin(array1), IteratorUtil.End(array1), cmpIter);

            Assert.AreEqual(7, p.First.Position);
            Assert.AreEqual(array1[7], p.First.Read());
            Assert.AreEqual(9, p.Second.Position);
            Assert.AreEqual(array2[9], p.Second.Read());

            p = Algorithm.Mismatch(IteratorUtil.Begin(array1), IteratorUtil.End(array1), cmpIter, EqualityComparer <int> .Default);
            Assert.AreEqual(7, p.First.Position);
            Assert.AreEqual(array1[7], p.First.Read());
            Assert.AreEqual(9, p.Second.Position);
            Assert.AreEqual(array2[9], p.Second.Read());
        }
Example #3
0
        [Test] public void FillN_Iterator()
        {
            int[] array = new int[10];
            ListIterator <int> begin = IteratorUtil.Begin(array);
            ListIterator <int> end   = IteratorUtil.End(array);

            begin.MoveNext();
            end.MovePrev();

            OutputIterator <int> result = Algorithm.FillN(begin, 8, 42);

            Assert.AreEqual(0, array[0]);
            Assert.AreEqual(0, array[array.Length - 1]);
            for (int i = 1; i < array.Length - 1; ++i)
            {
                Assert.AreEqual(42, array[i]);
            }
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(typeof(ListIterator <int>), result);
            Assert.AreEqual(9, (result as ListIterator <int>).Position);
        }
Example #4
0
        [Test] public void Copy_EnumerableString()
        {
            // test using simple int arrays
            string[] src  = Constants.TEST_STRING_ARRAY;
            string[] dest = new string[src.Length + 2];
            dest[0] = "XXXX";
            dest[dest.Length - 1] = "XXXX";

            ListIterator <string> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();

            Algorithm.Copy(src, destIter);

            Assert.AreEqual(dest[0], "XXXX");
            Assert.AreEqual(dest[dest.Length - 1], "XXXX");
            for (int i = 0; i < src.Length; ++i)
            {
                Assert.AreEqual(dest[i + 1], src[i]);
            }
        }
Example #5
0
        [Test] public void Copy_EnumerableInt()
        {
            // test using simple int arrays
            int[] src  = Constants.TEST_INT_ARRAY;
            int[] dest = new int[src.Length + 2];
            dest[0] = Int32.MinValue;
            dest[dest.Length - 1] = Int32.MinValue;

            ListIterator <int> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();

            Algorithm.Copy(src, destIter);

            Assert.AreEqual(dest[0], Int32.MinValue);
            Assert.AreEqual(dest[dest.Length - 1], Int32.MinValue);
            for (int i = 0; i < src.Length; ++i)
            {
                Assert.AreEqual(dest[i + 1], src[i]);
            }
        }
Example #6
0
        public static bool NextPermutation <T>(BidirectionalIterator <T> begin, BidirectionalIterator <T> end, IComparer <T> comparer)
        {
            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 (comparer.Compare(i_value, ii.Read()) == -1)
                {
                    BidirectionalIterator <T> j = IteratorUtil.Clone(end);
                    for (j.MovePrev(); comparer.Compare(i_value, j.Read()) != -1; j.MovePrev())
                    {
                        ;
                    }
                    IteratorUtil.Swap(i, j);
                    Reverse(ii, end);
                    return(true);
                }
                if (i.Equals(begin))
                {
                    Reverse(begin, end);
                    return(false);
                }
            }
        }
Example #7
0
        [Test] public void ReplaceIf_List()
        {
            ListIterator <int> destIter = IteratorUtil.Begin(dest);

            Algorithm.Copy(IteratorUtil.Begin(src), IteratorUtil.End(src), destIter); // we assume Algorithm.Copy is good. It has its own unit test.

            Algorithm.ReplaceIf(dest, 42, Is29);

            Assert.AreEqual(dest[dest.Length - 1], Int32.MinValue);
            Assert.AreEqual(dest[dest.Length - 2], Int32.MinValue);
            for (int i = 0; i < count; ++i)
            {
                if (src[i] == 29)
                {
                    Assert.AreEqual(42, dest[i]);
                }
                else
                {
                    Assert.AreEqual(src[i], dest[i]);
                }
            }
        }
Example #8
0
        [Test] public void ReplaceCopy_Comparer()
        {
            ListIterator <int> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();

            Algorithm.ReplaceCopy(IteratorUtil.Begin(src), IteratorUtil.End(src), destIter, 29, 42, EqualityComparer <int> .Default);

            Assert.AreEqual(dest[0], Int32.MinValue);
            Assert.AreEqual(dest[dest.Length - 1], Int32.MinValue);
            for (int i = 0; i < count; ++i)
            {
                if (src[i] == 29)
                {
                    Assert.AreEqual(42, dest[i + 1]);
                }
                else
                {
                    Assert.AreEqual(src[i], dest[i + 1]);
                }
            }
        }
Example #9
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
        }
Example #10
0
        public static InputIterator <T> AdjacentFind <T>(InputIterator <T> begin, InputIterator <T> end, IEqualityComparer <T> comparer)
        {
            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 (comparer.Equals(t1, t2))
                {
                    return(IteratorUtil.Clone(begin));
                }
            }
            return(null);
        }
Example #11
0
        public void TestAddRange()
        {
            int[] array = new int[]{0,1,2,3,4,5,6};
            Vector<int> v = new Vector<int>(array);
            int[] splice = new int[]{-1,-2,-3};

            v.AddRange(splice);
            Assert.AreEqual(array.Length+splice.Length, v.Count);
            Assert.AreEqual(5, v[5]);
            Assert.AreEqual(6, v[6]);
            Assert.AreEqual(-1, v[7]);
            Assert.AreEqual(-2, v[8]);
            Assert.AreEqual(-3, v[9]);

            v = new Vector<int>(array);
            v.AddRange(IteratorUtil.Begin(splice), IteratorUtil.End(splice));
            Assert.AreEqual(array.Length+splice.Length, v.Count);
            Assert.AreEqual(5, v[5]);
            Assert.AreEqual(6, v[6]);
            Assert.AreEqual(-1, v[7]);
            Assert.AreEqual(-2, v[8]);
            Assert.AreEqual(-3, v[9]);
        }
Example #12
0
        [Test] public void SwapRanges_Iterator()
        {
            int[] src2 = new int[src.Length];
            for (int i = 0; i < count; ++i)
            {
                src2[i] = -src[i];
            }

            ListIterator <int> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();
            Algorithm.Copy(IteratorUtil.Begin(src), IteratorUtil.End(src), destIter); // we assume Algorithm.Copy is good. It has its own unit test.

            Algorithm.SwapRanges(IteratorUtil.Begin(src2), IteratorUtil.End(src2), destIter);

            Assert.AreEqual(dest[0], Int32.MinValue);
            Assert.AreEqual(dest[dest.Length - 1], Int32.MinValue);
            for (int i = 0; i < count; ++i)
            {
                Assert.AreEqual(-src[i], dest[i + 1]);
                Assert.AreEqual(src[i], src2[i]);
            }
        }
Example #13
0
        [Test] public void Mismatch_InputIterator()
        {
            array1[7]++;
            ListIterator <int> cmpIter = IteratorUtil.Begin(array2);

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

            InputIterator <int> begin = IteratorUtil.Begin(array1);
            InputIterator <int> end   = IteratorUtil.End(array1);
            InputIterator <int> cmp   = cmpIter;
            Pair <InputIterator <int>, InputIterator <int> > p = Algorithm.Mismatch(begin, end, cmp);

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

            p = Algorithm.Mismatch(begin, end, cmp, EqualityComparer <int> .Default);
            Assert.AreEqual(array1[7], p.First.Read());
            Assert.IsInstanceOfType(typeof(ListIterator <int>), p.First);
            Assert.AreEqual(array2[9], p.Second.Read());
            Assert.IsInstanceOfType(typeof(ListIterator <int>), p.Second);
        }
Example #14
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);
        }
Example #15
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);
        }
Example #16
0
 public static void Copy <T>(InputIterator <T> begin, InputIterator <T> end, IList <T> target)
 {
     Copy(begin, end, IteratorUtil.Begin(target));
 }
Example #17
0
 public static void Copy <T>(IEnumerable <T> enumerable, IList <T> target)
 {
     Copy(enumerable, IteratorUtil.Begin(target));
 }
Example #18
0
 public static OutputIterator <T> RemoveCopy <T>(IList <T> source, OutputIterator <T> dest, T value)
     where T : IEquatable <T>
 {
     return(RemoveCopy(IteratorUtil.Begin(source), IteratorUtil.End(source), dest, value));
 }
Example #19
0
 public static ListIterator <T> RemoveCopy <T>(IList <T> list, IList <T> dest, T value, IEqualityComparer <T> comparer)
 {
     return(RemoveCopy(IteratorUtil.Begin(list), IteratorUtil.End(list), IteratorUtil.Begin(dest), value, comparer));
 }
Example #20
0
 public static OutputIterator <T> RemoveCopy <T>(IList <T> source, OutputIterator <T> dest, T value, IEqualityComparer <T> comparer)
 {
     return(RemoveCopy(IteratorUtil.Begin(source), IteratorUtil.End(source), dest, value, comparer));
 }
Example #21
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));
 }
Example #22
0
 public static ListIterator <T> UniqueCopy <T>(IList <T> list, IList <T> dest, IEqualityComparer <T> comparer)
 {
     return(UniqueCopy(IteratorUtil.Begin(list), IteratorUtil.End(list), IteratorUtil.Begin(dest), comparer));
 }
Example #23
0
 public static ListIterator <T> UniqueCopy <T>(IList <T> source, IList <T> dest)
     where T : IEquatable <T>
 {
     return(UniqueCopy(IteratorUtil.Begin(source), IteratorUtil.End(source), IteratorUtil.Begin(dest)));
 }
Example #24
0
 public static OutputIterator <T> UniqueCopy <T>(IList <T> source, OutputIterator <T> dest, IEqualityComparer <T> comparer)
 {
     return(UniqueCopy(IteratorUtil.Begin(source), IteratorUtil.End(source), dest, comparer));
 }
Example #25
0
 public static RandomAccessIterator <T> UniqueCopy <T>(IList <T> source, RandomAccessIterator <T> dest)
     where T : IEquatable <T>
 {
     return(UniqueCopy(IteratorUtil.Begin(source), IteratorUtil.End(source), (OutputIterator <T>)dest) as RandomAccessIterator <T>);
 }
Example #26
0
 public static ForwardIterator <T> UniqueCopy <T>(IList <T> source, ForwardIterator <T> dest)
     where T : IEquatable <T>
 {
     return(UniqueCopy(IteratorUtil.Begin(source), IteratorUtil.End(source), (OutputIterator <T>)dest) as ForwardIterator <T>);
 }
Example #27
0
 static public void RandomShuffle <T>(IList <T> list, RandomShuffleFunc func)
 {
     RandomShuffle(IteratorUtil.Begin(list), IteratorUtil.End(list), func);
 }
Example #28
0
 public static void Copy <T>(IList <T> source, int startIndex, IList <T> dest, int destIndex)
 {
     Copy(IteratorUtil.Begin(source).OffsetBy(startIndex), IteratorUtil.End(source),
          IteratorUtil.Begin(dest).OffsetBy(destIndex));
 }
Example #29
0
 public static void Generate <T>(IList <T> list, Functional.GenFunction <T> func)
 {
     Generate(IteratorUtil.Begin(list), IteratorUtil.End(list), func);
 }
Example #30
0
 public static void ReplaceCopy <T>(IList <T> list, IList <T> dest, T oldValue, T newValue, IEqualityComparer <T> comparer)
 {
     ReplaceCopy(IteratorUtil.Begin(list), IteratorUtil.End(list), IteratorUtil.Begin(dest), oldValue, newValue, comparer);
 }