From 025c23f199c411cc2ec1a6d2e85bf24460150ceb Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 26 Dec 2009 19:36:38 +0000 Subject: [PATCH] Store routes in a map by name rather than a set --- source/designer/toolbar.cpp | 20 ++++++++++---------- source/engineer/routeselect.cpp | 14 +++++++------- source/libmarklin/layout.cpp | 30 ++++++++++++++++-------------- source/libmarklin/layout.h | 4 ++-- source/network/server.cpp | 6 +++--- 5 files changed, 38 insertions(+), 36 deletions(-) diff --git a/source/designer/toolbar.cpp b/source/designer/toolbar.cpp index 12c8388..fd3c116 100644 --- a/source/designer/toolbar.cpp +++ b/source/designer/toolbar.cpp @@ -68,33 +68,33 @@ void Toolbar::route_selected(unsigned index, const string &) designer.get_layout()->add_route(*route); designer.edit_route(*route); - const set &routes = designer.get_layout()->get_routes(); + const map &routes = designer.get_layout()->get_routes(); int selected = -1; unsigned n = 0; - for(set::const_iterator i=routes.begin(); (selected<0 && i!=routes.end()); ++i, ++n) - if(*i==route) + for(map::const_iterator i=routes.begin(); (selected<0 && i!=routes.end()); ++i, ++n) + if(i->second==route) selected = n; drp_routes->set_selected_index(selected); } else { - const set &routes = designer.get_layout()->get_routes(); - set::const_iterator i = routes.begin(); + const map &routes = designer.get_layout()->get_routes(); + map::const_iterator i = routes.begin(); advance(i, index); - designer.edit_route(**i); + designer.edit_route(*i->second); } } void Toolbar::update_routes() { drp_routes->clear(); - const set &routes = designer.get_layout()->get_routes(); + const map &routes = designer.get_layout()->get_routes(); int selected = -1; unsigned n = 0; - for(set::const_iterator i=routes.begin(); i!=routes.end(); ++i, ++n) + for(map::const_iterator i=routes.begin(); i!=routes.end(); ++i, ++n) { - drp_routes->append((*i)->get_name()); - if(*i==designer.get_current_route()) + drp_routes->append(i->second->get_name()); + if(i->second==designer.get_current_route()) selected = n; } drp_routes->append("(new route)"); diff --git a/source/engineer/routeselect.cpp b/source/engineer/routeselect.cpp index 8b526ae..3413220 100644 --- a/source/engineer/routeselect.cpp +++ b/source/engineer/routeselect.cpp @@ -30,12 +30,12 @@ RouteSelect::RouteSelect(Engineer &e, const GLtk::Resources &r, Train &t): drp_route->set_geometry(GLtk::Geometry(10, geom.h-50, geom.w-20, 20)); drp_route->append("(none)"); drp_route->set_selected_index(0); - const set &routes = engineer.get_layout().get_routes(); + const map &routes = engineer.get_layout().get_routes(); unsigned n = 1; - for(set::const_iterator i=routes.begin(); i!=routes.end(); ++i, ++n) + for(map::const_iterator i=routes.begin(); i!=routes.end(); ++i, ++n) { - drp_route->append((*i)->get_name()); - if(*i==train.get_route()) + drp_route->append(i->second->get_name()); + if(i->second==train.get_route()) drp_route->set_selected_index(n); } } @@ -44,11 +44,11 @@ void RouteSelect::on_ok_clicked() { if(drp_route->get_selected_index()>0) { - const set &routes = engineer.get_layout().get_routes(); - set::const_iterator i = routes.begin(); + const map &routes = engineer.get_layout().get_routes(); + map::const_iterator i = routes.begin(); advance(i, drp_route->get_selected_index()-1); - train.set_route(*i); + train.set_route(i->second); } else train.set_route(0); diff --git a/source/libmarklin/layout.cpp b/source/libmarklin/layout.cpp index 1df37d3..38336ae 100644 --- a/source/libmarklin/layout.cpp +++ b/source/libmarklin/layout.cpp @@ -41,21 +41,23 @@ void Layout::remove_track(Track &t) void Layout::add_route(Route &r) { - if(routes.insert(&r).second) - signal_route_added.emit(r); + if(routes.count(r.get_name())) + throw KeyError("Duplicate route name"); + routes[r.get_name()] = &r; + signal_route_added.emit(r); } Route &Layout::get_route(const string &name) const { - for(set::const_iterator i=routes.begin(); i!=routes.end(); ++i) - if((*i)->get_name()==name) - return **i; - throw KeyError("Unknown route", name); + map::const_iterator i = routes.find(name); + if(i==routes.end()) + throw KeyError("Unknown route", name); + return *i->second; } void Layout::remove_route(Route &r) { - if(routes.erase(&r)) + if(routes.erase(r.get_name())) signal_route_removed.emit(r); } @@ -75,11 +77,11 @@ void Layout::save(const string &fn) writer.write(st); } - for(set::iterator i=routes.begin(); i!=routes.end(); ++i) + for(map::iterator i=routes.begin(); i!=routes.end(); ++i) { DataFile::Statement st("route"); - st.append((*i)->get_name()); - (*i)->save(st.sub); + st.append(i->first); + i->second->save(st.sub); writer.write(st); } } @@ -110,10 +112,10 @@ void Layout::check_links() void Layout::check_routes() { - for(set::iterator i=routes.begin(); i!=routes.end(); ++i) + for(map::iterator i=routes.begin(); i!=routes.end(); ++i) { // We must copy the turnout map, since adding tracks to the route will (temporarily) mess it up - const map turnouts = (*i)->get_turnouts(); + const map turnouts = i->second->get_turnouts(); Track *track = 0; unsigned trk_path = 0; @@ -131,7 +133,7 @@ void Layout::check_routes() if(!track) continue; - (*i)->add_track(*track); + i->second->add_track(*track); const vector &eps = track->get_type().get_endpoints(); unsigned ep = 0; @@ -159,7 +161,7 @@ void Layout::check_routes() } else trk_path = 0; - (*i)->add_track(*next); + i->second->add_track(*next); track = next; } } diff --git a/source/libmarklin/layout.h b/source/libmarklin/layout.h index 8573a54..4ec4277 100644 --- a/source/libmarklin/layout.h +++ b/source/libmarklin/layout.h @@ -40,7 +40,7 @@ private: const Catalogue &catalogue; std::string base; std::set tracks; - std::set routes; + std::map routes; public: Layout(const Catalogue &); @@ -52,7 +52,7 @@ public: void add_track(Track &); void remove_track(Track &); void add_route(Route &); - const std::set &get_routes() const { return routes; } + const std::map &get_routes() const { return routes; } Route &get_route(const std::string &) const; void remove_route(Route &); void save(const std::string &); diff --git a/source/network/server.cpp b/source/network/server.cpp index cd52b48..044b310 100644 --- a/source/network/server.cpp +++ b/source/network/server.cpp @@ -130,11 +130,11 @@ Server::Connection::~Connection() void Server::Connection::handshake_done() { - const set &routes = server.trfc_mgr.get_layout().get_routes(); - for(set::const_iterator i=routes.begin(); i!=routes.end(); ++i) + const map &routes = server.trfc_mgr.get_layout().get_routes(); + for(map::const_iterator i=routes.begin(); i!=routes.end(); ++i) { RouteInfoPacket pkt; - pkt.name = (*i)->get_name(); + pkt.name = i->first; comm.send(pkt); } -- 2.45.2