예제 #1
0
        public void Complex_maximum_priority_queue_test_with_multiple_operation()
        {
            IPriorityQueue <string, int> priorityQueue = CreateMaximumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry1        = priorityQueue.Enqueue("a", -1);
            IPriorityQueueEntry <string> entry2        = priorityQueue.Enqueue("b", -2);
            IPriorityQueueEntry <string> entry3        = priorityQueue.Enqueue("c", -3);
            IPriorityQueueEntry <string> entry4        = priorityQueue.Enqueue("d", -4);

            priorityQueue.UpdatePriority(entry1, -5);
            priorityQueue.UpdatePriority(entry2, -5);
            priorityQueue.UpdatePriority(entry1, -7);
            priorityQueue.UpdatePriority(entry2, -1);
            priorityQueue.UpdatePriority(entry4, -4);
            priorityQueue.UpdatePriority(entry3, -6);
            priorityQueue.UpdatePriority(entry1, -10);

            Assert.AreEqual(-1, priorityQueue.PeekPriority);
            Assert.AreEqual("b", priorityQueue.Dequeue());

            Assert.AreEqual(-4, priorityQueue.PeekPriority);
            Assert.AreEqual("d", priorityQueue.Dequeue());

            Assert.AreEqual(-6, priorityQueue.PeekPriority);
            Assert.AreEqual("c", priorityQueue.Dequeue());

            Assert.AreEqual(-10, priorityQueue.PeekPriority);
            Assert.AreEqual("a", priorityQueue.Dequeue());

            Assert.AreEqual(0, priorityQueue.Count);
        }
예제 #2
0
        public void Remove(IPriorityQueueEntry <TItem> entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }
            BinaryHeapNode temp = entry as BinaryHeapNode;

            if (temp == null)
            {
                throw new InvalidCastException("Invalid heap entry format!");
            }
            if (temp.HeapIdentifier != identifier)
            {
                throw new ArgumentException("Heap does not contain this node!");
            }
            if (temp.Index == Count)
            {
                temp.HeapIdentifier = Guid.Empty;
                heap[Count--]       = null;
                return;
            }
            MoveNode(heap[Count], temp.Index);
            heap[Count--] = null;
            HeapifyUp(heap[HeapifyDown(heap[temp.Index])]);
            temp.HeapIdentifier = Guid.Empty;
        }
예제 #3
0
        public void Complex_minimum_priority_queue_test_with_multiple_operation()
        {
            IPriorityQueue <string, int> priorityQueue = CreateMinimumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry1        = priorityQueue.Enqueue("a", 1);
            IPriorityQueueEntry <string> entry2        = priorityQueue.Enqueue("b", 2);
            IPriorityQueueEntry <string> entry3        = priorityQueue.Enqueue("c", 3);
            IPriorityQueueEntry <string> entry4        = priorityQueue.Enqueue("d", 4);

            priorityQueue.UpdatePriority(entry1, 5);
            priorityQueue.UpdatePriority(entry2, 5);
            priorityQueue.UpdatePriority(entry1, 7);
            priorityQueue.UpdatePriority(entry2, 1);
            priorityQueue.UpdatePriority(entry4, 4);
            priorityQueue.UpdatePriority(entry3, 6);
            priorityQueue.UpdatePriority(entry1, 10);

            Assert.AreEqual(1, priorityQueue.PeekPriority);
            Assert.AreEqual("b", priorityQueue.Dequeue());

            Assert.AreEqual(4, priorityQueue.PeekPriority);
            Assert.AreEqual("d", priorityQueue.Dequeue());

            Assert.AreEqual(6, priorityQueue.PeekPriority);
            Assert.AreEqual("c", priorityQueue.Dequeue());

            Assert.AreEqual(10, priorityQueue.PeekPriority);
            Assert.AreEqual("a", priorityQueue.Dequeue());

            Assert.AreEqual(0, priorityQueue.Count);
        }
예제 #4
0
        public void Update_throws_exception_if_priority_is_null()
        {
            IPriorityQueue <string, string> priorityQueue = CreateMinimumPriorityQueue <string>();
            IPriorityQueueEntry <string>    entry         = priorityQueue.Enqueue("Item", "1");

            priorityQueue.UpdatePriority(entry, null);
        }
예제 #5
0
        public void Update_throws_exception_if_entry_is_null()
        {
            IPriorityQueue <string, int> priorityQueue = CreateMinimumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry         = priorityQueue.Enqueue("Item", 1);

            priorityQueue.UpdatePriority(null, 0);
        }
예제 #6
0
        public void UpdatePriority(IPriorityQueueEntry <TItem> entry, TPriority priority)
        {
            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }
            if (priority == null)
            {
                throw new ArgumentNullException("priority");
            }
            FibonacciNode node = entry as FibonacciNode;

            if (node == null)
            {
                throw new InvalidCastException("Invalid heap entry format!");
            }
            if (node.HeapIdentifier != identifier)
            {
                throw new ArgumentException("Heap does not contain this node!");
            }
            if (node.Parent != null && Compare(node.Parent.Priority, priority) > 0)
            {
                CutNode(node);
            }
            node.Priority = priority;

            if (Compare(head.Priority, priority) > 0)
            {
                head = node;
            }
        }
예제 #7
0
        public void Remove_single_item_from_priority_queue()
        {
            IPriorityQueue <string, int> priorityQueue = CreateMinimumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry         = priorityQueue.Enqueue("Test", 0);

            priorityQueue.Remove(entry);
            Assert.AreEqual(0, priorityQueue.Count);
        }
예제 #8
0
        public void Update_but_new_priority_equals_old_priority()
        {
            IPriorityQueue <string, int> priorityQueue = CreateMinimumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry         = priorityQueue.Enqueue("Test", 1);

            priorityQueue.UpdatePriority(entry, 1);
            Assert.AreEqual(1, priorityQueue.PeekPriority);
        }
예제 #9
0
        public void Peek_priority_throws_exception_after_clear()
        {
            IPriorityQueue <string, int> priorityQueue = CreateMinimumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry         = priorityQueue.Enqueue("Test", 1);

            priorityQueue.Clear();
            int priority = priorityQueue.PeekPriority;
        }
예제 #10
0
        public void Update_throws_exception_after_clear()
        {
            IPriorityQueue <string, int> priorityQueue = CreateMinimumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry         = priorityQueue.Enqueue("Test", 1);

            priorityQueue.Clear();
            priorityQueue.UpdatePriority(entry, 2);
        }
예제 #11
0
        public void Clear_priority_queue_count_is_zero()
        {
            IPriorityQueue <string, int> priorityQueue = CreateMinimumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry         = priorityQueue.Enqueue("Test", 1);

            priorityQueue.Clear();
            Assert.AreEqual(0, priorityQueue.Count);
        }
예제 #12
0
        public void Remove_item_from_priority_queue_if_already_removed()
        {
            IPriorityQueue <string, int> priorityQueue = CreateMinimumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry         = priorityQueue.Enqueue("Test", 0);

            priorityQueue.Remove(entry);
            priorityQueue.Remove(entry);
        }
예제 #13
0
        public void Decrease_priority_of_single_item()
        {
            IPriorityQueue <string, int> priorityQueue = CreateMinimumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry         = priorityQueue.Enqueue("Test", 1);

            priorityQueue.UpdatePriority(entry, 2);
            Assert.AreEqual(2, priorityQueue.PeekPriority);
        }
예제 #14
0
        public void Remove_throws_exception_if_parameter_comes_from_another_queue()
        {
            IPriorityQueue <string, int> priorityQueue1 = CreateMinimumPriorityQueue <int>();
            IPriorityQueue <string, int> priorityQueue2 = CreateMinimumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry1         = priorityQueue1.Enqueue("Test", 1);
            IPriorityQueueEntry <string> entry2         = priorityQueue2.Enqueue("Test", 1);

            priorityQueue1.Remove(entry2);
        }
예제 #15
0
        public void Update_priority_if_head_item_changed()
        {
            IPriorityQueue <string, int> priorityQueue = CreateMinimumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry1        = priorityQueue.Enqueue("Test1", 1);
            IPriorityQueueEntry <string> entry2        = priorityQueue.Enqueue("Test2", 2);

            priorityQueue.UpdatePriority(entry2, 0);
            Assert.AreEqual("Test2", priorityQueue.Peek);
            Assert.AreEqual(0, priorityQueue.PeekPriority);
        }
예제 #16
0
        public void Enumerate_items_if_only_enqueue_called()
        {
            IPriorityQueue <string, int> priorityQueue = CreateMinimumPriorityQueue <int>();
            IPriorityQueueEntry <string> entry1        = priorityQueue.Enqueue("Test1", 1);
            IPriorityQueueEntry <string> entry2        = priorityQueue.Enqueue("Test2", 2);
            IList <string> items = priorityQueue.Select(t => t).ToList();

            Assert.AreEqual(2, items.Count);
            Assert.IsTrue(items.Contains(entry1.Item));
            Assert.IsTrue(items.Contains(entry2.Item));
        }
예제 #17
0
        public void Remove(IPriorityQueueEntry <TItem> entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }

            FibonacciNode temp = entry as FibonacciNode;

            if (temp == null)
            {
                throw new InvalidCastException("Invalid heap entry format!");
            }

            if (temp.HeapIdentifier != identifier)
            {
                throw new ArgumentException("Heap does not contain this node!");
            }
            CutNode(temp);
            head = temp;
            Dequeue();
        }
예제 #18
0
        public void UpdatePriority(IPriorityQueueEntry <TItem> entry, TPriority priority)
        {
            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }
            if (priority == null)
            {
                throw new ArgumentNullException("priority");
            }
            BinaryHeapNode node = entry as BinaryHeapNode;

            if (node == null)
            {
                throw new InvalidCastException("Invalid heap entry format!");
            }
            if (node.HeapIdentifier != identifier)
            {
                throw new ArgumentException("Heap does not contain this node!");
            }
            node.Priority = priority;
            HeapifyUp(node);
        }