]> git.tdb.fi Git - r2c2.git/commitdiff
Use maps instead of lists in Layout3D
authorMikko Rasa <tdb@tdb.fi>
Tue, 26 Oct 2010 18:44:48 +0000 (18:44 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 26 Oct 2010 18:44:48 +0000 (18:44 +0000)
source/3d/layout.cpp
source/3d/layout.h
source/designer/designer.cpp
source/engineer/engineer.cpp

index e16b6e6d788a135efddad08753dd66348c2655d9..05c398d302a33b3fb22bcc968d82d90f21409abe 100644 (file)
@@ -38,30 +38,31 @@ Layout3D::Layout3D(Layout &l):
 Layout3D::~Layout3D()
 {
        while(!tracks.empty())
-               delete tracks.front();
+               delete tracks.begin()->second;
        while(!vehicles.empty())
-               delete vehicles.front();
+               delete vehicles.begin()->second;
 }
 
 void Layout3D::add_track(Track3D &t)
 {
-       tracks.push_back(&t);
+       if(tracks.count(&t.get_track()))
+               throw KeyError("Duplicate track");
+
+       tracks[&t.get_track()] = &t;
 }
 
 void Layout3D::remove_track(Track3D &t)
 {
-       list<Track3D *>::iterator i = find(tracks.begin(), tracks.end(), &t);
-       if(i!=tracks.end())
-               tracks.erase(i);
+       tracks.erase(&t.get_track());
 }
 
 Track3D &Layout3D::get_track(Track &t) const
 {
-       for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               if(&(*i)->get_track()==&t)
-                       return **i;
+       TrackMap::const_iterator i = tracks.find(&t);
+       if(i==tracks.end())
+               throw KeyError("Unknown track");
 
-       throw KeyError("Unknown track");
+       return *i->second;
 }
 
 Track3D *Layout3D::pick_track(float x, float y, float size) const
@@ -102,23 +103,24 @@ Track3D *Layout3D::pick_track(float x, float y, float size) const
 
 void Layout3D::add_vehicle(Vehicle3D &v)
 {
-       vehicles.push_back(&v);
+       if(vehicles.count(&v.get_vehicle()))
+               throw KeyError("Duplicate vehicle");
+
+       vehicles[&v.get_vehicle()] = &v;
 }
 
 void Layout3D::remove_vehicle(Vehicle3D &v)
 {
-       list<Vehicle3D *>::iterator i = find(vehicles.begin(), vehicles.end(), &v);
-       if(i!=vehicles.end())
-               vehicles.erase(i);
+       vehicles.erase(&v.get_vehicle());
 }
 
-Vehicle3D &Layout3D::get_vehicle(const Vehicle &v) const
+Vehicle3D &Layout3D::get_vehicle(Vehicle &v) const
 {
-       for(list<Vehicle3D *>::const_iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
-               if(&(*i)->get_vehicle()==&v)
-                       return **i;
+       VehicleMap::const_iterator i = vehicles.find(&v);
+       if(i==vehicles.end())
+               throw KeyError("Unknown vehicle");
 
-       throw KeyError("Unknown vehicle");
+       return *i->second;
 }
 
 void Layout3D::track_added(Track &t)
@@ -128,12 +130,9 @@ void Layout3D::track_added(Track &t)
 
 void Layout3D::track_removed(Track &t)
 {
-       for(list<Track3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               if(&(*i)->get_track()==&t)
-               {
-                       delete *i;
-                       return;
-               }
+       TrackMap::iterator i = tracks.find(&t);
+       if(i!=tracks.end())
+               delete i->second;
 }
 
 void Layout3D::vehicle_added(Vehicle &v)
index f09603de5b00dad0d9ad06178a7e1eef71d98aa0..f2de127a0cc19d96117b4a63f3fb3be8c042921c 100644 (file)
@@ -20,11 +20,15 @@ class Vehicle3D;
 
 class Layout3D: public sigc::trackable
 {
+public:
+       typedef std::map<Track *, Track3D *> TrackMap;
+       typedef std::map<Vehicle *, Vehicle3D *> VehicleMap;
+
 private:
        Layout &layout;
        Catalogue3D catalogue;
-       std::list<Track3D *> tracks;
-       std::list<Vehicle3D *> vehicles;
+       TrackMap tracks;
+       VehicleMap vehicles;
        Msp::GL::Scene scene;
        Msp::GL::Scene ep_scene;
        Msp::GL::Scene path_scene;
@@ -37,13 +41,13 @@ public:
 
        void add_track(Track3D &);
        void remove_track(Track3D &);
-       const std::list<Track3D *> &get_tracks() const { return tracks; }
+       const TrackMap &get_tracks() const { return tracks; }
        Track3D &get_track(Track &) const;
        Track3D *pick_track(float, float, float) const;
 
        void add_vehicle(Vehicle3D &);
        void remove_vehicle(Vehicle3D &);
-       Vehicle3D &get_vehicle(const Vehicle &) const;
+       Vehicle3D &get_vehicle(Vehicle &) const;
 
        Msp::GL::Scene &get_scene() { return scene; }
        Msp::GL::Scene &get_endpoint_scene() { return ep_scene; }
index 024e9fe7fdc3869fa3148ed61e28f23dc7798514..cb327c560efeb51236169b369675d42b16c26798 100644 (file)
@@ -137,9 +137,9 @@ Designer::Designer(int argc, char **argv):
 
        overlay = new Overlay3D(window, camera, ui_res.get_default_font());
 
-       const list<Track3D *> &tracks = layout_3d->get_tracks();
-       for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               update_track_icon(**i);
+       const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
+       for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               update_track_icon(*i->second);
 }
 
 Designer::~Designer()
@@ -587,12 +587,12 @@ void Designer::view_all()
        Point minp;
        Point maxp;
 
-       const list<Track3D *> &tracks = layout_3d->get_tracks();
-       for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+       const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
+       for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
        {
                Point tmin;
                Point tmax;
-               (*i)->get_bounds(0, tmin, tmax);
+               i->second->get_bounds(0, tmin, tmax);
                minp.x = min(minp.x, tmin.x);
                minp.y = min(minp.y, tmin.y);
                maxp.x = max(maxp.x, tmax.x);
index 32b77eda830989c4f1fc918afeec8bc2c4e49b59..68b85aa04cf3c3acdc8c89bd438b3ffcc75a5400 100644 (file)
@@ -319,7 +319,7 @@ void Engineer::pointer_motion(int x, int y)
 
 void Engineer::view_all()
 {
-       const list<Track3D *> &tracks = layout_3d.get_tracks();
+       const Layout3D::TrackMap &tracks = layout_3d.get_tracks();
 
        float view_aspect = float(window.get_width()-200)/window.get_height();
        float view_height = tan(camera.get_field_of_view()/2)*2;
@@ -332,10 +332,10 @@ void Engineer::view_all()
                float max_x = 0;
                float min_y = 0;
                float max_y = 0;
-               for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
                {
                        Point minp, maxp;
-                       (*i)->get_bounds(angle, minp, maxp);
+                       i->second->get_bounds(angle, minp, maxp);
                        min_x = min(min_x, minp.x);
                        max_x = max(max_x, maxp.x);
                        min_y = min(min_y, minp.y);