findCommonAncestor method

StateDefinition<State>? findCommonAncestor(
  1. Graph graph,
  2. StatePath fromAncestors,
  3. StatePath toAncestors
)

Walks up the tree looking for an ancestor that is common to the fromAncestors and toAncestors paths.

If no common ancestor is found then null is returned;

Implementation

StateDefinition? findCommonAncestor(
    Graph graph, StatePath fromAncestors, StatePath toAncestors) {
  final toAncestorSet = toAncestors.path.toSet();

  for (final ancestor in fromAncestors.path) {
    if (toAncestorSet.contains(ancestor)) {
      return ancestor;
    }
  }
  return null;
}