efficient idiomatic queue?

Alex Martelli aleax at aleax.it
Wed Jan 16 03:11:34 EST 2002


"David Eppstein" <eppstein at ics.uci.edu> wrote in message
news:eppstein-3B0408.21320615012002 at news.service.uci.edu...
> In article <a22rhr$bn1$1 at panix3.panix.com>, aahz at panix.com (Aahz Maruch)
> wrote:
>
> > Just to be a jerk, why not use a dict with a couple of index variables?
>
> Maybe you could unpack that a little? dicts are useful but AFAIK they
> aren't the same thing as queues, index variables or no.

Well, they obviously CAN be used to IMPLEMENT queues -- performance
characteristics apart:

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

No "being the same" asserted or implied, of course -- the dict and
the two indices are just tools to _implement_ a fifo queue, here.


Alex






More information about the Python-list mailing list