File: mxQueue.html

package info (click to toggle)
egenix-mx-base 2.0.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,028 kB
  • ctags: 4,762
  • sloc: ansic: 14,965; python: 11,739; sh: 313; makefile: 117
file content (398 lines) | stat: -rw-r--r-- 10,703 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
   <TITLE>Queue - A Queue Implementation for Python</TITLE>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    <STYLE TYPE="text/css">
      p { text-align: justify; }
      ul.indent { }
      body { }
    </STYLE>
</HEAD>

  <BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000">

    <HR NOSHADE WIDTH="100%">
    <H2>mxQueue - A Queue Implementation for Python</H2>

    <HR SIZE=1 NOSHADE WIDTH="100%">
    <TABLE WIDTH="100%">
      <TR>
	<TD>
	  <SMALL>
	    <A HREF="#Interface">Interface</A> :
	    <A HREF="#Examples">Examples</A> :
	    <A HREF="#API">C API</A> :
	    <A HREF="#Structure">Structure</A> :
	    <A HREF="#Support">Support</A> :
	    <A HREF="http://www.egenix.com/files/python/eGenix-mx-Extensions.html#Download-mxBASE"><B>Download</B></A> :
	    <A HREF="#Copyright">Copyright &amp; License</A> :
	    <A HREF="#History">History</A> :
	    <A HREF="" TARGET="_top">Home</A>
	</SMALL>
	</TD>
	<TD ALIGN=RIGHT VALIGN=TOP>
	  <SMALL>
	    <FONT COLOR="#FF0000">Version 2.0.3</FONT>
	  </SMALL>
	</TD>
    </TABLE>
    <HR SIZE=1 NOSHADE WIDTH="100%">

    <H3>Introduction</H3>

    <UL CLASS="indent">

      <HR>

	<P>
	  <B>XXX This documentation is unfinished...</B> 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 :-).

	<HR>

	<P>
	  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 <TT>queuebench.py</TT> gives an impression of how
	  the different methods relate w/r to speed:

	<PRE>
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 &lt;&lt; + &gt;&gt;): 0.85
Queue (with push_many + pop_many): 0.47
UserQueue: 1.79
	</PRE>

	<P>
	  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.

	<P>
	  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.

	<P>

    </UL><!--CLASS="indent"-->

    <A NAME="Interface">

    <H3>Interface</H3>

    <UL CLASS="indent">

	<P><B>Queue Constructors</B>

	<P>There are two ways to construct a <TT>Queue</TT> from scratch:

	<P><DL>

	  <DT><CODE><FONT COLOR="#000099">

		Queue([initial_size])

	      </FONT></CODE></DT>

	  <DD>Returns a new empty Queue instance allocating at least
	  the given number of slots for queue elements. If the
	  parameter is not given a reasonable default is
	  chosen.</DD><P>

	  <DT><CODE><FONT COLOR="#000099">

		QueueFromSequence(seq)

	      </FONT></CODE></DT>

	  <DD>Constructs a Queue instance from the given sequence. The
	  instance is filled with all the elements found in the
	  sequence by pushing the items from index 0 to len(seq)-1 in
	  that order, i.e. popping all elements from the Queue results
	  in a reversed sequence. </DD><P>

	</DL>

	  <B>Instance Methods</B>

	<P>A <TT>Queue</TT> instance has the following methods:

	<P><DL>

	  <DT><CODE><FONT COLOR="#000099">
		push(x)</FONT></CODE></DT>

	  <DD>
	    Pushes the object x onto the queue.</DD><P>

	  <DT><CODE><FONT COLOR="#000099">
		push_many(sequences)</FONT></CODE></DT>

	  <DD>
	    Pushes the objects in <CODE>sequence</CODE> 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.</CODE></DD><P>

	  <DT><CODE><FONT COLOR="#000099">
		pop()</FONT></CODE></DT>

	  <DD>
	    Pops the top element off of the queue.</DD><P>

	  <DT><CODE><FONT COLOR="#000099">
		pop_many(n)</FONT></CODE></DT>

	  <DD>
	    Pops the top <CODE>n</CODE> elements and returns them in
	    form of a tuple. If less than <CODE>n</CODE> 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. <CODE>s.pop_many(2) ==
	    (s.pop(),s.pop())</CODE></DD><P>

	  <DT><CODE><FONT COLOR="#000099">
		as_tuple()</FONT></CODE></DT>

	  <DD>
	    Returns the queue's content as tuple, without modifying
	    it.</DD><P>

	  <DT><CODE><FONT COLOR="#000099">
		as_list()</FONT></CODE></DT>

	  <DD>
	    Returns the queue's content as list, without modifying
	    it.</DD><P>

	  <DT><CODE><FONT COLOR="#000099">
		clear()</FONT></CODE></DT>

	  <DD>
	    Clears the queue.</DD><P>

	  <DT><CODE><FONT COLOR="#000099">
		resize([size=len(queue)])</FONT></CODE></DT>

	  <DD>
	    Resize the queue buffer to hold at least <CODE>size</CODE>
	    entries.
	    <P>
	      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. </DD><P>

	</DL>

	<P>Note that no method for testing emtpyness is provided. Use
	len() for that or simply test for trueness, e.g. <CODE>while
	s: print s.pop()</CODE> 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.

	<P>

    </UL><!--CLASS="indent"-->

    <A NAME="Examples">

    <H3>Examples of Use</H3>

    <UL CLASS="indent">

	<P>Well, there's not much to show:

	<FONT COLOR="#000099"><PRE>
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()
	</PRE></FONT>

    </UL><!--CLASS="indent"-->

    <A NAME="API">

    <H3>Supported Data Types in the C-API</H3>

    <UL CLASS="indent">

	<P>Please have look at the file <TT>mxQueue.h</TT> for
	details.  Basically all of the above Python interfaces are
	also available in the C API.

	<P>To access the module, do the following (note the
	similarities with Python's way of accessing functions from a
	module):

	<PRE>
#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);
...
	</PRE>
	<P>

    </UL><!--CLASS="indent"-->

    <A NAME="Structure">

    <H3>Package Structure</H3>

    <UL CLASS="indent">

	<PRE>
[Queue]
	mxQueue
	</PRE>

	<P>Entries enclosed in brackets are packages (i.e. they are
	directories that include a <TT>__init__.py</TT> file). Ones
	without brackets are just simple subdirectories that are not
	accessible via <CODE>import</CODE>. 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. <TT>/usr/local/lib/python-x.xx/site-packages</TT>).

	<P>The package Queue imports all symbols from the extension
	mxQueue, so <CODE>import Queue; s = Queue.Queue()</CODE> gives
	you a Queue instance in <CODE>s</CODE>.

	<P>

    </UL><!--CLASS="indent"-->

    <A NAME="Support">

    <H3>Support</H3>

    <UL CLASS="indent">

	<P>
	  eGenix.com is providing commercial support for this
	  package. If you are interested in receiving information
	  about this service please see the <A
	  HREF="http://www.egenix.com/files/python/eGenix-mx-Extensions.html#Support">eGenix.com
	  Support Conditions</A>.

    </UL><!--CLASS="indent"-->

    <A NAME="Copyright">

    <H3>Copyright &amp; License</H3>

    <UL CLASS="indent">

	<P>
	  &copy; 1999-2000, Copyright by Marc-Andr&eacute; Lemburg;
	  All Rights Reserved.  mailto: <A
	  HREF="mailto:mal@lemburg.com">mal@lemburg.com</A>
	<P>
	  &copy; 2000-2001, Copyright by eGenix.com Software GmbH,
	  Langenfeld, Germany; All Rights Reserved.  mailto: <A
	  HREF="mailto:info@egenix.com">info@egenix.com</A>

	<P>
	  This software is covered by the <A
	  HREF="mxLicense.html#Public"><B>eGenix.com Public
	  License Agreement</B></A>. The text of the license is also
	  included as file "LICENSE" in the package's main directory.

	<P>
	  <B> 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. </B>

    </UL><!--CLASS="indent"-->

    <A NAME="History">

    <H3>History & Future</H3>

    <UL CLASS="indent">
      
        <P>There were no significant changes between 2.0.2 and 2.0.3.

	<P>Changes from version 2.0.0 to 2.0.2:

	<UL>

	  <LI> Fixed a bug in the coercion code which surfaced due to
	  the rich comparison changes in Python 2.1. Python 2.1 will
	  now compare Queue objects to other objects without raising a
	  TypeError.
	    
	</UL>

	<P>Version 2.0.0 was the initial public version.

    </UL><!--CLASS="indent"-->

    <HR WIDTH="100%">
    <CENTER><FONT SIZE=-1>
        <P>
          &copy; 1999-2000, Copyright by Marc-Andr&eacute; Lemburg;
          All Rights Reserved.  mailto: <A
          HREF="mailto:mal@lemburg.com">mal@lemburg.com</A>
        <P>
          &copy; 2000-2001, Copyright by eGenix.com Software GmbH; 
          All Rights Reserved.  mailto: <A
          HREF="mailto:info@egenix.com">info@egenix.com</A>
    </FONT></CENTER>
    </FONT></CENTER>

  </BODY>
</HTML>