From 7a07398275abf496e3bf30d0097f8c263b398cc4 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Fri, 12 Aug 2011 18:58:07 +0100 Subject: [PATCH] Further track graph work in progress --- src/TrackGraph.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/TrackGraph.cpp b/src/TrackGraph.cpp index 95212ba..f0d805e 100644 --- a/src/TrackGraph.cpp +++ b/src/TrackGraph.cpp @@ -17,6 +17,7 @@ #include "ITrackGraph.hpp" #include "ILogger.hpp" +#include "IterateTrack.hpp" #include @@ -24,12 +25,76 @@ class TrackGraph : public ITrackGraph { public: TrackGraph(IMapPtr map); + // ITrackGraph interface void write_dot_file(const string& file) const; + const graph::Node& root() const; + const graph::Node& node(unsigned n) const; + +private: + void walk_track(TrackIterator it, + set& visited, + graph::Node& where); + graph::Node& add_node(graph::NodeType type); + + vector nodes; }; TrackGraph::TrackGraph(IMapPtr map) { + track::Position p; + track::Direction d; + tie(p, d) = map->start(); + + graph::Node& root = add_node(graph::NODE_ROOT); + + set visited; + walk_track(iterate_track(map, p, d), visited, root); +} + +void TrackGraph::walk_track(TrackIterator it, + set& visited, + graph::Node& where) +{ + if (visited.find(it.track) != visited.end()) { + // TODO: connect up with arc + } + + visited.insert(it.track); + + switch (it.status) { + case TRACK_STATION: + // TODO + case TRACK_OK: + walk_track(it.next(), visited, where); + break; + + case TRACK_NO_MORE: + break; + + case TRACK_CHOICE: + throw runtime_error("can't graph track with choice yet!"); + + } +} + +graph::Node& TrackGraph::add_node(graph::NodeType type) +{ + unsigned id = nodes.size(); + graph::Node n = { id, type }; + nodes.push_back(n); + return nodes.at(id); +} +const graph::Node& TrackGraph::root() const +{ + assert(nodes.size() > 0); + return nodes.at(0); +} + +const graph::Node& TrackGraph::node(unsigned n) const +{ + assert(n < nodes.size()); + return nodes.at(n); } void TrackGraph::write_dot_file(const string& file) const -- 2.39.2