/// <summary> /// Reverses the order of a stack. /// </summary> /// <returns>The reversed stack.</returns> internal ImmutableStack <T> Reverse() { var r = this.Clear(); for (ImmutableStack <T> f = this; !f.IsEmpty; f = f.Pop()) { r = r.Push(f.Peek()); } Debug.Assert(r != null); Debug.Assert(r.IsEmpty == IsEmpty); return(r); }
internal ImmutableStack <T> Reverse() { Contract.Ensures(Contract.Result <ImmutableStack <T> >() != null); Contract.Ensures(Contract.Result <ImmutableStack <T> >().IsEmpty == this.IsEmpty); var r = this.Clear(); for (ImmutableStack <T> f = this; !f.IsEmpty; f = f.Pop()) { r = r.Push(f.Peek()); } return(r); }
public ImmutableQueue <T> Dequeue() { if (IsEmpty) { throw new InvalidOperationException("Queue is empty."); } var stack = frontStack.Pop(); if (!stack.IsEmpty) { return(new ImmutableQueue <T> (stack, backStack)); } return(new ImmutableQueue <T> (Reverse(backStack), ImmutableStack <T> .Empty)); }
/// <summary> /// Moves to the first or next element. /// </summary> /// <returns>A value indicating whether there are any more elements.</returns> public bool MoveNext() { if (_remainingStack == null) { // initial move _remainingStack = _originalStack; } else if (!_remainingStack.IsEmpty) { _remainingStack = _remainingStack.Pop(); } return(!_remainingStack.IsEmpty); }
/// <summary> /// Advances enumeration to the next element. /// </summary> /// <returns>A value indicating whether there is another element in the enumeration.</returns> public bool MoveNext() { if (_remainingForwardsStack == null) { // This is the initial step. // Empty queues have no forwards or backwards _remainingForwardsStack = _originalQueue._forwards; _remainingBackwardsStack = _originalQueue.BackwardsReversed; } else if (!_remainingForwardsStack.IsEmpty) { _remainingForwardsStack = _remainingForwardsStack.Pop(); } else if (!_remainingBackwardsStack.IsEmpty) { _remainingBackwardsStack = _remainingBackwardsStack.Pop(); } return(!_remainingForwardsStack.IsEmpty || !_remainingBackwardsStack.IsEmpty); }
/// <summary> /// Returns a queue that is missing the front element. /// </summary> /// <returns>A queue; never <c>null</c>.</returns> /// <exception cref="InvalidOperationException">Thrown when the queue is empty.</exception> public ImmutableQueue<T> Dequeue() { if (this.IsEmpty) { throw new InvalidOperationException(SR.InvalidEmptyOperation); } ImmutableStack<T> f = _forwards.Pop(); if (!f.IsEmpty) { return new ImmutableQueue<T>(f, _backwards); } else if (_backwards.IsEmpty) { return ImmutableQueue<T>.Empty; } else { return new ImmutableQueue<T>(this.BackwardsReversed, ImmutableStack<T>.Empty); } }