maskUnderIndex method

AnimationMask maskUnderIndex(
  1. int root
)

The same, from an index into nodes.

Iterative rather than recursive, and it carries a seen set: a malformed document whose children point back up their own branch would otherwise walk for ever, and a decoder is exactly the place a cycle arrives from.

Implementation

AnimationMask maskUnderIndex(int root) {
  if (root < 0 || root >= nodes.length) return AnimationMask(const <int>[]);
  final found = <int>{};
  final pending = <int>[root];
  while (pending.isNotEmpty) {
    final index = pending.removeLast();
    if (index < 0 || index >= nodes.length) continue;
    if (!found.add(index)) continue;
    pending.addAll(nodes[index].children);
  }
  return AnimationMask(found);
}