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

Printing places

parent 9346ca7b
......@@ -49,6 +49,18 @@ Loco::GlobalIdent::PrettyPrint(std::ostream &os, const std::string &sep) const
return os << mRegion << sep << mIdent;
}
std::string
Loco::GlobalIdent::IdentString() const
{
return mRegion + kIdentSep + mIdent;
}
std::string
Loco::GlobalIdent::IdentStringWithHint(const std::string &region_hint) const
{
return (mRegion == region_hint ? mIdent : IdentString());
}
Loco::TimeSpec
Loco::parse_time_spec(const std::string &ts)
{
......
......@@ -16,20 +16,22 @@ Loco::SectionGroupPlace::SectionGroupPlace(
{
}
std::string
Loco::SectionGroupPlace::Desc() const
std::ostream &
Loco::SectionGroupPlace::Print(std::ostream &os,
const std::string &prefix) const
{
auto desc = kPrefix + "{";
os << prefix << "[" << Ident() << "]" << std::endl;
if (!mSections.empty())
{
desc += mSections.front()->Ident().IdentString();
std::for_each(mSections.begin() + 1, mSections.end(),
[&desc](const auto &sect) {
desc += kArraySep + sect->Ident().IdentString();
});
os << prefix << kTabString << "sections = ";
os << mSections.front()->Ident().IdentString();
std::for_each(
mSections.begin() + 1, mSections.end(), [&os](const auto &sect) {
os << " " << kArraySep << " " << sect->Ident().IdentString();
});
os << std::endl;
}
desc += "}";
return desc;
return os;
}
Loco::TrainPlace::TrainPlace(bool enter, unsigned train_id, TrainConstPtr train)
......
......@@ -33,7 +33,7 @@ namespace Loco
SectionGroupPlace(unsigned sg_id, std::vector<SectionConstPtr> sections);
std::string Ident() const;
std::string Desc() const;
std::ostream &Print(std::ostream &os, const std::string &prefix) const;
const std::vector<SectionConstPtr> &Sections() const;
const SectionConstPtr &FrontSection() const;
......
......@@ -23,6 +23,7 @@ Loco::SectionGraph::Build(const InputData &data,
BuildConflicts();
BuildNeighbours();
BuildConflictGroups();
BuildPlaces();
// Find conflicts:
// - for each node and point collect the sections it is in
// - points give incidence
......@@ -97,23 +98,7 @@ Loco::SectionGraph::PrintSectionInfo(std::ostream &os) const
{
mSections[i].Print(os, i);
}
os << "[[conflict groups]]" << std::endl;
for (unsigned i = 0; i < mConflictGroups.size(); ++i)
{
if (!mConflictGroups[i].empty())
{
os << kTabString << "[Group " << i << "]" << std::endl;
os << kTabString << kTabString << "sections = "
<< mSections[mConflictGroups[i].front()].mSection->Ident();
for (auto i_sect_id = mConflictGroups[i].begin() + 1;
i_sect_id != mConflictGroups[i].end(); ++i_sect_id)
{
os << " " << kArraySep << " "
<< mSections[*i_sect_id].mSection->Ident();
}
os << std::endl;
}
}
PrintPlaceDesc(os);
return os;
}
......@@ -219,17 +204,83 @@ Loco::SectionGraph::ChangeIdOfConflictGroup(unsigned old_id, unsigned new_id)
void
Loco::SectionGraph::CompressConflictGroups()
{
unsigned last = 0;
unsigned next_id = 0;
for (unsigned i = 0; i < mConflictGroups.size(); ++i)
{
if (!mConflictGroups[i].empty())
{
if (last < i)
if (next_id < i)
{
ChangeIdOfConflictGroup(i, last);
ChangeIdOfConflictGroup(i, next_id);
}
++last;
++next_id;
}
}
mConflictGroups.resize(last + 1);
mConflictGroups.resize(next_id);
}
void
Loco::SectionGraph::BuildPlaces()
{
mPlaces.clear();
mPlaces.reserve(mConflictGroups.size());
for (unsigned cg_id = 0; cg_id < mConflictGroups.size(); ++cg_id)
{
std::vector<SectionConstPtr> sections;
for (const auto &sect_id : mConflictGroups[cg_id])
{
sections.push_back(mSections[sect_id].mSection);
}
mPlaces.push_back(
std::make_shared<SectionGroupPlace>(cg_id, std::move(sections)));
}
}
std::ostream &
Loco::SectionGraph::PrintPlaceIdents(std::ostream &os,
const std::string &sep) const
{
if (mPlaces.empty())
{
return os;
}
os << mPlaces.front()->Ident();
std::for_each(
mPlaces.begin() + 1, mPlaces.end(),
[&os, &sep](const auto &place) { os << sep << place->Ident(); });
return os;
}
std::ostream &
Loco::SectionGraph::PrintPlaceDesc(std::ostream &os) const
{
os << "[[places]]" << std::endl;
for (const auto &place : mPlaces)
{
place->Print(os, kTabString);
}
return os;
}
std::ostream &
Loco::SectionGraph::PrintConflictGroups(std::ostream &os) const
{
os << "[[conflict groups]]" << std::endl;
for (unsigned i = 0; i < mConflictGroups.size(); ++i)
{
if (!mConflictGroups[i].empty())
{
os << kTabString << "[Group " << i << "]" << std::endl;
os << kTabString << kTabString << "sections = "
<< mSections[mConflictGroups[i].front()].mSection->Ident();
for (auto i_sect_id = mConflictGroups[i].begin() + 1;
i_sect_id != mConflictGroups[i].end(); ++i_sect_id)
{
os << " " << kArraySep << " "
<< mSections[*i_sect_id].mSection->Ident();
}
os << std::endl;
}
}
return os;
}
......@@ -14,6 +14,7 @@
#define __SECTION_GRAPH_H
#include "logical_graph.h"
#include "places.h"
namespace Loco
{
......@@ -50,6 +51,7 @@ namespace Loco
std::vector<std::vector<unsigned>> mConflictGroups;
std::unordered_map<NodeConstPtr, std::vector<unsigned>> mNodeSections;
std::unordered_map<PointConstPtr, std::vector<unsigned>> mPointSections;
std::vector<SGPlaceConstPtr> mPlaces;
void AddInfoForSection(const LogicalGraph &logical_graph,
SectionConstPtr sect);
......@@ -69,6 +71,7 @@ namespace Loco
void UnifyConflictGroups(const std::vector<unsigned> &sects);
void ChangeIdOfConflictGroup(unsigned old_id, unsigned new_id);
void CompressConflictGroups();
void BuildPlaces();
std::ostream &PrintVertices(std::ostream &os) const;
public:
......@@ -79,6 +82,12 @@ namespace Loco
std::ostream &PrintGraph(std::ostream &os) const;
std::ostream &PrintSectionInfo(std::ostream &os) const;
const std::vector<SGPlaceConstPtr> &Places() const;
std::ostream &PrintPlaceIdents(std::ostream &os,
const std::string &sep) const;
std::ostream &PrintPlaceDesc(std::ostream &os) const;
std::ostream &PrintConflictGroups(std::ostream &os) const;
};
} // namespace Loco
......@@ -111,4 +120,10 @@ Loco::SectionGraph::BuildPairs(
}
}
inline auto
Loco::SectionGraph::Places() const -> const std::vector<SGPlaceConstPtr> &
{
return mPlaces;
}
#endif
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