public void CopyTo() { // Create a new priority queue. ConcurrentPriorityQueue <int> queue = new ConcurrentPriorityQueue <int>(); // Create a new array of size 5. PriorityValuePair <int>[] arrayCopy = new PriorityValuePair <int> [5]; // Enqueue 3 elements into the queue. PriorityValuePair <int> elem = new PriorityValuePair <int>(3.0, 6); queue.Enqueue(1.0, 2); queue.Enqueue(elem); queue.Enqueue(2.0, 4); // Copy the queue data to the array, starting from index 1 (not 0). queue.CopyTo(arrayCopy, 1); // Expect the first array index to be unset, but all the rest to be set. // Note: The order of elements after the first can't be guaranteed, because the heap // implementing the queue internally doesn't store things in an exact linear order, // but we can be sure that the elements aren't going to be equal to null because we // set them. Assert.That(arrayCopy[0], Is.EqualTo(null)); Assert.That(arrayCopy[1], Is.EqualTo(elem)); Assert.That(arrayCopy[2], Is.Not.EqualTo(null)); Assert.That(arrayCopy[3], Is.Not.EqualTo(null)); Assert.That(arrayCopy[4], Is.EqualTo(null)); }
static void Main(string[] args) { ConcurrentPriorityQueue <string> queue = new ConcurrentPriorityQueue <string>(); queue.Enqueue(1000.0, "This "); queue.Enqueue(1000.0, "should "); queue.Enqueue(1000.0, "form "); queue.Enqueue(1000.0, "a "); queue.Enqueue(1000.0, "complete "); queue.Enqueue(1000.0, "and "); queue.Enqueue(1000.0, "understandable "); queue.Enqueue(1000.0, "sentence."); int numIterations = queue.Count; for (int x = 0; x < numIterations; x++) { Console.WriteLine("ITERATION " + (x + 1)); Console.WriteLine(""); // Print out the current state of the heap PriorityValuePair <string>[] heapArray = new PriorityValuePair <string> [queue.Count]; queue.CopyTo(heapArray, 0); for (int i = 0; i < heapArray.Length; i++) { Console.WriteLine(heapArray[i].Value + ", " + heapArray[i].Priority); } // Dequeue the next element PriorityValuePair <string> dequeued = queue.Dequeue(); Console.WriteLine(""); Console.WriteLine("DEQUEUED: " + dequeued.Value + ", " + dequeued.Priority); Console.WriteLine(""); } Console.ReadLine(); }