]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/layout.cpp
Maintain a Block pointer in Track
[r2c2.git] / source / libmarklin / layout.cpp
index f8ad397f183992507ea2eb0140d1d2ae20bccf2c..f1bc3211bab00b5a8e6d4403f8d0ad3607694834 100644 (file)
@@ -95,15 +95,6 @@ Block &Layout::get_block(unsigned id) const
        throw KeyError("Unknown block", lexical_cast(id));
 }
 
-Block &Layout::get_block_by_track(Track &t) const
-{
-       for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
-               if((*i)->has_track(t))
-                       return **i;
-
-       throw InvalidParameterValue("No block found for track");
-}
-
 void Layout::create_blocks()
 {
        set<Track *> used_tracks;
@@ -128,18 +119,19 @@ void Layout::create_blocks()
 
 void Layout::create_blocks(Track &track)
 {
+       /* Must collect the blocks in a set first while all tracks are still
+       guaranteed to have blocks and to avoid duplicate deletes */
+       set<Block *> del_blocks;
+
+       del_blocks.insert(&track.get_block());
+
        const vector<Track *> &links = track.get_links();
-       for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end();)
-       {
-               bool del = (*i)->has_track(track);
-               for(vector<Track *>::const_iterator j=links.begin(); (!del && j!=links.end()); ++j)
-                       del = (*i)->has_track(**j);
-
-               if(del)
-                       delete *i++;
-               else
-                       ++i;
-       }
+       for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
+               if(*i)
+                       del_blocks.insert(&(*i)->get_block());
+
+       for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
+               delete *i;
 
        create_blocks();
 }