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
impl<T> SortedList<T>where
T: Ord,
Public method implementations
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty SortedList.
§Example
use sortedlist_rs::SortedList;
let sorted_list: SortedList<i32> = SortedList::new();
Sourcepub fn kth_smallest(&self, k: usize) -> &T
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));
Sourcepub fn clear(&mut self)
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());
Sourcepub fn insert(&mut self, element: T)
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]);
Sourcepub fn remove(&mut self, k: usize) -> T
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);
Sourcepub fn binary_search(&self, element: &T) -> Result<usize, usize>
pub fn binary_search(&self, element: &T) -> Result<usize, usize>
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);
Sourcepub fn contains(&self, element: &T) -> bool
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));
Sourcepub fn len(&self) -> usize
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());
Sourcepub fn is_empty(&self) -> bool
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());
Sourcepub fn last(&self) -> Option<&T>
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());
Sourcepub fn first(&self) -> Option<&T>
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());
Sourcepub fn get(&self, index: usize) -> Option<&T>
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));
Trait Implementations§
Source§impl<T> Debug for SortedList<T>
impl<T> Debug for SortedList<T>
Source§impl<T> Default for SortedList<T>where
T: Ord,
impl<T> Default for SortedList<T>where
T: Ord,
Source§impl<T> From<&[T]> for SortedList<T>
impl<T> From<&[T]> for SortedList<T>
Source§impl<T> From<&mut [T]> for SortedList<T>
impl<T> From<&mut [T]> for SortedList<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more