stripRedundantParents method

void stripRedundantParents()

We don't need a discrete parent StatePath if the state of mind also contains a child of that parent.

Implementation

void stripRedundantParents() {
  final seen = <StatePath>{};
  final remove = <StatePath>{};

  /// sortest paths first as we need to see the parent paths first
  _leafPaths.sort((lhs, rhs) => lhs.path.length - rhs.path.length);

  for (final path in _leafPaths) {
    var next = path;

    /// chain up the path so we can compare each 'parent' path
    /// to the list of parents we have [seen].
    while (next.leaf.stateType != VirtualRoot().runtimeType) {
      if (seen.contains(next)) {
        remove.add(next);
      }

      next = next.parent;
    }
    seen.add(path);
  }

  for (final path in remove) {
    _leafPaths.remove(path);
  }
}