[Python-checkins] python/dist/src/Lib sets.py,1.3,1.4
gvanrossum@users.sourceforge.net
gvanrossum@users.sourceforge.net
Tue, 20 Aug 2002 14:38:39 -0700
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv2530
Modified Files:
sets.py
Log Message:
Move __init__ from BaseSet into Set and ImmutableSet. This causes a
tiny amount of code duplication, but makes it possible to give BaseSet
an __init__ that raises an exception.
Index: sets.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/sets.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** sets.py 20 Aug 2002 20:05:23 -0000 1.3
--- sets.py 20 Aug 2002 21:38:37 -0000 1.4
***************
*** 64,81 ****
# Constructor
! def __init__(self, seq=None):
! """Construct a set, optionally initializing it from a sequence."""
! self._data = {}
! if seq is not None:
! # I don't know a faster way to do this in pure Python.
! # Custom code written in C only did it 65% faster,
! # preallocating the dict to len(seq); without
! # preallocation it was only 25% faster. So the speed of
! # this Python code is respectable. Just copying True into
! # a local variable is responsible for a 7-8% speedup.
! data = self._data
! value = True
! for key in seq:
! data[key] = value
# Standard protocols: __len__, __repr__, __str__, __iter__
--- 64,73 ----
# Constructor
! def __init__(self):
! """This is an abstract class."""
! # Don't call this from a concrete subclass!
! if self.__class__ is BaseSet:
! raise NotImplementedError, ("BaseSet is an abstract class. "
! "Use Set or ImmutableSet.")
# Standard protocols: __len__, __repr__, __str__, __iter__
***************
*** 290,296 ****
def __init__(self, seq):
"""Construct an immutable set from a sequence."""
- # Override the constructor to make 'seq' a required argument
- BaseSet.__init__(self, seq)
self._hashcode = None
def __hash__(self):
--- 282,298 ----
def __init__(self, seq):
"""Construct an immutable set from a sequence."""
self._hashcode = None
+ self._data = data = {}
+ # I don't know a faster way to do this in pure Python.
+ # Custom code written in C only did it 65% faster,
+ # preallocating the dict to len(seq); without
+ # preallocation it was only 25% faster. So the speed of
+ # this Python code is respectable. Just copying True into
+ # a local variable is responsible for a 7-8% speedup.
+ value = True
+ # XXX Should this perhaps look for _as_immutable?
+ # XXX If so, should use self.update(seq).
+ for key in seq:
+ data[key] = value
def __hash__(self):
***************
*** 306,309 ****
--- 308,321 ----
# BaseSet + operations requiring mutability; no hashing
+
+ def __init__(self, seq=None):
+ """Construct an immutable set from a sequence."""
+ self._data = data = {}
+ if seq is not None:
+ value = True
+ # XXX Should this perhaps look for _as_immutable?
+ # XXX If so, should use self.update(seq).
+ for key in seq:
+ data[key] = value
# In-place union, intersection, differences