Subsections


2.4 ArrayDeque: Fast Deque Operations Using an Array

The ArrayQueue from the previous section is a data structure for representing a sequence that allows us to efficiently add to one end of the sequence and remove from the other end. The ArrayDeque data structure allows for efficient addition and removal at both ends. This structure implements the List interface by using the same circular array technique used to represent an ArrayQueue.


\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{initialize}(...
...th{\ensuremath{\mathit{n}} \gets \ensuremath{0}}\\
\end{flushleft}\end{leftbar}

The $ \ensuremath{\mathrm{get}(\ensuremath{\mathit{i}})}$ and $ \ensuremath{\mathrm{set}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ operations on an ArrayDeque are straightforward. They get or set the array element $ \ensuremath{\ensuremath{a}[}{\ensuremath{\ensuremath{(\ensuremath{\mathit{j}}+...
...nsuremath{\ensuremath{\mathrm{length}(\ensuremath{\mathit{a}})}}}\ensuremath{]}$.


\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{get}(\ensure...
...bf{return}} \ensuremath{\ensuremath{\mathit{y}}}\\
\end{flushleft}\end{leftbar}

The implementation of $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ is a little more interesting. As usual, we first check if $ \ensuremath{\ensuremath{\mathit{a}}}$ is full and, if necessary, call $ \ensuremath{\mathrm{resize}()}$ to resize $ \ensuremath{\ensuremath{\mathit{a}}}$. Remember that we want this operation to be fast when $ \ensuremath{\ensuremath{\mathit{i}}}$ is small (close to 0) or when $ \ensuremath{\ensuremath{\mathit{i}}}$ is large (close to $ \ensuremath{\ensuremath{\mathit{n}}}$). Therefore, we check if $ \ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}<\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}/2$. If so, we shift the elements $ \ensuremath{\ensuremath{\ensuremath{\mathit{a}}[0]}},\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\mathit{i}}-1]}}$ left by one position. Otherwise ( $ \ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}\ge\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}/2$), we shift the elements $ \ensuremath{\ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\mathit{i}}]}},\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\mathit{n}}-1]}}$ right by one position. See Figure 2.3 for an illustration of $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operations on an ArrayDeque.

Figure 2.3: A sequence of $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}})}$ operations on an ArrayDeque. Arrows denote elements being copied.
\includegraphics[scale=0.90909]{figs-python/arraydeque}


\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{add}(\ensure...
... \gets \ensuremath{\ensuremath{\mathit{n}} + 1}}\\
\end{flushleft}\end{leftbar}

By doing the shifting in this way, we guarantee that $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ never has to shift more than $ \min\{ \ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}, \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}} \}$ elements. Thus, the running time of the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ operation (ignoring the cost of a $ \ensuremath{\mathrm{resize}()}$ operation) is $ O(1+\min\{\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}},\ensuremath{\ensur...
...{\ensuremath{\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}\})$.

The implementation of the $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}})}$ operation is similar. It either shifts elements $ \ensuremath{\ensuremath{\ensuremath{\mathit{a}}[0]}},\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\mathit{i}}-1]}}$ right by one position or shifts the elements $ \ensuremath{\ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\mathit{i}}+1]}},\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\mathit{n}}-1]}}$ left by one position depending on whether $ \ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}<\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}/2$. Again, this means that $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}})}$ never spends more than $ O(1+\min\{\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}},\ensuremath{\ensur...
...{\ensuremath{\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}\})$ time to shift elements.


\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{remove}(\ens...
...bf{return}} \ensuremath{\ensuremath{\mathit{x}}}\\
\end{flushleft}\end{leftbar}

2.4.1 Summary

The following theorem summarizes the performance of the ArrayDeque data structure:

Theorem 2..3   An ArrayDeque implements the List interface. Ignoring the cost of calls to $ \ensuremath{\mathrm{resize}()}$, an ArrayDeque supports the operations Furthermore, beginning with an empty ArrayDeque, performing any sequence of $ m$ $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}})}$ operations results in a total of $ O(m)$ time spent during all calls to $ \ensuremath{\mathrm{resize}()}$.

opendatastructures.org