Skip to content

Commit

Permalink
bugfix: whenever ModuleItem gets created or deleted maps must be upda…
Browse files Browse the repository at this point in the history
…ted accordingly
  • Loading branch information
joern274 committed Sep 17, 2024
1 parent 194da1b commit 09a2ef0
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 92 deletions.
12 changes: 11 additions & 1 deletion plugins/gui/include/gui/module_model/module_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

namespace hal
{
class ModuleModel;

/**
* @ingroup gui
* @brief An item in the ModuleModel.
Expand All @@ -61,7 +63,14 @@ namespace hal
* @param id - The id of the netlist item this ModuleItem represents
* @param type - The type of the netlist item
*/
ModuleItem(const u32 id, const TreeItemType type);
ModuleItem(const u32 id, const TreeItemType type, ModuleModel* model);

/**
* Destructor.
*
* Must remove item from ModuleModel map as well.
*/
virtual ~ModuleItem();

/**
* Given a set of ModuleItems (in a map [id]->[ModuleItem]) this function adds each ModuleItem of this set as
Expand Down Expand Up @@ -142,5 +151,6 @@ namespace hal
QString mModuleType;

bool mHighlighted;
ModuleModel* mModuleModel; // reference to parent model
};
}
1 change: 1 addition & 0 deletions plugins/gui/include/gui/module_model/module_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace hal
*/
class ModuleModel : public BaseTreeModel
{
friend class ModuleItem;
Q_OBJECT

/**
Expand Down
19 changes: 17 additions & 2 deletions plugins/gui/src/module_model/module_item.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "gui/module_model/module_item.h"
#include "gui/module_model/module_model.h"

#include "hal_core/netlist/module.h"

Expand All @@ -8,11 +9,12 @@
namespace hal
{

ModuleItem::ModuleItem(const u32 id, const TreeItemType type) :
ModuleItem::ModuleItem(const u32 id, const TreeItemType type, ModuleModel *model) :
BaseTreeItem(),
mId(id),
mItemType(type),
mHighlighted(false)
mHighlighted(false),
mModuleModel(model)
{
switch(type)
{
Expand Down Expand Up @@ -40,6 +42,19 @@ ModuleItem::ModuleItem(const u32 id, const TreeItemType type) :
break;
}
}
mModuleModel->mModuleItemMaps[(int)mItemType]->insertMulti(id,this);
}

ModuleItem::~ModuleItem()
{
auto it = mModuleModel->mModuleItemMaps[(int)mItemType]->lowerBound(mId);
while (it != mModuleModel->mModuleItemMaps[(int)mItemType]->upperBound(mId))
{
if (it.value() == this)
it = mModuleModel->mModuleItemMaps[(int)mItemType]->erase(it);
else
++it;
}
}

void ModuleItem::appendExistingChildIfAny(const QMap<u32,ModuleItem*>& moduleMap)
Expand Down
141 changes: 59 additions & 82 deletions plugins/gui/src/module_model/module_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ namespace hal
Module* parentModule = g->get_module();
ModuleItem* parentItem;
bool insertToRoot = true;
ModuleItem* childItem = new ModuleItem(g->get_id(), ModuleItem::TreeItemType::Gate);
ModuleItem* childItem = new ModuleItem(g->get_id(), ModuleItem::TreeItemType::Gate, this);

while (parentModule && insertToRoot)
{
parentItem = parentMap.value(parentModule);
if (!parentItem)
{
parentItem = new ModuleItem(parentModule->get_id(), ModuleItem::TreeItemType::Module);
parentItem = new ModuleItem(parentModule->get_id(), ModuleItem::TreeItemType::Module, this);
parentMap.insert(parentModule, parentItem);
}
else
Expand Down Expand Up @@ -172,10 +172,10 @@ namespace hal
moduleAssignNets();

for(u32 id : gateIds)
newRootList.append(new ModuleItem(id, ModuleItem::TreeItemType::Gate));
newRootList.append(new ModuleItem(id, ModuleItem::TreeItemType::Gate, this));

for(u32 id : netIds)
newRootList.append(new ModuleItem(id, ModuleItem::TreeItemType::Net));
newRootList.append(new ModuleItem(id, ModuleItem::TreeItemType::Net, this));

for(auto item : newRootList)
mRootItem->appendChild(item);
Expand Down Expand Up @@ -232,50 +232,49 @@ namespace hal

void ModuleModel::removeModule(const u32 id)
{
auto it = mModuleMap.lowerBound(id);
while (it != mModuleMap.upperBound(id))
QList<ModuleItem*> trashcan; // access items to delete without map

for (auto it = mModuleMap.lowerBound(id); it != mModuleMap.upperBound(id); ++it)
trashcan.append(it.value());

for (ModuleItem* item : trashcan)
{
ModuleItem* item = it.value();
BaseTreeItem* parentItem = item->getParent();

removeChildItem(item,parentItem);

it = mModuleMap.erase(it);
}
}

void ModuleModel::removeGate(const u32 id)
{
auto it = mGateMap.lowerBound(id);
while (it != mGateMap.upperBound(id))
{
ModuleItem* item = it.value();
BaseTreeItem* parentItem = item->getParent();
QList<ModuleItem*> trashcan; // access items to delete without map

removeChildItem(item, parentItem);
for (auto it = mGateMap.lowerBound(id); it != mGateMap.upperBound(id); ++it)
trashcan.append(it.value());

it = mGateMap.erase(it);
for (ModuleItem* item : trashcan)
{
BaseTreeItem* parentItem = item->getParent();
removeChildItem(item,parentItem);
}
}

void ModuleModel::removeNet(const u32 id)
{
auto it = mNetMap.lowerBound(id);
while (it != mNetMap.upperBound(id))
{
ModuleItem* item = it.value();
BaseTreeItem* parentItem = item->getParent();
QList<ModuleItem*> trashcan; // access items to delete without map

removeChildItem(item, parentItem);
for (auto it = mNetMap.lowerBound(id); it != mNetMap.upperBound(id); ++it)
trashcan.append(it.value());

it = mNetMap.erase(it);
for (ModuleItem* item : trashcan)
{
BaseTreeItem* parentItem = item->getParent();
removeChildItem(item,parentItem);
}
}

ModuleItem* ModuleModel::createChildItem(u32 id, ModuleItem::TreeItemType itemType, BaseTreeItem *parentItem)
{
ModuleItem* retval = new ModuleItem(id, itemType);
mModuleItemMaps[(int)itemType]->insertMulti(id,retval);
ModuleItem* retval = new ModuleItem(id, itemType, this);

if (!parentItem) parentItem = mRootItem;
QModelIndex index = getIndexFromItem(parentItem);
Expand All @@ -297,15 +296,6 @@ namespace hal
while (itemToRemove->getChildCount())
{
ModuleItem* childItem = static_cast<ModuleItem*>(itemToRemove->getChildren().at(0));
int ityp = static_cast<int>(childItem->getType());
auto it = mModuleItemMaps[ityp]->lowerBound(childItem->id());
while (it != mModuleItemMaps[ityp]->upperBound(childItem->id()))
{
if (it.value() == childItem)
it = mModuleItemMaps[ityp]->erase(it);
else
++it;
}
removeChildItem(childItem,itemToRemove);
}

Expand Down Expand Up @@ -351,27 +341,25 @@ namespace hal

void ModuleModel::handleModuleGateRemoved(Module* mod, u32 gateId)
{
QList<QPair<ModuleItem*,ModuleItem*> > trashcan; // item to delete, parent

if (mTempGateAssignment.isAccumulate())
mTempGateAssignment.removeGateFromModule(gateId,mod);
else
{
auto it = mGateMap.lowerBound(gateId);
while (it != mGateMap.upperBound(gateId))
for (auto it = mGateMap.lowerBound(gateId); it != mGateMap.upperBound(gateId); ++it)
{
ModuleItem* item = it.value();
if (!item->isToplevelItem())
{
ModuleItem* parentItem = static_cast<ModuleItem*>(item->getParent());
if (parentItem->id() == mod->get_id())
{
removeChildItem(item, parentItem);
it = mGateMap.erase(it);
continue;
}
}
++it;
if (item->isToplevelItem()) continue;;

ModuleItem* parentItem = static_cast<ModuleItem*>(item->getParent());
if (parentItem->id() == mod->get_id())
trashcan.append(QPair<ModuleItem*,ModuleItem*>(item, parentItem));
}
}

for (const QPair<ModuleItem*,ModuleItem*>& trash : trashcan)
removeChildItem(trash.first, trash.second);
}

void ModuleModel::handleModuleGatesAssignBegin(Module* mod, u32 numberGates)
Expand Down Expand Up @@ -540,8 +528,8 @@ namespace hal
QSet<ModuleItem*> parentsHandled;
Q_ASSERT(gNetlist->get_gate_by_id(gateId));

auto itGat = mGateMap.lowerBound(gateId);
while (itGat != mGateMap.upperBound(gateId))
QList<QPair<ModuleItem*,ModuleItem*> > trashcan; // item to delete, parent
for (auto itGat = mGateMap.lowerBound(gateId); itGat != mGateMap.upperBound(gateId); ++itGat)
{
ModuleItem* gatItem = itGat.value();
if (gatItem->isToplevelItem()) continue;
Expand All @@ -550,16 +538,15 @@ namespace hal

if (oldParentItem->id() != moduleId)
{
removeChildItem(gatItem,oldParentItem);
itGat = mGateMap.erase(itGat);
trashcan.append(QPair<ModuleItem*,ModuleItem*>(gatItem,oldParentItem));
}
else
{
parentsHandled.insert(oldParentItem);
++itGat;
}

}
for (const QPair<ModuleItem*,ModuleItem*>& trash : trashcan)
removeChildItem(trash.first, trash.second);

if (!moduleId) return;
for (auto itMod = mModuleMap.lowerBound(moduleId); itMod != mModuleMap.upperBound(moduleId); ++itMod)
Expand Down Expand Up @@ -623,28 +610,30 @@ namespace hal
newParentId = newParentModule->get_id();
}

auto itNet = mNetMap.lowerBound(netId);
while (itNet != mNetMap.upperBound(netId))
QList<QPair<ModuleItem*,ModuleItem*> > trashcan; // item to delete, parent

for (auto itNet = mNetMap.lowerBound(netId); itNet != mNetMap.upperBound(netId); ++itNet)
{
if (itNet.value()->isToplevelItem()) continue;

ModuleItem* netItem = itNet.value();
ModuleItem* oldParentItem = static_cast<ModuleItem*>(netItem->getParent());
Q_ASSERT(oldParentItem);


if (newParentId == 0 || newParentId != oldParentItem->id())
{
removeChildItem(netItem,oldParentItem);
itNet = mNetMap.erase(itNet);
trashcan.append(QPair<ModuleItem*,ModuleItem*>(netItem,oldParentItem));
break;
}
else
{
parentsHandled.insert(oldParentItem);
++itNet;
}
}

for (const QPair<ModuleItem*,ModuleItem*>& trash : trashcan)
removeChildItem(trash.first, trash.second);

if (!newParentId) return;
for (auto itMod = mModuleMap.lowerBound(newParentId); itMod != mModuleMap.upperBound(newParentId); ++itMod)
{
Expand All @@ -667,15 +656,13 @@ namespace hal
u32 parentId = module->get_parent_module()->get_id();
Q_ASSERT(parentId > 0);

auto itSubm = mModuleMap.lowerBound(id);
while (itSubm != mModuleMap.upperBound(id))
QList<QPair<ModuleItem*,ModuleItem*> > trashcan; // item to delete, parent

for (auto itSubm = mModuleMap.lowerBound(id); itSubm != mModuleMap.upperBound(id); ++itSubm)
{
ModuleItem* submItem = itSubm.value();
if (submItem->isToplevelItem())
{
++itSubm;
continue;
}
if (submItem->isToplevelItem()) continue;

ModuleItem* oldParentItem = static_cast<ModuleItem*>(submItem->getParent());
Q_ASSERT(oldParentItem);

Expand All @@ -684,8 +671,7 @@ namespace hal
if (moduleItemToBeMoved)
{
// remove tree item recursively
removeChildItem(submItem,oldParentItem);
itSubm = mModuleMap.erase(itSubm);
trashcan.append(QPair<ModuleItem*,ModuleItem*>(submItem,oldParentItem));
}
else
{
Expand All @@ -700,15 +686,15 @@ namespace hal
oldParentItem->removeChild(submItem);
endRemoveRows();
mIsModifying = false;
++itSubm;
}
}
else
{
parentsHandled.insert(oldParentItem);
++itSubm;
}
}
for (const QPair<ModuleItem*,ModuleItem*>& trash : trashcan)
removeChildItem(trash.first, trash.second);

if (!parentId) return;
for (auto itMod = mModuleMap.lowerBound(parentId); itMod != mModuleMap.upperBound(parentId); ++itMod)
Expand All @@ -734,16 +720,7 @@ namespace hal

if (moduleItemToBeMoved && !moduleItemReassigned)
{
// stored item could not be reassigned, delete it
auto it = mModuleMap.lowerBound(id);
while (it != mModuleMap.upperBound(id))
{
if (it.value() == moduleItemToBeMoved)
it = mModuleMap.erase(it);
else
++it;
}
delete moduleItemToBeMoved;
removeChildItem(moduleItemToBeMoved, moduleItemToBeMoved->getParent());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,17 +410,17 @@ namespace hal

if (gSelectionRelay->numberSelectedModules())
{
ModuleItem sti(gSelectionRelay->selectedModulesList().at(0), ModuleItem::TreeItemType::Module);
ModuleItem sti(gSelectionRelay->selectedModulesList().at(0), ModuleItem::TreeItemType::Module, mModuleModel);
singleSelectionInternal(&sti);
}
else if (gSelectionRelay->numberSelectedGates())
{
ModuleItem sti(gSelectionRelay->selectedGatesList().at(0), ModuleItem::TreeItemType::Gate);
ModuleItem sti(gSelectionRelay->selectedGatesList().at(0), ModuleItem::TreeItemType::Gate, mModuleModel);
singleSelectionInternal(&sti);
}
else if (gSelectionRelay->numberSelectedNets())
{
ModuleItem sti(gSelectionRelay->selectedNetsList().at(0), ModuleItem::TreeItemType::Net);
ModuleItem sti(gSelectionRelay->selectedNetsList().at(0), ModuleItem::TreeItemType::Net, mModuleModel);
singleSelectionInternal(&sti);
}

Expand Down
Loading

0 comments on commit 09a2ef0

Please sign in to comment.