Interface : Examples : C API : Structure : Support : Download : Copyright & License : History : Home | Version 2.0.3 |
XXX This documentation is unfinished... it is basically a search&replace copy of the mxStack documentation since the mxQueue APIs are very similar to mxStack. Usage should be straight forward though. The next release will have proper documentation and also be advertised on the download page :-).
Though queues can be emulated with Python lists, this type provides a simple interface to the data structure, both in Python and in C. Because of the function call overhead calling the methods from Python it is only a tad faster than a corresponding list emulation. Called from within an C extension shows a more significant performance increase. The included queuebench.py gives an impression of how the different methods relate w/r to speed:
projects/Queue> python1.5 -O queuebench.py 1000 100 100 list: 1.11 tuples: 0.6 Queue (with push + pop): 0.72 Queue (with push + pop_many): 0.5 Queue (with << + >>): 0.85 Queue (with push_many + pop_many): 0.47 UserQueue: 1.79
Note that the tuple version has a few disadvantages when used for big queues: for one it uses lots of memory (20 bytes per entry slot; Queue uses 20 bytes + 4 bytes per entry slot) and deallocation can become a problem -- this is done using recursion with one level per queue element. For small queues it still is unbeatable, though (it has no function call overhead). BTW, the UserQueue implementation uses the same technique: the figures shown mainly result from Python method call overhead.
Because queues are normally used only temporarily, the Queue implementation only grows the memory buffer used for holding the entry slots. It never shrinks it. This has an advantage of reducing malloc overhead when doing e.g. depth first search, but also the disadvantage of using more memory in degenerate cases. To compensate for this, simply call the .resize() method every now and then. It forces the used buffer to be resized.
Queue Constructors
There are two ways to construct a Queue from scratch:
A Queue instance has the following methods:
You can call this method without argument to force the
queue to shrink its memory buffer to the minimal limit
needed to hold the contained elements.
Note that no method for testing emtpyness is provided. Use
len() for that or simply test for trueness, e.g.
Well, there's not much to show:
Please have look at the file mxQueue.h for
details. Basically all of the above Python interfaces are
also available in the C API.
To access the module, do the following (note the
similarities with Python's way of accessing functions from a
module):
Entries enclosed in brackets are packages (i.e. they are
directories that include a __init__.py file). Ones
without brackets are just simple subdirectories that are not
accessible via The package Queue imports all symbols from the extension
mxQueue, so
eGenix.com is providing commercial support for this
package. If you are interested in receiving information
about this service please see the eGenix.com
Support Conditions.
© 1999-2000, Copyright by Marc-André Lemburg;
All Rights Reserved. mailto: mal@lemburg.com
© 2000-2001, Copyright by eGenix.com Software GmbH,
Langenfeld, Germany; All Rights Reserved. mailto: info@egenix.com
This software is covered by the eGenix.com Public
License Agreement. The text of the license is also
included as file "LICENSE" in the package's main directory.
By downloading, copying, installing or otherwise using
the software, you agree to be bound by the terms and
conditions of the eGenix.com Public License
Agreement.
There were no significant changes between 2.0.2 and 2.0.3.
Changes from version 2.0.0 to 2.0.2:
Version 2.0.0 was the initial public version.
© 1999-2000, Copyright by Marc-André Lemburg;
All Rights Reserved. mailto: mal@lemburg.com
© 2000-2001, Copyright by eGenix.com Software GmbH;
All Rights Reserved. mailto: info@egenix.com
Interface
Instance Methods
Queue([initial_size])
QueueFromSequence(seq)
push(x)
push_many(sequences)
sequence
from left to
right onto the queue. If errors occur during this process,
the already pushed elements are discarded from the queue
and it returns to its original state.
pop()
pop_many(n)
n
elements and returns them in
form of a tuple. If less than n
elements are
on the queue, the tuple will contain all queue entries and
the queue will then be empty again. The order is top to
bottom, i.e. s.pop_many(2) ==
(s.pop(),s.pop())
as_tuple()
as_list()
clear()
resize([size=len(queue)])
size
entries.
while
s: print s.pop()
will loop as long as there are
elements left on the Queue s. This is much faster than going
through the method calling process -- even when the method
being called is written in C.
Examples of Use
from mx.Queue import *
s = Queue()
for i in range(1000):
s.push(i)
while s:
print s.pop()
# which could also be done as:
s = QueueFromSequence(range(1000))
while s:
print s.pop()
# or a little different
s = QueueFromSequence(range(1000))
print s.as_tuple()
print s.as_list()
Supported Data Types in the C-API
#include "mxQueue.h"
...
PyObject *v;
/* Import the mxQueue module */
if (mxQueue_ImportModuleAndAPI())
goto onError;
/* Access functions from the exported C API through mxQueue */
v = mxQueue.Queue(0);
if (!v)
goto onError;
/* Type checking */
if (mxQueue_Check(v))
printf("Works.\n");
Py_DECREF(v);
...
Package Structure
[Queue]
mxQueue
import
. These are used for
compiling the C extension modules which will get installed in
the same place where all your other site specific extensions
live (e.g. /usr/local/lib/python-x.xx/site-packages).
import Queue; s = Queue.Queue()
gives
you a Queue instance in s
.
Support
Copyright & License
History & Future