setParent method

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

Sets the parent of key to parent, or null for a root, refreshes the cached ancestors-expanded bit for key, and propagates that change through key's subtree.

Fires onParentChanged with (nid, oldParent, newParent) LAST, after both the parent write and the propagation, so a subscriber such as the order buffer's visible-subtree-size cache observes settled state and owns the shift itself rather than the caller doing it. See ParentChangedCallback for the rest of the invocation contract.

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);
}