From a5a6c1b8b902c960f8204f8d814ce579dfd5fa50 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Wed, 15 May 2024 11:09:23 -0400 Subject: Hovering over a connection only shows nearest hops --- src/network_set.cpp | 58 +++++++++-------------------------------------------- src/network_set.h | 7 ++----- src/subway_map.cpp | 3 +-- 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 @@ #include "network_set.h" void NetworkSet::Clear() { - networks_.clear(); network_by_item_.clear(); } -int NetworkSet::AddLink(int id1, int id2) { +void NetworkSet::AddLink(int id1, int id2) { if (id2 > id1) { // Make sure id1 < id2 std::swap(id1, id2); } - if (network_by_item_.count(id1)) { - if (network_by_item_.count(id2)) { - int network_id1 = network_by_item_[id1]; - int network_id2 = network_by_item_[id2]; - - networks_[network_id1].emplace(id1, id2); - - if (network_id1 != network_id2) { - for (const auto& [other_id1, other_id2] : networks_[network_id2]) { - network_by_item_[other_id1] = network_id1; - network_by_item_[other_id2] = network_id1; - } - - networks_[network_id1].merge(networks_[network_id2]); - networks_[network_id2].clear(); - } - - return network_id1; - } else { - int network_id = network_by_item_[id1]; - network_by_item_[id2] = network_id; - networks_[network_id].emplace(id1, id2); - - return network_id; - } - } else { - if (network_by_item_.count(id2)) { - int network_id = network_by_item_[id2]; - network_by_item_[id1] = network_id; - networks_[network_id].emplace(id1, id2); - - return network_id; - } else { - int network_id = networks_.size(); - network_by_item_[id1] = network_id; - network_by_item_[id2] = network_id; - networks_.emplace_back(); - networks_[network_id] = {{id1, id2}}; - - return network_id; - } + if (!network_by_item_.count(id1)) { + network_by_item_[id1] = {}; } + if (!network_by_item_.count(id2)) { + network_by_item_[id2] = {}; + } + + network_by_item_[id1].insert({id1, id2}); + network_by_item_[id2].insert({id1, id2}); } bool NetworkSet::IsItemInNetwork(int id) const { return network_by_item_.count(id); } -int NetworkSet::GetNetworkWithItem(int id) const { - return network_by_item_.at(id); -} - const std::set>& NetworkSet::GetNetworkGraph(int id) const { - return networks_.at(id); + return network_by_item_.at(id); } 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 { public: void Clear(); - int AddLink(int id1, int id2); + void AddLink(int id1, int id2); bool IsItemInNetwork(int id) const; - int GetNetworkWithItem(int id) const; - const std::set>& GetNetworkGraph(int id) const; private: - std::vector>> networks_; - std::map network_by_item_; + std::map>> network_by_item_; }; #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) { if (hovered_item_ && networks_.IsItemInNetwork(*hovered_item_)) { dc.SetBrush(*wxTRANSPARENT_BRUSH); - int network_id = networks_.GetNetworkWithItem(*hovered_item_); for (const auto &[item_id1, item_id2] : - networks_.GetNetworkGraph(network_id)) { + networks_.GetNetworkGraph(*hovered_item_)) { const SubwayItem &item1 = GD_GetSubwayItem(item_id1); const SubwayItem &item2 = GD_GetSubwayItem(item_id2); -- cgit 1.4.1