setParent method

void setParent(
  1. TKey key,
  2. TKey? parent
)

Sets the parent of key to parent (or null for root) and refreshes the cached _ancestorsExpandedByNid bit for key, propagating the change through key's subtree.

After the parent write and the ancestors-expanded propagation, fires onParentChanged with (nid, oldParent, newParent) for any subscriber (e.g. the visible-subtree-size cache in the order buffer). Callers no longer need to do the cache shift externally — subscribe to the callback instead. Note: fires unconditionally, including when oldParent == newParent — subscribers are responsible for short-circuiting no-ops.

Implementation

void setParent(TKey key, TKey? parent) {
  final nid = nids[key]!;
  final oldParentNid = _parentByNid[nid];
  final newParentNid = parent == null ? kNoParentNid : nids[parent]!;
  _parentByNid[nid] = newParentNid;
  final newAe = _computeAncestorsExpandedNid(nid);
  if (_ancestorsExpandedByNid[nid] != newAe) {
    _ancestorsExpandedByNid[nid] = newAe;
    final childAe = (newAe != 0 && _expandedByNid[nid] != 0) ? 1 : 0;
    _propagateAncestorsExpandedToDescendants(key, childAe);
  }
  onParentChanged?.call(nid, oldParentNid, newParentNid);
}