static private int CalculateNewCapacity(DequeFixed <T> d) { int newCapacity = d.Capacity; if (d.Count > newCapacity / 2) //More than half full { newCapacity *= 2; } return(newCapacity); }
//This is only thread safe when called from the thread that owns the Deque. public void Push(T w) { if (!deque.Push(w)) { DequeFixed <T> old = deque; DequeFixed <T> other = new DequeFixed <T>(CalculateNewCapacity(deque)); deque = other; MoveElements(old, other); other.Push(w); } }
static private void MoveElements(DequeFixed <T> old, DequeFixed <T> cur) { while (true) { T move = old.Steal(); if (move == null) { break; } else { cur.Push(move); } } }
public Deque(int initialSize) { deque = new DequeFixed <T>(initialSize); }