minotor 12.0.0 → 13.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +4 -4
  2. package/README.md +7 -4
  3. package/dist/cli/perf.d.ts +19 -9
  4. package/dist/cli.mjs +289 -206
  5. package/dist/cli.mjs.map +1 -1
  6. package/dist/parser.cjs.js +37 -19
  7. package/dist/parser.cjs.js.map +1 -1
  8. package/dist/parser.esm.js +37 -19
  9. package/dist/parser.esm.js.map +1 -1
  10. package/dist/router.cjs.js +1 -1
  11. package/dist/router.cjs.js.map +1 -1
  12. package/dist/router.d.ts +2 -1
  13. package/dist/router.esm.js +1 -1
  14. package/dist/router.esm.js.map +1 -1
  15. package/dist/router.umd.js +1 -1
  16. package/dist/router.umd.js.map +1 -1
  17. package/dist/routing/rangeResult.d.ts +34 -25
  18. package/dist/routing/result.d.ts +28 -9
  19. package/dist/routing/resultInternals.d.ts +4 -0
  20. package/dist/routing/routingResult.d.ts +29 -0
  21. package/dist/routing/state.d.ts +2 -2
  22. package/dist/stops/stopsIndex.d.ts +1 -1
  23. package/dist/timetable/route.d.ts +2 -0
  24. package/package.json +2 -2
  25. package/src/cli/minotor.ts +20 -3
  26. package/src/cli/perf.ts +98 -111
  27. package/src/cli/repl.ts +1 -1
  28. package/src/router.ts +3 -0
  29. package/src/routing/__tests__/plotter.test.ts +0 -11
  30. package/src/routing/__tests__/rangeResult.test.ts +1470 -46
  31. package/src/routing/__tests__/rangeRouter.test.ts +14 -8
  32. package/src/routing/__tests__/result.test.ts +375 -76
  33. package/src/routing/__tests__/state.test.ts +29 -0
  34. package/src/routing/plainRouter.ts +1 -6
  35. package/src/routing/plotter.ts +6 -5
  36. package/src/routing/rangeResult.ts +67 -52
  37. package/src/routing/rangeRouter.ts +3 -8
  38. package/src/routing/result.ts +95 -36
  39. package/src/routing/resultInternals.ts +5 -0
  40. package/src/routing/routingResult.ts +34 -0
  41. package/src/routing/state.ts +11 -3
  42. package/src/stops/__tests__/stopFinder.test.ts +13 -0
  43. package/src/stops/stopsIndex.ts +13 -19
  44. package/src/timetable/__tests__/io.test.ts +27 -0
  45. package/src/timetable/__tests__/route.test.ts +30 -0
  46. package/src/timetable/io.ts +15 -1
  47. package/src/timetable/route.ts +19 -5
  48. package/src/timetable/timetable.ts +1 -1
@@ -8,8 +8,6 @@ import { deserializeStopsMap, serializeStopsMap } from './io.js';
8
8
  import { StopsMap as ProtoStopsMap } from './proto/v1/stops.js';
9
9
  import { SourceStopId, SourceStopsMap, Stop, StopId } from './stops.js';
10
10
 
11
- type StopPoint = { id: StopId; lat: number; lon: number };
12
-
13
11
  /**
14
12
  * The StopsIndex class provides functionality to search for public transport stops
15
13
  * by name or geographic location. It leverages text search and geospatial indexing
@@ -20,13 +18,13 @@ export class StopsIndex {
20
18
  private readonly sourceStopsMap: SourceStopsMap;
21
19
  private readonly textIndex;
22
20
  private readonly geoIndex: KDTree;
23
- private readonly stopPoints: StopPoint[];
21
+ private readonly geoStopIds: StopId[];
24
22
 
25
23
  constructor(stops: Stop[]) {
26
24
  this.stops = stops;
27
25
  this.sourceStopsMap = new Map<SourceStopId, StopId>();
28
26
  const stopsSet = new Map<StopId, { id: StopId; name: string }>();
29
- this.stopPoints = [];
27
+ this.geoStopIds = [];
30
28
  for (let id = 0; id < stops.length; id++) {
31
29
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
32
30
  const stop = stops[id]!;
@@ -45,11 +43,7 @@ export class StopsIndex {
45
43
  }
46
44
 
47
45
  if (stop.lat && stop.lon) {
48
- this.stopPoints.push({
49
- id: id,
50
- lat: stop.lat,
51
- lon: stop.lon,
52
- });
46
+ this.geoStopIds.push(id);
53
47
  }
54
48
  }
55
49
  this.textIndex = createIndex({
@@ -60,11 +54,12 @@ export class StopsIndex {
60
54
  });
61
55
  const stopsArray = Array.from(stopsSet.values());
62
56
  addAll(this.textIndex, stopsArray);
63
- this.geoIndex = new KDTree(this.stopPoints.length);
64
- for (let i = 0; i < this.stopPoints.length; i++) {
57
+ this.geoIndex = new KDTree(this.geoStopIds.length);
58
+ for (let i = 0; i < this.geoStopIds.length; i++) {
65
59
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
66
- const { lat, lon } = this.stopPoints[i]!;
67
- this.geoIndex.add(lon, lat);
60
+ const stop = this.stops[this.geoStopIds[i]!]!;
61
+ // Coordinates were checked when collecting geoStopIds.
62
+ this.geoIndex.add(stop.lon as number, stop.lat as number);
68
63
  }
69
64
  this.geoIndex.finish();
70
65
  }
@@ -112,10 +107,9 @@ export class StopsIndex {
112
107
  * @returns An array of Stop objects that match the search query.
113
108
  */
114
109
  findStopsByName(query: string, maxResults = 5): Stop[] {
115
- const results = search(this.textIndex, query).map(
116
- (result: SearchResult) => this.stops[result.id as number] as Stop,
117
- );
118
- return results.slice(0, maxResults);
110
+ return search(this.textIndex, query)
111
+ .slice(0, maxResults)
112
+ .map((result: SearchResult) => this.stops[result.id as number] as Stop);
119
113
  }
120
114
 
121
115
  /**
@@ -140,8 +134,8 @@ export class StopsIndex {
140
134
  maxResults,
141
135
  radius,
142
136
  ).map((id) => {
143
- const stopPoint = this.stopPoints[id] as StopPoint;
144
- return this.stops[stopPoint.id] as Stop;
137
+ const stopId = this.geoStopIds[id] as StopId;
138
+ return this.stops[stopId] as Stop;
145
139
  });
146
140
  return nearestStops;
147
141
  }
@@ -169,3 +169,30 @@ describe('Timetable IO', () => {
169
169
  assert.deepStrictEqual(deserializedData, routes);
170
170
  });
171
171
  });
172
+
173
+ it('owns compact pickup/drop-off bytes independently of input views', () => {
174
+ const input = Buffer.alloc(128);
175
+ input[17] = 0x12;
176
+ input[29] = 0x34;
177
+ const proto = [17, 29].map((offset) => ({
178
+ stopTimes: new Uint8Array(new Uint16Array([10, 11]).buffer),
179
+ pickupDropOffTypes: input.subarray(offset, offset + 1),
180
+ stops: new Uint8Array(new Uint32Array([0]).buffer),
181
+ serviceRouteId: 0,
182
+ }));
183
+ assert.ok(proto[0]);
184
+ proto.push({ ...proto[0], pickupDropOffTypes: input.subarray(40, 40) });
185
+ const routes = deserializeRoutesAdjacency(proto);
186
+ const [firstRoute, secondRoute, emptyRoute] = routes;
187
+ assert.ok(firstRoute && secondRoute && emptyRoute);
188
+ const first = firstRoute.serialize().pickupDropOffTypes;
189
+ const second = secondRoute.serialize().pickupDropOffTypes;
190
+ assert.notStrictEqual(first.buffer, input.buffer);
191
+ assert.strictEqual(first.buffer, second.buffer);
192
+ assert.strictEqual(first.buffer.byteLength, 2);
193
+ input.fill(0);
194
+ assert.deepStrictEqual([...first], [0x12]);
195
+ assert.deepStrictEqual([...second], [0x34]);
196
+ assert.strictEqual(emptyRoute.serialize().pickupDropOffTypes.length, 0);
197
+ assert.deepStrictEqual(deserializeRoutesAdjacency([]), []);
198
+ });
@@ -279,3 +279,33 @@ describe('Route', () => {
279
279
  });
280
280
  });
281
281
  });
282
+
283
+ it('keeps first-index lookup and stable arrays consistent for unique and repeated stops', () => {
284
+ const route = Route.of({
285
+ id: 0,
286
+ serviceRouteId: 0,
287
+ trips: [
288
+ {
289
+ stops: [5, 7, 5, 9, 5].map((id, index) => ({
290
+ id,
291
+ arrivalTime: index,
292
+ departureTime: index,
293
+ })),
294
+ },
295
+ ],
296
+ });
297
+ assert.strictEqual(route.firstStopRouteIndex(5), 0);
298
+ assert.strictEqual(route.firstStopRouteIndex(7), 1);
299
+ assert.strictEqual(route.firstStopRouteIndex(99), undefined);
300
+ assert.deepStrictEqual(route.stopRouteIndices(99), []);
301
+ assert.deepStrictEqual(route.stopRouteIndices(5), [0, 2, 4]);
302
+ const unique = route.stopRouteIndices(7);
303
+ assert.deepStrictEqual(unique, [1]);
304
+ assert.strictEqual(route.stopRouteIndices(7), unique);
305
+ unique[0] = 3;
306
+ assert.strictEqual(route.firstStopRouteIndex(7), 3);
307
+ const repeated = route.stopRouteIndices(5);
308
+ assert.strictEqual(route.stopRouteIndices(5), repeated);
309
+ repeated.shift();
310
+ assert.strictEqual(route.firstStopRouteIndex(5), 2);
311
+ });
@@ -215,16 +215,30 @@ export const deserializeRoutesAdjacency = (
215
215
  protoRoutesAdjacency: ProtoRoute[],
216
216
  ): Route[] => {
217
217
  const routesAdjacency: Route[] = [];
218
+ // Own the compact payload so small protobuf views do not retain the input file.
219
+ const pickupDropOffStorage = new Uint8Array(
220
+ protoRoutesAdjacency.reduce(
221
+ (size, route) => size + route.pickupDropOffTypes.byteLength,
222
+ 0,
223
+ ),
224
+ );
225
+ let pickupOffset = 0;
218
226
 
219
227
  for (let i = 0; i < protoRoutesAdjacency.length; i++) {
220
228
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
221
229
  const value = protoRoutesAdjacency[i]!;
222
230
  const stops = bytesToUint32Array(value.stops);
231
+ pickupDropOffStorage.set(value.pickupDropOffTypes, pickupOffset);
232
+ const pickupDropOffTypes = pickupDropOffStorage.subarray(
233
+ pickupOffset,
234
+ pickupOffset + value.pickupDropOffTypes.byteLength,
235
+ );
236
+ pickupOffset += value.pickupDropOffTypes.byteLength;
223
237
  routesAdjacency.push(
224
238
  new Route(
225
239
  i,
226
240
  bytesToUint16Array(value.stopTimes),
227
- value.pickupDropOffTypes,
241
+ pickupDropOffTypes,
228
242
  stops,
229
243
  value.serviceRouteId,
230
244
  ),
@@ -85,7 +85,7 @@ export class Route {
85
85
  * ...
86
86
  * }
87
87
  */
88
- private readonly stopIndices: Map<StopId, StopRouteIndex[]>;
88
+ private readonly stopIndices: Map<StopId, StopRouteIndex | StopRouteIndex[]>;
89
89
  /**
90
90
  * The identifier of the route as a service shown to users.
91
91
  */
@@ -115,15 +115,17 @@ export class Route {
115
115
  this.serviceRouteId = serviceRouteId;
116
116
  this.nbStops = stops.length;
117
117
  this.nbTrips = this.stopTimes.length / (this.stops.length * 2);
118
- this.stopIndices = new Map<StopId, StopRouteIndex[]>();
118
+ this.stopIndices = new Map<StopId, StopRouteIndex | StopRouteIndex[]>();
119
119
  for (let i = 0; i < stops.length; i++) {
120
120
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
121
121
  const stopId = stops[i]!;
122
122
  const existingIndices = this.stopIndices.get(stopId);
123
- if (existingIndices) {
124
- existingIndices.push(i);
123
+ if (existingIndices === undefined) {
124
+ this.stopIndices.set(stopId, i);
125
+ } else if (typeof existingIndices === 'number') {
126
+ this.stopIndices.set(stopId, [existingIndices, i]);
125
127
  } else {
126
- this.stopIndices.set(stopId, [i]);
128
+ existingIndices.push(i);
127
129
  }
128
130
  }
129
131
  }
@@ -471,9 +473,21 @@ export class Route {
471
473
  if (stopIndex === undefined) {
472
474
  return [];
473
475
  }
476
+ if (typeof stopIndex === 'number') {
477
+ // Preserve stable, mutable array results for callers that request all indices.
478
+ const indices = [stopIndex];
479
+ this.stopIndices.set(stopId, indices);
480
+ return indices;
481
+ }
474
482
  return stopIndex;
475
483
  }
476
484
 
485
+ /** Returns the first occurrence without allocating an array. */
486
+ public firstStopRouteIndex(stopId: StopId): StopRouteIndex | undefined {
487
+ const indices = this.stopIndices.get(stopId);
488
+ return typeof indices === 'number' ? indices : indices?.[0];
489
+ }
490
+
477
491
  /**
478
492
  * Retrieves the id of a stop at a given index in a route.
479
493
  * @param stopRouteIndex The route index of the stop.
@@ -300,7 +300,7 @@ export class Timetable {
300
300
  if (!serviceRoute || !transportModes.has(serviceRoute.type)) continue;
301
301
  }
302
302
 
303
- const originStopIndex = route.stopRouteIndices(originStop)[0]!;
303
+ const originStopIndex = route.firstStopRouteIndex(originStop)!;
304
304
  const existingHopOnStopIndex = reachableRoutes.get(route);
305
305
  if (existingHopOnStopIndex !== undefined) {
306
306
  if (originStopIndex < existingHopOnStopIndex) {