Ejemplo n.º 1
0
        [Test] public void NthElement_Iterator_Comparison()
        {
            int[] copy = new int[src.Length];
            Algorithm.Copy(src, copy);
            Array.Sort(copy);

            ListIterator <int> begin = IteratorUtil.Begin(src);
            ListIterator <int> end   = IteratorUtil.End(src);
            ListIterator <int> nth   = IteratorUtil.AdvanceCopy(begin, 4);
            int pivotValue           = nth.Read();

            Algorithm.NthElement(begin, nth, end, Functional.Compare);
            Assert.AreEqual(0, begin.Position);
            Assert.AreEqual(src.Length, end.Position);

            Assert.AreEqual(copy[4], src[4]);

            for (int i = 0; i < 4; ++i)
            {
                Assert.IsTrue(src[i] < src[4]);
            }

            for (int i = 5; i < src.Length; ++i)
            {
                Assert.IsTrue(src[i] >= src[4]);
            }
        }
Ejemplo n.º 2
0
        static public void RandomShuffle <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, RandomShuffleFunc func)
        {
            if (begin.Equals(end))
            {
                return;
            }

            begin = IteratorUtil.Clone(begin);
            for (RandomAccessIterator <T> iter = IteratorUtil.AdvanceCopy(begin, 1); !iter.Equals(end); iter.MoveNext())
            {
                begin.Position = func(iter.Position + 1);
                IteratorUtil.Swap(iter, begin);
            }
        }