Struct SortedList

Source
pub struct SortedList<T>
where T: Ord,
{ /* private fields */ }
Expand description

A sorted list data structure

§Example

use sortedlist_rs::SortedList;

let array = vec![90, 19, 25];
let mut sorted_list = SortedList::from(array);

println!("{:?}", sorted_list);
// [19, 25, 90]

sorted_list.insert(100);
sorted_list.insert(1);
sorted_list.insert(20);
println!("{:?}", sorted_list);
// [1, 19, 20, 25, 90, 100]

let x = sorted_list.remove(3);
assert_eq!(25, x);
// removed the 3-rd smallest (0-indexed) element.

assert_eq!(&20, sorted_list.kth_smallest(2));

assert_eq!(20, sorted_list[2]);

println!("{:?}", sorted_list);
// [1, 19, 20, 90, 100]

Implementations§

Source§

impl<T> SortedList<T>
where T: Ord,

Public method implementations

Source

pub fn new() -> Self

Creates an empty SortedList.

§Example
use sortedlist_rs::SortedList;

let sorted_list: SortedList<i32> = SortedList::new();
Source

pub fn kth_smallest(&self, k: usize) -> &T

Find the k-th smallest (0-indexed) element in the SortedList.

§Example
use sortedlist_rs::SortedList;

let sorted_list = SortedList::from([10, 2, 3]);
assert_eq!(&3, sorted_list.kth_smallest(1));
Source

pub fn clear(&mut self)

Clears the SortedList.

§Example
use sortedlist_rs::SortedList;

let mut sorted_list = SortedList::from([10, 2, 3]);
sorted_list.clear();

assert_eq!(0, sorted_list.len());
assert_eq!(true, sorted_list.is_empty());
Source

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

Insert element into the SortedList.

§Example
use sortedlist_rs::SortedList;

let mut sorted_list = SortedList::new();
sorted_list.insert(10);
sorted_list.insert(6);
sorted_list.insert(99);

assert_eq!(3, sorted_list.len());
assert_eq!(6, sorted_list[0]);
assert_eq!(10, sorted_list[1]);
Source

pub fn remove(&mut self, k: usize) -> T

Pops the k-th smallest (0-indexed) element from the SortedList.

§Example
use sortedlist_rs::SortedList;

let mut sorted_list = SortedList::from([10, 2, 99, 20, 30]);
let popped = sorted_list.remove(3);

assert_eq!(30, popped);

Binary searches the given element in the SortedList. Returns Ok(i) for exact match, Err(i) otherwise.

§Example
use sortedlist_rs::SortedList;

let mut sorted_list = SortedList::from([10, 2, 99, 20, 30]);

let result = sorted_list.binary_search(&30);
assert_eq!(Ok(3), result);

let result = sorted_list.binary_search(&90);
assert_eq!(Err(4), result);
Source

pub fn contains(&self, element: &T) -> bool

Returns whether the SortedList contains a specific element.

§Example
use sortedlist_rs::SortedList;

let sorted_list = SortedList::from([10, 2, 99, 20]);

assert_eq!(true, sorted_list.contains(&10));
assert_eq!(false, sorted_list.contains(&90));
Source

pub fn len(&self) -> usize

Returns the number of elements stored in the SortedList.

§Example
use sortedlist_rs::SortedList;

let sorted_list = SortedList::from([10, 2, 99, 20]);

assert_eq!(4, sorted_list.len());
Source

pub fn is_empty(&self) -> bool

Returns whether the SortedList is empty.

§Example
use sortedlist_rs::SortedList;

let mut sorted_list: SortedList<i32> = SortedList::new();
assert_eq!(true, sorted_list.is_empty());

sorted_list.insert(1);
assert_eq!(false, sorted_list.is_empty());
Source

pub fn last(&self) -> Option<&T>

Returns the last element of the SortedList, i.e. the largest element.

§Example
use sortedlist_rs::SortedList;

let sorted_list = SortedList::from([10, 2, 99, 20]);

assert_eq!(Some(&99), sorted_list.last());
Source

pub fn first(&self) -> Option<&T>

Returns the first element of the SortedList, i.e. the smallest element.

§Example
use sortedlist_rs::SortedList;

let sorted_list = SortedList::from([10, 2, 99, 20]);

assert_eq!(Some(&2), sorted_list.first());
Source

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

Returns the element for the given index in the SortedList.

§Example
use sortedlist_rs::SortedList;

let sorted_list = SortedList::from([10, 2, 99, 20]);

assert_eq!(Some(&20), sorted_list.get(2));
Source

pub fn flatten(&self) -> Vec<&T>

Returns a flattened view of the SortedList.

§Example
use sortedlist_rs::SortedList;

let sorted_list = SortedList::from([10, 2, 99, 20]);
let flattened = sorted_list.flatten();

assert_eq!(vec![&2, &10, &20, &99], flattened);
Source

pub fn to_vec(&self) -> Vec<T>
where T: Clone,

Convert self into a new Vec.

§Example
use sortedlist_rs::SortedList;

let sorted_list = SortedList::from([10, 2, 99, 20]);
let v = sorted_list.to_vec();

assert_eq!(vec![2, 10, 20, 99], v);

Trait Implementations§

Source§

impl<T> Debug for SortedList<T>
where T: Ord + Debug,

Source§

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

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

impl<T> Default for SortedList<T>
where T: Ord,

Source§

fn default() -> Self

Creates an empty SortedList.

Source§

impl<T> From<&[T]> for SortedList<T>
where T: Ord + Clone,

Source§

fn from(array: &[T]) -> Self

Allocate a SortedList and fill it by cloning array’s items.

Source§

impl<T> From<&mut [T]> for SortedList<T>
where T: Ord + Clone,

Source§

fn from(array: &mut [T]) -> Self

Allocate a SortedList and fill it by cloning array’s items.

Source§

impl<T, const N: usize> From<[T; N]> for SortedList<T>
where T: Ord,

Source§

fn from(array: [T; N]) -> Self

Allocate a SortedList and move array’s item into it.

Source§

impl<T> From<IntoIter<T>> for SortedList<T>
where T: Ord,

Source§

fn from(iter: IntoIter<T>) -> Self

Creates a SortedList from an IntoIter

Source§

impl<T> From<Vec<T>> for SortedList<T>
where T: Ord,

Source§

fn from(array: Vec<T>) -> Self

Creates a SortedList from a Vec

Source§

impl<T> Index<usize> for SortedList<T>
where T: Ord,

Source§

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

Access the SortedList for the given index.

§Example
use sortedlist_rs::SortedList;

let sorted_list = SortedList::from([20, 2, 10, 99, 50, 32]);

assert_eq!(32, sorted_list[3]);
Source§

type Output = T

The returned type after indexing.

Auto Trait Implementations§

§

impl<T> Freeze for SortedList<T>

§

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

§

impl<T> Send for SortedList<T>
where T: Send,

§

impl<T> Sync for SortedList<T>
where T: Sync,

§

impl<T> Unpin for SortedList<T>
where T: Unpin,

§

impl<T> UnwindSafe for SortedList<T>
where T: UnwindSafe,

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> 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, 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.