How to retrieve SBase object via id or metaid in O(1)?

6 views
Skip to first unread message

Matthias König

unread,
Mar 13, 2019, 6:18:16 AM3/13/19
to libsbml-development
Hi all,
I often have to get SBase objects via id or metaid from documents.

Currently, there exist the functions

doc.getElementBySId(sid)
doc.getElementByMetaId(metaid)

but they are both at least O(n) (or possibly worse functions) which iterate over all the model elements (over all children in the XML Tree).

I think it would be a great addition to libsbml if there could be O(1) functions to retrieve SBases.
These would require some internal hashMaps which can be calculated in O(n), all subsequential calls to get elements would than be in O(1).
I saw there are some related functions used in flattening, i.e.,

model.populateAllElementIdList()
model.populateAllElementMetaIdList()
model.isPopulatedAllElementIdList()

which create lists of ids and metaids for models. One could have

doc.populateAllElementIdMap()
doc.isPopulatedAllElementIdMap()
doc.getElementBySidMap(sid)

doc.populateAllElementMetaIdMap()
doc.isPopulatedAllElementMetaIdMap()
doc.getElementByMetaIdMap(sid)

I am sure many users are unaware that getElementBySId is O(n).
And at least me forget this again and again and only find out again when I profile my code why it is so slow (and most of the time the getElement functions are the culprit).

I am basically writing code to create HashMaps for lookup in python, but I am sure many users want to acces SBases via Id or metaId in a fast manner.
An example is for instance retrieving objects ports are pointing to in comp.

If there is some way to access SBases in O(1) in the current libsbml please let me know?

Best Matthias


Frank T. Bergmann

unread,
Mar 13, 2019, 8:55:54 AM3/13/19
to libsbml-development
Hello Matthias, 

there is no such mechanism in libSBML that does it for you (and i'm not sure i would want to have that always when the model is still changing). What we have on the other hand is: 

allElements = doc.getListOfAllElements()

that will give you all elements, and from there you could once walk over it getting all ids / metaids for building your map. This is essentially what the getById / getByMetaId functions are doing. 

cheers
Frank

Matthias König

unread,
Mar 13, 2019, 11:55:48 AM3/13/19
to libsbml-development
Hi Frank,
thanks, this solves my problem.
Best Matthias
Reply all
Reply to author
Forward
0 new messages