public void BinomialTest2()
        {
            var heap = LazyBinomialHeap <int> .Empty;

            for (var i = 0; i < 0x100; i++)
            {
                heap = LazyBinomialHeap <int> .Insert(1, heap);

                var dumpHeap = DumpHeap(heap, true);
                //Console.WriteLine(dumpHeap, true);
                var blocks = dumpHeap.Split(';');

                var j = 0;
                var p = 0;
                for (var k = i + 1; k > 0; k >>= 1, j++)
                {
                    if (k % 2 == 0)
                    {
                        continue;
                    }
                    var q     = (int)Math.Pow(2, j);
                    var block = blocks[p++];
                    Assert.AreEqual(q, Counters.CountChar(block, '1') - 1);
                }
            }
        }
        public void DeleteLotsOfMinsTest()
        {
            const int size   = 1000;
            var       random = new Random(3456);
            var       heap   = LazyBinomialHeap <int> .Empty;

            for (var i = 0; i < size; i++)
            {
                heap = LazyBinomialHeap <int> .Insert(random.Next(size), heap);
            }
            Assert.IsFalse(heap.IsValueCreated);

            var last  = 0;
            var count = 0;

            while (!LazyBinomialHeap <int> .IsEmpty(heap))
            {
                var next = LazyBinomialHeap <int> .FindMin(heap);

                heap = LazyBinomialHeap <int> .DeleteMin(heap);

                Assert.IsTrue(last <= next);
                last = next;
                count++;
            }

            Assert.AreEqual(size, count);
        }
        public void InsertTest3()
        {
            var heap1 = Enumerable.Range(0, 3).Aggregate(LazyBinomialHeap <int> .Empty, (current, i) => LazyBinomialHeap <int> .Insert(i, current));
            var heap  = LazyBinomialHeap <int> .Insert(3, heap1);

            Assert.AreEqual("$[0, [2, [3]], [1]]", DumpHeap(heap, true));
        }
        public void InsertTest1()
        {
            var empty = LazyBinomialHeap <int> .Empty;
            var heap  = LazyBinomialHeap <int> .Insert(0, empty);

            Assert.AreEqual("$[0]", DumpHeap(heap, true));
        }
Esempio n. 5
0
            public void RunModel(IModel obj)
            {
                var heap = LazyBinomialHeap <int> .Empty;

                for (var i = 0; i < Size; i++)
                {
                    var val = _random.Next(Size);
                    heap = LazyBinomialHeap <int> .Insert(val, heap);

                    Assert.IsFalse(heap.IsValueCreated);
                }

                Console.WriteLine(LazyBinomialHeapTests.DumpHeap(heap, true));

                var last  = 0;
                var count = 0;

                while (!LazyBinomialHeap <int> .IsEmpty(heap))
                {
                    Assert.IsTrue(heap.IsValueCreated);

                    var next = LazyBinomialHeap <int> .FindMin(heap);

                    Assert.IsTrue(last <= next);
                    last = next;

                    heap = LazyBinomialHeap <int> .DeleteMin(heap);

                    count++;
                }

                Assert.AreEqual(Size, count);
            }
        public void EmptyTest()
        {
            var empty = LazyBinomialHeap <int> .Empty;

            Assert.IsTrue(LazyBinomialHeap <int> .IsEmpty(empty));

            var heap = LazyBinomialHeap <int> .Insert(1, empty);

            Assert.IsFalse(LazyBinomialHeap <int> .IsEmpty(heap));
        }
        public void BinomialTest1()
        {
            var heap = LazyBinomialHeap <int> .Empty;

            for (var i = 0; i < 16; i++)
            {
                heap = LazyBinomialHeap <int> .Insert(i, heap);

                var dumpHeap = DumpHeap(heap, true);
                // Console.WriteLine(dumpHeap, true);

                var semicolons = Counters.CountChar(dumpHeap, ';');
                Assert.AreEqual(Counters.CountBinaryOnes(i + 1), semicolons);
            }
        }
        public void MonolithicTest()
        {
            var empty = LazyBinomialHeap <int> .Empty;
            var x1    = LazyBinomialHeap <int> .Insert(3, empty);

            var x2 = LazyBinomialHeap <int> .Insert(2, x1);

            Assert.IsFalse(x1.IsValueCreated);
            Assert.IsFalse(x2.IsValueCreated);

            // Once we look at one element, the entire list will be forced created.
            Assert.IsNotNull(x2.Value);
            Assert.IsTrue(x1.IsValueCreated);
            Assert.IsTrue(x2.IsValueCreated);
        }
        public void DeleteMinTest()
        {
            var heap = Enumerable.Range(0, 8).Aggregate(LazyBinomialHeap <int> .Empty, (current, i) => LazyBinomialHeap <int> .Insert(i, current));

            heap = LazyBinomialHeap <int> .DeleteMin(heap);

            Assert.AreEqual("$[1]; [2, [3]]; [4, [6, [7]], [5]]", DumpHeap(heap, true));
        }
        public void FindMinTest()
        {
            var heap = Enumerable.Range(0, 8).Aggregate(LazyBinomialHeap <int> .Empty, (current, i) => LazyBinomialHeap <int> .Insert(i, current));
            var min  = LazyBinomialHeap <int> .FindMin(heap);

            Assert.AreEqual(0, min);
        }
        public void MergeTest5()
        {
            var heap1 = Enumerable.Range(0, 4).Aggregate(LazyBinomialHeap <int> .Empty, (current, i) => LazyBinomialHeap <int> .Insert(i, current));
            var heap2 = Enumerable.Range(10, 4).Aggregate(LazyBinomialHeap <int> .Empty, (current, i) => LazyBinomialHeap <int> .Insert(i, current));
            var heap  = LazyBinomialHeap <int> .Merge(heap1, heap2);

            Assert.AreEqual("$[0, [10, [12, [13]], [11]], [2, [3]], [1]]", DumpHeap(heap, true));
        }
        public void MergeTest2()
        {
            var empty = LazyBinomialHeap <int> .Empty;
            var heap2 = Enumerable.Range(0, 8).Aggregate(LazyBinomialHeap <int> .Empty, (current, i) => LazyBinomialHeap <int> .Insert(i, current));
            var heap  = LazyBinomialHeap <int> .Merge(empty, heap2);

            Assert.AreSame(heap2.Value, heap.Value);
        }