ArrayLinkedList
A hybrid data structure combining the benefits of arrays and linked lists, with O(1) indexed access, insertions, and deletions.
Features
- O(1) Operations: Insert/remove at ends, access by index, and replace elements
- Stable Indices: Indices remain valid after insertions/deletions
- Cache-Friendly: Contiguous memory layout for better performance
- Space Efficient: Reuses empty slots automatically
- Iterators: Bidirectional iteration with
rev()
, and partial iteration viaiter_after()
/iter_before()
Comparison
Operation | ArrayLinkedList |
Vec |
LinkedList |
Vec<Option<T>> |
---|---|---|---|---|
Push/Pop Front | O(1) | O(n)* | O(1) | O(1)† |
Push/Pop Back | O(1) | O(1) | O(1) | O(1) |
Remove by Index | O(1) | O(n)* | O(n) | O(1)† |
Cache Locality | ✅ | ✅ | ❌ | ✅ |
Stable Indices | ✅ | ❌ | ❌ | ❌ |
*Amortized | †Requires slot reuse logic
Example
use ArrayLinkedList;
let mut list = new;
// Add elements
let idx1 = list.push_front; // Index 0
let idx2 = list.push_back; // Index 1
let idx3 = list.push_front; // Index 2
// Access by index
assert_eq!;
assert_eq!;
assert_eq!;
// Iterate in logical order
let items: = list.iter.copied.collect;
assert_eq!;
// Replace elements while maintaining indices and moving the new element to the logical front
list.replace_front.unwrap;
assert_eq!;
// Iterate with indices
for in list.indexed
// Partial iteration
let after_30: = list.iter_before.copied.collect;
assert_eq!;