Yo, Ho! Welcome on deck, deque!

The newly released ds library (version 1.3) now includes an efficient deque implementation.

What is a deque?

A deque is a double-ended queue and pronounced “deck”. In contrast to a queue, which allows insertions at one end and removals at the opposite end, a deque allows insertions and removals at both ends. The new Deque interface defines four methods to modify the Deque along with two methods to access the element at the front (head of the list) and back (tail of the list):

interface Deque<T>
{
    function front():T;
    function back():T;
    
    function pushFront(x:T):Void;
    function popFront():T;
    
    function pushBack(x:T):Void;
    function popBack():T;
}

There are two common ways to implement a deque – by using a doubly-linked list or by using arrays. While the first option is very simple and straightforward, the latter one is more of a challenge because there are many approaches which all have their pros and cons.

Linked implementation – de.polygonal.ds.LinkedDeque

This is basically just a stripped down, lightweight version of the doubly linked list class DLL. A doubly-linked list is capable of doing insertions & removals in constant time, but the operation itself is rather slow, since we need to instantiate a node object that stores the element and manage pointers to prevent the list from falling apart.
Another important thing to watch out is that the LinkedDeque class requires considerable more memory – for each and every element we need a container object for storing the “cargo” (+16 bytes), a reference to the previous and next node (+4 bytes each) and finally a reference that points to the element (+4 bytes). So in Flash we need a total of 28 extra bytes per element. This means that 86% of the space is wasted on nodes and pointers!
Despite those shortcomings it’s still useful for small to average sizes; and to minimize costly node object instantiation the LinkedDeque class (and all linked structures in ds) comes with built-in node pooling – all you have to do is to create a LinkedDeque object with a reserved size greater than zero:

var deque = new LinkedDeque<Int>(100); //reuses up to 100 nodes

This is very useful if the maximum size is known in advance, as performance nearly doubles. The benchmarks below were all done with “node-caching” turned on.

Arrayed implementation – de.polygonal.ds.ArrayedDeque

While most sites explain how a doubly-linked list relates to a Deque, there is not much information available on how to efficiently implement a deque on top of an array. My first attempt used a circular array similar to the ArrayedQueue class. It seemed to be a good solution until I realized that resizing the deque would be slow and difficult to implement so I discarded this idea and started from scratch. This time I decided to use an array of arrays (turned out the C++ STL deque uses this approach) and I was very happy with the result.
Let me briefly explain how it works. Data is organized in chunks or blocks of memory. A block is simply a fixed-sized array and all blocks are stored in a separate, dynamic array. Upon initialization the deque only contains a single block but once it fills up an additional block is allocated: adding elements to the back calls something like “blockListArray.push(newBlock)” and adding elements to the front calls “blockListArray.unshift(newBlock)”.
Inserting elements at the beginning of an array is slow but in this case it doesn’t matter because the list of blocks is usually very small and it doesn’t happen very often. Therefore we can say that the ArrayedDeque performs modifications at both ends in amortized constant time.

Performance

So how do both classes perform? Showing raw numbers would be pointless without being able to compare them to some reference values. For this reason I’ve created a minimal deque implementation called “NaiveDeque”:

class NaiveDeque<T> implements Deque<T>
{
    var _data:Array<T>;
    
    public function new() {
        _data = new Array<T>();
    }
    
    public function front():T {
        return _data[0];
    }
    
    public function pushFront(x:T):Void {
        _data.unshift(x);
    }
    
    public function popFront():T {
        return _data.shift();
    }
    
    public function back():T {
        return _data[_data.length - 1];
    }
    
    public function pushBack(x:T):Void {
        _data.push(x);
    }
    
    public function popBack():T {
        return _data.pop();
    }
}

As you see it just uses what the Flash language has to offers. Clearly, the problem which the code above is that modifications at the beginning of the array are slow because a lot of memory is shifted around, either to fill a gap or to make room for a new element. So we kinda have the worst-case at the front and the best-case at the back. It’s not very fair to use this as a reference but on the other hand it shows how things can be drastically improved by using some elbow grease to write clever data structures. Here are the results:

deque benchmark

size 1000 2000 3000 4000 5000
NaiveDeque 1.0 1.0 1.0 1.0 1.0
LinkedDeque 1.65 4.4 6.6 7.2 7.7
ArrayedDeque 5.0 9.3 11.7 14.8 19.3

The benchmark results were taken by equally distributing n elements at both ends, like this:

//insert...
for (i in 0...n)
{
    if ((i & 1) == 0)
        deque.pushBack(x);
    else
        deque.pushFront(x);
}
//remove...
for (i in 0...n)
{
    if ((i & 1) == 0)
        deque.popBack();
    else
        deque.popFront();
}

Usage

One real-world application that comes to my mind is a software application’s list of undo operations; but as a deque is a very flexible abstract data structure it can be used in countless algorithms. The deque classes are all documented so I hope it’s clear how to use them. If you have problems feel free to contact me.

3 Comments

  1. Inpetto

    Excellent work as always. I was digging through some of your older articles and came across your implementation of SAT http://lab.polygonal.de/files/motor_flash_physics/sepaxis_theorem.html

    I know you released motor2 open source, but is there a testbed or open source example of how to do this collision detection & separation without all the additional physics and structures?

    Cheers

  2. Hi, this is an old demo ;) It was written in AS2 back then and I haven’t found the source code for this example. I was planning to include SAT code to the motor.geom package (decoupled from physics) but I haven’t found enough time to do so.

  3. Inpetto

    Not to worry! I didn’t realize the manifold pairs were done without the physics and other overheads – it didn’t take too long to isolate what I needed. Thanks for a great engine.

Get Adobe Flash player