dominance_ord/
lib.rs

1use std::cmp::Ordering;
2
3/// Trait for comparisons according to [dominance order][1].
4///
5/// The dominance relation is a partial order. Given solutions `a` and
6/// `b`, a dominance comparison has three possible outcomes:
7///
8/// - Either solution `a` dominates solution `b` ("a < b"),
9///
10/// - or solution `b` dominates solution `a` ("a > b"),
11///
12/// - or neither solution `a` nor `b` dominates each other ("a == b").
13///
14/// The dominance relation is for example used in non-dominated sort
15/// algorithms to obtain the Pareto fronts.
16///
17/// [1]: https://en.wikipedia.org/wiki/Dominance_order
18///
19pub trait DominanceOrd {
20    /// The type on which the dominance relation is defined.
21    type T;
22
23    /// Returns the dominance order.
24    fn dominance_ord(&self, a: &Self::T, b: &Self::T) -> Ordering {
25        if self.dominates(a, b) {
26            Ordering::Less
27        } else if self.dominates(b, a) {
28            Ordering::Greater
29        } else {
30            Ordering::Equal
31        }
32    }
33
34    /// Returns true if `a` dominates `b` ("a < b").
35    fn dominates(&self, a: &Self::T, b: &Self::T) -> bool {
36        match self.dominance_ord(a, b) {
37            Ordering::Less => true,
38            _ => false,
39        }
40    }
41}
42
43#[cfg(test)]
44mod tests;