From: Mikko Rasa Date: Fri, 29 Oct 2010 21:44:31 +0000 (+0000) Subject: Add Route::get_path to streamline route traversal X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=2707158feba5f9740d51618692914db05410f9a6;p=r2c2.git Add Route::get_path to streamline route traversal --- diff --git a/source/libmarklin/block.cpp b/source/libmarklin/block.cpp index b9b65ad..1519401 100644 --- a/source/libmarklin/block.cpp +++ b/source/libmarklin/block.cpp @@ -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); diff --git a/source/libmarklin/blockiter.cpp b/source/libmarklin/blockiter.cpp index 7618e4b..9615957 100644 --- a/source/libmarklin/blockiter.cpp +++ b/source/libmarklin/blockiter.cpp @@ -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::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)) diff --git a/source/libmarklin/route.h b/source/libmarklin/route.h index f8cf726..0b03637 100644 --- a/source/libmarklin/route.h +++ b/source/libmarklin/route.h @@ -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 &get_turnouts() const { return turnouts; } void add_track(Track &); void add_tracks(const std::set &); diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index 6962085..f901540 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -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));