コード例 #1
0
 private static IEnumerable <int> ExtractAll(PairingHeap <int> heap)
 {
     while (heap.TryExtractMinimum(out var min))
     {
         yield return(min);
     }
 }
コード例 #2
0
        public void GetItem_SingleElement_ReturnsValue()
        {
            var heap    = new PairingHeap <int>();
            var pointer = heap.Insert(42);

            Assert.That(heap[pointer], Is.EqualTo(42));
        }
コード例 #3
0
        public void Insert_Empty_IsNotEmpty()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(0);
            Assert.That(heap.IsEmpty, Is.False);
        }
コード例 #4
0
        public void ExtractMinimum_SingleElement_ReturnsElement()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(42);
            Assert.That(heap.ExtractMinimum(), Is.EqualTo(42));
        }
コード例 #5
0
        public void Remove_AllElements_IsEmpty(params int[] values)
        {
            var heap     = new PairingHeap <int>();
            var pointers = values.Select(heap.Insert).ToList();

            pointers.ForEach(heap.Remove);
            Assert.That(heap.IsEmpty, Is.True);
        }
コード例 #6
0
        public void Merge_WithEmpty_CountUnchanged()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(0);
            heap.Merge(new());
            Assert.That(heap.Count(), Is.EqualTo(1));
        }
コード例 #7
0
        public void Merge_WithNonEmpty_CountChanged()
        {
            var heap  = new PairingHeap <int>();
            var other = new PairingHeap <int>();

            other.Insert(0);
            heap.Merge(other);
            Assert.That(heap.Count(), Is.EqualTo(1));
        }
コード例 #8
0
        public void ExtractMinimum_SingleElement_RemovesElement()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(0);
            heap.ExtractMinimum();
            Assert.That(heap.IsEmpty, Is.True);
            Assert.That(() => heap.Minimum, Throws.InvalidOperationException);
        }
コード例 #9
0
        public void Merge_WithNonEmpty_ClearsArgument()
        {
            var heap  = new PairingHeap <int>();
            var other = new PairingHeap <int>();

            other.Insert(0);
            heap.Merge(other);
            Assert.That(other.IsEmpty, Is.True);
        }
コード例 #10
0
ファイル: PairingHeap.cs プロジェクト: SWeini/Cornucopia.Net
        /// <summary>
        ///     Destructively merges another heap.
        /// </summary>
        /// <param name="heap">Another heap with elements to add to this heap.</param>
        /// <remarks><paramref name="heap"/> is emptied in the process.</remarks>
        public void Merge(PairingHeap <T> heap)
        {
            if (heap._head == null)
            {
                return;
            }

            this._head = this._head == null ? heap._head : this.Meld(heap._head, this._head);
            heap._head = null;
        }
コード例 #11
0
        public void Minimum_ManyElements_IsCorrect(params int[] values)
        {
            var heap = new PairingHeap <int>();

            foreach (var value in values)
            {
                heap.Insert(value);
            }

            Assert.That(heap.Minimum, Is.EqualTo(values.Min()));
        }
コード例 #12
0
        public void Insert_MultipleTimesIntoEmpty_CountIsCorrect(int size)
        {
            var heap = new PairingHeap <int>();

            for (var i = 0; i < size; i++)
            {
                heap.Insert(0);
            }

            Assert.That(heap.Count(), Is.EqualTo(size));
        }
コード例 #13
0
        public void Remove_MiddleElement_DecreasesCount()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(2);
            var pointer = heap.Insert(1);

            heap.Insert(0);
            heap.Remove(pointer);
            Assert.That(heap.Count(), Is.EqualTo(2));
        }
コード例 #14
0
        public void Decrease_MiddleElement_ExtractsAllElementsCorrectly()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(2);
            var pointer = heap.Insert(1);

            heap.Insert(0);
            heap.Decrease(pointer, -1);
            var elements = ExtractAll(heap).ToArray();

            Assert.That(elements, Is.EqualTo(new[] { -1, 0, 2 }));
        }
コード例 #15
0
        public void Remove_ManyElements_DecreasesCount()
        {
            var heap = new PairingHeap <int>();

            for (var i = 0; i < 15; i++)
            {
                heap.Insert(i);
            }

            var pointer = heap.Insert(5);

            heap.Remove(pointer);

            Assert.That(heap.Count(), Is.EqualTo(15));
        }
コード例 #16
0
        public void Merge_TwoNonEmpty_ExtractsAllElementsCorrectly()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(0);
            heap.Insert(2);

            var other = new PairingHeap <int>();

            other.Insert(1);
            other.Insert(3);

            heap.Merge(other);
            Assert.That(ExtractAll(heap), Is.EqualTo(new[] { 0, 1, 2, 3 }));
        }
コード例 #17
0
        public void ExtractMinimum_ManyElements_CanSortSequence(params int[] values)
        {
            var heap = new PairingHeap <int>();

            foreach (var value in values)
            {
                heap.Insert(value);
            }

            var sorted = new List <int>();

            while (!heap.IsEmpty)
            {
                sorted.Add(heap.ExtractMinimum());
            }

            Assert.That(sorted, Is.EqualTo(values.OrderBy(x => x).ToList()));
        }
コード例 #18
0
        public void ExtractMinimum_Empty_Throws()
        {
            var heap = new PairingHeap <int>();

            Assert.That(() => heap.ExtractMinimum(), Throws.InvalidOperationException);
        }
コード例 #19
0
        public void Ctor_DefaultComparer_CountIsZero()
        {
            var heap = new PairingHeap <int>(Comparer <int> .Default);

            Assert.That(heap.Count(), Is.Zero);
        }
コード例 #20
0
        public void Count_Empty_IsZero()
        {
            var heap = new PairingHeap <int>();

            Assert.That(heap.Count(), Is.Zero);
        }