doesanyoneknowaboutafastorderedsetimplementationforpythonthat :
remembers内部订单
has an index () method (liketheonelistsoffer ) )。
allimplementationsifoundaremissingthe.index () method。
解决方案
youcanalwaysadditinasubclass.hereisabasicimplementationfortheorderedsetyoulinkedinacomment 3360
classindexorderedset (有序集) :
if key in self.map:
returnnext(IforI,einenumerate(self ) if e==elem ) ) ) ) ) ) ) ) ) ) )。
else:
raisekeyerror(thatelementisn ) tintheset ) )。
You mentioned you only need add,index,and in-order iteration.youcangetthisbyusinganordereddictasstorage.asa bonus, youcansubclassthecollections.setabstractclasstogettheothersetoperationsfrozensetssupport 3360
from itertools import count,izip
fromcollectionsimportordereddict,Set
类xorderedset (set ) :
“' ' An OrderedFrozenSet-like object
分配常数时间索引
but doesn ' tallowyoutoremoveelements ' ' '
self.num=count (
self.dict=有序dict (izip (iterable,self.num ) )
自助在if elem not in :
self.dict[elem]=next(self.num )
return self.dict[elem]
return elem in self.dict
returnlen(self.Dict ) ) ) ) ) ) )。
returniter(self.Dict ) ) ) ) ) ) )。
return'indexorderedset({} ).format (self.dict.keys ) )
you can ' tsubclasscollections.mutablesetbecauseyoucan ' tsupportremovingelementsfromthesetandkeeptheindexescorrect。