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;
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();
}