From 85decd2746c4c207161d4f71c4e28e74f8457aa1 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Fri, 2 Dec 2022 14:49:00 +0000 Subject: [PATCH] Refactor sorting path cache It'll be easier to experiment on with the cache storage abstracted away. --- src/api/sorting/plugin_graph.cpp | 22 ++++++++++------- src/api/sorting/plugin_graph.h | 42 ++++++++++++++++---------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/api/sorting/plugin_graph.cpp b/src/api/sorting/plugin_graph.cpp index 05231781..448d0c2e 100644 --- a/src/api/sorting/plugin_graph.cpp +++ b/src/api/sorting/plugin_graph.cpp @@ -115,8 +115,14 @@ std::string describeEdgeType(EdgeType edgeType) { } } -bool operator==(const GraphPath& lhs, const GraphPath& rhs) { - return lhs.from == rhs.from && lhs.to == rhs.to; +bool PathsCache::IsPathCached(const vertex_t& fromVertex, + const vertex_t& toVertex) const { + return pathsCache_.count({fromVertex, toVertex}); +} + +void PathsCache::CachePath(const vertex_t& fromVertex, + const vertex_t& toVertex) { + pathsCache_.insert({fromVertex, toVertex}); } size_t PluginGraph::CountVertices() const { @@ -306,7 +312,7 @@ void PluginGraph::CheckForCycles() const { bool PluginGraph::PathExists(const vertex_t& fromVertex, const vertex_t& toVertex) { - if (pathsCache_.count(GraphPath{fromVertex, toVertex}) != 0) { + if (pathsCache_.IsPathCached(fromVertex, toVertex)) { return true; } @@ -330,7 +336,7 @@ bool PluginGraph::PathExists(const vertex_t& fromVertex, for (const auto adjacentV : boost::make_iterator_range(boost::adjacent_vertices(v, graph_))) { if (forwardVisited.count(adjacentV) == 0) { - pathsCache_.insert(GraphPath{fromVertex, adjacentV}); + pathsCache_.CachePath(fromVertex, adjacentV); forwardVisited.insert(adjacentV); forwardQueue.push(adjacentV); @@ -346,7 +352,7 @@ bool PluginGraph::PathExists(const vertex_t& fromVertex, for (const auto adjacentV : boost::make_iterator_range( boost::inv_adjacent_vertices(v, graph_))) { if (reverseVisited.count(adjacentV) == 0) { - pathsCache_.insert(GraphPath{adjacentV, toVertex}); + pathsCache_.CachePath(adjacentV, toVertex); reverseVisited.insert(adjacentV); reverseQueue.push(adjacentV); @@ -361,9 +367,7 @@ bool PluginGraph::PathExists(const vertex_t& fromVertex, void PluginGraph::AddEdge(const vertex_t& fromVertex, const vertex_t& toVertex, EdgeType edgeType) { - const auto graphPath = GraphPath{fromVertex, toVertex}; - - if (pathsCache_.count(graphPath) != 0) { + if (pathsCache_.IsPathCached(fromVertex, toVertex)) { return; } @@ -376,7 +380,7 @@ void PluginGraph::AddEdge(const vertex_t& fromVertex, } boost::add_edge(fromVertex, toVertex, edgeType, graph_); - pathsCache_.insert(graphPath); + pathsCache_.CachePath(fromVertex, toVertex); } void PluginGraph::AddHardcodedPluginEdges(Game& game) { diff --git a/src/api/sorting/plugin_graph.h b/src/api/sorting/plugin_graph.h index e1147fc9..5591b5cc 100644 --- a/src/api/sorting/plugin_graph.h +++ b/src/api/sorting/plugin_graph.h @@ -39,6 +39,19 @@ #include "api/sorting/plugin_sorting_data.h" #include "loot/exception/cyclic_interaction_error.h" +namespace std { +template +struct hash> { + size_t operator()(const pair& pair) const { + size_t seed = 0; + boost::hash_combine(seed, pair.first); + boost::hash_combine(seed, pair.second); + + return seed; + } +}; +} + namespace loot { typedef boost::adjacency_list> std::string describeEdgeType(EdgeType edgeType); -struct GraphPath { - vertex_t from; - vertex_t to; +class PathsCache { +public: + bool IsPathCached(const vertex_t& fromVertex, const vertex_t& toVertex) const; + void CachePath(const vertex_t& fromVertex, const vertex_t& toVertex); + +private: + std::unordered_set> pathsCache_; }; -bool operator==(const GraphPath& lhs, const GraphPath& rhs); -} - -namespace std { -template<> -struct hash { - size_t operator()(const loot::GraphPath& graphPath) const { - size_t seed = 0; - boost::hash_combine(seed, graphPath.from); - boost::hash_combine(seed, graphPath.to); - - return seed; - } -}; -} - -namespace loot { class PluginGraph { public: size_t CountVertices() const; @@ -97,7 +97,7 @@ private: EdgeType edgeType); RawPluginGraph graph_; - std::unordered_set pathsCache_; + PathsCache pathsCache_; }; }