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));Sourcepub fn flatten(&self) -> Vec<&T>
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);Sourcepub fn to_vec(&self) -> Vec<T>where
T: Clone,
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);Sourcepub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter(&self) -> impl Iterator<Item = &T>
Returns an iterator over the elements of the SortedList.
§Example
use sortedlist_rs::SortedList;
let sorted_list = SortedList::from([10, 2, 99, 20]);
let mut iterator = sorted_list.iter();
assert_eq!(Some(&2), iterator.next());
assert_eq!(Some(&10), iterator.next());
assert_eq!(Some(&20), iterator.next());
assert_eq!(Some(&99), iterator.next());
assert_eq!(None, iterator.next());