³ò î/,?c@s9dZddklZddkZddd„ƒYZdS(sH set.py: Generic set object for Python $Revision: 1.8 $ $Date: 2003/05/28 21:41:20 $ This object acts like an algebraic set, but the members must be immutable---that is, numbers, strings, or tuples of immutable elements. The advantage of restricting members to tuples is that we can implement the set by using the members as keys in a dictionary. This means that random-access functions, such as testing for membership and deleting, have time complexity O(log n). Exports: Set(*L): [ L is a sequence of immutable objects -> return a new Set object containing the unique members of L ] Note: In Python 2.2 and beyond, if you want to pass a pre-made list X to this constructor, use `Set(*X)' .__contains__(self, x): # x in self [ x is immutable -> if x is in self -> return 1 else -> return 0 ] .__delitem__(self,x): # del self[x] [ x is immutable -> if self contains x -> self := self - {x} else -> raise KeyError ] .__iter__(self): # for x in self: ... [ return a new generator that yields all the members of self ] Note: Any changes to self after the generator is activated will not affect the generated sequence. .__repr__(self): # repr(self) [ return self displayed as a string ] .__str__(self): # str(self) [ return self displayed as a string ] .add(x): [ x is immutable -> self := self union {x} ] .contains(x): [ same as self.__contains__(x) ] .delete(x): [ x is immutable -> if x is a member of self -> self := self - {x} ] else -> I ] .diff(s): [ s is a Set object -> return a new Set containing elements of self not found in s ] .genSorted(comparator=None): [ comparator is a function with the same calling sequence as cmp(), whose domain includes all members of self, defaulting to cmp() -> return a new generator that yields all the members of self, sorted by the comparator function ] Note: Any changes to self after the generator is activated will not affect the generated sequence. .intersect(s): [ if s is a Set object -> return a new Set containing elements in both self and s ] .members(): [ return a new list of the members of self ] .union(s): [ if s is a Set object -> return a new Set containing all unique elements of self combined with s ] iÿÿÿÿ(t generatorsNtSetcBsŒeZdZd„Zd„Zd„Zd„Zd„ZeZd„Z d„Z d„Z d „Z e d „Zd „Zd „Zd „ZRS(s Generic set object Invariant: self.__map: a dictionary whose keys are the elements contained in self and whose values are all None cGs6h|_|o"x|D]}|i|ƒqWndS(s'Constructor for the Set object N(t _Set__maptadd(tselftLtx((s(/u/www/docs/tcc/projects/pystyler/set.pyt__init__Ws cCs|ii|ƒS(sMembership test, `x in self'(Rthas_key(RR((s(/u/www/docs/tcc/projects/pystyler/set.pyt __contains__hscCs|i|=dS(s6Delete x from self. Raises KeyError if x not in self.N(R(RR((s(/u/www/docs/tcc/projects/pystyler/set.pyt __delitem__osccs)x|iiƒD] }|VqWt‚dS(sGenerate the elements of self in no particular order. Note: If self.__map is changed after the generator starts running, and before it completes, the generated set is that at the time __iter__ was first called. N(Rtkeyst StopIteration(Rtkey((s(/u/www/docs/tcc/projects/pystyler/set.pyt__iter__vs  cCs3|iiƒ}|iƒdditt|ƒƒS(s=Display self. Items are sorted for user convenience s{%s}s, (RR tsorttjointmaptstr(RR((s(/u/www/docs/tcc/projects/pystyler/set.pyt__str__ˆs cCsd|i|Ds