Skip to main content

Cost

Trait Cost 

Source
pub trait Cost:
    Copy
    + Ord
    + Add<Output = Self> {
    const ZERO: Self;

    // Required methods
    fn saturating_add(self, other: Self) -> Self;
    fn saturating_times(self, factor: u32) -> Self;
}
Expand description

A non-negative scalar edge or path cost.

Searches add costs along a path and compare the sums. The default cost type of this ecosystem is u32.

§Contract

  • Non-negative. Every value is >= Cost::ZERO, so extending a path never makes it cheaper. This is why only unsigned integers implement the trait: a negative edge cost silently voids the optimality of Dijkstra, A* and their relatives.
  • Total order. Ord is the cost order; smaller is cheaper.
  • + follows the underlying integer. For the integer types and Decimal, an overflowing + panics in debug builds and wraps in release builds, exactly like the primitive. Pick a type wide enough for the longest path, or use Cost::saturating_add where a sum only serves as a priority. NonNegF64 never overflows; it reaches infinity instead.

Implemented for u8, u16, u32, u64, u128, usize, Decimal, the pair Lexicographic and (feature float) NonNegF64.

§Examples

A function generic over the cost type:

use search_core::Cost;

fn path_cost<C: Cost>(edges: &[C]) -> C {
    edges.iter().fold(C::ZERO, |sum, &edge| sum + edge)
}

assert_eq!(path_cost(&[3_u32, 4, 5]), 12);
assert_eq!(path_cost::<u8>(&[]), 0);

Required Associated Constants§

Source

const ZERO: Self

The cost of the empty path, and the smallest value of the type.

Required Methods§

Source

fn saturating_add(self, other: Self) -> Self

Adds two costs, saturating at the largest value instead of overflowing.

Used where a sum only serves as a priority, such as a weighted A* key.

Source

fn saturating_times(self, factor: u32) -> Self

Multiplies the cost by a small integer factor, saturating at the largest value.

Used to compare weighted sums such as den * g + num * h without division. A factor of 0 always yields Cost::ZERO.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Cost for u8

Source§

const ZERO: u8 = 0

Source§

fn saturating_add(self, other: u8) -> u8

Source§

fn saturating_times(self, factor: u32) -> u8

Source§

impl Cost for u16

Source§

const ZERO: u16 = 0

Source§

fn saturating_add(self, other: u16) -> u16

Source§

fn saturating_times(self, factor: u32) -> u16

Source§

impl Cost for u32

Source§

const ZERO: u32 = 0

Source§

fn saturating_add(self, other: u32) -> u32

Source§

fn saturating_times(self, factor: u32) -> u32

Source§

impl Cost for u64

Source§

const ZERO: u64 = 0

Source§

fn saturating_add(self, other: u64) -> u64

Source§

fn saturating_times(self, factor: u32) -> u64

Source§

impl Cost for u128

Source§

const ZERO: u128 = 0

Source§

fn saturating_add(self, other: u128) -> u128

Source§

fn saturating_times(self, factor: u32) -> u128

Source§

impl Cost for usize

Source§

const ZERO: usize = 0

Source§

fn saturating_add(self, other: usize) -> usize

Source§

fn saturating_times(self, factor: u32) -> usize

Implementors§

Source§

impl<P, S> Cost for Lexicographic<P, S>
where P: Cost, S: Cost,

Source§

impl<const K: u32> Cost for Decimal<K>