pub struct ArrayList<T> { /* private fields */ }Expand description
A simple ArrayList implementation
§How?
ptr stores the first element of the ArrayList,
cap stores the capacity of the ArrayList grows dynamically if you try to push an element after the allocated memory is already full,
it expands by allocating a new chunk of memory double the size of the old one,it copies all the elements in the new ArrayList and deallocates the old one,
len stores the offset in which the top element is stored.
STACK
ptr cap len
+-------+-------+-------+
| 0x123 | 4 | 2 | ArrayList
+-------+-------+-------+
HEAP
+-------+-------+-------+-------+
| 1 | 2 | ? | ? | ... unallocated memory
+-------+-------+-------+-------+
^ ^ ^
ptr len cap
Implementations§
Source§impl<T> ArrayList<T>
impl<T> ArrayList<T>
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Gets the nth element in the ArrayList.
§Arguments
index- index to get inArrayList.
§Returns
Returns Some(&element) if the specified index is within bounds, else returns None.
§Examples
use tryingarraylist::ArrayList;
let mut list = ArrayList::new(10);
list.push(42);
assert!(Some(&42) == list.get(0));
//out of bounds
assert!(None == list.get(1));
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the lenght of the ArrayList.
§Examples
use tryingarraylist::ArrayList;
let mut list = ArrayList::new(10);
list.push(1);
list.push(2);
list.push(3);
assert!(list.len() == 3);
Sourcepub fn remove(&mut self, index: usize) -> T
pub fn remove(&mut self, index: usize) -> T
Removes the element at the specified index from the ArrayList, shifting all subsequent
elements to the left to fill the gap.
§Arguments
index- The index of the element to be removed.
§Returns
Returns element if the specified index is within bounds, else panics.
§Examples
use tryingarraylist::ArrayList;
let mut list = ArrayList::new(10);
list.push(1);
list.push(2);
list.push(3);
assert_eq!(list.remove(1), 2);
assert_eq!(list.remove(0), 1);
Sourcepub fn loc(&self) -> *mut T
pub fn loc(&self) -> *mut T
Returns the memory location (pointer) in which the first element of the ArrayList is stored
Sourcepub fn insert(&mut self, index: usize, element: T)
pub fn insert(&mut self, index: usize, element: T)
Inserts the element at the specified index from the ArrayList, shifting all subsequent
elements to the right to make a gap to insert new element.
§Arguments
index- The index of the element to be inserted in.element- The element to be inserted.
§Examples
use tryingarraylist::ArrayList;
let mut list = ArrayList::new(10);
list.push(1);
list.push(2);
list.insert(1,3);
assert_eq!(list.pop(), 2);
assert_eq!(list.pop(), 3);
assert_eq!(list.pop(), 1);
Sourcepub fn reverse(&mut self)
pub fn reverse(&mut self)
Reverses the order of the elements in the ArrayList
§Examples
use tryingarraylist::ArrayList;
let mut list = ArrayList::new(10);
list.push(1);
list.push(2);
list.push(3);
list.reverse();
assert_eq!(list.pop(), 1);
assert_eq!(list.pop(), 2);
assert_eq!(list.pop(), 3);
Sourcepub fn sort(&mut self)where
T: Ord,
pub fn sort(&mut self)where
T: Ord,
Sorts the elements in the ArrayList using the quicksort algorithm
§Examples
use tryingarraylist::ArrayList;
let mut list = ArrayList::new(10);
list.push(6);
list.push(5);
list.push(7);
list.push(4);
list.push(8);
list.push(3);
list.push(9);
list.push(2);
list.push(10);
list.push(1);
list.sort();
assert_eq!(list.pop(), 10);
assert_eq!(list.pop(), 9);
assert_eq!(list.pop(), 8);
assert_eq!(list.pop(), 7);
assert_eq!(list.pop(), 6);
assert_eq!(list.pop(), 5);
assert_eq!(list.pop(), 4);
assert_eq!(list.pop(), 3);
assert_eq!(list.pop(), 2);
assert_eq!(list.pop(), 1);
Trait Implementations§
Source§impl<'de, T: Deserialize<'de>> Deserialize<'de> for ArrayList<T>
impl<'de, T: Deserialize<'de>> Deserialize<'de> for ArrayList<T>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<T> Extend<T> for ArrayList<T>
impl<T> Extend<T> for ArrayList<T>
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)