Skip to content

Commit

Permalink
Replaced recursive implementation of getData with an iterative one
Browse files Browse the repository at this point in the history
Signed-off-by: Jared Duffey <[email protected]>
  • Loading branch information
JDuffeyBQ committed Jun 20, 2024
1 parent a036ea7 commit a0903ac
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions src/simplnx/DataStructure/DataStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,40 +272,33 @@ DataObject* DataStructure::getData(const std::optional<DataObject::IdType>& iden
return iter->second.lock().get();
}

DataObject* traversePath(DataObject* obj, const DataPath& path, usize index)
{
if(path.getLength() == index)
{
return obj;
}
auto col = dynamic_cast<BaseGroup*>(obj);
if(col == nullptr)
{
return nullptr;
}
DataObject* child = (*col)[path[index]];
return traversePath(child, path, index + 1);
}

DataObject* DataStructure::getData(const DataPath& path)
{
if(path.empty())
{
return nullptr;
}
auto topLevel = getTopLevelData();
for(DataObject* obj : topLevel)
DataObject* targetObject = m_RootGroup[path[0]];
for(usize index = 1; index < path.getLength(); index++)
{
if(obj == nullptr)
if(targetObject == nullptr)
{
continue;
return nullptr;
}
if(obj->getName() == path[0])
if(!targetObject->isGroup())
{
return traversePath(obj, path, 1);
return nullptr;
}
auto* groupObject = static_cast<BaseGroup*>(targetObject);
DataObject* childObject = (*groupObject)[path[index]];
if(childObject == nullptr)
{
return nullptr;
}
targetObject = childObject;
}
return nullptr;

return targetObject;
}

DataObject& DataStructure::getDataRef(const DataPath& path)
Expand Down Expand Up @@ -364,20 +357,27 @@ const DataObject* DataStructure::getData(const DataPath& path) const
{
return nullptr;
}

auto topLevel = getTopLevelData();
for(DataObject* obj : topLevel)
const DataObject* targetObject = m_RootGroup[path[0]];
for(usize index = 1; index < path.getLength(); index++)
{
if(obj == nullptr)
if(targetObject == nullptr)
{
continue;
return nullptr;
}
if(!targetObject->isGroup())
{
return nullptr;
}
if(obj->getName() == path[0])
const auto* groupObject = static_cast<const BaseGroup*>(targetObject);
const DataObject* childObject = (*groupObject)[path[index]];
if(childObject == nullptr)
{
return traversePath(obj, path, 1);
return nullptr;
}
targetObject = childObject;
}
return nullptr;

return targetObject;
}

const DataObject& DataStructure::getDataRef(const DataPath& path) const
Expand Down

0 comments on commit a0903ac

Please sign in to comment.