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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Stack - A Stack 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>mxStack - A Stack Implementation for Python</H2>
<HR SIZE=1 NOSHADE WIDTH="100%">
<TABLE WIDTH="100%">
<TR>
<TD>
<SMALL>
<A HREF="#Interface">Interface</A> :
<A HREF="#Constants">Constants</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 & 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.1.0</FONT>
</SMALL>
</TD>
</TABLE>
<HR SIZE=1 NOSHADE WIDTH="100%">
<H3>Introduction</H3>
<UL CLASS="indent">
<P>
Though stacks 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>stackbench.py</TT> gives an impression of how
the different methods relate w/r to speed:
<PRE>
mx/Stack> python stackbench.py 1000 100 10
list: 0.24
tuples: 0.15
Stack (with push + pop): 0.17
Stack (with push + pop_many): 0.17
Stack (with << + >>): 0.19
Stack (with push_many + pop_many): 0.17
UserStack: 0.33
</PRE>
<P>
Note that the tuple version has a few disadvantages when
used for big stacks: for one it uses lots of memory (20
bytes per entry slot; Stack uses 20 bytes + 4 bytes per
entry slot) and deallocation can become a problem -- this is
done using recursion with one level per stack element. For
small stacks it still is unbeatable, though (it has no
function call overhead). BTW, the UserStack implementation
uses the same technique: the figures shown mainly result
from Python method call overhead.
<P>
Because stacks are normally used only temporarily, the Stack
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>
The mxStack package defines the following interfaces.
<H4>Stack Constructors</H4>
<UL CLASS="indent">
<P>There are two ways to construct a <TT>Stack</TT> from scratch:
<P><DL>
<DT><CODE><FONT COLOR="#000099">
Stack([initial_size])
</FONT></CODE></DT>
<DD>Returns a new empty Stack instance allocating at least
the given number of slots for stack elements. If the
parameter is not given a reasonable default is
chosen.</DD><P>
<DT><CODE><FONT COLOR="#000099">
StackFromSequence(seq)
</FONT></CODE></DT>
<DD>Constructs a Stack 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 Stack results
in a reversed sequence. </DD><P>
</DL>
</UL><!--CLASS="indent"-->
<H4>Stack Instance Methods</H4>
<UL CLASS="indent">
<P>A <TT>Stack</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 stack.</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 stack. If errors occur during this process,
the already pushed elements are discarded from the stack
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 stack.</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 stack, the tuple will contain all stack entries and
the stack 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 stack's content as tuple, without modifying
it.</DD><P>
<DT><CODE><FONT COLOR="#000099">
as_list()</FONT></CODE></DT>
<DD>
Returns the stack's content as list, without modifying
it.</DD><P>
<DT><CODE><FONT COLOR="#000099">
clear()</FONT></CODE></DT>
<DD>
Clears the stack.</DD><P>
<DT><CODE><FONT COLOR="#000099">
resize([size=len(stack)])</FONT></CODE></DT>
<DD>
Resize the stack buffer to hold at least <CODE>size</CODE>
entries.
<P>
You can call this method without argument to force the
stack to shrink its memory buffer to the minimal limit
needed to hold the contained elements. </DD><P>
<DT><CODE><FONT COLOR="#000099">
__getitem__(index)</FONT></CODE></DT>
<DD>
This is not really a method, but a slot providing access
to the items on the Stack without popping them off the
Stack.
<P>
<CODE>index</CODE> works just like for Python lists,
i.e. negative indices are normalized using the current
length of the Stack.
<P>
An <CODE>IndexError</CODE> is raised for invalid
indices. This makes the Stack compatible to the
<CODE>for</CODE>-loop statement allowing you to iterate
over the Stack contents from bottom to top.
</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 Stack 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="Constants">
<H4>Constants</H4>
<UL CLASS="indent">
<P>
<DL>
<DT><CODE><FONT COLOR="#000099">
Error</FONT></CODE></DT>
<DD>
Error class used for package specific errors. It is a
subclass of exceptions.IndexError.</DD><P>
<DT><CODE><FONT COLOR="#000099">
EmptyError</FONT></CODE></DT>
<DD>
Error class used to signal an empty queue. It is a
subclass of Error.</DD><P>
</DL>
<P>
</UL><!--CLASS="indent"-->
</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.Stack import *
s = Stack()
for i in range(1000):
s.push(i)
while s:
print s.pop()
# which could also be done as:
s = StackFromSequence(range(1000))
while s:
print s.pop()
# or a little different
s = StackFromSequence(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>mxStack.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 "mxStack.h"
...
PyObject *v;
/* Import the mxStack module */
if (mxStack_ImportModuleAndAPI())
goto onError;
/* Access functions from the exported C API through mxStack */
v = mxStack.Stack(0);
if (!v)
goto onError;
/* Type checking */
if (mxStack_Check(v))
printf("Works.\n");
Py_DECREF(v);
...
</PRE>
<P>
</UL><!--CLASS="indent"-->
<A NAME="Structure">
<H3>Package Structure</H3>
<UL CLASS="indent">
<PRE>
[Stack]
mxStack
</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 Stack imports all symbols from the extension
mxStack, so <CODE>import Stack; s = Stack.Stack()</CODE> gives
you a Stack 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 & License</H3>
<UL CLASS="indent">
<P>
© 1998-2000, Copyright by Marc-André Lemburg;
All Rights Reserved. mailto: <A
HREF="mailto:mal@lemburg.com">mal@lemburg.com</A>
<P>
© 2000-2004, 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>Changes from version 2.0.3 to 2.1.0:
<UL>
<LI> Documented the exception objects used by the package.
Added EmptyError exception.
</UL>
<P>There were no significant changes between 2.0.2 and 2.0.3.
<P>Changes from 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 Stack objects to other objects
without raising a TypeError.
</UL>
<P>Changes from <A HREF="mxStack-0.2.zip">0.2.2</A> to 2.0.0:
<UL>
<LI>Added .pop_many(), .resize() and .clear().
<P><LI>Added mxStack_PopMany, mxStack_PushMany,
mxStack_Clear, mxStack_Length to the C API.
<P><LI>Fixed a memory leak in StackFromSequence().
<P><LI>Fixed in bug in the non-zero testing code.
<P><LI>Added __getitem__ slot and mxStack_GetItem C API.
After an idea by Adam Deprince.
<P><LI> <B>Moved</B> the package under a new top-level
package 'mx'. It is part of the <I>eGenix.com mx BASE
distribution</I>.
</UL>
<P>Changes from <A HREF="mxStack-0.1.zip">0.1</A> to 0.2.2:
<UL>
<LI>Version 0.2.2: Fixed a bug that caused stack.push()
to dump core when called without argument. Also added some
minor speedups.
<P><LI>Version 0.2.1: added method pop_many().
<P><LI>Converted the module into a package called
'Stack'. This imports the C extension mxStack.
</UL>
<P>
</UL><!--CLASS="indent"-->
<HR WIDTH="100%">
<CENTER><FONT SIZE=-1>
<P>
© 1998-2000, Copyright by Marc-André Lemburg;
All Rights Reserved. mailto: <A
HREF="mailto:mal@lemburg.com">mal@lemburg.com</A>
<P>
© 2000-2004, 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>
|