[Patches] [Patch #102627] A Set class which *can* use the .firstkey() patch

noreply@sourceforge.net noreply@sourceforge.net
Wed, 6 Dec 2000 10:46:14 -0800


Patch #102627 has been updated. 

Project: python
Category: library
Status: Open
Submitted by: moshez
Assigned to : Nobody
Summary: A Set class which *can* use the .firstkey() patch

Follow-Ups:

Date: 2000-Dec-03 04:47
By: moshez

Comment:
OK, this patch implements a Set class with an added advantage that on 102626-patched interpreter it has a method called .element() which returns some element of the set.

Short summary:

* supports boolean operators, substraction
* supports iteration semi-efficiently
* supports checking for containment semi-efficiently
* supports auto-freezing when hash() is called, so can
  be used as key in dictionaries, and so, as an element
  in sets.

Greg Wilson: see if this doesn't solve 95% of the problem
you're trying to solve with the Set built-in type PEP.

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

Date: 2000-Dec-06 10:04
By: gvanrossum

Comment:
I thought that instead of firstkey() we were going to implement popitem() instead?
-------------------------------------------------------

Date: 2000-Dec-06 10:46
By: tim_one

Comment:
Changed Category to Library.  Comments:

Style:  don't use hard tabs; add doc strings.

Space efficiency:  "_frozen  = 0" is better at class level (most uses of sets never need it).

__hash__: (1) you can cache the hash if the set is frozen, and in my experience that's important for efficiency.  (2) it doesn't work correctly, although it may take some effort to provoke a failure.  The problem is that you're deferring to hash(_make_elements()), _make_elements() returns a tuple, and the hash of a tuple depends on the order the keys happen to get materialized.  But the hash of a set must be independent of the order the keys happen to get listed.  See the code fragments I posted for a correct Set __hash__.

__ior__:  dict.update is much quicker than explicit iteration.  The ability to apply set operators to raw sequences is unimportant, and not fully supported even if that is your intent (e.g., you don't have the "right-side" operators defined here).

Confusing:  most of the operators are defined in terms of add() and remove().  This will make the error msgs when they're applied to frozen sets confusing, because indirect.  It's also much slower than doing the deed directly.

In short, this looks like the first Set class someone writes off the top of their head in Python.  That isn't bad, but something in the std distribution should be more than a finger exercise.

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

-------------------------------------------------------
For more info, visit:

http://sourceforge.net/patch/?func=detailpatch&patch_id=102627&group_id=5470