/// <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()); }
public void TestQueueLikeBehavior() { var queue = ImmutableTreeQueue.Create <int>(); var reference = new Queue <int>(); for (int i = 0; i < 2 * 4 * 4; i++) { int item = Generator.GetInt32(); queue = queue.Enqueue(item); reference.Enqueue(item); } while (!queue.IsEmpty) { var expected = reference.Peek(); Assert.Equal(expected, queue.Peek()); Assert.Equal(expected, reference.Dequeue()); IImmutableQueue <int> immutableQueue = queue; queue = queue.Dequeue(out int value); Assert.Equal(expected, value); queue.Validate(ValidationRules.None); Assert.Equal(reference, queue); // Test through the IImmutableQueue<T> interface (initialized above) immutableQueue = immutableQueue.Dequeue(out value); Assert.Equal(expected, value); Assert.Equal(reference, immutableQueue); } Assert.Empty(queue); Assert.Empty(reference); }
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 static Tuple <IImmutableQueue <T>, T> deqUnsafe <T>(IImmutableQueue <T> queue) { T value; var newqueue = queue.Dequeue(out value); return(tuple(newqueue, value)); }
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 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()); }
public static Tuple <IImmutableQueue <T>, Option <T> > deq <T>(IImmutableQueue <T> queue) { try { T value; var newqueue = queue.Dequeue(out value); return(tuple(newqueue, Some(value))); } catch (InvalidOperationException) { return(tuple(queue, Option <T> .None)); } }
public bool MoveNext() { while (!cancelToken.IsCancellationRequested) { if (!buffer.IsEmpty) { lock (dequeueLocker) { if (!buffer.IsEmpty) { buffer = buffer.Dequeue(out current); Console.WriteLine("Deuque"); return(true); } } } waitable.Task.Wait(); if (!cancelToken.IsCancellationRequested) { lock (dequeueLocker) { if (!buffer.IsEmpty) { buffer = buffer.Dequeue(out current); Console.WriteLine("Deuque"); return(true); } else { Console.WriteLine("Awaited empty queue"); } } } } Console.WriteLine("Stream finished"); return(false); }
public void DequeueOutValue() { var queue = ImmutableQueue <int> .Empty.Enqueue(5).Enqueue(6); int head; queue = queue.Dequeue(out head); Assert.Equal(5, head); var emptyQueue = queue.Dequeue(out head); Assert.Equal(6, head); Assert.True(emptyQueue.IsEmpty); // Also check that the interface extension method works. IImmutableQueue <int> interfaceQueue = queue; Assert.Same(emptyQueue, interfaceQueue.Dequeue(out head)); Assert.Equal(6, head); }
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; }