1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! The [`Label`] trait – what the algorithm carries at each `(round, stop)`
//! cell – plus the default single-criterion implementation [`ArrivalTime`].
//!
//! For multi-criterion impls (trade-off queries) see [`crate::labels`].
use crate;
use crateDuration;
use crateSecondOfDay;
/// Per-trip context passed to [`Label::extend_by_trip`]. Single-criterion
/// labels read only `arrival`; multi-criterion impls (fare-aware,
/// route-preference, transfer-penalty) draw on the remaining fields.
/// A label attached to a `(round, stop)` cell during the RAPTOR scan.
///
/// Most callers can ignore this trait. [`Timetable::query`](crate::Timetable::query)
/// uses [`ArrivalTime`] (single-criterion: minimise arrival time, fewest
/// transfers), matching the original RAPTOR paper.
///
/// The trait exists for *multi-criterion* routing: minimising arrival time
/// alongside another criterion and returning a Pareto front of trade-offs.
/// [`ArrivalAndWalk`](crate::labels::ArrivalAndWalk) (arrival vs walking
/// time) and [`ArrivalAndFare`](crate::labels::ArrivalAndFare) (arrival vs
/// accumulated fare via [`Label::Ctx`]) are worked examples. The builder
/// entry points are
/// [`Timetable::query_with_label`](crate::Timetable::query_with_label) and
/// [`Query::with_context`](crate::Query::with_context).
///
/// The algorithm maintains a Pareto front (a *bag* of mutually
/// non-dominated labels) per `(round, stop)`. Multi-criterion impls produce
/// Pareto fronts at the targets; single-criterion `ArrivalTime` bags stay
/// size 1.
///
/// # Defining a custom label
///
/// The example below sketches a route-preference label. Every route has an
/// integer "badness" score (lower is preferred); the algorithm returns a
/// Pareto front of `(arrival_time, worst_score_on_journey)`.
///
/// 1. Define the label `struct` and a `Ctx` carrying the lookup table.
/// `Ctx` is borrowed immutably by every callback; put heavy tables here.
/// 2. Implement [`Label`]. [`Label::extend_by_trip`] receives `route` and
/// `trip` for score lookups; [`Label::extend_by_footpath`] is the place
/// for walk-side criteria.
/// 3. Build the `Ctx` once and supply it via
/// [`Query::with_context`](crate::Query::with_context) before
/// `.depart_at(...)`.
///
/// ```no_run
/// use std::collections::HashMap;
/// use vulture::{
/// Duration, Journey, Label, RouteIdx, SecondOfDay, StopIdx, Timetable, TripContext,
/// };
///
/// // 1. Ctx: a route -> badness-score lookup. Default = empty map
/// // (every route scores zero).
/// #[derive(Default, Debug, Clone)]
/// pub struct RouteScores(pub HashMap<RouteIdx, u32>);
///
/// // 2. Label: arrival time + worst score encountered along the
/// // journey so far. Pareto-dominance is component-wise.
/// #[derive(Copy, Clone, Debug, PartialEq, Eq)]
/// pub struct ArrivalAndWorstScore {
/// pub arrival: SecondOfDay,
/// pub worst: u32,
/// }
///
/// impl Label for ArrivalAndWorstScore {
/// type Ctx = RouteScores;
/// const UNREACHED: Self = Self {
/// arrival: SecondOfDay::MAX,
/// worst: 0,
/// };
///
/// fn from_departure(_ctx: &Self::Ctx, at: SecondOfDay) -> Self {
/// Self { arrival: at, worst: 0 }
/// }
///
/// fn extend_by_trip(self, ctx: &Self::Ctx, leg: TripContext) -> Self {
/// let score = ctx.0.get(&leg.route).copied().unwrap_or(0);
/// Self {
/// arrival: leg.arrival,
/// worst: self.worst.max(score),
/// }
/// }
///
/// fn extend_by_footpath(
/// self,
/// _ctx: &Self::Ctx,
/// _from_stop: StopIdx,
/// _to_stop: StopIdx,
/// walk: Duration,
/// ) -> Self {
/// Self { arrival: self.arrival + walk, worst: self.worst }
/// }
///
/// fn dominates(&self, other: &Self) -> bool {
/// self.arrival <= other.arrival && self.worst <= other.worst
/// }
///
/// fn arrival(&self) -> SecondOfDay { self.arrival }
/// }
///
/// // 3. Wire it in at query time.
/// # fn run<T: Timetable>(
/// # tt: &T,
/// # start: StopIdx,
/// # target: StopIdx,
/// # scores: HashMap<RouteIdx, u32>,
/// # ) {
/// let journeys: Vec<Journey<ArrivalAndWorstScore>> = tt
/// .query_with_label::<ArrivalAndWorstScore>()
/// .with_context(RouteScores(scores))
/// .from(start)
/// .to(target)
/// .max_transfers(5)
/// .depart_at(SecondOfDay::hms(9, 0, 0))
/// .run();
///
/// for j in &journeys {
/// println!("arrives {}, worst route score {}", j.label.arrival, j.label.worst);
/// }
/// # }
/// ```
/// Single-criterion label = arrival time at a stop. Default `L`
/// throughout the algorithm. Constructing from a `SecondOfDay` is direct;
/// extracting back is `arrival()`.
;