From 6e5d36dbc3f1e4a221d424fa7d57b07998df67a8 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 26 Oct 2010 18:44:48 +0000 Subject: [PATCH] Use maps instead of lists in Layout3D --- source/3d/layout.cpp | 49 ++++++++++++++++++------------------ source/3d/layout.h | 12 ++++++--- source/designer/designer.cpp | 12 ++++----- source/engineer/engineer.cpp | 6 ++--- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/source/3d/layout.cpp b/source/3d/layout.cpp index e16b6e6..05c398d 100644 --- a/source/3d/layout.cpp +++ b/source/3d/layout.cpp @@ -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::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::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::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::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::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) diff --git a/source/3d/layout.h b/source/3d/layout.h index f09603d..f2de127 100644 --- a/source/3d/layout.h +++ b/source/3d/layout.h @@ -20,11 +20,15 @@ class Vehicle3D; class Layout3D: public sigc::trackable { +public: + typedef std::map TrackMap; + typedef std::map VehicleMap; + private: Layout &layout; Catalogue3D catalogue; - std::list tracks; - std::list 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 &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; } diff --git a/source/designer/designer.cpp b/source/designer/designer.cpp index 024e9fe..cb327c5 100644 --- a/source/designer/designer.cpp +++ b/source/designer/designer.cpp @@ -137,9 +137,9 @@ Designer::Designer(int argc, char **argv): overlay = new Overlay3D(window, camera, ui_res.get_default_font()); - const list &tracks = layout_3d->get_tracks(); - for(list::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 &tracks = layout_3d->get_tracks(); - for(list::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); diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index 32b77ed..68b85aa 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -319,7 +319,7 @@ void Engineer::pointer_motion(int x, int y) void Engineer::view_all() { - const list &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::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); -- 2.45.2