/// <summary> /// Removes the last element of the queue. If it was not active (ie. not the head element), /// it will not be deactivated, just dequeued. Returns null if the queue was empty. /// </summary> /// <returns></returns> public T PopTail() { if (IsEmpty) { return(null); } T tail = _queue.PeekLast(); if (_queue.Count == 1) { tail.OnDeactivated(false); } tail.OnDequeued(true); _queue.RemoveLast(); return(tail); }
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]); } }