struct Node
{
- Track *track;
- unsigned ep;
+ TrackIter track;
Node *prev;
float dist;
Node():
- track(0), ep(0), prev(0), dist(0)
+ prev(0), dist(0)
{ }
- Node(Track &t, unsigned e):
- track(&t), ep(e), prev(0), dist(0)
+ Node(const TrackIter &t):
+ track(t), prev(0), dist(0)
{ }
- Node(Track &t, unsigned e, Node &r, float d):
- track(&t), ep(e), prev(&r), dist(prev->dist+d)
+ Node(const TrackIter &t, Node &r, float d):
+ track(t), prev(&r), dist(prev->dist+d)
{ }
bool operator<(const Node &other) const
};
template<typename Pred>
-list<Track *> dijkstra(Track &from, unsigned ep, const Pred &goal)
+list<Track *> dijkstra(const TrackIter &from, const Pred &goal)
{
map<Key, Node> track_nodes;
priority_queue<Node> nodes;
Node *final = 0;
- nodes.push(Node(from, ep));
+ nodes.push(from);
while(!nodes.empty())
{
Node lowest = nodes.top();
nodes.pop();
- Key key(lowest.track, lowest.ep);
+ Key key(lowest.track.track(), lowest.track.entry());
if(track_nodes.count(key))
continue;
break;
}
- const TrackType &type = lowest.track->get_type();
- const vector<TrackType::Endpoint> &eps = type.get_endpoints();
- const vector<Track *> &links = lowest.track->get_links();
- for(unsigned i=0; i<eps.size(); ++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 paths = lowest.track->get_type().get_endpoints()[lowest.track.entry()].paths;
+ for(unsigned i=0; paths>>i; ++i)
+ if(paths&(1<<i))
+ {
+ TrackIter next = lowest.track.next(i);
+ if(!next)
+ continue;
- unsigned mask = eps[i].paths&eps[lowest.ep].paths;
- if(!mask)
- continue;
+ if(track_nodes.count(Key(next.track(), next.entry())))
+ continue;
- unsigned path=0;
- for(; !(mask&1); ++path, mask>>=1) ;
- nodes.push(Node(*links[i], links[i]->get_endpoint_by_link(*lowest.track), ref, type.get_path_length(path)));
- }
+ nodes.push(Node(next, ref, lowest.track->get_type().get_path_length(i)));
+ }
}
list<Track *> result;
for(Node *node=final; node; node=node->prev)
- result.push_front(node->track);
+ result.push_front(&*node->track);
return result;
}
template<typename Pred>
-Route *create_route(Track &from, unsigned ep, const Pred &goal)
+Route *create_route(const TrackIter &from, const Pred &goal)
{
- list<Track *> tracks = dijkstra(from, ep, goal);
+ list<Track *> tracks = dijkstra(from, goal);
if(tracks.empty())
return 0;
- Route *route = new Route(from.get_layout());
+ Route *route = new Route(from->get_layout());
for(list<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
route->add_track(**i);
tracks.erase(&t);
}
-Route *Route::find(Track &from, unsigned ep, Track &to)
+Route *Route::find(const TrackIter &from, Track &to)
{
- return create_route(from, ep, TrackMatch(to));
+ return create_route(from, TrackMatch(to));
}
-Route *Route::find(Track &from, unsigned ep, const Route &to)
+Route *Route::find(const TrackIter &from, const Route &to)
{
- return create_route(from, ep, TrackInSet(to.get_tracks()));
+ return create_route(from, TrackInSet(to.get_tracks()));
}
-Route *Route::find(Track &from, unsigned ep, const set<Track *> &to)
+Route *Route::find(const TrackIter &from, const set<Track *> &to)
{
- return create_route(from, ep, TrackInSet(to));
+ return create_route(from, TrackInSet(to));
}
TrackIter next = (rsv_blocks.empty() ? cur_blocks : rsv_blocks).back().next().track_iter();
if(!r->has_track(*next))
{
- lead = Route::find(*next, next.entry(), *r);
+ lead = Route::find(next, *r);
if(!lead)
return false;
create_lead_route(lead, lead);
TrackIter next = (rsv_blocks.empty() ? cur_blocks : rsv_blocks).back().next().track_iter();
- Route *route = Route::find(*next, next.entry(), to);
+ Route *route = Route::find(next, to);
if(!route)
return false;
create_lead_route(route, route);
if(!track)
return false;
- unsigned ep = track->get_endpoint_by_link(from);
-
set<Track *> tracks;
for(list<RouteRef>::iterator i=routes.begin(); i!=routes.end(); ++i)
tracks.insert(i->route->get_tracks().begin(), i->route->get_tracks().end());
- RefPtr<Route> diversion = Route::find(*track, ep, tracks);
+ RefPtr<Route> diversion = Route::find(track, tracks);
if(!diversion)
return false;