Crates.io | altdeque |
lib.rs | altdeque |
version | 1.0.0 |
source | src |
created_at | 2022-12-04 14:45:54.070843 |
updated_at | 2022-12-04 14:45:54.070843 |
description | An alternative deque implementation. |
homepage | |
repository | https://github.com/alexthill/altdeque |
max_upload_size | |
id | 729620 |
size | 131,039 |
An Alernative Deque implemention written in Rust.
AltDeque is an alternative to the standard library's VecDeque
. It exposes
mostly the same methods and has the performance characteristics. But
instead of using a ring buffer to achieve efficient insertion on both
ends, it uses two stacks. One stack to push
and pop
elements for each
end of the deuque. If pop
is called on one end but it's stack is empty, then all
elements from the other stack are removed, reversed and put into it. This
operation takes O(n) time (where n is the length of the deque) but
after it n elements can be popped in constant time resulting in an
amortized runtime of O(1) for popping.
For more efficient memory usage both stacks are located at the ends of one allocated buffer:
growth -> <- growth
+- back stack --+ +- front stack -+
| | | |
v v v v
+---+---+---+---+---+---+---+---+---+---+---+---+
| 4 | 5 | 6 | 7 | | | | | 0 | 1 | 2 | 3 |
+---+---+---+---+---+---+---+---+---+---+---+---+
| |
head -+ +- tail
This stack based approach has some advantages over a ringbuffer:
But it also has a few disadvantges:
In my simple tests AltDeque
and VecDeque
are about equally fast for a simply
push_back
and pop_front
workload.
Some of the code and a lot of the docs and examples are taken from the code in the rust repository, so credits to it's contributors.