-
Notifications
You must be signed in to change notification settings - Fork 0
/
structures.h
386 lines (335 loc) · 8.9 KB
/
structures.h
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
#ifndef STRUCTURES_H
#define STRUCTURES_H
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
template<typename Base, typename T>
inline bool instanceof(const T* ptr)
{
return dynamic_cast<const Base*>(ptr) != nullptr;
}
struct Lane
{
unordered_map<string, string> attbs;
};
struct Edge
{
unordered_map<string, string> attbs;
vector<Lane> lanes;
};
struct Junction
{
unordered_map<string, string> attbs;
// vector<string> inc_edges;
// vector<string> out_edges;
};
struct Connection
{
unordered_map<string, string> attbs;
};
struct Constant
{
public:
static double v;
};
// Define and initialize static member
double Constant::v = 1;
class Point
{
public:
double x, y;
bool equals(Point* p)
{
return (this->x == p->x && this->y == p->y);
}
};
struct ComparePoint
{
bool operator()(const Point* a, const Point* b) const
{
if (a == nullptr || b == nullptr)
{
throw std::invalid_argument("Null pointer passed to operator<");
}
// sort by x, y
return a->x == b->x ? a->y < b->y : a->x < b->x;
}
};
class Shape
{
public:
Point* start;
Point* end;
double length;
string name;
bool equals(const Shape* s) const
{
return (this->start->equals(s->start) && this->end->equals(s->end));
}
double time = 0;
double getTime()
{
if (time == 0)
{
time = this->length / Constant::v;
}
return time;
}
};
struct CompareShape
{
bool operator()(const Shape* a, const Shape* b) const
{
if (a == nullptr || b == nullptr)
{
throw std::invalid_argument("Null pointer passed to operator<");
}
// sort by start, end
return
// a->name == b->name ?
a->start->x == b->start->x ? a->start->y == b->start->y ? a->end->x == b->end->x ? a->end->y < b->end->y : a->end->x < b->end->x : a->start->y < b->start->y : a->start->x < b->start->x;
// a->name < b->name;
}
};
class PausingShape : public Shape
{
public:
PausingShape()
{
this->time = 0;
this->length = 0;
this->name = "artificial";
}
PausingShape(double time)
{
this->time = time;
this->length = 0;
this->name = "artificial";
}
PausingShape(Shape* shape, string name)
{
this->start = shape->start;
this->end = shape->end;
this->length = shape->length;
this->time = shape->getTime();
this->name = name;
}
// ghi de ham getTime
double getTime()
{
return this->time;
}
};
class ArtificialShape : public PausingShape {
public:
ArtificialShape(double time) : PausingShape(time) {}
//Phương thức getTime được ghi đè như sau
double getTime() {
return this->time;
}
};
class TENode
{
public:
vector<pair<TENode*, Shape*>> srcs;
Point* origin;
double time;
int key = -1;
string name;
vector<pair<TENode*, Shape*>> tgts;
TENode() {}
TENode(Point* origin, double time, vector<pair<TENode*, Shape*>> tgts)
{
this->origin = origin;
this->time = time;
for (auto it : tgts)
{
this->tgts.push_back(make_pair(it.first, it.second));
}
this->name = "";
}
void insertSource(Shape* shape)
{
// Nếu srcs có chứa một pair mà phần tử đầu của nó giống hệt s->start
// thì không thêm cặp null và s vào
// Ngược lại thi thêm cặp null và s vào
bool flag = false;
for (int i = 0; i < this->srcs.size(); i++)
{
if (this->srcs.at(i).second->equals(shape))
{
flag = true;
break;
}
}
if (!flag)
{
pair<TENode*, Shape*> pair = make_pair(nullptr, shape);
this->srcs.push_back(pair);
}
}
void insertTarget(Shape* s)
{
// Nếu tgts có chứa một pair mà phần tử đầu của nó giống hệt s->end
// thì không thêm cặp null và s vào
// Ngược lại thi thêm cặp null và s vào
bool flag = false;
for (int i = 0; i < this->tgts.size(); i++)
{
if (this->tgts.at(i).second->equals(s))
{
flag = true;
break;
}
}
if (!flag)
{
pair<TENode*, Shape*> pair =
make_pair(nullptr, s);
this->tgts.push_back(pair);
}
}
bool equals(TENode* te_node)
{
return this->origin->equals(te_node->origin);
}
void insertSourcesAndTargets(TENode* node, vector<pii> fromNode, vector<pii> toNode)
{
for (int i = 0; i < fromNode.size(); i++)
{
int atNode = fromNode.at(i).first;
int toThis = fromNode.at(i).second;
if (this->srcs.at(atNode).first == nullptr)
{
this->srcs.at(atNode).first = node;
}
if (node->tgts.at(toThis).first == nullptr)
{
node->tgts.at(toThis).first = this;
}
}
for (int i = 0; i < toNode.size(); i++)
{
int atNode = toNode.at(i).first;
int fromThis = toNode.at(i).second;
if (this->tgts.at(fromThis).first == nullptr)
{
this->tgts.at(fromThis).first = node;
}
if (node->srcs.at(atNode).first == nullptr)
{
node->srcs.at(atNode).first = this;
}
}
}
int indexInSources(Shape* shape)
{
int index = 0;
for (auto it : srcs)
{
Shape* s = it.second;
if (shape->equals(s))
{
return index;
}
index++;
}
return -1;
}
bool endOfLane()
{
if (this->tgts.size() <= 1)
return true;
for (int i = 0; i < this->srcs.size(); i++)
{
auto shape = this->srcs.at(i).second;
auto name = shape->name;
if (name.compare("artificial") == 0) continue;
for (int j = 0; j < this->tgts.size(); j++)
{
auto nextShape = this->tgts.at(j).second;
auto nextName = nextShape->name;
if (nextName.compare("artificial") == 0 || nextName.compare(name) == 0) continue;
return true;
}
}
return false;
}
string isStation(string stations) {
for (int i = 0; i < this->srcs.size(); i++)
{
auto shape = this->srcs.at(i).second;
auto name = shape->name;
if (name.compare("artificial") == 0 || stations.find("$" + name + "$") == string::npos) continue;
return name;
}
return "";
}
virtual TENode* transform(TENode* node) {
return this;
}
virtual void connect2Artificial(TENode* node) {
}
virtual void createConnection(TENode* node) {
}
};
class Station : public TENode
{
public:
Station() : TENode() {}
Station(TENode* temp, string name) : TENode()
{
this->srcs = temp->srcs;
this->name = name;
this->tgts = temp->tgts;
this->origin = temp->origin;
this->time = temp->time;
for (auto& pr : this->srcs)
{
for (auto& pt : pr.first->tgts)
{
if (pt.first->equals(temp))
{
pt.first = this;
}
}
}
for (auto& pr : this->tgts)
{
for (auto& pt : pr.first->srcs)
{
if (pt.first->equals(temp))
{
pt.first = this;
}
}
}
}
};
class ArtificialStation : public Station {
public:
double bestTime;
double amplitude;
double earliness;
double tardiness;
ArtificialStation(string name, double bestTime, double amplitude) : Station() {
this->name = name;
this->time = bestTime;
this->bestTime = bestTime;
this->amplitude = amplitude;
this->earliness = bestTime - amplitude;
this->tardiness = bestTime + amplitude;
}
void createConnection(TENode* node) {
if (instanceof<Station>(node) && !instanceof<ArtificialStation>(node)) {
if (node->name.compare(this->name) == 0) {
auto penaltyT = max(this->earliness - node->time, 0.0);
penaltyT = max(penaltyT, node->time - this->tardiness);
auto aShape = new ArtificialShape(penaltyT);
node->tgts.push_back(make_pair(this, aShape));
this->srcs.push_back(make_pair(node, aShape));
}
}
}
};
#endif // !STRUCTURES_H