]> git.tdb.fi Git - r2c2.git/commitdiff
Add Route::get_path to streamline route traversal
authorMikko Rasa <tdb@tdb.fi>
Fri, 29 Oct 2010 21:44:31 +0000 (21:44 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 29 Oct 2010 21:44:31 +0000 (21:44 +0000)
source/libmarklin/block.cpp
source/libmarklin/blockiter.cpp
source/libmarklin/route.cpp
source/libmarklin/route.h
source/libmarklin/train.cpp

index b9b65ad710dca9dd833177ee993b31562c3c4b5b..1519401220edf59481427c68621985c0cf528f69 100644 (file)
@@ -103,12 +103,7 @@ float Block::get_path_length(unsigned entry, const Route *route) const
        float result = 0;
        while(t_iter && has_track(*t_iter))
        {
-               int path = -1;
-               if(t_iter->get_turnout_id() && route)
-                       path = route->get_turnout(t_iter->get_turnout_id());
-               if(path==-1)
-                       path = t_iter->get_active_path();
-
+               unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
                result += t_iter->get_type().get_path_length(path);
 
                t_iter = t_iter.next(path);
index 7618e4b15943dd39ba044bdc90f948a13d2fceaa..96159577cb6c488aa062ba33298f0499075cddfd 100644 (file)
@@ -48,19 +48,14 @@ int BlockIter::get_exit(const Route *route) const
                if(!_block->has_track(*t_iter))
                        throw LogicError("Block traversal strayed out of the block");
 
-               int path = -1;
-               if(t_iter->get_turnout_id() && route)
-                       path = route->get_turnout(t_iter->get_turnout_id());
-               if(path==-1)
-                       path = t_iter->get_active_path();
-
+               unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
                TrackIter t_exit = t_iter.reverse(path);
 
                for(unsigned i=0; i<eps.size(); ++i)
                        if(eps[i].track==t_exit.track() && eps[i].track_ep==t_exit.entry())
                                return i;
 
-               t_iter = t_iter.next(path);
+               t_iter = t_exit.flip();
        }
 
        return -1;
index 556cbf4014c57d474a5e7e604c01f08c7f1a5352..663bd8847c08f2467c1d9376b8900271bcba7428 100644 (file)
@@ -230,6 +230,17 @@ int Route::get_turnout(unsigned id) const
        return -1;
 }
 
+unsigned Route::get_path(Track &trk) const
+{
+       if(unsigned tid = trk.get_turnout_id())
+       {
+               map<unsigned, int>::const_iterator i = turnouts.find(tid);
+               if(i!=turnouts.end())
+                       return i->second;
+       }
+       return trk.get_active_path();
+}
+
 void Route::add_track(Track &trk)
 {
        if(tracks.count(&trk))
index f8cf72628ec5e1e17df0e1903835100dc1fa717a..0b036371e2cf29bb922249015e8dad9f6f265440 100644 (file)
@@ -57,6 +57,7 @@ public:
        void set_turnout(unsigned, unsigned);
        void update_turnouts();
        int get_turnout(unsigned) const;
+       unsigned get_path(Track &) const;
        const std::map<unsigned, int> &get_turnouts() const { return turnouts; }
        void add_track(Track &);
        void add_tracks(const std::set<Track *> &);
index 69620857a5064110d4d2a409d3e554cf365166f8..f901540528a9afa3e14674361d58de3e4d967cd8 100644 (file)
@@ -301,8 +301,7 @@ bool Train::divert(Track &from)
                        break;
                }
 
-               unsigned tid = track->get_turnout_id();
-               track = track.next(tid ? route->route->get_turnout(tid) : 0);
+               track = track.next(route->route->get_path(*track));
 
                if(!track || track.looped())
                        return false;
@@ -339,8 +338,7 @@ bool Train::divert(Track &from)
                else if(!diversion->has_track(*track))
                        throw LogicError("Pathfinder returned a bad route");
 
-               unsigned tid = track->get_turnout_id();
-               track = track.next(tid ? diversion->get_turnout(tid) : 0);
+               track = track.next(diversion->get_path(*track));
        }
 
        if(route==end)
@@ -1274,8 +1272,7 @@ bool Train::is_valid_diversion(const Route &diversion, const TrackIter &from)
        TrackLoopIter track1 = from;
        while(diversion.has_track(*track1))
        {
-               unsigned tid = track1->get_turnout_id();
-               unsigned path = (tid ? diversion.get_turnout(tid) : 0);
+               unsigned path = diversion.get_path(*track1);
                diversion_len += track1->get_type().get_path_length(path);
 
                track1 = track1.next(path);
@@ -1292,8 +1289,7 @@ bool Train::is_valid_diversion(const Route &diversion, const TrackIter &from)
        TrackLoopIter track2 = from;
        while(1)
        {
-               unsigned tid = track2->get_turnout_id();
-               unsigned path = (tid ? route->route->get_turnout(tid) : 0);
+               unsigned path = route->route->get_path(*track2);
                route_len += track2->get_type().get_path_length(path);
 
                bool ok = (track2!=from && diversion.has_track(*track2));