-
Notifications
You must be signed in to change notification settings - Fork 32
/
reversal.c
50 lines (40 loc) · 1.58 KB
/
reversal.c
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
/* Copyright 2013 Bliksem Labs. See the LICENSE file at the top-level directory of this distribution and at https://github.com/bliksemlabs/rrrr/. */
//#include "reversal.h"
#include "router.h"
#include "tdata.h"
#include "list.h"
#include <stdbool.h>
#include <stdio.h>
#define OUTPUT_LEN 8000
void reversal (tdata_t *tdata, router_request_t *req, bool verbose) {
/* Initialize router */
router_t router;
router_setup (&router, tdata);
char result_buf[OUTPUT_LEN];
router_route (&router, req);
if (verbose) {
router_request_dump (&router, req);
router_result_dump(&router, req, result_buf, OUTPUT_LEN);
printf("%s", result_buf);
}
/* Repeat search in reverse to compress transfers */
uint32_t n_reversals = req->arrive_by ? 1 : 2;
/* but do not reverse requests starting on board (they cannot be compressed, earliest arrival is good enough) */
if (req->start_trip_trip != NONE) n_reversals = 0;
for (uint32_t i = 0; i < n_reversals; ++i) {
router_request_reverse (&router, req); // handle case where route is not reversed
router_route (&router, req);
if (verbose) {
printf ("Repeated search with reversed request: \n");
router_request_dump (&router, req);
router_result_dump(&router, req, result_buf, OUTPUT_LEN);
printf("%s", result_buf);
}
}
/* Output only final result in non-verbose mode */
if (!verbose) {
router_result_dump(&router, req, result_buf, OUTPUT_LEN);
printf("%s", result_buf);
}
router_teardown(&router);
}