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

            Assert.That(heap[pointer], Is.EqualTo(42));
        }
Exemplo n.º 3
0
        public void Insert_Empty_IsNotEmpty()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(0);
            Assert.That(heap.IsEmpty, Is.False);
        }
Exemplo n.º 4
0
        public void ExtractMinimum_SingleElement_ReturnsElement()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(42);
            Assert.That(heap.ExtractMinimum(), Is.EqualTo(42));
        }
Exemplo n.º 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);
        }
Exemplo n.º 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));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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;
        }
Exemplo n.º 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()));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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 }));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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 }));
        }
Exemplo n.º 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()));
        }
Exemplo n.º 18
0
        public void ExtractMinimum_Empty_Throws()
        {
            var heap = new PairingHeap <int>();

            Assert.That(() => heap.ExtractMinimum(), Throws.InvalidOperationException);
        }
Exemplo n.º 19
0
        public void Ctor_DefaultComparer_CountIsZero()
        {
            var heap = new PairingHeap <int>(Comparer <int> .Default);

            Assert.That(heap.Count(), Is.Zero);
        }
Exemplo n.º 20
0
        public void Count_Empty_IsZero()
        {
            var heap = new PairingHeap <int>();

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