Struct ArrayList

Source
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>

Source

pub fn new(capacity: usize) -> Self

Creates a new ArrayList with the given capacity.

§Arguments
  • capacity - The initial capacity of the ArrayList.
§Examples
use tryingarraylist::ArrayList;
 
let list: ArrayList<i32> = ArrayList::new(10);
 
Source

pub fn push(&mut self, element: T)

Adds an element to the end of the ArrayList.

§Arguments
  • item - The item to push into the ArrayList.
§Examples
use tryingarraylist::ArrayList;
 
let mut list = ArrayList::new(10);
list.push(42);
 
Source

pub fn get(&self, index: usize) -> Option<&T>

Gets the nth element in the ArrayList.

§Arguments
  • index - index to get in ArrayList.
§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));
 
Source

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);
 
Source

pub fn pop(&mut self) -> T

Pops the top most element in the ArrayList.

§Returns

Returns element if the ArrayList is not empty, else panics.

§Examples
use tryingarraylist::ArrayList;
 
let mut list = ArrayList::new(10);
list.push(1);
list.push(2);
 
assert!(list.pop() == 2));
assert!(list.pop() == 1);
 
Source

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);
 
Source

pub fn loc(&self) -> *mut T

Returns the memory location (pointer) in which the first element of the ArrayList is stored

Source

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);
 
Source

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);
 
Source

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<T: Clone> Clone for ArrayList<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for ArrayList<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, T: Deserialize<'de>> Deserialize<'de> for ArrayList<T>

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> Drop for ArrayList<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> Extend<T> for ArrayList<T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T> FromIterator<T> for ArrayList<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: Hash> Hash for ArrayList<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Index<usize> for ArrayList<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for ArrayList<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> IntoIterator for ArrayList<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Ord> Ord for ArrayList<T>

Source§

fn cmp(&self, other: &ArrayList<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for ArrayList<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for ArrayList<T>

Source§

fn partial_cmp(&self, other: &ArrayList<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Serialize> Serialize for ArrayList<T>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Eq> Eq for ArrayList<T>

Auto Trait Implementations§

§

impl<T> Freeze for ArrayList<T>

§

impl<T> RefUnwindSafe for ArrayList<T>
where T: RefUnwindSafe,

§

impl<T> !Send for ArrayList<T>

§

impl<T> !Sync for ArrayList<T>

§

impl<T> Unpin for ArrayList<T>

§

impl<T> UnwindSafe for ArrayList<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,