dedupPaths method
Takes a list of PartialStatePath
s and returns an ordered list
of states from the leaf on the longest branch.
It is expected that the partial paths are all relative to a common
ancestor.
We also dedup the States as we build the path.
Implementation
List<StateDefinition> dedupPaths(List<PartialStatePath> paths) {
final maxLength = paths.fold<int>(
0, (longest, element) => math.max(longest, element.path.length));
final seenPaths = <StateDefinition>{};
final orderedPaths = <StateDefinition>[];
for (var i = 0; i < maxLength; i++) {
for (final statePath in paths) {
/// Only take if the path is long enough
if (statePath.path.length >= (maxLength - i)) {
final ofInterest =
statePath.path[i - (maxLength - statePath.path.length)];
if (!seenPaths.contains(ofInterest)) {
orderedPaths.add(ofInterest);
}
seenPaths.add(ofInterest);
}
}
}
return orderedPaths;
}