self.weights = {}
self.parents = {}
+ def add(self, object, weight):
+ if object not in self.parents:
+ self.parents[object] = object
+ self.weights[object] = weight
+
+ def __contains__(self, object):
+ return object in self.parents
+
def __getitem__(self, object):
"""Find and return the name of the set containing the object."""
# check for previously unknown object
if object not in self.parents:
+ assert(False)
self.parents[object] = object
self.weights[object] = 1
return object
heaviest = max([(self.weights[r], r) for r in roots])[1]
for r in roots:
if r != heaviest:
- self.weights[heaviest] += self.weights[r]
self.parents[r] = heaviest