mirror of
https://github.com/loot/libloot.git
synced 2026-07-27 14:16:01 -07:00
Refactor sorting path cache
It'll be easier to experiment on with the cache storage abstracted away.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -39,6 +39,19 @@
|
||||
#include "api/sorting/plugin_sorting_data.h"
|
||||
#include "loot/exception/cyclic_interaction_error.h"
|
||||
|
||||
namespace std {
|
||||
template<typename A, typename B>
|
||||
struct hash<pair<A, B>> {
|
||||
size_t operator()(const pair<A, B>& 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<boost::listS,
|
||||
boost::listS,
|
||||
@@ -52,28 +65,15 @@ typedef boost::associative_property_map<std::map<vertex_t, size_t>>
|
||||
|
||||
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<std::pair<vertex_t, vertex_t>> pathsCache_;
|
||||
};
|
||||
|
||||
bool operator==(const GraphPath& lhs, const GraphPath& rhs);
|
||||
}
|
||||
|
||||
namespace std {
|
||||
template<>
|
||||
struct hash<loot::GraphPath> {
|
||||
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<GraphPath> pathsCache_;
|
||||
PathsCache pathsCache_;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user