disjoint-set icon indicating copy to clipboard operation
disjoint-set copied to clipboard

Add __len__ function

Open Roxxik opened this issue 1 year ago • 0 comments

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

Roxxik avatar Apr 03 '23 19:04 Roxxik