Immutability in C# Part 10: A double-ended queue

Immutability in C# Part 10: A double-ended queue

Rate This
  • Comments 28

Based on the comments, the implementation of a single-ended queue as two stacks was somewhat mind-blowing for a number of readers. People, you ain't seen nothing yet.

Before we get into the actual bits and bytes of the solution, think for a bit about how you might implement an immutable queue which could act like both a stack or a queue at any time. You can think of a stack as "it goes on the left end, it comes off the left end", and a queue as "it goes on the left end, it comes off the right end". Now we want "it goes on and comes off either end". For short, we'll call a double ended queue a "deque" (pronounced "deck"), and give our immutable deque this interface:

    public interface IDeque<T>
    {
        T PeekLeft();
        T PeekRight();
        IDeque<T> EnqueueLeft(T value);
        IDeque<T> EnqueueRight(T value);
        IDeque<T> DequeueLeft();
        IDeque<T> DequeueRight();
        bool IsEmpty { get; }
    }

Attempt #1

We built a single-ended queue out of two stacks. Can we pull a similar trick here?  How about we have the "left stack" and the "right stack". Enqueuing on the left pushes on the left stack, enqueuing on the right pushes on the right stack, and so on.

Unfortunately, this has some problems. What if you are dequeuing on the right and you run out of items on the right-hand stack?  Well, no problem, we'll pull the same trick as before -- reverse the left stack and swap it with the right stack.

The trouble with that is, suppose the left stack is { 1000, 999, ..., 3, 2, 1 } and the right stack is empty. Someone dequeues the deque on the right. We reverse the stack, swap them and pop the new right stack. Now we have an empty left-hand stack and { 2, 3, 4, .... 1000 } on the right hand stack. It took 1000 steps to do this. Now someone tries to dequeue on the left. We reverse the right queue, swap, and pop, and now we have { 999, 998, ... 3, 2 }.  That took 999 steps. If we keep on dequeuing alternating on the right and left we end up doing on average five hundred pushes per step. That's terrible performance. Clearly this is an O(n2) algorithm.

Attempt #2

Our attempt to model this as a pair of stacks seems to be failing. Let's take a step back and see if we can come up with a recursively defined data structure which makes it more apparent that there is cheap access to each end.

The standard recursive definition of a stack is "a stack is either empty, or an item (the head) followed by a stack (the tail)". It seems like we ought to be able to say "a deque is either empty, or an item (the left) followed by a deque (the middle) followed by an item (the right)".

Perhaps you have already seen the problem with this definition; a deque by this definition always has an even number of elements! But we can fix that easily enough.  A deque is:

1) empty, or
2) a single item, or
3) a left item followed by a middle deque followed by a right item.

Awesome. Let's implement it.

    // WARNING: THIS IMPLEMENTATION IS AWFUL. DO NOT USE THIS CODE.
    public sealed class Deque<T> : IDeque<T>
    {
        private sealed class EmptyDeque : IDeque<T>
        {
            public bool IsEmpty { get { return true; } }
            public IDeque<T> EnqueueLeft(T value) { return new SingleDeque(value); }
            public IDeque<T> EnqueueRight(T value) { return new SingleDeque(value); }
            public IDeque<T> DequeueLeft() { throw new Exception("empty deque"); }
            public IDeque<T> DequeueRight() { throw new Exception("empty deque"); }
            public T PeekLeft () { throw new Exception("empty deque"); }
            public T PeekRight () { throw new Exception("empty deque"); }
        }
        private sealed class SingleDeque : IDeque<T>
        {
            public SingleDeque(T t) { item = t; }
            private readonly T item;
            public bool IsEmpty { get { return false; } }
            public IDeque<T> EnqueueLeft(T value) { return new Deque<T>(value, Empty, item); }
            public IDeque<T> EnqueueRight(T value) { return new Deque<T>(item, Empty, value); }
            public IDeque<T> DequeueLeft() { return Empty; }
            public IDeque<T> DequeueRight() { return Empty; }
            public T PeekLeft () { return item; }
            public T PeekRight () { return item; }
        }
        private static readonly IDeque<T> empty = new EmptyDeque();
        public static IDeque<T> Empty { get { return empty; } }
        public bool IsEmpty { get { return false; } }
        private Deque(T left, IDeque<T> middle, T right)
        {
            this.left = left;
            this.middle = middle;
            this.right = right;
        }
        private readonly T left;
        private readonly IDeque<T> middle;
        private readonly T right;
        public IDeque<T> EnqueueLeft(T value)
        {
            return new Deque<T>(value, middle.EnqueueLeft(left), right);
        }
        public IDeque<T> EnqueueRight(T value)
        {
            return new Deque<T>(left, middle.EnqueueRight(right), value);
        }
        public IDeque<T> DequeueLeft()
        {
            if (middle.IsEmpty) return new SingleDeque(right);
            return new Deque<T>(middle.PeekLeft(), middle.DequeueLeft(), right);
        }
        public IDeque<T> DequeueRight()
        {
            if (middle.IsEmpty) return new SingleDeque(left);
            return new Deque<T>(left, middle.DequeueRight(), middle.PeekRight());
        }
        public T PeekLeft () { return left; }
        public T PeekRight () { return right; }
    }

I seem to have somewhat anticipated my denouement, but this is coding, not mystery novel writing. What is so awful about this implementation? It seems like a perfectly straightforward implementation of the abstract data type. But it turns out to be actually worse than the two-stack implementation we first considered. What are your thoughts on the matter?

Next time, what's wrong with this code and some groundwork for fixing it.

 

  • Give that man a cigar!

    The part of the trick that you've not explicitly stated is that the inner deque is a deque of _stacks of elements_, not of _elements_.  

    I'll be presenting an implementation of this technique in the next few days.  

  • WOW! Truly mind blowing stuff.

    Am I wrong or will the final version be:

    IDeque<T> left;

    IDeque<IDeque<T>> middle;

    IDeque<T> right;

  • Very close. We're actually going to make a "lite" deque that can store between one and four elements, and then we'll have

    Dequelette<T> left

    IDeque<Dequelette<T>> middle

    Dequelette<T> right

    (Since the Dequelette does not meet the IDeque contract, I don't want to say that it does.)

    That gives us fast access to the stuff at the end points, and logarithmic access to the stuff in the middle -- unlike, say, a binary tree, which gives us fast access to the stuff "in the local middle" and logarithmic access to the stuff at the end points. It's like pulling a binary tree inside out, a bit.

    But patience! We'll get to it next week.

  • Welcome to the fortieth issue of Community Convergence. This week we have two new releases of note: We

  • I've convinced myself that this works in C#, but I'm curious as to how the compiler remains happy and sane. The number of concrete types involved seems to be O(log N) in the number of elements, which, while very slow-growing, does not have an upper bound. Are the concrete types really only created as needed at run-time? Would you be unable to implement this same structure using C++ templates? Or have I fundamentally misunderstood something?

    I'm eager to see the next installment!

  • Doesn't the compiler simply use the same implementation for Foo<T> as it does for Foo<Foo<T>>, if T is a reference type? After all, there is no need for the _implementation_ to be strongly typed if only typesafe usage of it is allowed to compile.

  • Shouldn't the interface be:

    public interface IDeque<T>

    {

       T PeekLeft();

       T PeekRight();

       IDeque<T> EnqueueLeft(T value);

       IDeque<T> EnqueueRight(T value);

       IDeque<T> DequeueLeft(out T value);

       IDeque<T> DequeueRight(out T value);

       bool IsEmpty { get; }

    }

    ie, add argument "out T value" to Dequeue operations. Same goes for previously posted data structures ...

  • No. That would be conflating two logically separate operations into one method. One method should do one thing, and do it well.  Examining the data structure and producing a new data structure are two entirely different operations, so they should be represented by two methods.

  • I understand your point, thought it seems to conflict with current .NET mutable collections API (not saying one is wrong vs others, just different way of seeing things I guess).

  • Mutable collections must conflate unrelated operations because mutable collections are impossible to reason about.

    Suppose for example you want to pop a stack AND know what value you popped. In an immutable stack you can ask "are you empty?" and then peek and then pop.  In a mutable stack asking "are you empty?" tells you nothing about whether peeking is safe. Someone could have popped everything off the stack on a different thread.

    In a mutable stack, if you peek and then pop you have absolutely no idea if the value you peeked is the value that was just popped off. Someone might have done a push after your peek.

    Mutable structures are "threadsafe" only insofar as they guarantee to behave as though their operations are atomic, not that combinations of those operations are atomic. Therefore, any operations which must be logically atomic in a mutable structure must be conflated together.  That's why mutable collections have methods that do everything all at once, rather than cleanly separating different operations into different methods.

    Immutable structures give you a much stronger thread safety -- they give you the ability to reason logically from past calls to future calls. In an immutable stack, the value you peek is always the value you pop.

  • I've been loving this immutability series! Any hope for a System.Collections.Immutable someday? :-)

  • Thanks for your thoughtful answer. Following on what you say, then there is no advantage of putting 2 logical operations in one method in a mutable stack who is not threadsafe. A thread could push a value before Pop() get a chance to peek. In that case, an external lock is necessary and then, why not have 2 methods to make this requirement explicit ? Am I right in thinking that way ?

  • That is a reasonable way of thinking about it, yes.

Page 2 of 2 (28 items) 12