@@ -859,6 +859,44 @@ dbOStream& operator<<(dbOStream& stream, const _dbBlock& block)
859859 return stream;
860860}
861861
862+
863+ /* *
864+ * @brief Rebuilds the name-to-ID hash maps for hierarchical objects within
865+ * their respective modules.
866+ *
867+ * This function is used during database loading (operator>>) to restore the
868+ * fast-lookup hashes that were stored in the ODB.
869+ *
870+ * @tparam T The public ODB object type (e.g., dbInst, dbModNet).
871+ * @tparam T_impl The internal implementation type (e.g., _dbInst, _dbModNet).
872+ * @param block The database block containing the objects.
873+ * @param table The internal database table storing the object implementations.
874+ * @param module_field Member pointer to the field storing the parent module ID
875+ * (e.g., &_dbInst::module_ or &_dbModInst::parent_).
876+ * @param hash_field Member pointer to the hash map member in _dbModule where
877+ * the object ID should be stored.
878+ */
879+ template <typename T, typename T_impl>
880+ static void rebuildModuleHash (_dbBlock& block,
881+ dbTable<T_impl>* table,
882+ dbId<_dbModule> T_impl::*module_field,
883+ std::unordered_map<std::string, dbId<T_impl>> _dbModule::*
884+ hash_field)
885+ {
886+ dbSet<T> items ((dbBlock*) &block, table);
887+ for (T* obj : items) {
888+ T_impl* _obj = (T_impl*) obj;
889+ dbId<_dbModule> mid = _obj->*module_field;
890+ if (mid == 0 ) {
891+ mid = block.top_module_ ;
892+ }
893+ _dbModule* module = block.module_tbl_ ->getPtr (mid);
894+ if (module && _obj->name_ ) {
895+ (module ->*hash_field)[_obj->name_ ] = _obj->getId ();
896+ }
897+ }
898+ }
899+
862900dbIStream& operator >>(dbIStream& stream, _dbBlock& block)
863901{
864902 _dbDatabase* db = block.getImpl ()->getDatabase ();
@@ -952,47 +990,28 @@ dbIStream& operator>>(dbIStream& stream, _dbBlock& block)
952990 }
953991 if (db->isSchema (kSchemaDbRemoveHash )) {
954992 // Construct dbinst_hash_
955- dbSet<dbInst> insts ((dbBlock*) &block, block.inst_tbl_ );
956- for (dbInst* obj : insts) {
957- _dbInst* _obj = (_dbInst*) obj;
958- _dbModule* module = nullptr ;
959- if (_obj->module_ == 0 ) {
960- module = block.module_tbl_ ->getPtr (block.top_module_ );
961- } else {
962- module = block.module_tbl_ ->getPtr (_obj->module_ );
963- }
964- if (_obj->name_ ) {
965- module ->dbinst_hash_ [_obj->name_ ] = _obj->getId ();
966- }
967- }
993+ rebuildModuleHash<dbInst>(
994+ block, block.inst_tbl_ , &_dbInst::module_, &_dbModule::dbinst_hash_);
968995 }
969996 if (db->isSchema (kSchemaBlockOwnsScanInsts )) {
970997 stream >> *block.scan_inst_tbl_ ;
971998 }
972999 stream >> *block.modinst_tbl_ ;
9731000 if (db->isSchema (kSchemaDbRemoveHash )) {
9741001 // Construct modinst_hash_
975- dbSet<dbModInst> modinsts ((dbBlock*) &block, block.modinst_tbl_ );
976- for (dbModInst* obj : modinsts) {
977- _dbModInst* _obj = (_dbModInst*) obj;
978- _dbModule* module = block.module_tbl_ ->getPtr (_obj->parent_ );
979- if (_obj->name_ ) {
980- module ->modinst_hash_ [_obj->name_ ] = _obj->getId ();
981- }
982- }
1002+ rebuildModuleHash<dbModInst>(block,
1003+ block.modinst_tbl_ ,
1004+ &_dbModInst::parent_,
1005+ &_dbModule::modinst_hash_);
9831006 }
9841007 if (db->isSchema (kSchemaUpdateHierarchy )) {
9851008 stream >> *block.modbterm_tbl_ ;
9861009 if (db->isSchema (kSchemaDbRemoveHash )) {
9871010 // Construct modbterm_hash_
988- dbSet<dbModBTerm> modbterms ((dbBlock*) &block, block.modbterm_tbl_ );
989- for (dbModBTerm* obj : modbterms) {
990- _dbModBTerm* _obj = (_dbModBTerm*) obj;
991- _dbModule* module = block.module_tbl_ ->getPtr (_obj->parent_ );
992- if (_obj->name_ ) {
993- module ->modbterm_hash_ [_obj->name_ ] = _obj->getId ();
994- }
995- }
1011+ rebuildModuleHash<dbModBTerm>(block,
1012+ block.modbterm_tbl_ ,
1013+ &_dbModBTerm::parent_,
1014+ &_dbModule::modbterm_hash_);
9961015 }
9971016 if (db->isSchema (kSchemaDbRemoveHash )) {
9981017 stream >> *block.busport_tbl_ ;
@@ -1001,14 +1020,10 @@ dbIStream& operator>>(dbIStream& stream, _dbBlock& block)
10011020 stream >> *block.modnet_tbl_ ;
10021021 if (db->isSchema (kSchemaDbRemoveHash )) {
10031022 // Construct modnet_hash_
1004- dbSet<dbModNet> modnets ((dbBlock*) &block, block.modnet_tbl_ );
1005- for (dbModNet* obj : modnets) {
1006- _dbModNet* _obj = (_dbModNet*) obj;
1007- _dbModule* module = block.module_tbl_ ->getPtr (_obj->parent_ );
1008- if (_obj->name_ ) {
1009- module ->modnet_hash_ [_obj->name_ ] = _obj->getId ();
1010- }
1011- }
1023+ rebuildModuleHash<dbModNet>(block,
1024+ block.modnet_tbl_ ,
1025+ &_dbModNet::parent_,
1026+ &_dbModule::modnet_hash_);
10121027 }
10131028 }
10141029 stream >> *block.powerdomain_tbl_ ;
0 commit comments