elevator_core/dispatch/nearest_car.rs
1//! Nearest-car dispatch — assigns each call to the closest idle elevator.
2
3use super::{DispatchStrategy, RankContext};
4
5/// Scores `(car, stop)` by absolute distance between the car and the stop.
6///
7/// Paired with the Hungarian assignment in the dispatch system, this
8/// yields the globally minimum-total-distance matching across the group
9/// — no two cars can be sent to the same hall call.
10pub struct NearestCarDispatch;
11
12impl NearestCarDispatch {
13 /// Create a new `NearestCarDispatch`.
14 #[must_use]
15 pub const fn new() -> Self {
16 Self
17 }
18}
19
20impl Default for NearestCarDispatch {
21 fn default() -> Self {
22 Self::new()
23 }
24}
25
26impl DispatchStrategy for NearestCarDispatch {
27 fn rank(&mut self, ctx: &RankContext<'_>) -> Option<f64> {
28 Some((ctx.car_position - ctx.stop_position).abs())
29 }
30}