[Patches] [ python-Patches-1002085 ] O(1) xrange membership testing

SourceForge.net noreply at sourceforge.net
Wed Aug 4 14:51:21 CEST 2004


Patches item #1002085, was opened at 2004-08-02 12:43
Message generated for change (Comment added) made by tim_one
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1002085&group_id=5470

Category: Core (C code)
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Mac-arena the Bored Zo (boredzo)
Assigned to: Nobody/Anonymous (nobody)
Summary: O(1) xrange membership testing

Initial Comment:
this patch applies to anonymous CVS as of 2004-08-02 at 07:
54:58 PDT.

it adds O(1) membership testing; for example:

Python 2.4a1+ (#2, Aug  2 2004, 09:11:43) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on 
darwin
Type "help", "copyright", "credits" or "license" for more 
information.
>>> import sys
>>> sys.maxint
2147483647
>>> sys.maxint in xrange(sys.maxint)
False

in current CVS, this would take untold hours to complete 
(because Python arrives at this answer by iterating on the 
sequence). this patch adds a __contains__ method to the 
xrange object which examines the xrange's pattern and 
determines the correct answer instantly based on that pattern.

----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2004-08-04 08:51

Message:
Logged In: YES 
user_id=31435

You should know that xrange objects *used* to have 
efficient "in" testing, and a whole bunch of other features 
too.  They were deliberately removed in 2.2a1.  Search 
for "xrange" in your Python NEWS file.

----------------------------------------------------------------------

Comment By: Mac-arena the Bored Zo (boredzo)
Date: 2004-08-04 01:59

Message:
Logged In: YES 
user_id=711099

__contains__ never fails anymore; it now falls back on the old 
behaviour of iterating on the sequence when the object being tested 
for membership is not a Python int. (I didn't know it did that before. 
another cool Python feature. :)

also, the new patch is current with anonymous CVS as of 2004-08-03 
at 10:47:53 PM PDT.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2004-08-03 08:50

Message:
Logged In: YES 
user_id=21627

This patch is incorrect. Currently,

"s" in xrange(3,4)

gives False. With the patch, it raises an exception.

Your patch should only take integer objects into account. 
Any other object might have an __eq__ that makes it compare
equal to a number from the range.

----------------------------------------------------------------------

Comment By: Mac-arena the Bored Zo (boredzo)
Date: 2004-08-02 15:42

Message:
Logged In: YES 
user_id=711099

found and fixed a bug: the new __contains__ method didn't check for 
an exception from PyInt_AsLong.

----------------------------------------------------------------------

Comment By: Mac-arena the Bored Zo (boredzo)
Date: 2004-08-02 14:25

Message:
Logged In: YES 
user_id=711099

heh, no I didn't check the checkbox. thanks for reminding me. :)

the original patch didn't include unit tests, but that is a good idea. ;) 
so I've added changes to test_xrange.py.

----------------------------------------------------------------------

Comment By: Ronald Oussoren (ronaldoussoren)
Date: 2004-08-02 13:28

Message:
Logged In: YES 
user_id=580910

You forgot to add the patch (did you check the checkbox?).

BTW. Does your patch include unittests?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1002085&group_id=5470


More information about the Patches mailing list