class EquivalenceGrouper

Last modified: October 27, 2015

Contents

EquivalenceGrouper

In module gamera.toolkits.musicstaves.equivalence_grouper

This class provides an easy way to parttion a set into equivalence classes, i.e. disjoint subsets where the members in each subset are equivalent.

Objects can be joined using .join(), tested for equivalence using .joined(), and all disjoint sets can be retrieved using an iterator over the class.

The objects being joined must be hashable.

from equivalence_grouper import EquivalenceGrouper

g = EquivalenceGrouper()

# mark items as equivalent
g.join('a', 'b')
g.join('b', 'c')
g.join('d', 'e')

# all classes can be accessed by iterating over g
[c for c in g]
>>> [['a', 'b', 'c'], ['d', 'e']]

# check for equivalence
g.joined('a', 'b')
>>> True
# transitivity is automatically detected
g.joined('a', 'c')
>>> True
g.joined('a', 'd')
>>> False
Author:Michael Droettboom and Christoph Dalitz

__init__

Optionally you can provide a list of items to the constructor which are first considered not to be equivalent. You can use .join() afterwards to mark items as equivalent and/or to add addiitonal items.

join

Join given arguments into the same subset, i.e. mark them as equivalent.

Items not yet part of any subset are added in a new subset. Accepts one or more arguments. To just add a new item x to the superset, just call .join(x).

joined

Returns True if a and b are equivalent, i.e. are members of the same subset (equivalence class).

__iter__

Returns an iterator returning each of the disjoint subsets as a list.