< Summary

Information
Class: Elsa.Extensions.EnumerableTopologicalSortExtensions
Assembly: Elsa.Features
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Features/Extensions/EnumerableTopologicalSortExtensions.cs
Line coverage
91%
Covered lines: 11
Uncovered lines: 1
Coverable lines: 12
Total lines: 39
Line coverage: 91.6%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TSort(...)100%22100%
Visit(...)75%8885.71%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Features/Extensions/EnumerableTopologicalSortExtensions.cs

#LineLine coverage
 1// ReSharper disable once CheckNamespace
 2namespace Elsa.Extensions;
 3
 4/// <summary>
 5/// Adds extension methods to sort collections by their dependencies, also known as a topological sort.
 6/// </summary>
 7public static class EnumerableTopologicalSortExtensions
 8{
 9    /// <summary>
 10    /// Returns a topologically sorted copy of the specified list.
 11    /// </summary>
 12    // ReSharper disable once InconsistentNaming
 13    public static IEnumerable<T> TSort<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> dependencies, bool throwOn
 14    {
 315        var sorted = new List<T>();
 316        var visited = new HashSet<T>();
 17
 4218        foreach (var item in source)
 1819            Visit(item, visited, sorted, dependencies, throwOnCycle);
 20
 321        return sorted;
 22    }
 23
 24    private static void Visit<T>(T item, ISet<T> visited, ICollection<T> sorted, Func<T, IEnumerable<T>> dependencies, b
 25    {
 4026        if (visited.Add(item))
 27        {
 8228            foreach (var dep in dependencies(item))
 2229                Visit(dep, visited, sorted, dependencies, throwOnCycle);
 30
 1931            sorted.Add(item);
 32        }
 33        else
 34        {
 2135            if (throwOnCycle && !sorted.Contains(item))
 036                throw new Exception("Cyclic dependency found");
 37        }
 2138    }
 39}