pub trait CostExt: Cost {
// Required methods
fn saturating_sub(self, other: Self) -> Self;
fn to_f64(self) -> f64;
}Expand description
Priority arithmetic that Cost lacks: subtraction and conversion to f64.
Some searches order nodes by priorities that subtract or divide costs: the (G - g) / h
order of anytime nonparametric A*, the 2g + h - h_opposite key of front-to-end
bidirectional searches, or a potential such as dynamic potential search. Those searches
require this trait; every other search needs Cost alone. Like WideMul and
ExactArith, it is a separate trait, so cost types without it keep working.
| Cost | saturating_sub | to_f64 |
|---|---|---|
u8 to u128, usize | saturates at 0 | nearest float; exact up to 2^53 |
Decimal<K> | on raw units, saturates at 0 | the raw unit count, not divided by 10^K |
NonNegF64 (feature float) | clamps at 0, also ∞ - ∞ | the value itself |
A user-defined cost type implements it next to Cost.
§Examples
use search_core::{CostExt, Decimal};
assert_eq!(CostExt::saturating_sub(3_u32, 5), 0);
assert_eq!(7_u64.to_f64(), 7.0);
// Decimal converts its raw units: 1.50 at scale 10^2 is 150.
assert_eq!(Decimal::<2>::from_raw(150).to_f64(), 150.0);Required Methods§
Sourcefn saturating_sub(self, other: Self) -> Self
fn saturating_sub(self, other: Self) -> Self
Subtracts other, saturating at Cost::ZERO.
For priorities that subtract one estimate from another, such as a G - g slack.
Sourcefn to_f64(self) -> f64
fn to_f64(self) -> f64
The cost as a float, possibly rounded; for Decimal<K> the raw units, scaled by
10^K.
Only for priorities that divide costs, such as a (G - g) / h order. Rounding can
change the expansion order among nearly equal priorities, but never a returned cost.
Within one cost type the result is proportional to the cost, so quotients and the
order of differences are what the exact values give, up to rounding. Do not compare
the result against a threshold in cost units: for Decimal<K> it is off by 10^K.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".