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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! 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`]. Bundles the
/// boarding-and-alighting information so the trait method has a
/// single sidecar argument rather than an eight-positional signature
/// — single-criterion labels ignore most of it; multi-criterion
/// impls (fare-aware, route-preference, transfer-penalty) read the
/// fields they need to evaluate per-trip criteria.
/// A label attached to a `(round, stop)` cell during the RAPTOR scan.
///
/// **Most users can ignore this trait.** [`Timetable::query`](crate::Timetable::query) uses
/// [`ArrivalTime`] (single-criterion: minimise arrival time, fewest
/// transfers), which is what the original RAPTOR paper describes and
/// what almost every routing application wants.
///
/// The trait exists so the algorithm can be reused for *multi-criterion*
/// routing – minimising arrival time *and* something else at the same
/// time, returning a Pareto front of trade-offs. Reach for it when a
/// single "best" answer is the wrong shape: e.g. a fare-aware query
/// that should also report the cheapest journey alongside the fastest.
/// The bundled
/// [`ArrivalAndWalk`](crate::labels::ArrivalAndWalk) (arrival vs.
/// walking time) and
/// [`ArrivalAndFare`](crate::labels::ArrivalAndFare) (arrival vs.
/// accumulated fare from a route → fare table threaded via [`Label::Ctx`])
/// are worked examples; see [`Timetable::query_with_label`](crate::Timetable::query_with_label)
/// for the builder entry point and [`Query::with_context`](crate::Query::with_context)
/// for supplying lookup tables.
///
/// The algorithm maintains a Pareto front (a *bag* of mutually
/// non-dominated labels) per `(round, stop)`, so multi-criterion impls
/// produce real Pareto fronts at the targets rather than a single
/// tiebroken label. Single-criterion `ArrivalTime` bags stay size 1,
/// with no behaviour change versus a non-bag implementation.
///
/// # Defining a custom label
///
/// The example below sketches a *route-preference* label: every route
/// has an integer "badness" score (lower = nicer route — perhaps more
/// scenic, better A/C, fewer crowds) and the algorithm should return
/// a Pareto front of `(arrival_time, worst_score_on_journey)`
/// trade-offs. The fast journey may pick the worst route; a slightly
/// slower journey may avoid it entirely.
///
/// 1. Define your label `struct` and a `Ctx` carrying the lookup
/// table. `Ctx` is borrowed immutably by every callback, so put
/// your big tables here rather than cloning them into the label.
/// 2. Implement [`Label`]. The per-trip context passed to
/// [`Label::extend_by_trip`] gives you `route` and `trip` to look
/// up scores; [`Label::extend_by_footpath`] is the place to
/// accumulate walk-side criteria.
/// 3. At query time, 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()`.
;