473,569 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fifo queue

hi,

how would u improve this code?

class queue:
def __init__(self, size=8):
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.exten d( self.data[:self.tail] )
self.data.exten d( [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.

Mar 18 '07 #1
4 5535
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.
Mar 18 '07 #2
drochom schreef:
hi,

how would u improve this code?

class queue:
...
I think I'd use collections.deq ue [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
Mar 18 '07 #3
Paul Rubin <http://ph****@NOSPAM.i nvalidwrote:
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(collectio ns.deque):
push = collections.deq ue.append
pop = collections.deq ue.popleft

could be even better.
Alex
Mar 18 '07 #4
"drochom" <dr*****@google mail.comwrote:
>
how would u improve this code?
I would add at least one comment...
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Mar 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3069
by: Luca | last post by:
I have the need of a container of integers showing both the characteristics of an associative container (all integer elements different from each other) and the FIFO behaviour. Do you know if there is a ready-made container for that or if I have to use - for example - a set<int> and produce by myself FIFO behaviour, or maybe I can use a...
2
3079
by: worli | last post by:
Hi All, I have a strange requirement. I have a dynamic input numeric data stream e.g. 2, 2, 4, 5 etc.... ( each input number range from 1 to 10 ). lets take a simple case where all inputs will have same value = 1 ( which may not be the case ) I need to create a FIFO queue in which at any given time I need the total element value = 10. (...
2
8990
by: Michele Moccia | last post by:
How can I implement a "time critical" fifo in c++ ? I just have to manage sequences of raw bytes, no user defined types. An std::queue<unsigned char> or std::deque<unsigned char> seems to be slow. I've compared that to a platform depended solution (i tried to use a MS Window pipe in the same process to create a fifo and it's very faster). ...
8
14186
by: Jack | last post by:
I want to implement a fixed-size FIFO queue for characters. I only want to use array not linked list. For example, const int N = 10; char c_array; The question is when the queue is full, there are ten characters in the
5
3258
by: Dinsdale | last post by:
I have an application that recieves text data via external input (i.e. serial) and displays it on the screen (we use carraige return as a delimiter). At this point I use a regular old text box and when the text size gets too big I truncate the string and re-set the TextBox.Text property. This solution is very crappy as it creates a flickering...
4
2332
by: David Bear | last post by:
I'm looking to see if there are any examples or prewritting fifo queue classes. I know this is a broad topic. I'm looking to implement a simple application where a web server enqueue and pickle using a local socket on to a 'queue server' -- and then I will have another application dequeue the pickles again using a local socket. -- David...
0
1742
by: kmoeWW | last post by:
Hi all, I'm using the PICC CCS C compiler and am having issues with starting an enque/deque for my program. What I need it to do is to hold about 9 data packets, each packet is an array of 4 HEX integers. Each array is a different command for the program. example of array commands: int ph = {0x18, 0x2B, 0x00, 0x00}; int zt =...
2
7803
by: Spoon | last post by:
Hello, I'm wondering whether the STL defines a data structure with the following features: o provides push_front() and pop_back() (like std::list) to implement a FIFO buffer. o allows fast lookup (like std::map) so I can easily search for a specific item in the buffer.
5
4382
by: akenato | last post by:
Hi for me this is th first time that I use c++. I have to do this exercise. Somebody can help me? Thanx. Implement a FIFO-queue ( first in, first out ) as a ring buffer. Use a static array as the data structure. The program needs to be able to add elements to the end of the queue, tell the value of the oldest element and remove from the queue....
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.