about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/network_set.cpp58
-rw-r--r--src/network_set.h7
-rw-r--r--src/subway_map.cpp3
3 files changed, 13 insertions, 55 deletions
diff --git a/src/network_set.cpp b/src/network_set.cpp index 3238dcd..6d2a098 100644 --- a/src/network_set.cpp +++ b/src/network_set.cpp
@@ -1,68 +1,30 @@
1#include "network_set.h" 1#include "network_set.h"
2 2
3void NetworkSet::Clear() { 3void NetworkSet::Clear() {
4 networks_.clear();
5 network_by_item_.clear(); 4 network_by_item_.clear();
6} 5}
7 6
8int NetworkSet::AddLink(int id1, int id2) { 7void NetworkSet::AddLink(int id1, int id2) {
9 if (id2 > id1) { 8 if (id2 > id1) {
10 // Make sure id1 < id2 9 // Make sure id1 < id2
11 std::swap(id1, id2); 10 std::swap(id1, id2);
12 } 11 }
13 12
14 if (network_by_item_.count(id1)) { 13 if (!network_by_item_.count(id1)) {
15 if (network_by_item_.count(id2)) { 14 network_by_item_[id1] = {};
16 int network_id1 = network_by_item_[id1];
17 int network_id2 = network_by_item_[id2];
18
19 networks_[network_id1].emplace(id1, id2);
20
21 if (network_id1 != network_id2) {
22 for (const auto& [other_id1, other_id2] : networks_[network_id2]) {
23 network_by_item_[other_id1] = network_id1;
24 network_by_item_[other_id2] = network_id1;
25 }
26
27 networks_[network_id1].merge(networks_[network_id2]);
28 networks_[network_id2].clear();
29 }
30
31 return network_id1;
32 } else {
33 int network_id = network_by_item_[id1];
34 network_by_item_[id2] = network_id;
35 networks_[network_id].emplace(id1, id2);
36
37 return network_id;
38 }
39 } else {
40 if (network_by_item_.count(id2)) {
41 int network_id = network_by_item_[id2];
42 network_by_item_[id1] = network_id;
43 networks_[network_id].emplace(id1, id2);
44
45 return network_id;
46 } else {
47 int network_id = networks_.size();
48 network_by_item_[id1] = network_id;
49 network_by_item_[id2] = network_id;
50 networks_.emplace_back();
51 networks_[network_id] = {{id1, id2}};
52
53 return network_id;
54 }
55 } 15 }
16 if (!network_by_item_.count(id2)) {
17 network_by_item_[id2] = {};
18 }
19
20 network_by_item_[id1].insert({id1, id2});
21 network_by_item_[id2].insert({id1, id2});
56} 22}
57 23
58bool NetworkSet::IsItemInNetwork(int id) const { 24bool NetworkSet::IsItemInNetwork(int id) const {
59 return network_by_item_.count(id); 25 return network_by_item_.count(id);
60} 26}
61 27
62int NetworkSet::GetNetworkWithItem(int id) const {
63 return network_by_item_.at(id);
64}
65
66const std::set<std::pair<int, int>>& NetworkSet::GetNetworkGraph(int id) const { 28const std::set<std::pair<int, int>>& NetworkSet::GetNetworkGraph(int id) const {
67 return networks_.at(id); 29 return network_by_item_.at(id);
68} 30}
diff --git a/src/network_set.h b/src/network_set.h index e0ef043..e6f0c07 100644 --- a/src/network_set.h +++ b/src/network_set.h
@@ -11,18 +11,15 @@ class NetworkSet {
11 public: 11 public:
12 void Clear(); 12 void Clear();
13 13
14 int AddLink(int id1, int id2); 14 void AddLink(int id1, int id2);
15 15
16 bool IsItemInNetwork(int id) const; 16 bool IsItemInNetwork(int id) const;
17 17
18 int GetNetworkWithItem(int id) const;
19
20 const std::set<std::pair<int, int>>& GetNetworkGraph(int id) const; 18 const std::set<std::pair<int, int>>& GetNetworkGraph(int id) const;
21 19
22 private: 20 private:
23 21
24 std::vector<std::set<std::pair<int, int>>> networks_; 22 std::map<int, std::set<std::pair<int, int>>> network_by_item_;
25 std::map<int, int> network_by_item_;
26}; 23};
27 24
28#endif /* end of include guard: NETWORK_SET_H_3036B8E3 */ 25#endif /* end of include guard: NETWORK_SET_H_3036B8E3 */
diff --git a/src/subway_map.cpp b/src/subway_map.cpp index d331b7d..99638aa 100644 --- a/src/subway_map.cpp +++ b/src/subway_map.cpp
@@ -112,9 +112,8 @@ void SubwayMap::OnPaint(wxPaintEvent &event) {
112 if (hovered_item_ && networks_.IsItemInNetwork(*hovered_item_)) { 112 if (hovered_item_ && networks_.IsItemInNetwork(*hovered_item_)) {
113 dc.SetBrush(*wxTRANSPARENT_BRUSH); 113 dc.SetBrush(*wxTRANSPARENT_BRUSH);
114 114
115 int network_id = networks_.GetNetworkWithItem(*hovered_item_);
116 for (const auto &[item_id1, item_id2] : 115 for (const auto &[item_id1, item_id2] :
117 networks_.GetNetworkGraph(network_id)) { 116 networks_.GetNetworkGraph(*hovered_item_)) {
118 const SubwayItem &item1 = GD_GetSubwayItem(item_id1); 117 const SubwayItem &item1 = GD_GetSubwayItem(item_id1);
119 const SubwayItem &item2 = GD_GetSubwayItem(item_id2); 118 const SubwayItem &item2 = GD_GetSubwayItem(item_id2);
120 119