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.
(self, keys: set[TKey], values: set[TValue])
| 9352 | self._root_id_to_values: dict[int, set[TValue]] = {} |
| 9353 | |
| 9354 | def add_mapping(self, keys: set[TKey], values: set[TValue]) -> None: |
| 9355 | """Adds a 'Set[TKey] -> Set[TValue]' mapping. If there already exists a mapping |
| 9356 | containing one or more of the given keys, we merge the input mapping with the old one. |
| 9357 | |
| 9358 | Note that the given set of keys must be non-empty -- otherwise, nothing happens. |
| 9359 | """ |
| 9360 | if not keys: |
| 9361 | return |
| 9362 | |
| 9363 | subtree_roots = [self._lookup_or_make_root_id(key) for key in keys] |
| 9364 | new_root = subtree_roots[0] |
| 9365 | |
| 9366 | root_values = self._root_id_to_values[new_root] |
| 9367 | root_values.update(values) |
| 9368 | for subtree_root in subtree_roots[1:]: |
| 9369 | if subtree_root == new_root or subtree_root not in self._root_id_to_values: |
| 9370 | continue |
| 9371 | self._id_to_parent_id[subtree_root] = new_root |
| 9372 | root_values.update(self._root_id_to_values.pop(subtree_root)) |
| 9373 | |
| 9374 | def items(self) -> list[tuple[set[TKey], set[TValue]]]: |
| 9375 | """Returns all disjoint mappings in key-value pairs.""" |