Go to the documentation of this file.00001
00002 class OrderedSet(list):
00003 """Naive and minimalist implementation of an ordered set"""
00004
00005 def __init__(self, iterable=None):
00006 """x.__init__(...) initializes x."""
00007 if iterable != None:
00008 self.update(iterable)
00009
00010 def __sub__(self, orderedset):
00011 """x.__sub__(y) <==> x-y"""
00012
00013 if not isinstance(orderedset, OrderedSet):
00014 return OrderedSet()
00015 result = OrderedSet()
00016 for x in self:
00017 if not (x in orderedset):
00018 result.append(x)
00019 return result
00020
00021 def add(self, item):
00022 """Add an element to the ordered set"""
00023 if item in self:
00024 return
00025 self.append(item)
00026
00027 def difference(self, orderedset):
00028 """Return the difference of two ordered sets as a new ordered set."""
00029 return (self - orderedset)
00030
00031 def update(self, iterable):
00032 """Update an ordered set with the union of itself and another."""
00033 for x in iterable:
00034 self.add(x)