-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessData.cpp
1144 lines (1128 loc) · 33 KB
/
processData.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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* =========================================================================================
* Name : processData.cpp
* Description : student code for Assignment 1 - Data structures and Algorithms - Fall 2017
* =========================================================================================
*/
#include "eventLib.h"
#include "dbLib.h"
// removeHead version for List of list
template<>
int L1List<L1List<NinjaInfo>>::removeHead()
{
if (_pHead)
{
if (pTail == _pHead) pTail = nullptr;
_pHead->data.clean();
L1Item<L1List<NinjaInfo_t>> *p = _pHead;
_pHead = p->pNext;
delete p;
_size--;
return 0;
}
return -1;
}
template<>
L1Item<char[ID_MAX_LENGTH]>::L1Item(char *a) : pNext{ nullptr }
{
strcpy(data, a);
}
template<>
int L1List<char[ID_MAX_LENGTH]>::insertHead(char *a)
{
L1Item<char[ID_MAX_LENGTH]> *p = new L1Item<char[ID_MAX_LENGTH]>(a);
p->pNext = _pHead;
_pHead = p;
if (!pTail) pTail = _pHead;
_size++;
return 0;
}
//##########################
template<>
int L1List<char[ID_MAX_LENGTH]>::push_back(char *a)
{
if (_pHead == NULL) {
_pHead = new L1Item<char[ID_MAX_LENGTH]>(a);
pTail = _pHead;
}
else {
pTail->pNext = new L1Item<char[ID_MAX_LENGTH]>(a);
pTail = pTail->pNext;
}
_size++;
return 0;
}
// Traverse in a list of a specific ninja's data and insert new item in ascending order of time
template<>
void L1List<NinjaInfo>::traverseAndInsert(NinjaInfo &a)
{
if (!_pHead)
{
push_back(a);
return;
}
if (a.timestamp <= _pHead->data.timestamp)
{
insertHead(a);
return;
}
if (!_pHead->pNext)
{
push_back(a);
return;
}
L1Item<NinjaInfo> *pCurr{ _pHead->pNext };
L1Item<NinjaInfo> *pPre{ _pHead };
while (1)
{
if (a.timestamp <= pCurr->data.timestamp)
{
pPre->pNext = new L1Item<NinjaInfo>(a);
pPre->pNext->pNext = pCurr;
return;
}
if (a.timestamp > pCurr->data.timestamp && !pCurr->pNext)
{
push_back(a);
return;
}
pPre = pCurr;
pCurr = pCurr->pNext;
}
}
//############################
// return "-1"/ID
template<>
const char* L1List<L1List<NinjaInfo>>::traverseAndRemove(char *killerID, int &targetIndex)
{
if (!killerID) { targetIndex = -1; return "-1"; }
if (!_pHead) { targetIndex = -1; return "-1"; }
// find out whether the killer exist
L1Item<L1List<NinjaInfo>> *p{ _pHead }, *pPreTarget{ nullptr }, *pPre{ nullptr };
bool isTargetExist{ 0 };
char *target{ new char[ID_MAX_LENGTH] }; //{""}: doesn't work in C++11
strcpy(target, "");
size_t index{ 0 };
while (true)
{
char *possibleTarget{ p->data[0].id };
int cmp{ strcmp(possibleTarget, killerID) };
if (cmp < 0 && strcmp(possibleTarget, target) > 0)
{
strcpy(target, possibleTarget);
pPreTarget = pPre; // save for killing
if (!isTargetExist) isTargetExist = 1;
targetIndex = index;
}
//else if (cmp == 0) isKillerExist = 1;
if (!p->pNext)
{
if (!isTargetExist) { delete[] target; targetIndex = -1; return "-1"; }
// killing
if (!pPreTarget) removeHead(); // target is head
else if (!pPreTarget->pNext->pNext) // target is tail
{
delete pTail; // the process already include cleaning
pPreTarget->pNext = nullptr;
pTail = pPreTarget;
_size--;
}
else
{
p = pPreTarget->pNext; // p <-- target
pPreTarget->pNext = p->pNext;
delete p; // the process already include cleaning
_size--;
}
return target;
}
pPre = p;
p = p->pNext;
index++;
}
}
//############################
template<>
bool L1List<L1List<NinjaInfo>>::find(char *ninjaID, int &idx)
{
L1Item<L1List<NinjaInfo>> *p{ _pHead };
int i{ 0 };
while (p)
{
if (ninjaID == p->data[0]) { idx = i; return 1; }
p = p->pNext;
i++;
}
idx = -1;
return 0;
}
// // removeHead version for List of list
//template<>
//int L1List<L1List<NinjaInfo>>::removeHead()
//{
// if (_pHead)
// {
// if (pTail == _pHead) pTail = nullptr;
// _pHead->data.clean();
// L1Item<L1List<NinjaInfo_t>> *p = _pHead;
// _pHead = p->pNext;
// delete p;
// _size--;
// return 0;
// }
// return -1;
//}
// // Traverse in a list of a specific ninja's data and insert new item in ascending order of time
//template<>
//void L1List<NinjaInfo>::traverseAndInsert(NinjaInfo &a)
//{
// if (!_pHead)
// {
// push_back(a);
// return;
// }
// if (a.timestamp <= _pHead->data.timestamp)
// {
// insertHead(a);
// return;
// }
// if (!_pHead->pNext)
// {
// push_back(a);
// return;
// }
// L1Item<NinjaInfo> *pCurr{ _pHead->pNext };
// L1Item<NinjaInfo> *pPre{ _pHead };
// while (1)
// {
// if (a.timestamp <= pCurr->data.timestamp)
// {
// pPre->pNext = new L1Item<NinjaInfo>(a);
// pPre->pNext->pNext = pCurr;
// return;
// }
// if (a.timestamp > pCurr->data.timestamp && !pCurr->pNext)
// {
// push_back(a);
// return;
// }
// pPre = pCurr;
// pCurr = pCurr->pNext;
// }
//}
// Traverse the list of list and insert data to the suitable list in ascending order
void traverse(L1List<L1List<NinjaInfo>> &L, NinjaInfo &a)
{
if (!L.getHead())
{
L1List<NinjaInfo> l(a);
L.insertHead(l);
return;
}
L1Item<L1List<NinjaInfo_t>> *p{ L.getHead() };
try
{
while (1)
{
if (a == p->data[0])
{
p->data.traverseAndInsert(a);
return;
}
p = p->pNext;
if (!p)
{
L1List<NinjaInfo> l(a);
L.push_back(l);
return;
}
}
return;
}
catch (DSAException &e)
{
cerr << '\n' << e.getError() << ": " << e.getErrorText() << '\n';
return;
}
}
// traverse and insert item to new list, then remove that item from the list
void traverse(L1List<NinjaInfo> &lN, L1List<L1List<NinjaInfo>> &lL)
{
try
{
L1Item<NinjaInfo> *p{ lN.getHead() };
while (p)
{
traverse(lL, p->data);
p = p->pNext;
}
return;
}
catch (DSAException &e)
{
cerr << '\n' << e.getError() << ": " << e.getErrorText() << '\n';
return;
}
}
//using L1Event = L1List<ninjaEvent_t>;
void printEvent(L1Item<ninjaEvent_t> *a)
{
if (a->pNext) cout << a->data.code << ' ';
else cout << a->data.code << '\n';
}
// struct for the coordinates
struct PointOnEarth
{
double longt{ 0.0 }, lat{ 0.0 };
PointOnEarth() {}
PointOnEarth(double a, double b) :longt{ a }, lat{ b } {}
};
// structure for global data
//struct TimeAnalytics
//
class PointerToGData
{
public:
// attributes
L1List<ninjaEvent_t> pLE; // copy list of events list for reprint
L1List<L1List<NinjaInfo_t>> pLL; // list of lists
double *totalDistance{ nullptr }; // total distance of traveling array
time_t *totalUnmovingTime{ nullptr }; // array of total unmoved time of each ninja
size_t *numOfTimesUnmoving{ nullptr }; // array of the number of times each ninja unmove
time_t *firstTimeMoving{ nullptr }; // array of each ninja's first time moving
time_t *lastTimeUnmoving{ nullptr }; // array of each ninja's last time unmoving
time_t *totalMovingTime{ nullptr }; // array of each ninja's total moving time
// methods
PointerToGData() {}
~PointerToGData();
const char* findMaxID();
const char* findAndKill(char *killerID);
void statisticizeData(); // Ninjas'data statistic
const char* findFirstTimeMovingOf(char *ninjaID);
const char* findLastTimeUnmovingOf(char *ninjaID);
int findNumberOfUnmovingTimesOf(char *ninjaID);
string findSumOfTravelingDistanceOf(char *ninjaID);
const char* findMaxTrvlDist();
const char* findMaxTrvlTime();
const char* findMaxUnmovingTime();
//void findAndPrintListOfNinjaTrapped(PointOnEarth &A, PointOnEarth &B, PointOnEarth &C, PointOnEarth &D);
void findAndPrintListOfNinjasLost();
// friend function
//friend bool checkIfTowGivenLineSegmentsIntersect(PointOnEarth &A, PointOnEarth &B, PointOnEarth &C, PointOnEarth &D);
//friend bool checkIfAGivenLineSegmentIntersectAGivenSquare(PointOnEarth &A, PointOnEarth &B, PointOnEarth &C, PointOnEarth &D, PointOnEarth &E, PointOnEarth &F);
//friend bool checkIfAPointIsInsideOrOnTheEdgeOfAGivenSquare(PointOnEarth &A, PointOnEarth &B, PointOnEarth &C, PointOnEarth &D, PointOnEarth &E);
//private:
};
PointerToGData::~PointerToGData()
{
delete[] totalDistance;
delete[] totalUnmovingTime;
delete[] numOfTimesUnmoving;
delete[] firstTimeMoving;
delete[] lastTimeUnmoving;
delete[] totalMovingTime;
}
// return "-1"/ID
const char* PointerToGData::findMaxID()
{
try
{
L1Item<L1List<NinjaInfo>> *p{ pLL.getHead() };
if (!p) { return "empty"; }
char *maxID{ p->data[0].id };
p = p->pNext;
while (p)
{
char *ctmp{ p->data[0].id };
if (strcmp(maxID, ctmp) < 0) maxID = ctmp;
p = p->pNext;
}
return maxID;
}
catch (DSAException &e)
{
cerr << '\n' << e.getError() << ": " << e.getErrorText() << '\n';
return "empty";
}
}
////############################
//// return "-1"/ID
//template<>
//const char* L1List<L1List<NinjaInfo>>::traverseAndRemove(char *killerID, int &targetIndex)
//{
// if (!killerID) { targetIndex = -1; return "-1"; }
// if (!_pHead) { targetIndex = -1; return "-1"; }
// // find out whether the killer exist
// L1Item<L1List<NinjaInfo>> *p{ _pHead }, *pPreTarget{ nullptr }, *pPre{ nullptr };
// bool isTargetExist{ 0 };
// char *target{ new char[ID_MAX_LENGTH] }; //{""}: doesn't work in C++11
// strcpy( target , "" );
// size_t index{ 0 };
// while (true)
// {
// char *possibleTarget{ p->data[0].id };
// int cmp{ strcmp(possibleTarget, killerID) };
// if (cmp < 0 && strcmp(possibleTarget, target) > 0)
// {
// strcpy(target, possibleTarget);
// pPreTarget = pPre; // save for killing
// if (!isTargetExist) isTargetExist = 1;
// targetIndex = index;
// }
// //else if (cmp == 0) isKillerExist = 1;
// if (!p->pNext)
// {
// if (!isTargetExist) { delete[] target; targetIndex = -1; return "-1"; }
// // killing
// if (!pPreTarget) removeHead(); // target is head
// else if (!pPreTarget->pNext->pNext) // target is tail
// {
// delete pTail; // the process already include cleaning
// pPreTarget->pNext = nullptr;
// pTail = pPreTarget;
// }
// else
// {
// p = pPreTarget->pNext; // p <-- target
// pPreTarget->pNext = p->pNext;
// delete p; // the process already include cleaning
// }
// _size--;
// return target;
// }
// pPre = p;
// p = p->pNext;
// index++;
// }
//}
// return true/false, refernece parameter is referrenced to a copy of found ID which should be deallocated
const char* PointerToGData::findAndKill(char *killerID)
{
int targetIndex;
const char *ch{ pLL.traverseAndRemove(killerID, targetIndex) };
// update statistical data
if (targetIndex != -1) // if someone was killed --> update data
{
size_t s{ pLL.getSize() }; // 1 unit less than the original
double *tD = new double[s]();
time_t *tUT = new time_t[s]();
size_t *nOTU = new size_t[s]();
time_t *fTM = new time_t[s]();
time_t *lTU = new time_t[s]();
time_t *tMT = new time_t[s]();
for (size_t i = 0, j = 0; i < s; i++, j++) // copy stage
{
if (j == targetIndex) j++; // skip if index == targetIndex
tD[i] = totalDistance[j];
tUT[i] = totalUnmovingTime[j];
nOTU[i] = numOfTimesUnmoving[j];
fTM[i] = firstTimeMoving[j];
lTU[i] = lastTimeUnmoving[j];
tMT[i] = totalMovingTime[j];
}
// clear old data
delete[] totalDistance;
delete[] totalUnmovingTime;
delete[] numOfTimesUnmoving;
delete[] firstTimeMoving;
delete[] lastTimeUnmoving;
delete[] totalMovingTime;
// update
totalDistance = tD;
totalUnmovingTime = tUT;
numOfTimesUnmoving = nOTU;
firstTimeMoving = fTM;
lastTimeUnmoving = lTU;
totalMovingTime = tMT;
}
return ch;
}
//############################
const double epsilon{ 1e-12 }, conventionalUnmvDist{5e-3};
void PointerToGData::statisticizeData()
{
if (pLL.isEmpty()) return;
size_t s{ pLL.getSize() };
totalDistance = new double[s]();
totalUnmovingTime = new time_t[s]();
numOfTimesUnmoving = new size_t[s]();
firstTimeMoving = new time_t[s]();
lastTimeUnmoving = new time_t[s]();
totalMovingTime = new time_t[s]();
L1Item<L1List<NinjaInfo>> *p{ pLL.getHead() };
// traverse and statisticize
for (size_t i = 0; i < s; i++)
{
L1Item<NinjaInfo> *pBegin{ p->data.getHead() }; // save for finding first time moving
firstTimeMoving[i] = pBegin->data.timestamp;
L1Item<NinjaInfo> *plist{ pBegin }; // for traversing
L1Item<NinjaInfo> *pCurrTUnmv{ pBegin }; // the beginning of current unmoving period
//lastTimeUnmoving[i] = pBegin->data.timestamp; // temporary first time unmoving
bool isMoving{ 1 };
while (plist->pNext)
{
// check for first time moving
if (firstTimeMoving[i] == pBegin->data.timestamp && !isMoving && totalMovingTime[i] == 0)
{
if (distanceEarth(pBegin->data.latitude, pBegin->data.longitude, plist->pNext->data.latitude, plist->pNext->data.longitude) - conventionalUnmvDist >= epsilon)
{
firstTimeMoving[i] = plist->pNext->data.timestamp;
//lastTimeUnmoving[i] = firstTimeMoving[i]; // temporary current time unmoving
//pCurrTUnmv = plist;
}
else if (!plist->pNext->pNext) firstTimeMoving[i] = 0;
}
// sum of distance
totalDistance[i] += distanceEarth(plist->data.latitude, plist->data.longitude, plist->pNext->data.latitude, plist->pNext->data.longitude);
// sum of unmoving time and last time unmoving
if (distanceEarth(plist->pNext->data.latitude, plist->pNext->data.longitude, pCurrTUnmv->data.latitude, pCurrTUnmv->data.longitude) - conventionalUnmvDist < epsilon) // <= 0
{
totalUnmovingTime[i] += plist->pNext->data.timestamp - plist->data.timestamp;
if (isMoving)
{
isMoving = 0;
lastTimeUnmoving[i] = pCurrTUnmv->data.timestamp; // temporary
numOfTimesUnmoving[i]++;
}
}
else
{
totalMovingTime[i] += plist->pNext->data.timestamp - plist->data.timestamp;
pCurrTUnmv = plist->pNext;
if (!isMoving) { isMoving = 1; }
}
plist = plist->pNext;
}
p = p->pNext;
}
return;
}
//############################
//template<>
//bool L1List<L1List<NinjaInfo>>::find(char *ninjaID, int &idx)
//{
// auto p{ _pHead };
// int i{ 0 };
// while (p)
// {
// if (ninjaID == p->data[0]) { idx = i; return 1; }
// p = p->pNext;
// i++;
// }
// idx = -1;
// return 0;
//}
// return "-1"/string time (should be deallocated)
const char* PointerToGData::findFirstTimeMovingOf(char *ninjaID)
{
int idx;
if (pLL.find(ninjaID, idx))
{
if (firstTimeMoving[idx] == 0) return "empty";
char *des = new char[27];
strPrintTime(des, firstTimeMoving[idx]);
return des;
}
return "-1";
}
const char* PointerToGData::findLastTimeUnmovingOf(char *ninjaID)
{
int idx;
if (pLL.find(ninjaID, idx))
{
if (lastTimeUnmoving[idx] == 0) return "Non-stop";
char *des = new char[27];
strPrintTime(des, lastTimeUnmoving[idx]);
return des;
}
return "-1";
}
int PointerToGData::findNumberOfUnmovingTimesOf(char *ninjaID)
{
int idx;
if (pLL.find(ninjaID, idx))
{
return numOfTimesUnmoving[idx];
}
return -1;
}
string PointerToGData::findSumOfTravelingDistanceOf(char *ninjaID)
{
int idx;
//if (pLL.isEmpty()) return "empty";
if (pLL.find(ninjaID, idx))
{
string str;
stringstream buf;
buf << fixed << setprecision(12) << totalDistance[idx];
buf >> str;
return str;
}
return "-1";
}
//###########################
const char* PointerToGData::findMaxTrvlDist()
{
L1Item<L1List<NinjaInfo>> *p{ pLL.getHead() };
if (!p) return "-1";
double maxDist{ totalDistance[0] };
const char *maxTrvlNinjaID{ p->data[0].id }; // set max to head
p = p->pNext;
size_t i{ 1 };
while (p)
{
if (totalDistance[i] > maxDist)
{
maxDist = totalDistance[i];
maxTrvlNinjaID = p->data[0].id;
}
p = p->pNext;
i++;
}
return maxTrvlNinjaID;
}
const char* PointerToGData::findMaxTrvlTime()
{
L1Item<L1List<NinjaInfo>> *p{ pLL.getHead() };
if (!p) return "-1";
time_t maxTrvlTime{ totalMovingTime[0] };
const char *mostMovingNinjaID{ p->data[0].id }; // set max to head
p = p->pNext;
size_t i{ 1 };
while (p)
{
if (totalMovingTime[i] > maxTrvlTime)
{
maxTrvlTime = totalMovingTime[i];
mostMovingNinjaID = p->data[0].id;
}
p = p->pNext;
i++;
}
return mostMovingNinjaID;
}
const char* PointerToGData::findMaxUnmovingTime()
{
L1Item<L1List<NinjaInfo>> *p{ pLL.getHead() };
if (!p) return "-1";
time_t maxUnmovingTime{ totalUnmovingTime[0] };
const char *mostUnmovingNinjaID{ p->data[0].id };
p = p->pNext;
size_t i{ 1 };
while (p)
{
if (totalUnmovingTime[i] > maxUnmovingTime)
{
maxUnmovingTime = totalUnmovingTime[i];
mostUnmovingNinjaID = p->data[0].id;
}
p = p->pNext;
i++;
}
return mostUnmovingNinjaID;
}
//##########################
//// AB & CD
//bool checkIfTowGivenLineSegmentsIntersect(PointOnEarth &A, PointOnEarth &B, PointOnEarth &C, PointOnEarth &D)
//{
// // using cross product method ( [AB, BC] )
// double abc{ (B.longt - A.longt)*(C.lat - B.lat) - (C.longt - B.longt)*(B.lat - A.lat) };
// double abd{ (B.longt - A.longt)*(D.lat - B.lat) - (D.longt - B.longt)*(B.lat - A.lat) };
// if (abc < epsilon && abc >= 0) // if A, B, C are collinear => abc = 0
// {
// if ((C.lat - A.lat)*(C.lat - B.lat) < epsilon && (C.longt - A.longt)*(C.longt - B.longt) < epsilon) // if ((yC - yA)(yC - yB)) <= 0
// return 1; // if AB // Ox
// else if (abd < epsilon && abd >= 0) // if A, B, D are collinear
// {
// if ((D.lat - A.lat)*(D.lat - B.lat) < epsilon && (D.longt - A.longt)*(D.longt - B.longt) < epsilon) // if ((yD - yA)(yD - yB)) <= 0
// return 1;
// else if ((D.lat - A.lat)*(C.lat - A.lat) < 0 || (D.longt - A.longt)*(C.longt - A.longt) < 0)
// return 1; // AB is a part of CD
// else return 0; // A, B, C, D are all collinear but the two line segments do not intersect
// }
// else return 0;
// }
// if (abd < epsilon && abd >=0) // if A, B, D are collinear
// {
// if ((D.lat - A.lat)*(D.lat - B.lat) < epsilon && (D.longt - A.longt)*(D.longt - B.longt) < epsilon) // if ((yD - yA)(yD - yB)) <= 0
// return 1;
// else return 0;
// }
// double cda{ (D.longt - C.longt)*(A.lat - D.lat) - (A.longt - D.longt)*(D.lat - C.lat) };
// double cdb{ (D.longt - C.longt)*(B.lat - D.lat) - (B.longt - D.longt)*(D.lat - C.lat) };
// if (cda >= 0 && cda < epsilon) // C, D, A are collinear
// {
// if ((A.lat - C.lat)*(A.lat - D.lat) < epsilon && (A.longt - C.longt)*(A.longt - D.longt) < epsilon)
// return 1;
// else return 0;
// }
// if (cdb >= 0 && cdb < epsilon) // C, D, B are collinear
// {
// if ((B.lat - C.lat)*(B.lat - D.lat) < epsilon && (B.longt - C.longt)*(B.longt - D.longt) < epsilon)
// return 1;
// else return 0;
// }
// if (abc*abd < 0 && cda * cdb < 0) // (A, B, C) , (A, B, D) differ in orientation and so do (C, D, A) & (C, D, B).
// return 1;
// return 0;
//}
//// square ABCD, line segment EF
//bool checkIfAGivenLineSegmentIntersectAGivenSquare(PointOnEarth &A, PointOnEarth &B, PointOnEarth &C, PointOnEarth &D, PointOnEarth &E, PointOnEarth &F) // square ABCD, line segment EF
//{
// return checkIfTowGivenLineSegmentsIntersect(A, B, E, F) || checkIfTowGivenLineSegmentsIntersect(B, C, E, F) || checkIfTowGivenLineSegmentsIntersect(C, D, E, F) || checkIfTowGivenLineSegmentsIntersect(D, A, E, F);
//}
//bool checkIfAPointIsInsideOrOnTheEdgeOfAGivenSquare(PointOnEarth &A, PointOnEarth &B, PointOnEarth &C, PointOnEarth &D, PointOnEarth &E)
//{
// double abe ((B.longt - A.longt)*(E.lat - B.lat) - (E.longt - B.longt)*(B.lat - A.lat));
// double bce ((C.longt - B.longt)*(E.lat - C.lat) - (E.longt - C.longt)*(C.lat - B.lat));
// double cde ((D.longt - C.longt)*(E.lat - D.lat) - (E.longt - D.longt)*(D.lat - C.lat));
// double dae ((A.longt - D.longt)*(E.lat - A.lat) - (E.longt - A.longt)*(A.lat - D.lat));
// if (abe >= 0 && abe < epsilon)
// {
// if ((E.lat - A.lat)*(E.lat - B.lat) < epsilon && (E.longt - A.longt)*(E.longt - B.longt) < epsilon)
// return 1;
// else return 0;
// }
// if (bce >= 0 && bce < epsilon)
// {
// if ((E.lat - B.lat)*(E.lat - C.lat) < epsilon && (E.longt - B.longt)*(E.longt - C.longt) < epsilon)
// return 1;
// else return 0;
// }
// if (cde >= 0 && cde < epsilon)
// {
// if ((E.lat - C.lat)*(E.lat - D.lat) < epsilon && (E.longt - C.longt)*(E.longt - D.longt) < epsilon)
// return 1;
// else return 0;
// }
// if (dae >= 0 && dae < epsilon)
// {
// if ((E.lat - D.lat)*(E.lat - A.lat) < epsilon && (E.longt - D.longt)*(E.longt - A.longt) < epsilon)
// return 1;
// else return 0;
// }
// if ((abe >= epsilon && bce >= epsilon && cde >= epsilon && dae >= epsilon) || (abe < 0 && bce < 0 && cde < 0 && dae < 0)) // if (A,B,E), (B,C,E), (C,D,E), (D,A,E) all have the same orientation
// return 1;
// return 0;
//}
//template<>
//L1Item<char[ID_MAX_LENGTH]>::L1Item(char *a) : pNext{ nullptr }
//{
// strcpy(data, a);
//}
//template<>
//int L1List<char[ID_MAX_LENGTH]>::insertHead(char *a)
//{
// L1Item<char[ID_MAX_LENGTH]> *p = new L1Item<char[ID_MAX_LENGTH]>(a);
// p->pNext = _pHead;
// _pHead = p;
// if (!pTail) pTail = _pHead;
// _size++;
// return 0;
//}
//void PointerToGData::findAndPrintListOfNinjaTrapped(PointOnEarth &A, PointOnEarth &B, PointOnEarth &C, PointOnEarth &D) // the trap is the square ABCD
//{
// auto p{ pLL.getHead() };
// if (!p) { cout << "-1\n"; return; }
// PointOnEarth currP, nxtP;
// L1List<char[ID_MAX_LENGTH]> ninjaTrappedList;
// while (true)
// {
// auto pSubList{ p->data.getHead() };
// if (pSubList)
// {
// if (pSubList->pNext) // multi point
// {
// while (pSubList->pNext)
// {
// currP.longt = pSubList->data.longitude;
// currP.lat = pSubList->data.latitude;
// nxtP.longt = pSubList->pNext->data.longitude;
// nxtP.lat = pSubList->pNext->data.latitude;
// if (checkIfAGivenLineSegmentIntersectAGivenSquare(A, B, C, D, currP, nxtP))
// {
// char *ch = new char[ID_MAX_LENGTH];
// strcpy(ch, pSubList->data.id);
// ninjaTrappedList.insertHead(ch);
// delete[] ch;
// break;
// }
// pSubList = pSubList->pNext;
// }
// }
// else // single point
// {
// // check if a point is inside or on the edge of a square
// //double maxLongtOfSquare{ A.longt }; // find max longtitude
// //if (maxLongtOfSquare < B.longt) maxLongtOfSquare = B.longt;
// //if (maxLongtOfSquare < C.longt) maxLongtOfSquare = C.longt;
// //if (maxLongtOfSquare < D.longt) maxLongtOfSquare = D.longt;
// currP.longt = pSubList->data.longitude; // create a segment
// currP.lat = pSubList->data.latitude; // with this point is a head
// //nxtP.longt = maxLongtOfSquare; // and the other head is a point on the far right of
// //nxtP.lat = currP.lat; // the square with the same ordinate
// //if (checkIfTowGivenLineSegmentsIntersect(A,B,currP,nxtP) ^ checkIfTowGivenLineSegmentsIntersect(B,C,currP,nxtP) ^ checkIfTowGivenLineSegmentsIntersect(C,D,currP,nxtP) ^ checkIfTowGivenLineSegmentsIntersect(D,A,currP, nxtP)) // if the number of intersect times is odd then the point is inside or on the edge of the square
// if (checkIfAPointIsInsideOrOnTheEdgeOfAGivenSquare(A, B, C, D, currP))
// {
// char *ch = new char[ID_MAX_LENGTH];
// strcpy(ch, pSubList->data.id);
// ninjaTrappedList.insertHead(ch);
// delete[] ch;
// }
// }
// }
// if (!p->pNext)
// {
// if (ninjaTrappedList.isEmpty()) cout << "-1\n";
// else
// {
// do
// {
// if (ninjaTrappedList.getHead()->pNext) cout << ninjaTrappedList[0] << ' ';
// else cout << ninjaTrappedList[0] << '\n';
// ninjaTrappedList.removeHead();
// } while (!ninjaTrappedList.isEmpty());
// }
// return;
// }
// else p = p->pNext;
// }
//}
////##########################
//template<>
//int L1List<char[ID_MAX_LENGTH]>::push_back(char *a)
//{
// if (_pHead == NULL) {
// _pHead = new L1Item<char[ID_MAX_LENGTH]>(a);
// pTail = _pHead;
// }
// else {
// pTail->pNext = new L1Item<char[ID_MAX_LENGTH]>(a);
// pTail = pTail->pNext;
// }
// _size++;
// return 0;
//}
void PointerToGData::findAndPrintListOfNinjasLost()
{
L1Item<L1List<NinjaInfo>> *p{ pLL.getHead() };
if (!p) { cout << "-1\n"; return; }
L1List<PointOnEarth> traversingList;
L1List<char[ID_MAX_LENGTH]> ninjaLostList;
bool found{ 0 };
while (true)
{
// go through a data list of a specific ninja
L1Item<NinjaInfo> *pSubList{ p->data.getHead() };
if (pSubList)
{
PointOnEarth coordinate{ pSubList->data.longitude, pSubList->data.latitude };
traversingList.insertHead(coordinate);
pSubList = pSubList->pNext;
while (pSubList && !found)
{
// check whether the point is a moving point
if (distanceEarth(pSubList->data.latitude, pSubList->data.longitude, traversingList.getTail()->data.lat, traversingList.getTail()->data.longt) - conventionalUnmvDist >= epsilon) // sub > 0
{
coordinate.longt = pSubList->data.longitude; coordinate.lat = pSubList->data.latitude;
traversingList.push_back(coordinate);
// scan traversingList for existence of a extremely close to the tail point
L1Item<PointOnEarth> *pTravL{ traversingList.getHead() };
L1Item<PointOnEarth> *pTrvLTail{ traversingList.getTail() };
while (pTravL->pNext)
{
// if found
if (distanceEarth(pTrvLTail->data.lat, pTrvLTail->data.longt, pTravL->data.lat, pTravL->data.longt) - conventionalUnmvDist < epsilon) // sub <= 0 //
{
char *ch = new char[ID_MAX_LENGTH];
strcpy(ch, pSubList->data.id);
ninjaLostList.push_back(ch);
delete[] ch;
found = 1;
break;
}
pTravL = pTravL->pNext;
}
}
pSubList = pSubList->pNext;
}
}
if (!p->pNext)
{
if (ninjaLostList.isEmpty()) cout << "-1\n";
else
{
do
{
if (ninjaLostList.getHead()->pNext) cout << ninjaLostList[0] << ' ';
else cout << ninjaLostList[0] << '\n';
ninjaLostList.removeHead();
} while (!ninjaLostList.isEmpty());
}
return;
}
p = p->pNext;
found = 0;
traversingList.clean();
}
}
//template<>
//L1List<char>::L1List(char a) : _pHead{ new L1Item<char>(a) }, pTail{ _pHead }, _size{ 1 }{}
//template<>
//void L1List<char>::insertHead(char a)
//{
// L1Item<char> *p = new L1Item<char>(a);
// p->pNext = _pHead;
// _pHead = p;
// if (!pTail) pTail = _pHead;
// _size++;
// return;
//}
// function handle envent
bool processEvent(ninjaEvent_t& event, L1List<NinjaInfo_t>& nList, void *pGData)
{
// TODO: Your code comes here
try
{
char c{ event.code[0] };
if ( c >= '0' && c <= '9')
{
switch (c)
{
case'0': // '0'
//if (event.code[1] == 0)
{
cout << event.code << ": ";
if (static_cast<PointerToGData*>(pGData)->pLE.isEmpty()) { cout << "empty\n";}
else static_cast<PointerToGData*>(pGData)->pLE.traverse(printEvent);
return 1;
}
/*else
{
return 0;
}*/
case'1': // '1' or "1x..."
c = event.code[1];
if (c == 0) // '1'
{
if (nList.isEmpty()) cout << event.code << ": " << "empty\n";
else cout << event.code << ": " << nList[0].id << '\n';
return 1;
}
else if (c >= '0' && c <= '4') // "1xABCD"
{
switch (c)
{
case'0': // "10"
if (event.code[2] == 0)
{
cout << event.code << ": " << static_cast<PointerToGData*>(pGData)->findMaxTrvlTime() << '\n';
return 1;
}
else
{
return 0;
}
case'1': // "11XYZT"
if (event.code[2] != 0)
{
const char *ch{ static_cast<PointerToGData*>(pGData)->findAndKill(&(event.code[2])) }; // result return is in heap
cout << event.code << ": " << ch << '\n';
if (strcmp(ch,"-1")) delete[] ch;
return 1;
}
else
{
return 0;
}
case'2': // "12"
if (event.code[2] == 0)
{
cout << event.code << ": " << static_cast<PointerToGData*>(pGData)->findMaxUnmovingTime() << '\n';
return 1;
}
else
{
return 0;
}
case'3': // "13ABCDmnpqEFGHuvrs"
if (event.code[2] != 0)
{
/*for (int i{ 2 }; i < 18; i++)
if (event.code[i] < '0' || event.code[i] > '9') return 0;*/
//PointOnEarth A{ 0.0, 0.0 }, B{ 0.0, 0.0 };
//{
// double coordinate[4]{ 0.0, 0.0, 0.0, 0.0 }; //longtA{ 0.0 }, latA{ 0.0 }, longtB{ 0.0 }, latB{ 0.0 }
// int lat{ static_cast<int>(nList[0].latitude) };
// int longt{ static_cast<int>(nList[0].longitude) };
// double coefficient{ 0.1 };
// for (int i = 2, k = 0; i < 18; i++)
// {
// if (isdigit(event.code[i]))
// {
// coordinate[k] += static_cast<double>(event.code[i] - '0') * coefficient;
// if (i % 4 == 1)
// {
// if (k % 2 == 0)
// {
// if (longt < 0) coordinate[k] = longt - coordinate[k];
// else coordinate[k] += longt;
// k++;
// }
// else
// {
// if (lat < 0) coordinate[k] = lat - coordinate[k];
// else coordinate[k] += lat;
// k++;
// }
// coefficient = 0.1;
// }
// else coefficient *= 0.1;
// }
// else return 0;
// }
// A.longt = coordinate[0]; A.lat = coordinate[1]; B.longt = coordinate[2]; B.lat = coordinate[3];
//}