///<summary>A modified copy with some more rebuilding work done.</summary> private Queue <T> IterRebuild() { switch (_incomingTraverser.Count) { case 0: return(With(ready: _partialOutgoing, reStack: DropStack <T> .Empty, reFeed: _incoming.UnderlyingStack)); case 1: return(With(ready: _partialOutgoing.Push(_incomingTraverser.Peek), reStack: DropStack <T> .Empty, reFeed: _incoming.UnderlyingStack)); default: return(With(reStack: _partialOutgoing.Push(_incomingTraverser.Peek), reFeed: _incomingTraverser.Pop())); } }
///<summary>A modified copy with an item added to the back of the queue.</summary> public Queue <T> Enqueue(T value) { if (Count == 0) { return(With(items: _incoming.Push(value), ready: _outgoing.Push(value), reFeed: _incomingTraverser.Push(value))); } return(With(items: _incoming.Push(value)).IterRebuild()); }
///<summary>A modified copy with more incremental rebuilding performed (in order to eventually discard garbage).</summary> private DropCollectStack <T> IterRebuild() { return // iteratively transfer from _itemsTraverser to _partialReversedItems (!_itemsTraverser.IsEmpty ? With( itemsTraverser: _itemsTraverser.Pop(), partialReversedItems: _partialReversedItems.Push(_itemsTraverser.Peek)) // then iteratively transfer from _partialReversedItems to _partialRebuiltItems : !_partialReversedItems.IsEmpty ? With( partialReversedItems: _partialReversedItems.Pop(), partialRebuiltItems: _partialRebuiltItems.Push(_partialReversedItems.Peek())) // then, if there are new items, place them in _itemsTraverser for iterative transfer : _items.Count > _partialRebuiltItems.Count ? With( itemsTraverser: _items.Pop().KeepOnly(_items.Count - _partialRebuiltItems.Count - 1), partialReversedItems: _partialReversedItems.Push(_items.Peek)) // if there were no new items, then we've finished rebuilding and can use the result : new DropCollectStack <T>(_partialRebuiltItems, _partialRebuiltItems)); }
///<summary>A modified copy with an item added to the front of the queue.</summary> public DropCollectStack <T> Push(T value) { return(With(_items.Push(value))); }