efficient idiomatic queue?
Terry Reedy
tjreedy at home.com
Wed Jan 16 14:11:41 EST 2002
"Alex Martelli" <aleax at aleax.it> wrote in message
news:a23cjl$dps$1 at serv1.iunet.it...
> class DictFifo:
> def __init__(self):
> self.data = {}
> self.nextin = 0
> self.nextout = 0
> def append(self, value):
> self.data[self.nextin] = value
> self.nextin += 1
> def pop(self):
> value = self.data[self.nextout]
> del self.data[self.nextout]
> self.nextout += 1
This clearly indicates the point of abstract data types separating
inplementation from interface. In the real world, there are at least
two ways of implementing FIFO (first-in,first-out) service discipline.
One is to keep people in a physical line (sequence). Another is to
'label' people in their order of entry -- for instance by giving them
or having them take a sequenced ticket -- and call them in order
while letting them physically mill around before getting called.
DictInfo above, while counter-intuitive to me at first, nicely mirrors
the second method, with sequential counts being the numbered
'tickets'.
Terry J. Reedy
More information about the Python-list
mailing list