-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRailNetwork.cpp
More file actions
134 lines (116 loc) · 4.79 KB
/
Copy pathRailNetwork.cpp
File metadata and controls
134 lines (116 loc) · 4.79 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
#include "RailNetwork.h"
#include <iostream>
#include <algorithm>
void Switch::setActiveTrack(int track_id) {
auto it = std::find(connected_tracks.begin(), connected_tracks.end(), track_id);
if (it != connected_tracks.end()) {
active_track = track_id;
}
}
RailNetwork::RailNetwork() {}
void RailNetwork::addStation(int id, const std::string& name, const GPS& location, int capacity) {
stations[id] = std::make_shared<Station>(id, name, location, capacity);
adjacency_list[id] = std::vector<int>();
}
void RailNetwork::addTrack(int id, int station1_id, int station2_id, double distance, double speed_limit) {
tracks[id] = std::make_shared<Track>(id, station1_id, station2_id, distance, speed_limit);
adjacency_list[station1_id].push_back(station2_id);
adjacency_list[station2_id].push_back(station1_id);
}
void RailNetwork::addSwitch(int id, int station_id, const std::vector<int>& track_ids) {
auto sw = std::make_shared<Switch>(id, station_id);
sw->connected_tracks = track_ids;
if (!track_ids.empty()) {
sw->active_track = track_ids[0];
}
switches[id] = sw;
}
std::shared_ptr<Station> RailNetwork::getStation(int id) const {
auto it = stations.find(id);
return (it != stations.end()) ? it->second : nullptr;
}
std::shared_ptr<Track> RailNetwork::getTrack(int id) const {
auto it = tracks.find(id);
return (it != tracks.end()) ? it->second : nullptr;
}
std::shared_ptr<Switch> RailNetwork::getSwitch(int id) const {
auto it = switches.find(id);
return (it != switches.end()) ? it->second : nullptr;
}
const std::vector<int>& RailNetwork::getAdjacentStations(int station_id) const {
static std::vector<int> empty;
auto it = adjacency_list.find(station_id);
return (it != adjacency_list.end()) ? it->second : empty;
}
std::shared_ptr<Track> RailNetwork::getTrackBetween(int station1_id, int station2_id) const {
for (const auto& pair : tracks) {
auto track = pair.second;
if (!track->is_active) continue;
if ((track->station1_id == station1_id && track->station2_id == station2_id) ||
(track->station1_id == station2_id && track->station2_id == station1_id)) {
return track;
}
}
return nullptr;
}
void RailNetwork::manualSwitchOverride(int switch_id, int track_id) {
auto sw = getSwitch(switch_id);
if (sw) {
sw->setActiveTrack(track_id);
std::cout << "[MANUAL OVERRIDE] Switch " << switch_id << " set to track " << track_id << std::endl;
}
}
void RailNetwork::deactivateTrack(int track_id) {
auto track = getTrack(track_id);
if (track) {
track->is_active = false;
std::cout << "[TRACK DEACTIVATED] Track " << track_id << " deactivated" << std::endl;
}
}
void RailNetwork::activateTrack(int track_id) {
auto track = getTrack(track_id);
if (track) {
track->is_active = true;
std::cout << "[TRACK ACTIVATED] Track " << track_id << " activated" << std::endl;
}
}
void RailNetwork::buildTexasNetwork() {
addStation(1, "Houston Central", GPS(29.7604, -95.3698), 150);
addStation(2, "Dallas Fort Worth", GPS(32.7767, -96.7970), 200);
addStation(3, "San Antonio", GPS(29.4241, -98.4936), 120);
addStation(4, "Austin", GPS(30.2672, -97.7431), 130);
addStation(5, "El Paso", GPS(31.7619, -106.4850), 100);
addStation(6, "Corpus Christi", GPS(27.8006, -97.3964), 80);
addStation(7, "Fort Worth", GPS(32.7555, -97.3308), 110);
addStation(8, "Lubbock", GPS(33.5779, -101.8552), 90);
addStation(9, "Amarillo", GPS(35.2220, -101.8313), 85);
addStation(10, "Beaumont", GPS(30.0802, -94.1266), 75);
addTrack(1, 1, 2, 362.0, 90.0);
addTrack(2, 1, 3, 306.0, 85.0);
addTrack(3, 1, 4, 265.0, 85.0);
addTrack(4, 2, 7, 52.0, 80.0);
addTrack(5, 3, 4, 129.0, 80.0);
addTrack(6, 4, 7, 313.0, 85.0);
addTrack(7, 1, 6, 335.0, 80.0);
addTrack(8, 3, 6, 230.0, 75.0);
addTrack(9, 2, 8, 490.0, 90.0);
addTrack(10, 8, 9, 197.0, 85.0);
addTrack(11, 5, 9, 466.0, 90.0);
addTrack(12, 1, 10, 139.0, 75.0);
addTrack(13, 4, 8, 572.0, 90.0);
addTrack(14, 5, 3, 885.0, 95.0);
addTrack(15, 7, 9, 575.0, 90.0);
addSwitch(1, 1, {1, 2, 3, 7, 12});
addSwitch(2, 2, {1, 4, 9});
addSwitch(3, 3, {2, 5, 8, 14});
addSwitch(4, 4, {3, 5, 6, 13});
addSwitch(5, 7, {4, 6, 15});
std::cout << "Texas Rail Network initialized successfully." << std::endl;
}
void RailNetwork::printNetworkStats() const {
std::cout << "\n===== RAIL NETWORK STATISTICS =====" << std::endl;
std::cout << "Total Stations: " << stations.size() << std::endl;
std::cout << "Total Tracks: " << tracks.size() << std::endl;
std::cout << "Total Switches: " << switches.size() << std::endl;
std::cout << "===================================\n" << std::endl;
}