Beispiel #1
0
        public TestHeap()
        {
            List <HeapNode> heapArray = new List <HeapNode>();

            heapArray.Add(new HeapNode(2, 5, 100));
            heapArray.Add(new HeapNode(1, 8, 100));
            heapArray.Add(new HeapNode(3, 3, 100));
            heapArray.Add(new HeapNode(4, 6, 100));
            heapArray.Add(new HeapNode(5, 2, 100));
            heapArray.Add(new HeapNode(6, 3, 100));
            heapArray.Add(new HeapNode(7, 1, 100));
            heapArray.Add(new HeapNode(1, 8, 100));
            heapArray.Add(new HeapNode(2, 5, 100));
            heapArray.Add(new HeapNode(3, 3, 100));
            heapArray.Add(new HeapNode(4, 6, 100));
            heapArray.Add(new HeapNode(5, 2, 100));
            heapArray.Add(new HeapNode(6, 3, 100));
            heapArray.Add(new HeapNode(7, 1, 100));

            MinHeap EmptyConstruct  = new MinHeap();
            MinHeap SingleConstruct = new MinHeap(heapArray[0]);
            MinHeap ArrayConstruct  = new MinHeap(heapArray);


            for (int i = 0; i < heapArray.Count; i++)
            {
                if (i == 0)
                {
                    EmptyConstruct.InsertNode(heapArray[i]);
                }
                else
                {
                    EmptyConstruct.InsertNode(heapArray[i]);
                    SingleConstruct.InsertNode(heapArray[i]);
                }
            }

            Console.WriteLine("Empty");
            EmptyConstruct.PrintHeap();
            Console.WriteLine("Single");
            SingleConstruct.PrintHeap();
            Console.WriteLine("Array");
            ArrayConstruct.PrintHeap();

            Console.WriteLine("Array min checked");
            Console.WriteLine(ArrayConstruct.GetMin());
            Console.WriteLine("Array min removed");
            Console.WriteLine(ArrayConstruct.RemoveMin());
            Console.WriteLine("Array");
            ArrayConstruct.PrintHeap();


            Console.Write("\n\nConsumers completed. Press any key...");
            Console.ReadKey();
        }
Beispiel #2
0
 /*
  * Thread run function
  */
 public void Run()
 {
     Console.WriteLine("Starting producer..."); // print that producer has started
     // loop over each production cycle
     for (int iProd = 1; iProd <= m_numProductions; iProd++)
     {
         // diplay information about current priority queue
         Console.WriteLine("Current priority queue has " + m_heap.Size() + " nodes.");
         Console.WriteLine("Producer adding " + m_numProcesses + " nodes.");
         int idleTime = randomIdle.GetNext();
         // produce the process for the heap
         for (int iProc = 1; iProc <= m_numProcesses; iProc++)
         {
             int id       = iProd + iProc + (iProd - 1) * m_numProcesses - iProd; //create a unique process id
             int priority = randomPriority.GetNext();                             //get random priority
             int timems   = randomTimeSlice.GetNext();                            //get random time slice
             m_heap.InsertNode(new HeapNode(id, priority, timems));               //add process to heap
         }
         //sleep the producer
         Console.WriteLine("Producer going to sleep.");
         Thread.Sleep(idleTime);
     }
     m_productionFinished.Set(true); // flag that the producer has finished
     Console.WriteLine("Producer is exiting.");
 }