-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport_catalogue.cpp
147 lines (125 loc) · 3.71 KB
/
transport_catalogue.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
#include <unordered_set>
#include <vector>
#include <optional>
#include <stdexcept>
#include "transport_catalogue.h"
namespace tg {
//add stop
void TransportGuide::AddStop(std::string name, Coordinates coordinates) {
if (name_to_stop_.count(name) != 0) {
name_to_stop_.at(name)->coordinates = std::move(coordinates);
}
else
{
stops_.push_back({ name, coordinates });
name_to_stop_[name] = &stops_.back();
stops_ptr_set_.insert(std::make_shared<Stop>(stops_.back()));
}
}
//add bus
void TransportGuide::AddBus(std::string name, std::vector<std::string>& stop_names, bool isCircle) {
std::vector<const Stop*> stops;
stops.reserve(stop_names.size());
for (auto& stop : stop_names) {
if (name_to_stop_.count(stop) == 0) {
//Перед тем как добавить автобус
//Нужно чтобы были добавлены все остановки, даже те
//координаты которых мы не знаем
AddStop(stop, { 0,0 });
}
const Stop* stopPtr = name_to_stop_.at(stop);
stops.push_back(stopPtr);
}
if (!isCircle && stop_names.size() > 0) {
for (int i = stops.size() - 2; i >= 0; --i) {
stops.push_back(stops[i]);
}
}
buses_.push_back({ name, stops, isCircle });
this->name_to_route_.insert({ name, &buses_.back() });
buses_ptr_set_.insert(std::make_shared<Bus>(buses_.back()));
for (auto stop_pointer : stops) {
stop_to_routes_[stop_pointer].insert(&buses_.back());
}
}
//find bus by name
const Bus* TransportGuide::FindBus(std::string name) const {
if (name_to_route_.find(name) != name_to_route_.end()) {
return name_to_route_.at(name);
}
else {
return nullptr;
}
}
//find stop by name
const Stop* TransportGuide::FindStop(std::string name) const {
if (name_to_stop_.find(name) != name_to_stop_.end()) {
return name_to_stop_.at(name);
}
else {
return nullptr;
}
}
//find all buses that have given stop in route
const std::unordered_set<Bus*> TransportGuide::FindAllBusesToStop(const Stop* stop) {
if (stop_to_routes_.find(stop) != stop_to_routes_.end()) {
return stop_to_routes_.at(stop);
}
else
{
return std::unordered_set<Bus*>();
}
}
bool TransportGuide::IsStopDontHaveBuses(const std::shared_ptr<Stop> stop) {
auto search_for_stop = name_to_stop_.find(stop.get()->name);
if (search_for_stop != name_to_stop_.end()) {
if (stop_to_routes_.find(search_for_stop->second) != stop_to_routes_.end()) {
return true;
}
else {
return false;
}
}
return false;
}
int TransportGuide::GetRealStopsDistance(const Stop* stop_A, const Stop* stop_B) const {
std::pair<const Stop*, const Stop*> pairAB{ stop_A, stop_B };
if (stops_distance.count(pairAB) > 0) {
return stops_distance.at(pairAB);
}
else {
if (stop_A == stop_B) {
return 0;
}
std::pair<const Stop*, const Stop*> pairBA{ stop_B, stop_A };
if (stops_distance.count(pairBA) > 0) {
return stops_distance.at(pairBA);
}
}
return 0;
}
//Add stop distance between A and B
void TransportGuide::SetStopsDistance(const std::string stop_name_A, const std::string stop_name_B, int distance) {
auto stopA = FindStop(stop_name_A);
auto stopB = FindStop(stop_name_B);
if (stopA == nullptr || stopB == nullptr)
return;
stops_distance[std::pair<const Stop*, const Stop*> {stopA, stopB}] = distance;
}
bool TransportGuide::HasStop(std::string name) const
{
return name_to_stop_.count(name) != 0;
}
std::list<Bus> TransportGuide::GetBuses() {
return buses_;
}
std::list<Stop> TransportGuide::GetStops() {
return stops_;
}
Stops TransportGuide::GetStopsSharedPtrs() {
return stops_ptr_set_;
}
Buses TransportGuide::GetBusesSharedPtrs() {
return buses_ptr_set_;
}
}