-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUNGPathFinder.cpp
More file actions
executable file
·488 lines (447 loc) · 13.8 KB
/
Copy pathUNGPathFinder.cpp
File metadata and controls
executable file
·488 lines (447 loc) · 13.8 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// UNGPathFinder.cpp : implementation file
//
#include "stdafx.h"
#include "MapEditor.h"
#include "UNGPathFinder.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUNGPathFinder
CUNGPathFinder::CUNGPathFinder()
{
lpReportListBegin = lpReportListEnd = NULL;
}
CUNGPathFinder::~CUNGPathFinder()
{
// Destroy all TEIR and ADJ records
if(lpReportListBegin != NULL)
DestroyAdj(lpReportListBegin);
}
/////////////////////////////////////////////////////////////////////////////
// CUNGPathFinder message handlers
void CUNGPathFinder::InitPathFinder(LPMAP pMap)
{
lpMap = pMap;
lpTeirListBegin = lpTeirListEnd = NULL;
lpFFIncidenceIndex = NULL;
lpTHCurrent = NULL;
nTHBuildingNumber = 0;
if(lpReportListBegin != NULL)
{
DestroyAdj(lpReportListBegin);
lpReportListBegin = lpReportListEnd = NULL;
}
}
/////////////////////////////////////////////////////////////////////////////
// Service
/////////////////////////////////////////////////////////////////////////////
void CUNGPathFinder::AddAdjToTeir(LPADJSTATION lpAdjStation, LPTEIRSTHOLDER lpTH)
{
if(lpTH != NULL && lpAdjStation != NULL) // if Teir and AdjStation Exists
{
// We will add it to the teir
if(lpTH->lpFstAdjStation == NULL) // The List is empty
lpTH->lpFstAdjStation = lpTH->lpLstAdjStation = lpAdjStation;
else // We have some existing records
{
// And we will update the pointers
lpTH->lpLstAdjStation->pNextObject = lpAdjStation;
lpAdjStation->pPrevObject = lpTH->lpLstAdjStation;
lpTH->lpLstAdjStation = lpAdjStation;
}
}
}
BOOL CUNGPathFinder::AddTeirRecord(LPTEIRSTHOLDER lpTH)
{
if(lpTH != NULL) // It is exists
{
if(lpTeirListBegin == NULL) // No teirs
lpTeirListBegin = lpTeirListEnd = lpTH;
else // If we have some
{
lpTeirListEnd->pNextObject = lpTH;
lpTH->pPrevObject = lpTeirListEnd;
lpTeirListEnd = lpTH;
}
lpTH->nTeir = (++nTHBuildingNumber);
return TRUE;
}
return FALSE;
}
void CUNGPathFinder::AddAdjToReport(LPADJSTATION lpAdj)
{
LPADJSTATION lpCurAdj;
if((lpCurAdj = CreateAdjRecord()) == NULL)
ASSERT(FALSE);
//else (if new worked)
lpCurAdj->dwLabel = lpAdj->dwLabel;
lpCurAdj->lpStation = lpAdj->lpStation;
// Then we will add it
if(lpReportListBegin == NULL) // Create the list
lpReportListBegin = lpReportListEnd = lpCurAdj;
else
{
lpReportListEnd->pNextObject = lpCurAdj;
lpCurAdj->pPrevObject = lpReportListEnd;
lpReportListEnd = lpCurAdj;
}
}
// Find a minimal temp label and consider it as PERMANENT
LPADJSTATION CUNGPathFinder::UpdateTempLabels()
{
LPTEIRSTHOLDER lpTH = lpTeirListBegin;
LPADJSTATION lpAdj;
LPADJSTATION lpAdjFound = NULL;
DWORD dwMinimalTime;
BOOL bInitDone = FALSE; // We need to get the initial value somewhere
// Loop thru teirs
if(lpTH != NULL) // We have some teirs
{
// Init MinimalTime
while(lpTH != NULL)
{
lpAdj = lpTH->lpFstAdjStation;
while(lpAdj != NULL) // We have some adj entries
{
if(lpAdj->bTemp == FALSE)
{
// we've found a station with the temp label
// If the time is not initialize
if(!bInitDone)
{
dwMinimalTime = lpAdj->dwLabel;
bInitDone = TRUE;
}
// Let's compare
if(lpAdj->dwLabel <= dwMinimalTime)
lpAdjFound = lpAdj;
}
lpAdj = lpAdj->pNextObject;
}
lpTH = lpTH->pNextObject;
}
}
if(lpAdjFound != NULL) //we found the station
lpAdjFound->bTemp = TRUE;
return lpAdjFound;
}
void CUNGPathFinder::HighLightWay(LPADJSTATION lpAdjEndPoint)
{
LPADJSTATION lpAdj;
LPTEIRSTHOLDER lpTH;
LPINCIDENCE lpCurrentInc;
lpTH = GetTeirByStation(lpAdjEndPoint->lpStation, &lpAdj);
LPADJSTATION lpPrevAdj;
// Add the start point
while(lpTH != NULL)
{
lpPrevAdj = lpTH->lpFstAdjStation;
while(lpPrevAdj != NULL)
{
// If they are incident
if((lpCurrentInc = IsConnectionExists(lpAdjEndPoint->lpStation, lpPrevAdj->lpStation)) != NULL)
{
// If Deikstra was right
if(lpPrevAdj->dwLabel == (lpAdjEndPoint->dwLabel - TransformTime(lpCurrentInc)))
{
// Highlight the station
lpPrevAdj->lpStation->bHighlight = TRUE;
// Add to the report list
AddAdjToReport(lpAdjEndPoint);
lpAdjEndPoint = lpPrevAdj;
}
}
lpPrevAdj = lpPrevAdj->pNextObject;
}
lpTH = lpTH->pPrevObject;
}
AddAdjToReport(lpAdjEndPoint); // Add the first station
}
DWORD CUNGPathFinder::AddTime(DWORD dwFirst, DWORD dwSecond)
{
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// Creation
/////////////////////////////////////////////////////////////////////////////
LPADJSTATION CUNGPathFinder::CreateAdjRecord()
{
LPADJSTATION lpAdjStation;
if((lpAdjStation = new(ADJSTATION)) == NULL)
return NULL; // Couldnt create
// New worked
lpAdjStation->dwLabel = 0;
lpAdjStation->bTemp = FALSE;
lpAdjStation->lpStation = NULL;
lpAdjStation->pNextObject = lpAdjStation->pPrevObject = NULL;
return lpAdjStation;
}
LPTEIRSTHOLDER CUNGPathFinder::CreateTeirRecord()
{
LPTEIRSTHOLDER lpBldTeir;
if((lpBldTeir = new(TEIRSTHOLDER)) == NULL)
return NULL;
// New Worked
lpBldTeir->lpFstAdjStation = lpBldTeir->lpLstAdjStation = NULL;
lpBldTeir->nTeir = 0;
lpBldTeir->pNextObject = lpBldTeir->pPrevObject = NULL;
return lpBldTeir;
}
/////////////////////////////////////////////////////////////////////////////
// Destruction
/////////////////////////////////////////////////////////////////////////////
void CUNGPathFinder::DestroyAdj(LPADJSTATION lpAdjStation)
{
if(lpAdjStation->pNextObject != NULL) // If we have the next record we will
DestroyAdj(lpAdjStation->pNextObject); // Destroy it at first
delete(lpAdjStation);
}
void CUNGPathFinder::DestroyTeir(LPTEIRSTHOLDER lpTH)
{
if(lpTH->pNextObject != NULL) // If we have the next record we will
DestroyTeir(lpTH->pNextObject); // Destroy it at first
if(lpTH->lpFstAdjStation != NULL) // Destroy AdjRecords if exist
DestroyAdj(lpTH->lpFstAdjStation);
delete(lpTH);
}
BOOL CUNGPathFinder::DestroyTeirs()
{
if(lpTeirListBegin != NULL) // We have teirs
DestroyTeir(lpTeirListBegin);
lpTeirListBegin = lpTeirListEnd = NULL; // Null the pointers
nTHBuildingNumber = 0;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// Search
/////////////////////////////////////////////////////////////////////////////
LPINCIDENCE CUNGPathFinder::IsConnectionExists(LPSTATION lpStationA, LPSTATION lpStationB)
{
LPINCIDENCE lpIncLocalVar = lpMap->pIncidence;
while(lpIncLocalVar != NULL)
{
if(((lpIncLocalVar->lpAlpha == lpStationA) && (lpIncLocalVar->lpBeta == lpStationB)) ||
((lpIncLocalVar->lpAlpha == lpStationB) && (lpIncLocalVar->lpBeta == lpStationA)))
return lpIncLocalVar;
lpIncLocalVar = lpIncLocalVar->pNextEntry;
}
return NULL;
}
LPINCIDENCE CUNGPathFinder::FindFirstIncidence(LPSTATION lpStation)
{
lpFFIncidenceIndex = lpMap->pIncidence;
if(lpFFIncidenceIndex != NULL)
lpFFIncidenceIndex = SearchIncidence(lpFFIncidenceIndex, lpStation);
return lpFFIncidenceIndex;
}
LPINCIDENCE CUNGPathFinder::FindNextIncidence(LPSTATION lpStation)
{
LPINCIDENCE lpLocalIncVar = lpFFIncidenceIndex->pNextEntry;
if(lpLocalIncVar != NULL)
{
lpLocalIncVar = SearchIncidence(lpFFIncidenceIndex->pNextEntry, lpStation);
lpFFIncidenceIndex = lpLocalIncVar;
}
return lpLocalIncVar;
}
LPINCIDENCE CUNGPathFinder::SearchIncidence(LPINCIDENCE lpCurrentInc, LPSTATION lpStation)
{
if(lpStation != NULL) // Current pointers are not NULL
{
LPINCIDENCE lpLocalIncVar = lpCurrentInc;
while(lpLocalIncVar != NULL)
{
if((lpLocalIncVar->lpAlpha == lpStation) ||
(lpLocalIncVar->lpBeta == lpStation))
return lpLocalIncVar;
lpLocalIncVar = lpLocalIncVar->pNextEntry;
}
}
return NULL;
}
LPTEIRSTHOLDER CUNGPathFinder::GetTeirByStation(LPSTATION lpStation, LPADJSTATION *lppAdj)
{
LPTEIRSTHOLDER lpTH = lpTeirListBegin;
LPADJSTATION lpAdj;
*lppAdj = NULL;
if(lpTH != NULL) // We have some teirs
{
while(lpTH != NULL)
{
lpAdj = lpTH->lpFstAdjStation;
while(lpAdj != NULL) // We have some adj entries
{
if(lpAdj->lpStation == lpStation)
{
*lppAdj = lpAdj;
return lpTH; // Oh yeah, we did it
}
lpAdj = lpAdj->pNextObject;
}
lpTH = lpTH->pNextObject;
}
}
return lpTH;
}
LPSTATION CUNGPathFinder::GetOppositeStation(LPINCIDENCE lpIncidence, LPSTATION lpStation)
{
if(lpIncidence != NULL)
{
if(lpIncidence->lpAlpha == lpStation)
return lpIncidence->lpBeta;
return lpIncidence->lpAlpha;
}
return NULL;
}
DWORD CUNGPathFinder::TransformTime(LPINCIDENCE lpIncidence)
{
WORD wViceVersaTime = 0;
DWORD dwTransformed = 0;
// Get the route time
if(lpIncidence->wRouteTime != 0)
{
// Exchange Two Values
BYTE bTimeMin = LOBYTE(lpIncidence->wRouteTime);
BYTE bTimeSec = (HIBYTE(lpIncidence->wRouteTime) & 0x3F);
wViceVersaTime = MAKEWORD(bTimeSec, bTimeMin);
return(dwTransformed = (DWORD)wViceVersaTime);
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// Search
/////////////////////////////////////////////////////////////////////////////
// Here is the main subroutine in this program
int CUNGPathFinder::FindPath(LPSTATION lpAlpha, LPSTATION lpBeta)
{
// Create Teir 0 and insert there the Alpha station
// With the permanent label 0;
// CurrentTeir is 0;
if((lpTHCurrent = CreateTeirRecord()) == NULL)
return PF_ERROR; // We couldnt create a record;
AddTeirRecord(lpTHCurrent);
// New worked and we can Insert Alpha to the first teir
// At first lets create a structure for it
LPADJSTATION lpAdjAlpha;
if((lpAdjAlpha = CreateAdjRecord()) == NULL)
return PF_ERROR; // We couldnt create a record;
// Fill in the information
lpAdjAlpha->bTemp = TRUE; // Label is permanent
lpAdjAlpha->lpStation = lpAlpha;
// Lets add it to the first teir;
AddAdjToTeir(lpAdjAlpha, lpTHCurrent);
// Now lets create the second teir and search incidence Station
// for the Alpha station
if((lpTHCurrent = CreateTeirRecord()) == NULL)
return PF_ERROR; // We couldnt create a record;
AddTeirRecord(lpTHCurrent);
int iCResult = 0;
LPADJSTATION lpLocalAdj = NULL;
while((iCResult = CreateAndAddTemp(lpAdjAlpha, lpTHCurrent)) != PF_NOTFOUND)
{
// Lets found a station with the minimal TEMPORARY label
if((lpAdjAlpha = UpdateTempLabels()) != NULL) //We cannot add anymore
{
if(lpAdjAlpha->lpStation == lpBeta) // We found our last station
{
HighLightWay(lpAdjAlpha); // We will highlight the way
DestroyTeirs();
return PF_ALLOK;
}
// It the station already exists somewhere in the teirs
if((lpTHCurrent = GetTeirByStation(lpAdjAlpha->lpStation, &lpLocalAdj)) != NULL)
if(lpTHCurrent->pNextObject == NULL) // it is the last
{
// Create the new teir
if((lpTHCurrent = CreateTeirRecord()) == NULL)
return PF_ERROR; // We couldnt create a record;
AddTeirRecord(lpTHCurrent);
}
else
lpTHCurrent = lpTHCurrent->pNextObject;
}
else
{
// We couldnt find a way
DestroyTeirs();
return PF_NOTFOUND;
}
}
DestroyTeirs(); // Destroy Teirs and Exit
return PF_NOTFOUND;
}
//
int CUNGPathFinder::CreateAndAddTemp(LPADJSTATION lpAdj, LPTEIRSTHOLDER lpTH)
{
int iExistsCount = 0;
int iIterateCount = 0;
// Find the first Incident Station
if(lpAdj->lpStation != NULL) // If it is exists
{
LPINCIDENCE lpLocalIncVar;
if((lpLocalIncVar = FindFirstIncidence(lpAdj->lpStation)) != NULL)
{
// We found Incidence
if(ProcessStation(lpLocalIncVar, lpAdj, lpTH) == PF_ALREADYEXISTS)
iExistsCount++;
iIterateCount++;
// Lets look forward..
while ((lpLocalIncVar = FindNextIncidence(lpAdj->lpStation)) != NULL)
{
if(ProcessStation(lpLocalIncVar, lpAdj, lpTH) == PF_ALREADYEXISTS)
iExistsCount++;
iIterateCount++;
}
}
}
if(iIterateCount == 0) // No Iterations
return PF_NOTFOUND;
if(iExistsCount == iIterateCount) // if we couldnt create anything
return PF_ALREADYEXISTS;
return PF_ALLOK;
}
int CUNGPathFinder::ProcessStation(LPINCIDENCE lpCurInc, LPADJSTATION lpAdj, LPTEIRSTHOLDER lpTH)
{
LPSTATION lpConnectedSt = GetOppositeStation(lpCurInc, lpAdj->lpStation);
// We found it let's check if it is already exists
LPTEIRSTHOLDER lpCurTH;
LPADJSTATION lpCurAdjSt;
if((lpCurTH = GetTeirByStation(lpConnectedSt, &lpCurAdjSt)) == NULL)
{
// this station is the first-chance
if((lpCurAdjSt = CreateAdjRecord()) == NULL)
return PF_ERROR;
// We have the new Adj Record
// Lets's add our station to the AdjRecord
lpCurAdjSt->lpStation = lpConnectedSt;
AddAdjToTeir(lpCurAdjSt, lpTH); // Add the station to the teir
// Insert the new label
InsertLabel(lpCurAdjSt, (lpAdj->dwLabel + TransformTime(lpCurInc)));
}
else // the connected station exists
{
if(!lpCurAdjSt->bTemp) // If the label is TEMPORARY
{
InsertLabel(lpCurAdjSt, (lpAdj->dwLabel + TransformTime(lpCurInc)));
return PF_ALREADYEXISTS;
// If it is PERMANENT we will just exit
}
}
return PF_ALLOK;
}
// Inserts the new label
void CUNGPathFinder::InsertLabel(LPADJSTATION lpAdj, DWORD dwNewTime)
{
// At first we will get the last time;
DWORD dwLastTime;
// The lets compare like Deikstra did
if((dwLastTime = lpAdj->dwLabel) != 0) // The station already has a label
{
lpAdj->dwLabel = min(dwNewTime, dwLastTime);
}
else
lpAdj->dwLabel = dwNewTime;
}