using namespace Marklin;
+typedef std::pair<const Track *, unsigned> Key;
+
struct Node
{
const Track *track;
TrackMatch(const Track &t): track(t) { }
- bool operator()(const Track &t) { return &t==&track; }
+ bool operator()(const Track &t) const { return &t==&track; }
+};
+
+struct TrackInRoute
+{
+ const Route &route;
+
+ TrackInRoute(const Route &r): route(r) { }
+
+ bool operator()(const Track &t) const { return route.get_tracks().count(&t); }
};
template<typename Pred>
-list<const Track *> dijkstra(const Track &from, unsigned ep, Pred goal)
+list<const Track *> dijkstra(const Track &from, unsigned ep, const Pred &goal)
{
- // XXX Fails to find some routes - should use track+ep as key
- map<const Track *, Node> track_nodes;
+ map<Key, Node> track_nodes;
priority_queue<Node> nodes;
Node *final = 0;
Node lowest = nodes.top();
nodes.pop();
- if(track_nodes.count(lowest.track))
+ Key key(lowest.track, lowest.ep);
+ if(track_nodes.count(key))
continue;
- Node &ref = track_nodes[lowest.track] = lowest;
+ Node &ref = track_nodes[key] = lowest;
if(goal(*lowest.track))
{
final = &ref;
const vector<Track *> &links = lowest.track->get_links();
for(unsigned i=0; i<eps.size(); ++i)
{
- if(i==lowest.ep || !links[i] || track_nodes.count(links[i]))
+ if(i==lowest.ep || !links[i])
+ continue;
+
+ unsigned link_ep = links[i]->get_endpoint_by_link(*lowest.track);
+ if(track_nodes.count(Key(links[i], link_ep)))
continue;
unsigned mask = eps[i].paths&eps[lowest.ep].paths;
return result;
}
+unsigned count = 0;
+
+template<typename Pred>
+Route *create_route(const Track &from, unsigned ep, const Pred &goal)
+{
+ list<const Track *> tracks = dijkstra(from, ep, goal);
+
+ Route *route = new Route(from.get_layout(), format("-%d-", ++count));
+ for(list<const Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ route->add_track(**i);
+
+ route->set_temporary(true);
+
+ return route;
+}
+
}
Route::Route(Layout &l, const string &n):
layout(l),
- name(n)
+ name(n),
+ temporary(false)
{
layout.add_route(*this);
layout.signal_track_removed.connect(sigc::mem_fun(this, &Route::track_removed));
layout.remove_route(*this);
}
+void Route::set_temporary(bool t)
+{
+ temporary = t;
+}
+
int Route::get_turnout(unsigned id) const
{
map<unsigned, int>::const_iterator i = turnouts.find(id);
Route *Route::find(const Track &from, unsigned ep, const Track &to)
{
- TrackMatch goal(to);
- list<const Track *> tracks = dijkstra(from, ep, goal);
-
- static unsigned n = 0;
- Route *route = new Route(from.get_layout(), format("Route::find %d", ++n));
- for(list<const Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
- route->add_track(**i);
+ return create_route(from, ep, TrackMatch(to));
+}
- return route;
+Route *Route::find(const Track &from, unsigned ep, const Route &to)
+{
+ return create_route(from, ep, TrackInRoute(to));
}