diff --git a/Source/Model/Classes/NMR_Model.cpp b/Source/Model/Classes/NMR_Model.cpp index c91d0939a..fe797fe2b 100644 --- a/Source/Model/Classes/NMR_Model.cpp +++ b/Source/Model/Classes/NMR_Model.cpp @@ -395,23 +395,33 @@ namespace NMR { } } - // Retrieve a unique Resource ID ModelResourceID CModel::generateResourceID() { ModelResourceID highestID = 0; - if (m_ResourceMap.empty()) + if(m_ResourceMap.empty()) + { + return 1; + } + + // find the lowest unoccupied ModelResourceID + std::unordered_set occupiedIDs; + for(const auto &entry : m_ResourceMap) { - return 1; + occupiedIDs.insert( + entry.second->getPackageResourceID()->getModelResourceID()); } - highestID = std::max_element(m_ResourceMap.begin(), m_ResourceMap.end(), - [](const auto &a, const auto &b) { - return a.first < b.first; - })->first; + ModelResourceID newResourceID = 1; + + // Loop until we find an unoccupied resource ID + while(occupiedIDs.find(newResourceID) != occupiedIDs.end()) + { + ++newResourceID; + } - return highestID + 1; + return newResourceID; } void CModel::updateUniqueResourceID(UniqueResourceID nOldID, UniqueResourceID nNewID)