Commit 25e59bb3 authored by Kučera Petr RNDr. Ph.D.'s avatar Kučera Petr RNDr. Ph.D.
Browse files

Subgraph between traffic points

parent 002269fe
......@@ -292,14 +292,6 @@ Loco::SectionGraph::PrintConflictGroups(std::ostream &os) const
return os;
}
auto
Loco::SectionGraph::GetSubgraph(const std::vector<SectionCPtr> &sources,
const std::vector<SectionCPtr> &targets) const
-> Subgraph
{
return GetSubgraphWithIds(SectionIds(sources), SectionIds(targets));
}
std::vector<unsigned>
Loco::SectionGraph::SectionIds(const std::vector<SectionCPtr> &sections) const
{
......@@ -312,13 +304,15 @@ Loco::SectionGraph::SectionIds(const std::vector<SectionCPtr> &sections) const
}
auto
Loco::SectionGraph::GetSubgraphWithIds(std::vector<unsigned> sources,
Loco::SectionGraph::NewSubgraphWithIds(std::vector<unsigned> sources,
std::vector<unsigned> targets) const
-> Subgraph
-> SGSubgraphCPtr
{
if (sources.empty() || targets.empty())
{
return {sources, targets, {}};
return std::make_shared<const SGSubgraph>(
sources, targets,
std::unordered_map<unsigned, SGSubgraph::NodeInfo>());
}
// Run BFS from sources,
// Mark the nodes which have path from the source (CLOSED and TERMINAL at the
......@@ -386,7 +380,7 @@ Loco::SectionGraph::GetSubgraphWithIds(std::vector<unsigned> sources,
// Prune the pred and succ nodes which are not reachable from both sides
// Nodes which are either sources or targets or have pred and succ nonempty
// are the nodes of the subgraphs
std::unordered_map<unsigned, Subgraph::NodeInfo> node_info;
std::unordered_map<unsigned, SGSubgraph::NodeInfo> node_info;
// prune the predecessors
auto unreachable = [&state, &back_state](unsigned i) {
......@@ -431,6 +425,22 @@ Loco::SectionGraph::GetSubgraphWithIds(std::vector<unsigned> sources,
node_info.insert({i, {std::move(succ[i]), std::move(pred[i])}});
}
}
return {sources, targets, node_info};
return std::make_shared<const SGSubgraph>(sources, targets, node_info);
}
auto
Loco::SectionGraph::Subgraph(const TrafficPointCPtr &source,
const TrafficPointCPtr &target) const
-> SGSubgraphCPtr
{
auto key = std::make_pair(source, target);
auto i_subg = mSubgraphs.find(key);
if (i_subg != mSubgraphs.end())
{
return i_subg->second;
}
auto subg = NewSubgraphWithIds(SectionIds(source->Sections()),
SectionIds(target->Sections()));
mSubgraphs.insert({std::move(key), subg});
return subg;
}
......@@ -13,6 +13,8 @@
#ifndef __SECTION_GRAPH_H
#define __SECTION_GRAPH_H
#include <map>
#include "logical_graph.h"
#include "places.h"
......@@ -22,6 +24,23 @@ namespace Loco
// Two sections are conflicting if they share a physical point
// Conflicting sections are grouped together into a meta section
struct SGSubgraph
{
struct NodeInfo
{
std::vector<unsigned> mSucc;
std::vector<unsigned> mPred;
};
std::vector<unsigned> mSources;
std::vector<unsigned> mTargets;
std::unordered_map<unsigned, NodeInfo> mSections;
SGSubgraph(std::vector<unsigned> sources, std::vector<unsigned> targets,
std::unordered_map<unsigned, NodeInfo> sections);
};
using SGSubgraphCPtr = std::shared_ptr<const SGSubgraph>;
class SectionGraph
{
public:
......@@ -46,22 +65,6 @@ namespace Loco
std::ostream &Print(std::ostream &os, unsigned id) const;
};
struct Subgraph
{
struct NodeInfo
{
std::vector<unsigned> mSucc;
std::vector<unsigned> mPred;
};
std::vector<unsigned> mSources;
std::vector<unsigned> mTargets;
std::unordered_map<unsigned, NodeInfo> mSections;
Subgraph(std::vector<unsigned> sources, std::vector<unsigned> targets,
std::unordered_map<unsigned, NodeInfo> mSections);
};
private:
std::vector<SectionInfo> mSections;
std::unordered_map<SectionCPtr, unsigned> mSectionId;
......@@ -69,6 +72,9 @@ namespace Loco
std::unordered_map<NodeCPtr, std::vector<unsigned>> mNodeSections;
std::unordered_map<PointCPtr, std::vector<unsigned>> mPointSections;
std::vector<SGPlaceCPtr> mPlaces;
mutable std::map<std::pair<TrafficPointCPtr, TrafficPointCPtr>,
SGSubgraphCPtr>
mSubgraphs;
void AddInfoForSection(const LogicalGraph &logical_graph,
SectionCPtr sect);
......@@ -93,10 +99,8 @@ namespace Loco
std::vector<unsigned>
SectionIds(const std::vector<SectionCPtr> &sect) const;
std::ostream &PrintVertices(std::ostream &os) const;
Subgraph GetSubgraphWithIds(std::vector<unsigned> sources,
std::vector<unsigned> targets) const;
Subgraph GetSubgraph(const std::vector<SectionCPtr> &sources,
const std::vector<SectionCPtr> &targets) const;
SGSubgraphCPtr NewSubgraphWithIds(std::vector<unsigned> sources,
std::vector<unsigned> targets) const;
public:
SectionGraph(const InputData &data, const LogicalGraph &logical_graph)
......@@ -111,10 +115,12 @@ namespace Loco
const std::string &sep) const;
std::ostream &PrintPlaceDesc(std::ostream &os) const;
std::ostream &PrintConflictGroups(std::ostream &os) const;
SGSubgraphCPtr Subgraph(const TrafficPointCPtr &source,
const TrafficPointCPtr &target) const;
};
using SectionGraphCPtr = std::shared_ptr<const SectionGraph>;
using SGSubgraphCPtr = std::shared_ptr<const SectionGraph::Subgraph>;
} // namespace Loco
......@@ -147,4 +153,12 @@ Loco::SectionGraph::BuildPairs(
}
}
inline Loco::SGSubgraph::SGSubgraph(
std::vector<unsigned> sources, std::vector<unsigned> targets,
std::unordered_map<unsigned, NodeInfo> sections)
: mSources(std::move(sources)), mTargets(std::move(targets)),
mSections(std::move(sections))
{
}
#endif
......@@ -39,6 +39,12 @@ namespace Loco
mSections(std::move(sections))
{
}
const std::vector<SectionCPtr>
Sections() const
{
return mSections;
}
};
using TrafficPointCPtr = std::shared_ptr<const TrafficPoint>;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment