#sorting #partial-ord #min-max #binary-search #error #f64 #f32

no-std try-partialord

Safe failable sort, min, max, binary_search functions for PartialOrd. No need to wrap f32, f64 to sort any more.

4 releases

0.1.3 Apr 9, 2021
0.1.2 Apr 8, 2021
0.1.1 Apr 8, 2021
0.1.0 Apr 8, 2021

#982 in Algorithms

Download history 9/week @ 2024-06-17 9/week @ 2024-06-24 3/week @ 2024-07-15 8/week @ 2024-07-22 15/week @ 2024-07-29 8/week @ 2024-08-05 8/week @ 2024-08-12 10/week @ 2024-08-26 6/week @ 2024-09-02 8/week @ 2024-09-09 5/week @ 2024-09-16 26/week @ 2024-09-23 19/week @ 2024-09-30

58 downloads per month

MIT license

71KB
1K SLoC

try-partialOrd

Safe failable sort, min, max, binary_search functions for PartialOrd. No need to wrap f32, f64 to sort any more.

This crate provides helper traits for type with only PartialOrd but not Ord( like f32, f64), to use methods where Ord is needed, like sort, min, max and binary_search. These methods are almost same as the methods for Ord, exept that it returns InvalidOrderError when the partial_cmp returns None. These traits have try_ methods like try_sort for slice::sort. This is safer than using something like sort_by with ignoreing None case of partial_cmp because it handle error instead of panic.

Sort is using the same logic as std.

use try_partialord::*;
use rand::distributions::Standard;
use rand::prelude::*;
let mut vec: Vec<f32> = Standard.sample_iter(thread_rng()).take(100).collect();
//no NAN in vec so sort should succed
let sort_result = vec.try_sort();
assert!(sort_result.is_ok());
assert!(vec.try_is_sorted().unwrap_or(false));
vec.push(f32::NAN);
//NAN in vec so sort should fail
let sort_result = vec.try_sort();
assert!(sort_result.is_err());
assert!(vec.try_is_sorted().is_err());

No runtime deps