Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > fifo queue

Reply
Thread Tools

fifo queue

 
 
drochom
Guest
Posts: n/a
 
      03-18-2007
hi,

how would u improve this code?

class queue:
def __init__(self, size=:
self.space = size
self.data = [None]*self.space
self.head = 0
self.tail = 0
self.len = 0
def __len__(self): return self.len
def push(self, x):
if self.len==self.space:
self.data.extend( self.data[:self.tail] )
self.data.extend( [None]* (self.space-self.tail) )
self.tail+=self.space
self.space*=2
self.data[self.tail]=x
self.tail+=1
if self.tail==self.space:
self.tail=0
self.len+=1
def pop(self):
if self.len:
elem = self.data[self.head]
self.head+=1
if self.head==self.space:
self.head=0
return elem
def top(self):
if self.len==0:
raise Exception, 'queue is empty'
return self.data[self.head]

thx in adv.

 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      03-18-2007
Unless the queue is really large, just use the pop operation to get
stuff off the top of the queue. That causes O(n) operations but it
should be fast if n is small.

class queue(list):
push = append
def pop(self):
return list.pop(self,0)

should do about what you wrote.
 
Reply With Quote
 
 
 
 
Roel Schroeven
Guest
Posts: n/a
 
      03-18-2007
drochom schreef:
> hi,
>
> how would u improve this code?
>
> class queue:
> ...


I think I'd use collections.deque [1] instead of using a ring buffer as
you're doing (I think) and doing a lot of manual bookkeeping.

[1] http://docs.python.org/lib/deque-objects.html



--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      03-18-2007
Paul Rubin <http://(E-Mail Removed)> wrote:

> Unless the queue is really large, just use the pop operation to get
> stuff off the top of the queue. That causes O(n) operations but it
> should be fast if n is small.
>
> class queue(list):
> push = append
> def pop(self):
> return list.pop(self,0)
>
> should do about what you wrote.


If it IS large, then:

import collections
class queue(collections.deque):
push = collections.deque.append
pop = collections.deque.popleft

could be even better.


Alex
 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      03-20-2007
"drochom" <(E-Mail Removed)> wrote:
>
>how would u improve this code?


I would add at least one comment...
--
Tim Roberts, http://www.velocityreviews.com/forums/(E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Program blocked in Queue.Queue.get and Queue.Queue.put Kris Python 0 01-04-2012 03:46 PM
FIFO, FIFO, It's Off To Queue We Go... Lawrence D'Oliveiro NZ Computing 7 05-09-2009 08:37 AM
any body having complete code for a synchronous FIFO or know a link where FIFO codes are available chaitu VHDL 1 05-31-2007 03:31 PM
any body having complete code for a synchronous FIFO or know a link where FIFO codes are available chaitu VHDL 0 05-31-2007 02:28 PM
Is Queue.Queue.queue.clear() thread-safe? Russell Warren Python 4 06-27-2006 03:03 PM



Advertisments