disjoint-set
disjoint-set copied to clipboard
Add __len__ function
I just needed this function and quickly implemented it. I thought it might be helpful for the data structure.
My REPL:
>>> def len_d(self):
... i = 0
... for key in self._data:
... if key == self._data[key]:
... i += 1
... return i
...
>>> DisjointSet.__len__ = len_d
>>> list(d.itersets())
[{1, 2}, {3}, {4}, {5}]
>>> len(d)
4
>>> d.union(3,4)
>>> list(d.itersets())
[{1, 2}, {3, 4}, {5}]
>>> len(d)
3
>>> d.union(5,4)
>>> list(d.itersets())
[{1, 2}, {3, 4, 5}]
>>> len(d)
2