#![expect(
clippy::print_stdout,
clippy::approx_constant,
reason = "This example is for demonstration, not a test."
)]
use pretty_assertions::assert_eq;
use skiplist::{FnComparator, SkipMap};
fn main() {
let mut scores: SkipMap<f64, &str, 16, _> =
SkipMap::with_comparator(FnComparator(f64::total_cmp));
#[expect(
clippy::approx_constant,
reason = "intentional approximations for illustration"
)]
let constants: &[(f64, &str)] = &[
(3.141_592, "pi approximation"),
(2.718_281, "e approximation"),
(1.414_213, "sqrt(2) approximation"),
(0.577_215, "Euler-Mascheroni constant"),
(1.618_033, "golden ratio"),
];
for &(value, label) in constants {
scores.insert(value, label);
}
println!("Constants in ascending order:");
for (value, label) in &scores {
println!(" {value:.3} - {label}");
}
assert_eq!(scores.get(&3.141_592), Some(&"pi approximation"));
let mut with_nan: SkipMap<f64, &str, 16, _> =
SkipMap::with_comparator(FnComparator(f64::total_cmp));
with_nan.insert(1.0, "one");
with_nan.insert(f64::INFINITY, "infinity");
with_nan.insert(f64::NAN, "nan");
with_nan.insert(-1.0, "minus one");
println!("\nKeys including NaN and infinity (total order):");
for (key, label) in &with_nan {
if key.is_nan() {
println!(" NaN - {label}");
} else {
println!(" {key} - {label}");
}
}
let last = with_nan.last_key_value().map(|(_, v)| *v);
assert_eq!(last, Some("nan"));
let first = with_nan.first_key_value().map(|(_, v)| *v);
assert_eq!(first, Some("minus one"));
println!("\nDone!");
}