#![expect(
clippy::print_stdout,
reason = "This example is for demonstration, not a test."
)]
use std::cmp::Ordering;
use pretty_assertions::assert_eq;
use skiplist::{FnComparator, OrderedSkipList};
fn main() {
let comparator =
FnComparator(|a: &&str, b: &&str| -> Ordering { a.len().cmp(&b.len()).then(a.cmp(b)) });
let mut list: OrderedSkipList<&str, 16, _> = OrderedSkipList::with_comparator(comparator);
list.insert("banana");
list.insert("fig");
list.insert("apple");
list.insert("kiwi");
list.insert("date");
list.insert("cherry");
list.insert("plum");
println!("Fruits sorted by length, then alphabetically:");
for fruit in &list {
println!(" {} ({} chars)", fruit, fruit.len());
}
let sorted: Vec<&str> = list.iter().copied().collect();
assert_eq!(
sorted,
["fig", "date", "kiwi", "plum", "apple", "banana", "cherry"]
);
assert_eq!(list.get_first(&"kiwi"), Some(&"kiwi"));
let min_length: usize = 5;
let mut long_words: OrderedSkipList<&str, 16, _> =
OrderedSkipList::with_comparator(FnComparator(move |a: &&str, b: &&str| {
a.len().cmp(&b.len()).then(a.cmp(b))
}));
for fruit in &list {
if fruit.len() >= min_length {
long_words.insert(fruit);
}
}
println!("\nFruits with 5+ characters (custom order):");
for fruit in &long_words {
println!(" {fruit}");
}
let long: Vec<&str> = long_words.iter().copied().collect();
assert_eq!(long, ["apple", "banana", "cherry"]);
println!("\nDone!");
}