示例#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
        /// <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);
        }