unsigned Layout::allocate_turnout_id(bool dbl)
{
- unsigned result = next_turnout_id++;
- if(dbl)
- ++next_turnout_id;
+ set<unsigned> used_ids;
+ for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ if((*i)->get_turnout_id())
+ used_ids.insert((*i)->get_turnout_id());
+
+ unsigned result = next_turnout_id;
+ while(used_ids.count(result) || (dbl && used_ids.count(result+1)))
+ ++result;
+ next_turnout_id = result+1+dbl;
+
return result;
}