3 unstable releases
Uses new Rust 2024
| new 0.2.2 | Feb 3, 2026 |
|---|---|
| 0.2.1 | Mar 29, 2025 |
| 0.2.0 |
|
| 0.1.0 | Apr 18, 2021 |
#598 in Data structures
Used in collision-detection
39KB
542 lines
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 array_linked_list::ArrayLinkedList;
let mut list = ArrayLinkedList::new();
// Add elements
let idx1 = list.push_front(10); // Index 0
let idx2 = list.push_back(20); // Index 1
let idx3 = list.push_front(30); // Index 2
// Access by index
assert_eq!(list[idx1], Some(10));
assert_eq!(list[idx2], Some(20));
assert_eq!(list[idx3], Some(30));
// Iterate in logical order
let items: Vec<_> = list.iter().copied().collect();
assert_eq!(items, [30, 10, 20]);
// Replace elements while maintaining indices and moving the new element to the logical front
list.replace_front(idx2, 99).unwrap();
assert_eq!(list.iter().copied().collect::<Vec<_>>(), [99, 30, 10]);
// Iterate with indices
for (idx, val) in list.indexed() {
println!("Index {idx}: {val}");
}
// Partial iteration
let after_30: Vec<_> = list.iter_before(idx1).copied().collect();
assert_eq!(after_30, vec![99, 30]);