]> git.tdb.fi Git - r2c2.git/commitdiff
Make the pathfinder use TrackIter
authorMikko Rasa <tdb@tdb.fi>
Fri, 29 Oct 2010 17:57:01 +0000 (17:57 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 29 Oct 2010 17:57:01 +0000 (17:57 +0000)
source/libmarklin/route.cpp
source/libmarklin/route.h
source/libmarklin/train.cpp

index daa044f9b9b4054b774421df3e6ada5168f76c43..556cbf4014c57d474a5e7e604c01f08c7f1a5352 100644 (file)
@@ -24,21 +24,20 @@ typedef std::pair<Track *, unsigned> Key;
 
 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
@@ -64,20 +63,20 @@ struct TrackInSet
 };
 
 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;
 
@@ -88,44 +87,37 @@ list<Track *> dijkstra(Track &from, unsigned ep, const Pred &goal)
                        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);
 
@@ -380,19 +372,19 @@ void Route::track_removed(Track &t)
        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));
 }
 
 
index 75241d8b604bea37c278e30f45e9894f2f867476..f8cf72628ec5e1e17df0e1903835100dc1fa717a 100644 (file)
@@ -18,6 +18,7 @@ namespace Marklin {
 
 class Layout;
 class Track;
+class TrackIter;
 
 class Route: public sigc::trackable
 {
@@ -68,9 +69,9 @@ private:
        void track_removed(Track &);
 
 public:
-       static Route *find(Track &, unsigned, Track &);
-       static Route *find(Track &, unsigned, const Route &);
-       static Route *find(Track &, unsigned, const std::set<Track *> &);
+       static Route *find(const TrackIter &, Track &);
+       static Route *find(const TrackIter &, const Route &);
+       static Route *find(const TrackIter &, const std::set<Track *> &);
 };
 
 } // namespace Marklin
index 6ab617ec1bbba2da119a58fc0689b017208ba192..b87447f625fa31a897769987140d8da44b6e0c74 100644 (file)
@@ -215,7 +215,7 @@ bool Train::set_route(const Route *r)
                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);
@@ -252,7 +252,7 @@ bool Train::go_to(Track &to)
 
        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);
@@ -314,12 +314,10 @@ bool Train::divert(Track &from)
        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;