@@ -17,6 +17,7 @@ limitations under the License.
1717#include " libcellml/annotator.h"
1818
1919#include < algorithm>
20+ #include < format>
2021#include < sstream>
2122#include < unordered_map>
2223
@@ -236,13 +237,16 @@ void Annotator::AnnotatorImpl::listComponentIdsAndItems(const ComponentPtr &comp
236237 if (idList.count (id) != 0 ) {
237238 // Get the range of items with this identifier:
238239 auto rangePair = idList.equal_range (id);
240+ auto compEquiv = owningComponent (equivalentVariable);
241+ auto compVar = owningComponent (variable);
239242 for (auto it = rangePair.first ; it != rangePair.second ; ++it) {
240243 // Make sure it's also a CONNECTION item.
241244 if (it->second ->type () == CellmlElementType::CONNECTION ) {
242245 auto testPair = it->second ->variablePair ();
243- if ((owningComponent (testPair->variable1 ()) == owningComponent (equivalentVariable)) && (owningComponent (testPair->variable2 ()) == owningComponent (variable))) {
244- found = true ;
245- } else if ((owningComponent (testPair->variable2 ()) == owningComponent (equivalentVariable)) && (owningComponent (testPair->variable1 ()) == owningComponent (variable))) {
246+ auto compTest1 = owningComponent (testPair->variable1 ());
247+ auto compTest2 = owningComponent (testPair->variable2 ());
248+ if (((compTest1 == compEquiv) && (compTest2 == compVar))
249+ || ((compTest2 == compEquiv) && (compTest1 == compVar))) {
246250 found = true ;
247251 }
248252 }
@@ -352,7 +356,7 @@ AnyCellmlElementPtr Annotator::AnnotatorImpl::convertToWeak(const AnyCellmlEleme
352356
353357 converted->mPimpl ->mType = type;
354358
355- switch (item-> type () ) {
359+ switch (type) {
356360 case CellmlElementType::COMPONENT :
357361 case CellmlElementType::COMPONENT_REF : {
358362 ComponentWeakPtr weakComponent = item->component ();
@@ -517,7 +521,7 @@ void Annotator::AnnotatorImpl::addIssueInvalidArgument(CellmlElementType type)
517521void Annotator::AnnotatorImpl::addIssueNonUnique (const std::string &id)
518522{
519523 auto issue = Issue::IssueImpl::create ();
520- issue->mPimpl ->setDescription (" The identifier '" + id + " ' occurs " + std::to_string ( mIdList .count (id)) + " times in the model so a unique item cannot be located." );
524+ issue->mPimpl ->setDescription (" The identifier '" + id + " ' occurs " + std::format ( " {} " , mIdList .count (id)) + " times in the model so a unique item cannot be located." );
521525 issue->mPimpl ->setLevel (Issue::Level::WARNING );
522526 issue->mPimpl ->setReferenceRule (Issue::ReferenceRule::ANNOTATOR_ID_NOT_UNIQUE );
523527 addIssue (issue);
@@ -1417,20 +1421,26 @@ size_t Annotator::itemCount(const std::string &id)
14171421
14181422void Annotator::AnnotatorImpl::doUpdateComponentHash (const ComponentPtr &component, std::string &idsString)
14191423{
1420- for (size_t i = 0 ; i < component->variableCount (); ++i) {
1421- idsString += " v=" + std::to_string (i) + component->variable (i)->id ();
1424+ const auto variableCount = component->variableCount ();
1425+ const auto resetCount = component->resetCount ();
1426+ const auto componentCount = component->componentCount ();
1427+
1428+ idsString.reserve (idsString.size () + 24 * (variableCount + resetCount + componentCount));
1429+
1430+ for (size_t i = 0 ; i < variableCount; ++i) {
1431+ idsString += " v=" + std::format (" {}" , i) + component->variable (i)->id ();
14221432 }
14231433
1424- for (size_t i = 0 ; i < component-> resetCount () ; ++i) {
1434+ for (size_t i = 0 ; i < resetCount; ++i) {
14251435 auto reset = component->reset (i);
1426- idsString += " r=" + std::to_string ( i) + reset->id () + " rv=" + reset->resetValueId () + " tv=" + reset->testValueId ();
1436+ idsString += " r=" + std::format ( " {} " , i) + reset->id () + " rv=" + reset->resetValueId () + " tv=" + reset->testValueId ();
14271437 }
14281438
14291439 // Note that MathML identifiers are not yet included.
14301440
1431- for (size_t i = 0 ; i < component-> componentCount () ; ++i) {
1441+ for (size_t i = 0 ; i < componentCount; ++i) {
14321442 auto child = component->component (i);
1433- idsString += " c=" + std::to_string ( i) + child->id () + " ce=" + child->encapsulationId ();
1443+ idsString += " c=" + std::format ( " {} " , i) + child->id () + " ce=" + child->encapsulationId ();
14341444 doUpdateComponentHash (child, idsString);
14351445 }
14361446}
@@ -1443,26 +1453,27 @@ size_t Annotator::AnnotatorImpl::generateHash()
14431453 if (model != nullptr ) {
14441454 std::string idsString;
14451455 size_t i;
1446- idsString += " m=" + model->id () + " me=" + model->encapsulationId ();
14471456
14481457 auto importSources = getAllImportSources (model);
1458+ idsString.reserve (64 + 32 * (importSources.size () + model->unitsCount () + model->componentCount ()));
1459+ idsString += " m=" + model->id () + " me=" + model->encapsulationId ();
14491460 i = 0 ;
14501461 for (auto &importSource : importSources) {
1451- idsString += " i=" + std::to_string ( ++i) + importSource->id ();
1462+ idsString += " i=" + std::format ( " {} " , ++i) + importSource->id ();
14521463 }
14531464
14541465 for (i = 0 ; i < model->unitsCount (); ++i) {
14551466 auto units = model->units (i);
1456- idsString += " U=" + std::to_string ( i) + units->id ();
1467+ idsString += " U=" + std::format ( " {} " , i) + units->id ();
14571468 for (size_t j = 0 ; j < units->unitCount (); ++j) {
1458- idsString += " u=" + std::to_string ( j) + units->unitId (j);
1469+ idsString += " u=" + std::format ( " {} " , j) + units->unitId (j);
14591470 }
14601471 }
14611472
14621473 for (i = 0 ; i < model->componentCount (); ++i) {
14631474 auto component = model->component (i);
1464- idsString += " c=" + std::to_string ( i) + component->id ();
1465- idsString += " cr=" + std::to_string ( i) + component->encapsulationId ();
1475+ idsString += " c=" + std::format ( " {} " , i) + component->id ();
1476+ idsString += " cr=" + std::format ( " {} " , i) + component->encapsulationId ();
14661477 doUpdateComponentHash (component, idsString);
14671478 }
14681479
0 commit comments