From bd649cbc44e04f5e456ca19b7a32ebe479c130e1 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 26 Oct 2010 18:30:46 +0000 Subject: [PATCH] Don't give out a turnout ID that's already in use --- source/libmarklin/layout.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/source/libmarklin/layout.cpp b/source/libmarklin/layout.cpp index f1bc321..338612a 100644 --- a/source/libmarklin/layout.cpp +++ b/source/libmarklin/layout.cpp @@ -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 used_ids; + for(set::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; } -- 2.45.2