X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Froute.cpp;fp=source%2Flibmarklin%2Froute.cpp;h=926dd28dd3a903ac468ea9f25f953c56b38f5b13;hb=f79b3fd58999a73a4a28663b46fdd96c58167a18;hp=d2ed3b7c6fba23d4d0d0b200d619bb61e09dda81;hpb=2073ac9922720867bdc6871d4a9b4cfcaf6a3b97;p=r2c2.git diff --git a/source/libmarklin/route.cpp b/source/libmarklin/route.cpp index d2ed3b7..926dd28 100644 --- a/source/libmarklin/route.cpp +++ b/source/libmarklin/route.cpp @@ -19,11 +19,11 @@ namespace { using namespace Marklin; -typedef std::pair Key; +typedef std::pair Key; struct Node { - const Track *track; + Track *track; unsigned ep; Node *prev; float dist; @@ -32,11 +32,11 @@ struct Node track(0), ep(0), prev(0), dist(0) { } - Node(const Track &t, unsigned e): + Node(Track &t, unsigned e): track(&t), ep(e), prev(0), dist(0) { } - Node(const Track &t, unsigned e, Node &r, float d): + Node(Track &t, unsigned e, Node &r, float d): track(&t), ep(e), prev(&r), dist(prev->dist+d) { } @@ -46,24 +46,24 @@ struct Node struct TrackMatch { - const Track &track; + Track &track; - TrackMatch(const Track &t): track(t) { } + TrackMatch(Track &t): track(t) { } - bool operator()(const Track &t) const { return &t==&track; } + bool operator()(Track &t) const { return &t==&track; } }; struct TrackInSet { - const set &tracks; + const set &tracks; - TrackInSet(const set &t): tracks(t) { } + TrackInSet(const set &t): tracks(t) { } - bool operator()(const Track &t) const { return tracks.count(&t); } + bool operator()(Track &t) const { return tracks.count(&t); } }; template -list dijkstra(const Track &from, unsigned ep, const Pred &goal) +list dijkstra(Track &from, unsigned ep, const Pred &goal) { map track_nodes; priority_queue nodes; @@ -112,7 +112,7 @@ list dijkstra(const Track &from, unsigned ep, const Pred &goal) if(!final) throw InvalidParameterValue("Could not find a route"); - list result; + list result; for(Node *node=final; node; node=node->prev) result.push_front(node->track); @@ -120,12 +120,12 @@ list dijkstra(const Track &from, unsigned ep, const Pred &goal) } template -Route *create_route(const Track &from, unsigned ep, const Pred &goal) +Route *create_route(Track &from, unsigned ep, const Pred &goal) { - list tracks = dijkstra(from, ep, goal); + list tracks = dijkstra(from, ep, goal); Route *route = new Route(from.get_layout()); - for(list::iterator i=tracks.begin(); i!=tracks.end(); ++i) + for(list::iterator i=tracks.begin(); i!=tracks.end(); ++i) route->add_track(**i); route->set_name("Pathfinder"); @@ -178,7 +178,7 @@ void Route::set_turnout(unsigned addr, unsigned path) void Route::update_turnouts() { set found; - for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) if(unsigned tid = (*i)->get_turnout_id()) { found.insert(tid); @@ -237,7 +237,7 @@ int Route::get_turnout(unsigned id) const return -1; } -void Route::add_track(const Track &trk) +void Route::add_track(Track &trk) { if(tracks.count(&trk)) return; @@ -257,17 +257,17 @@ void Route::add_track(const Track &trk) update_turnouts(); } -void Route::add_tracks(const set &trks) +void Route::add_tracks(const set &trks) { - set pending; - for(set::const_iterator i=trks.begin(); i!=trks.end(); ++i) + set pending; + for(set::const_iterator i=trks.begin(); i!=trks.end(); ++i) if(!tracks.count(*i)) pending.insert(*i); while(!pending.empty()) { bool found = false; - for(set::const_iterator i=pending.begin(); i!=pending.end(); ++i) + for(set::const_iterator i=pending.begin(); i!=pending.end(); ++i) if(tracks.empty() || check_validity(**i)==7) { tracks.insert(*i); @@ -283,9 +283,9 @@ void Route::add_tracks(const set &trks) update_turnouts(); } -void Route::add_track_chain(const Track &start, unsigned ep, const TurnoutMap &trnts) +void Route::add_track_chain(Track &start, unsigned ep, const TurnoutMap &trnts) { - const Track *track = &start; + Track *track = &start; while(1) { if(track->get_type().is_dead_end()) @@ -323,10 +323,10 @@ void Route::save(list &st) const st.push_back((DataFile::Statement("turnout"), i->first, i->second)); } -unsigned Route::check_validity(const Track &trk) const +unsigned Route::check_validity(Track &trk) const { unsigned result = 4; - for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) { int epi=(*i)->get_endpoint_by_link(trk); if(epi>=0) @@ -380,17 +380,17 @@ void Route::track_removed(Track &t) tracks.erase(&t); } -Route *Route::find(const Track &from, unsigned ep, const Track &to) +Route *Route::find(Track &from, unsigned ep, Track &to) { return create_route(from, ep, TrackMatch(to)); } -Route *Route::find(const Track &from, unsigned ep, const Route &to) +Route *Route::find(Track &from, unsigned ep, const Route &to) { return create_route(from, ep, TrackInSet(to.get_tracks())); } -Route *Route::find(const Track &from, unsigned ep, const set &to) +Route *Route::find(Track &from, unsigned ep, const set &to) { return create_route(from, ep, TrackInSet(to)); }