]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/layout.cpp
Store routes in a map by name rather than a set
[r2c2.git] / source / libmarklin / layout.cpp
index 67d646b1d5f542b1e5b156713c7ef79156660ba0..38336ae8d98f5ff6c3dfe3cbd3b4bc0e3c4bb4ab 100644 (file)
@@ -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<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
-               if((*i)->get_name()==name)
-                       return **i;
-       throw KeyError("Unknown route", name);
+       map<string, Route *>::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<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
+       for(map<string, Route *>::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<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
+       for(map<string, Route *>::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<unsigned, int> turnouts = (*i)->get_turnouts();
+               const map<unsigned, int> 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<Endpoint> &eps = track->get_type().get_endpoints();
                unsigned ep = 0;
@@ -145,9 +147,7 @@ void Layout::check_routes()
                Track *start = track;
                while(1)
                {
-                       int out_ep = track->traverse(ep, trk_path);
-                       if(out_ep<0)
-                               break;
+                       unsigned out_ep = track->traverse(ep, trk_path);
                        Track *next = track->get_links()[out_ep];
                        if(!next || next == start)
                                break;
@@ -161,7 +161,7 @@ void Layout::check_routes()
                        }
                        else
                                trk_path = 0;
-                       (*i)->add_track(*next);
+                       i->second->add_track(*next);
                        track = next;
                }
        }