7 个版本

使用旧的 Rust 2015

0.1.0 2016 年 8 月 11 日
0.0.6 2015 年 4 月 7 日
0.0.4 2015 年 3 月 9 日
0.0.3 2014 年 12 月 23 日

#800数据结构

Download history 32409/week @ 2024-03-14 32666/week @ 2024-03-21 25405/week @ 2024-03-28 31506/week @ 2024-04-04 29528/week @ 2024-04-11 38240/week @ 2024-04-18 35239/week @ 2024-04-25 29896/week @ 2024-05-02 26629/week @ 2024-05-09 32422/week @ 2024-05-16 31307/week @ 2024-05-23 35547/week @ 2024-05-30 29913/week @ 2024-06-06 29381/week @ 2024-06-13 38080/week @ 2024-06-20 27822/week @ 2024-06-27

130,872 每月下载量
用于 38 个 crate (直接使用 20 个)

MIT/Apache

20KB
173

用于泛型集合的实验性比较器。

文档可在 https://contain-rs.github.io/compare/compare 找到。

要使用 compare 与 Cargo,请将以下内容添加到 Cargo.toml

[dependencies]
compare = "0.1"

并将以下内容添加到 crate 根目录

extern crate compare;

lib.rs:

比较器。

比较器是实现 Compare 特质的任何类型,它强制执行一个 全序。它的 compare 方法接受两个值,这两个值可以是相同类型或不同类型,并返回它们之间的排序。

比较器对于参数化排序方法和某些数据结构的行为非常有用。

最基本的比较器是 Natural,它简单地委托给类型的 [Ord] (https://doc.rust-lang.net.cn/std/cmp/trait.Ord.html) 实现。

use compare::{Compare, natural};
use std::cmp::Ordering::{Less, Equal, Greater};

let a = &1;
let b = &2;

let cmp = natural();
assert_eq!(cmp.compare(a, b), Less);
assert_eq!(cmp.compare(b, a), Greater);
assert_eq!(cmp.compare(a, a), Equal);

有方便的方法来检查每个六个关系

use compare::{Compare, natural};

let a = &1;
let b = &2;

let cmp = natural();
assert!(cmp.compares_lt(a, b));
assert!(cmp.compares_le(a, b));
assert!(cmp.compares_ge(b, a));
assert!(cmp.compares_gt(b, a));
assert!(cmp.compares_eq(a, a));
assert!(cmp.compares_ne(a, b));

Compare 特质还提供了默认方法,它消耗一个比较器以生成具有不同行为的新的比较器,类似于 迭代器适配器。例如,所有比较器都可以 反转

use compare::{Compare, natural};
use std::cmp::Ordering::Greater;

let cmp = natural().rev();
assert_eq!(cmp.compare(&1, &2), Greater);

可以通过使用类型为 Fn(&L, &R) -> Ordering 的闭包来实现不基于类型自然排序的比较器。例如,可以通过长度而不是内容来比较切片

use compare::Compare;
use std::cmp::Ordering::{Less, Greater};

let a = [1, 2, 3];
let b = [4, 5];

let cmp = |l: &[i32], r: &[i32]| l.len().cmp(&r.len());
assert_eq!(cmp.compare(&a, &b), Greater);

let cmp = cmp.rev();
assert_eq!(cmp.compare(&a, &b), Less);

比较器可以按字典顺序(https://en.wikipedia.org/wiki/Lexicographical_order)组合,以便首先根据一个键比较值,如果第一个键相等,则根据另一个键

use compare::Compare;
use std::cmp::Ordering::{Less, Equal, Greater};

struct Pet { name: &'static str, age: u8 }

let fido4 = &Pet { name: "Fido", age: 4 };
let ruff2 = &Pet { name: "Ruff", age: 2 };
let fido3 = &Pet { name: "Fido", age: 3 };

let name_cmp = |l: &Pet, r: &Pet| l.name.cmp(r.name);
assert_eq!(name_cmp.compare(fido4, ruff2), Less);
assert_eq!(name_cmp.compare(fido4, fido3), Equal);
assert_eq!(name_cmp.compare(ruff2, fido3), Greater);

let age_cmp = |l: &Pet, r: &Pet| l.age.cmp(&r.age);
assert_eq!(age_cmp.compare(fido4, ruff2), Greater);
assert_eq!(age_cmp.compare(fido4, fido3), Greater);
assert_eq!(age_cmp.compare(ruff2, fido3), Less);

let name_age_cmp = name_cmp.then(age_cmp);
assert_eq!(name_age_cmp.compare(fido4, ruff2), Less);
assert_eq!(name_age_cmp.compare(fido4, fido3), Greater);
assert_eq!(name_age_cmp.compare(ruff2, fido3), Greater);

通常情况下,通过相同的键比较相同类型的两个值会显得重复,因此可以将键提取逻辑提取出来,简化之前的示例。

use compare::{Compare, Extract};
use std::cmp::Ordering::{Less, Greater};

struct Pet { name: &'static str, age: u8 }

let fido4 = &Pet { name: "Fido", age: 4 };
let ruff2 = &Pet { name: "Ruff", age: 2 };
let fido3 = &Pet { name: "Fido", age: 3 };

let name_age_cmp = Extract::new(|p: &Pet| p.name)
             .then(Extract::new(|p: &Pet| p.age));

assert_eq!(name_age_cmp.compare(fido4, ruff2), Less);
assert_eq!(name_age_cmp.compare(fido4, fido3), Greater);
assert_eq!(name_age_cmp.compare(ruff2, fido3), Greater);

没有运行时依赖。