connection-scan-algorithm
Version:
Connection Scan Algorithm
45 lines (44 loc) • 1.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Implementation of the connection scan algorithm.
*/
class ConnectionScanAlgorithm {
constructor(connections, transfers, resultsFactory) {
this.connections = connections;
this.transfers = transfers;
this.resultsFactory = resultsFactory;
}
/**
* Return an index of connections that achieve the earliest arrival time at each stop.
*/
scan(origins, destinations, date, dow) {
const results = this.resultsFactory.create({ ...origins });
for (const origin in origins) {
this.scanTransfers(results, origin);
}
for (const c of this.connections) {
if (c.trip.service.runsOn(date, dow) && results.isReachable(c) && results.isBetter(c)) {
const newStopReached = results.setConnection(c);
if (newStopReached) {
this.scanTransfers(results, c.destination);
}
if (results.isFinished(destinations, c.departureTime)) {
break;
}
}
}
return results.getConnectionIndex();
}
scanTransfers(results, origin) {
for (const transfer of this.transfers[origin]) {
if (results.isTransferBetter(transfer)) {
const newStopReached = results.setTransfer(transfer);
if (newStopReached) {
this.scanTransfers(results, transfer.destination);
}
}
}
}
}
exports.ConnectionScanAlgorithm = ConnectionScanAlgorithm;