[NEWBIE] Priority Queue in Python
Scottie
me at nospam.net
Sun Feb 11 04:38:04 EST 2001
You could look at the pre-release code in PriQueue.sourceforge.net for an
example of the heap version. I have a few changes yet in mind. "skim"
rather than "cream" for the faster equivalent of:
def ?(self,x): self.push(x); return self.pop()
This I only decided on as I started implementing double-ended queues and
determined that skim has dredge as a reasonable "other-ended" verb. The
last interface decision is in exposure of the contents. I am leaning
towards a sequence-like interface (so index will work to find elements),
allowing delete of single internal elements (after which everything moves
appropriately), and re-prioritizing by replacing an entry by one with a new
priority (which also has the slightly unsettling effect of moving things
around on you, so that after:
p,v = pq[N]
pq[N] = (p+1,v)
you typically have pq[N] with some completely unrelated value, and (p+1,v)
is at some completely different index, M. Of course very few ranges make
sense. pq[:] works, as does pq[1:], but I can think of very few others.
I have the code to expand/contract the array at less frequent times, but
have left it out until I'm happy with the interface itself. Anybody have
any comments on the interface as is?
-Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list