]> git.tdb.fi Git - r2c2.git/commitdiff
Don't give out a turnout ID that's already in use
authorMikko Rasa <tdb@tdb.fi>
Tue, 26 Oct 2010 18:30:46 +0000 (18:30 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 26 Oct 2010 18:30:46 +0000 (18:30 +0000)
source/libmarklin/layout.cpp

index f1bc3211bab00b5a8e6d4403f8d0ad3607694834..338612a104e24a94567430084e61a6e0e3e4cd88 100644 (file)
@@ -75,9 +75,16 @@ void Layout::remove_track(Track &t)
 
 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;
 }