/// <summary> /// Retrieves the item at the head of the queue, and returns a queue with the head element removed. /// </summary> /// <typeparam name="T">The type of elements stored in the queue.</typeparam> /// <param name="queue">The queue to dequeue from.</param> /// <param name="value">Receives the value from the head of the queue.</param> /// <returns>The new queue with the head element removed.</returns> /// <exception cref="InvalidOperationException">Thrown when the stack is empty.</exception> public static IImmutableQueue <T> Dequeue <T>(this IImmutableQueue <T> queue, out T value) { Requires.NotNull(queue, nameof(queue)); value = queue.Peek(); return(queue.Dequeue()); }
private async Task ProcessQueueInternal() { // Always yield if the task was queued. await Task.Yield(); queueMonitor.Enter(); try { while (queueCount > 0) { var current = actionsQueue.Peek(); actionsQueue = actionsQueue.Dequeue(); queueCount--; queueMonitor.Exit(); try { if (current != null) { await current(); } } finally { queueMonitor.Enter(); } } } finally { // Exit monitor regardless of this method entering the monitor. machine.Monitor.Exit(); queueMonitor.Exit(); } }
public void DeserializeQueueInterface() { string json = @"[ ""One"", ""II"", ""3"" ]"; IImmutableQueue <string> data = JsonSerializer.Deserialize <IImmutableQueue <string> >(json); Assert.False(data.IsEmpty); Assert.Equal("One", data.Peek()); data = data.Dequeue(); Assert.Equal("II", data.Peek()); data = data.Dequeue(); Assert.Equal("3", data.Peek()); }
public void EnqueueDequeueTest() { this.EnqueueDequeueTestHelper(new GenericParameterHelper(1), new GenericParameterHelper(2), new GenericParameterHelper(3)); this.EnqueueDequeueTestHelper <GenericParameterHelper>(); // interface test IImmutableQueue <GenericParameterHelper> queueInterface = ImmutableQueue.Create <GenericParameterHelper>(); IImmutableQueue <GenericParameterHelper> populatedQueueInterface = queueInterface.Enqueue(new GenericParameterHelper(5)); Assert.Equal(new GenericParameterHelper(5), populatedQueueInterface.Peek()); }
public static Option <T> peek <T>(IImmutableQueue <T> queue) { try { return(Some(queue.Peek())); } catch (InvalidOperationException) { return(None); } }
public static IImmutableQueue <T> Dequeue <T>( this IImmutableQueue <T> queue, out T value) { if (queue == null) { throw new ArgumentNullException(nameof(queue)); } value = queue.Peek(); return(queue.Dequeue()); }
private void RunFromQueueIfNotEmpty() { isRunning = true; isInQueue = true; while (queueCount > 0) { Action current = null; lock (actionsQueue) { current = actionsQueue.Peek(); actionsQueue = actionsQueue.Dequeue(); queueCount--; } current(); } isInQueue = false; isRunning = false; }
public static T peekUnsafe <T>(IImmutableQueue <T> queue) => queue.Peek();