class documentation

class DisjointDict(Generic[TKey, TValue]): (source)

View In Hierarchy

An variation of the union-find algorithm/data structure where instead of keeping track of just disjoint sets, we keep track of disjoint dicts -- keep track of multiple Set[Key] -> Set[Value] mappings, where each mapping's keys are guaranteed to be disjoint.

This data structure is currently used exclusively by 'group_comparison_operands' below to merge chains of '==' and 'is' comparisons when two or more chains use the same expression in best-case O(n), where n is the number of operands.

Specifically, the add_mapping() function and items() functions will take on average O(k + v) and O(n) respectively, where k and v are the number of keys and values we're adding for a given chain. Note that k <= n and v <= n.

We hit these average/best-case scenarios for most user code: e.g. when the user has just a single chain like 'a == b == c == d == ...' or multiple disjoint chains like 'a==b < c==d < e==f < ...'. (Note that a naive iterative merging would be O(n^2) for the latter case).

In comparison, this data structure will make 'group_comparison_operands' have a worst-case runtime of O(n*log(n)): 'add_mapping()' and 'items()' are worst-case O(k*log(n) + v) and O(k*log(n)) respectively. This happens only in the rare case where the user keeps repeatedly making disjoint mappings before merging them in a way that persistently dodges the path compression optimization in '_lookup_root_id', which would end up constructing a single tree of height log_2(n). This makes root lookups no longer amoritized constant time when we finally call 'items()'.

Method __init__ Undocumented
Method add​_mapping Adds a 'Set[TKey] -> Set[TValue]' mapping. If there already exists a mapping containing one or more of the given keys, we merge the input mapping with the old one.
Method items Returns all disjoint mappings in key-value pairs.
Method _lookup​_or​_make​_root​_id Undocumented
Method _lookup​_root​_id Undocumented
Instance Variable _id​_to​_parent​_id Undocumented
Instance Variable _key​_to​_id Undocumented
Instance Variable _root​_id​_to​_values Undocumented
def __init__(self): (source)

Undocumented

def add_mapping(self, keys, values): (source)

Adds a 'Set[TKey] -> Set[TValue]' mapping. If there already exists a mapping containing one or more of the given keys, we merge the input mapping with the old one.

Note that the given set of keys must be non-empty -- otherwise, nothing happens.

Parameters
keys:Set[TKey]Undocumented
values:Set[TValue]Undocumented
def items(self): (source)
Returns all disjoint mappings in key-value pairs.
Returns
List[Tuple[Set[TKey], Set[TValue]]]Undocumented
def _lookup_or_make_root_id(self, key): (source)

Undocumented

Parameters
key:TKeyUndocumented
Returns
intUndocumented
def _lookup_root_id(self, key): (source)

Undocumented

Parameters
key:TKeyUndocumented
Returns
intUndocumented
_id_to_parent_id: Dict[int, int] = (source)

Undocumented

_key_to_id: Dict[TKey, int] = (source)

Undocumented

_root_id_to_values: Dict[int, Set[TValue]] = (source)

Undocumented