public void TestPriorityQueue() { IComparer <int> comparer_value = new ComparerNatural <int>(); Assert.AreEqual(1, comparer_value.Compare(5, -1)); IComparer <int> comparer_index = new ComparerArrayIndex <int>(new ComparerNatural <int>(), new int[] { 5, -1 }); Assert.AreEqual(1, comparer_index.Compare(0, 1)); int[] values = new int[] { 0, 1, 1, 2, -5, 6, 7, 7, 19, -20 }; PriorityQueueC5 <int> queue = new PriorityQueueC5 <int>(new ComparerArrayIndex <int>(new ComparerNatural <int>(), values)); queue.Enqueue(0); queue.Enqueue(4); queue.Enqueue(8); Assert.AreEqual(8, queue.DequeueLast()); Assert.AreEqual(0, queue.DequeueLast()); Assert.AreEqual(4, queue.DequeueLast()); queue.Enqueue(4); queue.Enqueue(4); queue.Enqueue(8); queue.Enqueue(4); queue.Enqueue(9); Assert.AreEqual(8, queue.DequeueLast()); Assert.AreEqual(4, queue.DequeueLast()); Assert.AreEqual(4, queue.DequeueLast()); Assert.AreEqual(4, queue.DequeueLast()); Assert.AreEqual(9, queue.DequeueLast()); }
private void Process(int element_index) { // Console.WriteLine("Process" + element_index); // If we have inserted all its neigbours without finding one that is higher the magic begins: // Note that we can still be sure that the front elemenent in the queue is the same // (The priorety queue should guantee this) // If we are not working on anything if (d_component_stack.Count == 0) { //Console.WriteLine("Increase process" + element_index); // Increase process level add leave it in the queue d_component_stack.Push(new Tuple <ElementType, IList <int>, IList <MaxTreeNode <ElementType> > >(d_element_array[element_index], new List <int>(), new List <MaxTreeNode <ElementType> >())); } else { ElementType process_level = d_component_stack.Peek().Item1; // If we are higher than the current processing level if (this.element_value_comparer.Compare(process_level, d_element_array[element_index]) == -1) { //Console.WriteLine("Increase process" + element_index); // Do NOT remove the element from the fringe // Increase process level add it the the new component d_component_stack.Push(new Tuple <ElementType, IList <int>, IList <MaxTreeNode <ElementType> > >(d_element_array[element_index], new List <int>(), new List <MaxTreeNode <ElementType> >())); } // If element level is below the process level if (this.element_value_comparer.Compare(process_level, d_element_array[element_index]) == 1) { //Console.WriteLine("DecreaseProcessLevel" + element_index); // Do NOT remove the element from the fringe // Decrease process level (this pops the node) DecreaseProcessLevel(element_index); } // If element level is the same as process level if (this.element_value_comparer.Compare(process_level, d_element_array[element_index]) == 0) { // Explore node, check if we can insert all its neigbours // Remove the element from the fringe if (!CanAddNeigborsToFringe(element_index)) { //Console.WriteLine("Neigbors break" + element_index); // If CANNOT; Do NOT remove the element from the fringe AddNeigborsToFringe(element_index); // Add this neigtbour // Retry later with new fringe return; } else { d_fringe.DequeueLast(); AddNeigborsToFringe(element_index); //Console.WriteLine("Add" + element_index); // Add the Element to the new component d_component_stack.Peek().Item2.Add(element_index); d_done++; if (d_reporter != null) { d_reporter.Report(d_done, d_elements_to_queued.Length); } } } } }