faster entId allocation

This commit is contained in:
Darren Ranalli 2004-01-27 00:54:31 +00:00
parent 3576ea4cc9
commit 4927912722

View File

@ -1,7 +1,7 @@
"""EditMgrAI module: contains the EditMgrAI class"""
import EditMgrBase
if __debug__:
if __dev__:
from PythonUtil import list2dict
import EditorGlobals
@ -14,20 +14,37 @@ class EditMgrAI(EditMgrBase.EditMgrBase):
entIds = spec.getAllEntIds()
entIdDict = list2dict(entIds)
# dumb linear search for now
# TODO: make this smarter (cache last-allocated id)
# Note that this uses the ID range associated with the
# AI's username, not the username of the user who requested
# the new entity.
for id in xrange(*EditorGlobals.getEntIdAllocRange()):
allocRange = EditorGlobals.getEntIdAllocRange()
if not hasattr(self, 'lastAllocatedEntId'):
self.lastAllocatedEntId = allocRange[0]
idChosen = 0
while not idChosen:
# linear search for an unused entId starting with the
# last-allocated id
for id in xrange(self.lastAllocatedEntId, allocRange[1]):
print id
if not id in entIdDict:
idChosen = 1
break
else:
# we ran off the end of the range.
if self.lastAllocatedEntId != allocRange[0]:
# if we started in the middle, try again from
# the beginning
self.lastAllocatedEntId = allocRange[0]
else:
# every entId is used!!
self.notify.error('out of entIds')
# OK, we've chosen an unused entId. Add the entId to the data
# dict and do the insert
data.update({'entId': id})
self.lastAllocatedEntId = id
self.level.setAttribChange(self.entId, 'insertEntity', data)
# clear out the attrib, it shouldn't be kept in the spec