/
Router.cpp
163 lines (132 loc) · 4.56 KB
/
Router.cpp
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
#include <stdlib.h>
#include <string.h>
#include "Router.h"
void _stepBack(Station* current, Link* link, int nextSid, int targetSid, double* map, Station** stationsArray, List* nextStations, int* step, int* found)
{
if (current->sid != targetSid && (map[nextSid] == -1 || map[nextSid] > link->len + map[current->sid]))
{
*step = true;
map[nextSid] = link->len + map[current->sid];
listAdd(nextStations, stationsArray[nextSid]);
if (nextSid == targetSid)
{
*found = true;
}
}
}
int _stepForward(Station* current, Link* link, int nextSid, int targetSid, double* map, Station** stationsArray, Station** nextStation, Link** nextLink, double* minval, int* step, int* found)
{
if (map[nextSid] != -1 && map[nextSid] + link->len <= *minval)
{
if (nextSid == targetSid)
{
*found = true;
}
else
{
if (stationsArray[nextSid]->srtLids->count + stationsArray[nextSid]->endLids->count < 2) return 0;
}
*minval = map[nextSid];
*nextLink = link;
*nextStation = stationsArray[nextSid];
*step = true;
map[current->sid]++;
}
return 1;
}
Route* _routeNew(void* firstStation)
{
Route* result = (Route*)calloc(1, sizeof(Route));
result->links = listNew();
result->stations = listNew();
listAdd(result->stations, firstStation);
return result;
}
Route* routeCalculate(int sid1, int sid2, List* stations, List* links)
{
Station** stationsArray = (Station**)stations->data;
double* map = (double*)calloc(stations->count, sizeof(double));
for (int i = 0; i < stations->count; i++)
{
map[i] = -1;
}
int found = false;
int step = false;
List* currentStations = listNew();
List* nextStations = listNew();
listAdd(currentStations, stationsArray[sid2]);
map[sid2] = 0;
while (true)
{
listClear(nextStations);
step = false;
for (int j = 0; j < currentStations->count; j++)
{
Station* current = (Station*)currentStations->data[j];
for (int l = 0; l < current->srtLids->count; l++)
{
Link* link = (Link*)current->srtLids->data[l];
_stepBack(current, link, link->sid2, sid1, map, stationsArray, nextStations, &step, &found);
}
for (int l = 0; l < current->endLids->count; l++)
{
Link* link = (Link*)current->endLids->data[l];
_stepBack(current, link, link->sid1, sid1, map, stationsArray, nextStations, &step, &found);
}
}
if (!found && !step) break;
if (found && nextStations->count == 0) break;
listClear(currentStations);
for (int l = 0; l < nextStations->count; l++)
{
listAdd(currentStations, nextStations->data[l]);
}
}
listFree(currentStations);
listFree(nextStations);
Route* result = NULL;
if (found)
{
found = false;
Link* nextLink = NULL;
Station* nextStation = stationsArray[sid1];
double minval = map[sid1];
while (!found)
{
Station* cursid = nextStation;
step = false;
for (int l = 0; l < cursid->srtLids->count; l++)
{
Link* link = (Link*)cursid->srtLids->data[l];
_stepForward(cursid, link, link->sid2, sid2, map, stationsArray, &nextStation, &nextLink, &minval, &step, &found);
}
for (int l = 0; l < cursid->endLids->count; l++)
{
Link* link = (Link*)cursid->endLids->data[l];
_stepForward(cursid, link, link->sid1, sid2, map, stationsArray, &nextStation, &nextLink, &minval, &step, &found);
}
if (!found && !step) return NULL;
if (result == NULL)
{
result = _routeNew(stationsArray[sid1]);
}
result->length += nextLink->len;
listAdd(result->stations, nextStation);
listAdd(result->links, nextLink);
if (found) return result;
}
}
routeFree(&result);
free(map);
return NULL;
}
void routeFree(Route** route)
{
if (route != NULL && *route != NULL)
{
listFree((*route)->links);
listFree((*route)->stations);
free(*route);
*route = NULL;
}
}