pub struct ArrayList<T, const N: usize>{ /* private fields */ }
Expand description
A dynamic container that combines the characteristics of a Vec
and a LinkedList
,
implemented as an unrolled linked list with chunked storage.
The ArrayList
is backed by a XOR linked list, requiring only a single pointer
for bidirectional traversal between nodes. This design is both memory-efficient
and optimized for cache locality compared to traditional doubly linked lists.
§Type Parameters
T
: The type of elements stored in the list.N
: The maximum number of elements that each node (or chunk) can hold. This determines the granularity of the unrolled linked list. Larger values reduce the number of nodes but increase the size of each node.
§Features
- Chunked Storage: Each node can hold up to
N
elements, reducing the overhead of individual allocations compared to a traditional linked list. - Memory Efficiency: The XOR linked list design minimizes pointer storage requirements.
- Bidirectional Traversal: Supports efficient iteration in both forward and backward directions.
- Flexible Operations: Allows efficient insertions, deletions, and access at arbitrary positions.
§Example
use array_list::ArrayList;
let mut list: ArrayList<i64, 6> = ArrayList::new();
list.push_back(3);
list.push_front(1);
list.insert(1, 2);
assert!(!list.is_empty());
assert_eq!(list.len(), 3);
assert_eq!(list.pop_front(), Some(1));
assert_eq!(list.pop_front(), Some(2));
assert_eq!(list.pop_front(), Some(3));
§Applications
ArrayList
is suitable for use cases where:
- Frequent insertions or deletions occur in the middle of the list.
- Memory efficiency and improved cache performance over plain LinkedList are priorities.
- A hybrid structure that balances the strengths of
Vec
andLinkedList
is needed.
§Note
The structure name ArrayList
has no association with Java’s ArrayList
.
Implementations§
Source§impl<T, const N: usize> ArrayList<T, N>
impl<T, const N: usize> ArrayList<T, N>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new, empty ArrayList
.
This constructor initializes an ArrayList
with no elements and no allocated nodes.
It is a constant function, allowing the creation of ArrayList
instances in
compile-time contexts where applicable.
§Returns
A new, empty instance of ArrayList
.
§Example
use array_list::ArrayList;
let list: ArrayList<i64, 6> = ArrayList::new();
assert!(list.is_empty());
Sourcepub fn push_front(&mut self, value: T)
pub fn push_front(&mut self, value: T)
Adds an element to the front of the ArrayList
.
The element is inserted at the beginning of the list, shifting existing elements forward if necessary. If the first node is full, a new node will be allocated to accommodate the element.
§Parameters
value
: The element to add to the front of theArrayList
.
§Example
use array_list::ArrayList;
let mut list: ArrayList<i64, 6> = ArrayList::new();
list.push_front(10);
list.push_front(20);
assert_eq!(list.len(), 2);
assert_eq!(list.pop_front(), Some(20));
assert_eq!(list.pop_front(), Some(10));
Sourcepub fn push_back(&mut self, value: T)
pub fn push_back(&mut self, value: T)
Adds an element to the back of the ArrayList
.
The element is inserted at the end of the list. If the last node is full, a new node will be allocated to accommodate the element.
§Parameters
value
: The element to add to the back of theArrayList
.
§Example
use array_list::ArrayList;
let mut list: ArrayList<i64, 6> = ArrayList::new();
list.push_back(10);
list.push_back(20);
assert_eq!(list.len(), 2);
assert_eq!(list.pop_back(), Some(20));
assert_eq!(list.pop_back(), Some(10));
Sourcepub fn insert(&mut self, index: usize, value: T)
pub fn insert(&mut self, index: usize, value: T)
Inserts an element at the specified index, shifting subsequent elements to the right.
§Arguments
index
: The position at which to insert the new element.value
: The value to insert.
§Panics
- Panics if the
index
is out of bounds (greater than the list’s current length).
§Behavior
- Traverses the list to locate the appropriate node and index.
- If the target node is full, the method splits the node, redistributes elements, and inserts the value.
§Complexity
- O(n) for traversal to find the target node.
- O(N) within the node for shifting elements, where
N
is the node’s capacity.
§Examples
use array_list::ArrayList;
let mut list: ArrayList<i64, 3> = ArrayList::new();
list.push_back(10);
list.push_back(30);
list.insert(1, 20);
assert_eq!(list.get(0), Some(&10));
assert_eq!(list.get(1), Some(&20));
assert_eq!(list.get(2), Some(&30));
Sourcepub fn append(&mut self, other: &mut Self)
pub fn append(&mut self, other: &mut Self)
Moves all elements from other to the end of the list.
This reuses all the nodes from other and moves them into self. After this operation, other becomes empty.
§Complexity
- Time complexity: O(1)
- Memory complexity: O(1)
§Example
use array_list::ArrayList;
let mut list1: ArrayList<i32, 4> = ArrayList::new();
list1.push_back(1);
list1.push_back(2);
let mut list2: ArrayList<i32, 4> = ArrayList::new();
list2.push_back(3);
list2.push_back(4);
list1.append(&mut list2);
assert_eq!(list1.len(), 4);
assert_eq!(list1.get(0), Some(&1));
assert_eq!(list1.get(1), Some(&2));
assert_eq!(list1.get(2), Some(&3));
assert_eq!(list1.get(3), Some(&4));
Sourcepub fn pop_front(&mut self) -> Option<T>
pub fn pop_front(&mut self) -> Option<T>
Removes and returns the first element of the ArrayList
, if any.
This method removes the first element of the list and adjusts the head pointer
and internal structure accordingly. If the list is empty, it returns None
.
§Returns
Some(T)
if the list contains elements, whereT
is the removed element.None
if the list is empty.
§Complexity
- O(1) when the first element is removed and there are no structural changes (e.g., the head node has more elements).
- O(1) amortized, including the occasional node removal and re-linking operations.
§Examples
use array_list::ArrayList;
let mut list: ArrayList<i64, 4> = ArrayList::new();
list.push_front(10);
list.push_front(20);
assert_eq!(list.pop_front(), Some(20));
assert_eq!(list.pop_front(), Some(10));
assert_eq!(list.pop_front(), None);
Sourcepub fn pop_back(&mut self) -> Option<T>
pub fn pop_back(&mut self) -> Option<T>
Removes and returns the last element of the ArrayList
, if any.
This method removes the last element of the list and adjusts the tail pointer
and internal structure accordingly. If the list is empty, it returns None
.
§Returns
Some(T)
if the list contains elements, whereT
is the removed element.None
if the list is empty.
§Complexity
- O(1) when the last element is removed and there are no structural changes (e.g., the tail node has more elements).
- O(1) amortized, including the occasional node removal and re-linking operations.
§Examples
use array_list::ArrayList;
let mut list: ArrayList<i64, 4> = ArrayList::new();
list.push_back(10);
list.push_back(20);
assert_eq!(list.pop_back(), Some(20));
assert_eq!(list.pop_back(), Some(10));
assert_eq!(list.pop_back(), None);
Sourcepub fn remove(&mut self, index: usize) -> T
pub fn remove(&mut self, index: usize) -> T
Removes and returns the element at the specified index, shifting subsequent elements left.
§Arguments
index
: The position of the element to remove.
§Returns
- The element removed from the list.
§Panics
- Panics if the
index
is out of bounds.
§Behavior
- The method traverses the list to locate the node containing the element at the given index.
- The element is removed from the node, and subsequent elements within the node are shifted to the left.
- If the node becomes empty after removal, it is removed from the list, and the links between nodes are updated.
§Complexity
- O(n) to locate the target node.
- O(N) within the node for shifting elements, where
N
is the node’s capacity.
§Examples
use array_list::ArrayList;
let mut list: ArrayList<i64, 4> = ArrayList::new();
list.push_back(10);
list.push_back(20);
list.push_back(30);
list.push_back(40);
list.push_back(50);
assert_eq!(list.remove(1), 20);
assert_eq!(list.get(1), Some(&30));
assert_eq!(list.len(), 4);
// Attempting to remove out-of-bounds index will panic
// list.remove(10);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all elements from the ArrayList
, effectively making it empty.
This method deallocates all nodes in the list and resets the head, tail, and length to their initial states. The method does not shrink the capacity of the list’s nodes.
§Complexity
- Time complexity: O(n), where n is the total number of elements in the list.
§Example
use array_list::ArrayList;
let mut list: ArrayList<i32, 4> = ArrayList::new();
list.push_back(1);
list.push_back(2);
list.push_back(3);
assert_eq!(list.len(), 3);
list.clear();
assert_eq!(list.len(), 0);
assert!(list.is_empty());
assert_eq!(list.front(), None);
assert_eq!(list.back(), None);
Sourcepub fn front(&self) -> Option<&T>
pub fn front(&self) -> Option<&T>
Returns a reference to the first element of the ArrayList
, if any.
This method provides read-only access to the first element of the list without removing it.
§Returns
Some(&T)
if the list contains elements, where&T
is a reference to the first element.None
if the list is empty.
§Complexity
- O(1), as it directly accesses the first element in the list.
§Examples
use array_list::ArrayList;
let mut list: ArrayList<i64, 4> = ArrayList::new();
list.push_back(10);
list.push_back(20);
assert_eq!(list.front(), Some(&10));
list.pop_front();
assert_eq!(list.front(), Some(&20));
list.pop_front();
assert_eq!(list.front(), None);
Sourcepub fn front_mut(&mut self) -> Option<&mut T>
pub fn front_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the first element of the ArrayList
, if any.
This method provides mutable access to the first element of the list without removing it.
§Returns
Some(&mut T)
if the list contains elements, where&mut T
is a mutable reference to the first element.None
if the list is empty.
§Complexity
- O(1), as it directly accesses the first element in the list.
§Examples
use array_list::ArrayList;
let mut list: ArrayList<i64, 4> = ArrayList::new();
list.push_back(10);
list.push_back(20);
assert_eq!(list.front_mut(), Some(&mut 10));
list.pop_front();
assert_eq!(list.front_mut(), Some(&mut 20));
list.pop_front();
assert_eq!(list.front_mut(), None);
Sourcepub fn back(&self) -> Option<&T>
pub fn back(&self) -> Option<&T>
Returns a reference to the last element of the ArrayList
, if any.
This method provides read-only access to the last element of the list without removing it.
§Returns
Some(&T)
if the list contains elements, where&T
is a reference to the last element.None
if the list is empty.
§Complexity
- O(1), as it directly accesses the last element in the list.
§Examples
use array_list::ArrayList;
let mut list: ArrayList<i64, 4> = ArrayList::new();
list.push_back(10);
list.push_back(20);
assert_eq!(list.back(), Some(&20));
list.pop_back();
assert_eq!(list.back(), Some(&10));
list.pop_back();
assert_eq!(list.back(), None);
Sourcepub fn back_mut(&mut self) -> Option<&mut T>
pub fn back_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the last element of the ArrayList
, if any.
This method provides mutable access to the last element of the list without removing it.
§Returns
Some(&mut T)
if the list contains elements, where&mut T
is a mutable reference to the last element.None
if the list is empty.
§Complexity
- O(1), as it directly accesses the last element in the list.
§Examples
use array_list::ArrayList;
let mut list: ArrayList<i64, 4> = ArrayList::new();
list.push_back(10);
list.push_back(20);
assert_eq!(list.back_mut(), Some(&mut 20));
list.pop_back();
assert_eq!(list.back_mut(), Some(&mut 10));
list.pop_back();
assert_eq!(list.back_mut(), None);
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns a reference to the element at the specified index, if present.
§Arguments
index
: The position of the element in the list.
§Returns
Some(&T)
if the index is valid and the element exists.None
if the index is out of bounds.
§Complexity
- O(n), where
n
is the number of nodes in the list, as nodes are traversed sequentially.
§Examples
use array_list::ArrayList;
let mut list: ArrayList<i64, 4> = ArrayList::new();
list.push_back(10);
list.push_back(20);
assert_eq!(list.get(0), Some(&10));
assert_eq!(list.get(1), Some(&20));
assert_eq!(list.get(2), None); // Out of bounds
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the element at the specified index, if present.
§Arguments
index
: The position of the element in the list.
§Returns
Some(&mut T)
if the index is valid and the element exists.None
if the index is out of bounds.
§Complexity
- O(n), where
n
is the number of nodes in the list, as nodes are traversed sequentially.
§Examples
use array_list::ArrayList;
let mut list: ArrayList<i64, 4> = ArrayList::new();
list.push_back(10);
list.push_back(20);
assert_eq!(list.get_mut(0), Some(&mut 10));
assert_eq!(list.get_mut(1), Some(&mut 20));
assert_eq!(list.get_mut(2), None); // Out of bounds
Sourcepub fn iter(&self) -> Iter<'_, T, N> ⓘ
pub fn iter(&self) -> Iter<'_, T, N> ⓘ
Provides an iterator over list’s elements.
§Examples
use array_list::ArrayList;
let mut list: ArrayList<_, 2> = ArrayList::new();
list.push_back(0);
list.push_back(1);
list.push_back(2);
let mut iter = list.iter();
assert_eq!(iter.next(), Some(&0));
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), None);
Sourcepub fn cursor_front(&self) -> Cursor<'_, T, N>
pub fn cursor_front(&self) -> Cursor<'_, T, N>
Provides a cursor at the front element.
The cursor is pointing to the “ghost” non-element if the list is empty.
Sourcepub fn cursor_back(&self) -> Cursor<'_, T, N>
pub fn cursor_back(&self) -> Cursor<'_, T, N>
Provides a cursor at the back element.
The cursor is pointing to the “ghost” non-element if the list is empty.
Trait Implementations§
Source§impl<'a, T, const N: usize> Extend<&'a T> for ArrayList<T, N>
impl<'a, T, const N: usize> Extend<&'a T> for ArrayList<T, N>
Source§fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T, const N: usize> Extend<T> for ArrayList<T, N>
impl<T, const N: usize> Extend<T> for ArrayList<T, N>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)