Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Alternative DependencySorter to deal with cycles #219

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions flare_dart/lib/dependency_sorter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ class DependencySorter {
return _order;
}

bool visit(ActorComponent n, {HashSet<ActorComponent> cycleNodes}) {
cycleNodes ??= HashSet<ActorComponent>();
if (cycleNodes.contains(n)) {
// skip any nodes on a known cycle.
return true;
}
bool visit(ActorComponent n) {
if (_perm.contains(n)) {
return true;
}
Expand All @@ -41,7 +36,7 @@ class DependencySorter {
List<ActorComponent> dependents = n.dependents;
if (dependents != null) {
for (final ActorComponent d in dependents) {
if (!visit(d, cycleNodes: cycleNodes)) {
if (!visit(d)) {
return false;
}
}
Expand All @@ -55,9 +50,10 @@ class DependencySorter {

/// Sorts dependencies for Actors even when cycles are present
///
/// Any nodes that form part of a cycle can be found in `cycleNodes` after `sort`.
/// NOTE: Nodes isolated by cycles will not be found in `_order` or `cycleNodes`
/// e.g. `A -> B <-> C -> D` isolates D when running a sort based on A
/// Any nodes that form part of a cycle can be found in `cycleNodes` after
/// `sort`. NOTE: Nodes isolated by cycles will not be found in `_order` or
/// `cycleNodes` e.g. `A -> B <-> C -> D` isolates D when running a sort based
/// on A
mjtalbot marked this conversation as resolved.
Show resolved Hide resolved
class TarjansDependencySorter extends DependencySorter {
HashSet<ActorComponent> _cycleNodes;
HashSet<ActorComponent> get cycleNodes => _cycleNodes;
Expand All @@ -68,6 +64,16 @@ class TarjansDependencySorter extends DependencySorter {
_cycleNodes = HashSet<ActorComponent>();
}

@override
bool visit(ActorComponent n) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh this is nicer!

if (cycleNodes.contains(n)) {
// skip any nodes on a known cycle.
return true;
}

return super.visit(n);
}

@override
List<ActorComponent> sort(ActorComponent root) {
_order = <ActorComponent>[];
Expand All @@ -92,7 +98,7 @@ class TarjansDependencySorter extends DependencySorter {
});

// revisit the tree, skipping nodes on any cycle.
visit(root, cycleNodes: _cycleNodes);
visit(root);
}

return _order;
Expand Down