public void TestValuesAndWrapAround() { var d = new Deque <int>(4); d.AddLast(1); // internally it contains [H:1, 2, 3, 4] d.AddLast(2); d.AddLast(3); d.AddLast(4); Assert.IsTrue(d.PeekFirst() == 1 & d.PeekLast() == 4); d.RemoveFirst(); d.RemoveLast(); // now it's [0, H:2, 3, 0] Assert.IsTrue(d.Count == 2 && d.Capacity == 4); Assert.IsTrue(d.PeekFirst() == 2 & d.PeekLast() == 3); d.AddLast(4); d.RemoveFirst(); // now it's [0, 0, H:3, 4] Assert.IsTrue(d.Count == 2 && d.Capacity == 4); Assert.IsTrue(d.PeekFirst() == 3 & d.PeekLast() == 4); d.AddLast(5); d.RemoveFirst(); // now it's [5, 0, 0, H:4] Assert.IsTrue(d.Count == 2 && d.Capacity == 4); Assert.IsTrue(d.PeekFirst() == 4 & d.PeekLast() == 5); d.AddFirst(3); d.AddFirst(2); // now it's [5, H:2, 3, 4] Assert.IsTrue(d.Count == 4 && d.Capacity == 4); Assert.IsTrue(d.PeekFirst() == 2 & d.PeekLast() == 5); d.AddFirst(1); // reallocated to [H:1, 2, 3, 4, 5, 0, 0, 0] d.AddFirst(0); // now it's [1, 2, 3, 4, 5, 0, 0, H:0] Assert.IsTrue(d.Count == 6 && d.Capacity == 8); Assert.IsTrue(d.PeekFirst() == 0 & d.PeekLast() == 5); var arr = d.ToArray(); var exp = new int[] { 0, 1, 2, 3, 4, 5 }; Assert.IsTrue(arr.Length == exp.Length); for (int i = 0; i < arr.Length; i++) { Assert.IsTrue(arr[i] == exp[i]); } }
/// <summary> /// Removes and returns the head element of the queue, or null if the queue is empty. /// Also activates the next element in the queue, if one exists. /// </summary> /// <returns></returns> public T Dequeue() { if (IsEmpty) { return(null); } T head = _queue.PeekFirst(); head.OnDeactivated(false); head.OnDequeued(false); _queue.RemoveFirst(); if (!IsEmpty) { _queue.PeekFirst().OnActivated(); } return(head); }