public static bool Visits(this IEnumerable <DirectedEdge> edges, NodeIdentity node) { ArgumentHelpers.ThrowIfNull(() => edges); ArgumentHelpers.ThrowIfNull(() => node); return(edges.First().From == node || Reaches(edges, node)); }
public static bool Contains(this Path path, Path other) { ArgumentHelpers.ThrowIfNull(() => path); ArgumentHelpers.ThrowIfNull(() => other); if (path.Length < other.Length) { return(false); } else if (other.Length == 0) { return(true); } for (var i = 0; i <= path.Length - other.Length; i++) { var j = 0; for (j = 0; j < other.Length; j++) { if (path[i + j] != other[j]) { break; } } if (j == other.Length) { return(true); } } return(false); }
public static Path FindOverlap(Path first, Path second) { ArgumentHelpers.ThrowIfNull(() => first); ArgumentHelpers.ThrowIfNull(() => second); for (var i = 0; i < first.Length; i++) { for (var j = 0; j < second.Length; j++) { if (first[i + j] == second[j]) { // yay if (i + j == first.Length - 1) { return(Path.Of(first, second.GetRange(j + 1, second.Length - j - 1))); } } else { break; } } } return(Path.Empty); }
internal DirectedEdge(NodeIdentity from, NodeIdentity to) { ArgumentHelpers.ThrowIfNull(() => from); ArgumentHelpers.ThrowIfNull(() => to); From = from; To = to; }
public static IEnumerable <NodeIdentity> VisitAll(DirectedGraph graph, NodeIdentity start) { ArgumentHelpers.ThrowIfNull(() => graph); ArgumentHelpers.ThrowIfNull(() => start); var unvisited = new Stack <NodeIdentity>(); var spotted = new HashSet <NodeIdentity>(); void addNeighbors(IEnumerable <DirectedEdge> edges) { foreach (var neighbor in edges) { if (spotted.Add(neighbor.To)) { unvisited.Push(neighbor.To); } } } unvisited.Push(start); while (unvisited.Any()) { var current = unvisited.Pop(); yield return(current); addNeighbors(graph.EdgesFrom(current)); } }
public void ThrowsIfExpressionIsNull() { typeof(ArgumentHelpers).Invoking(_ => ArgumentHelpers.ThrowIfNull <object>(() => null)) .Should().Throw <ArgumentNullException>() .Which.ParamName.Should().Be("() => null"); }
public static bool Contains(this Path path, IEnumerable <DirectedEdge> edges) { ArgumentHelpers.ThrowIfNull(() => path); ArgumentHelpers.ThrowIfNull(() => edges); var other = Path.Of(edges); return(path.Contains(other)); }
public void IndicatesParameterByNameIfNull() { string nullParam = null; typeof(ArgumentHelpers).Invoking(_ => ArgumentHelpers.ThrowIfNull(() => nullParam)) .Should().Throw <ArgumentNullException>() .Which.ParamName.Should().Be(nameof(nullParam)); }
public static DirectedGraph Transform(DirectedGraph originalGraph, IEnumerable <Path> primePaths, NodeIdentity start, NodeIdentity end) { ArgumentHelpers.ThrowIfNull(() => originalGraph); ArgumentHelpers.ThrowIfNull(() => primePaths); var nodes = new List <NodeIdentity> { start, end }; var edges = new List <DirectedEdge>(); var nodePathLookup = new Dictionary <NodeIdentity, Path>(); nodePathLookup.Add(start, Path.Of(start)); nodePathLookup.Add(end, Path.Of(end)); foreach (var path in primePaths) { var node = NodeIdentity.Of(Describe(path)); nodes.Add(node); nodePathLookup.Add(node, path); } foreach (var firstPathKey in nodes) { if (firstPathKey == end) { continue; } var firstPath = nodePathLookup[firstPathKey]; foreach (var secondPathKey in nodes) { if (secondPathKey == firstPathKey || secondPathKey == start) { continue; } var secondPath = nodePathLookup[secondPathKey]; var maybeEdge = firstPath.Extend(secondPath, originalGraph); if (Path.IsEmpty(maybeEdge)) { continue; } if (!ContainsAnyOtherPrimePath(primePaths, maybeEdge, firstPath, secondPath)) { edges.Add(DirectedEdge.Between(firstPathKey, secondPathKey)); } } } return(DirectedGraph.Of(edges, nodes)); }
static void Main(string[] args) { if (ArgumentHelpers.IsHelpArgument(args)) { Console.WriteLine("Writes all the currently installed versions of \"classic\" .NET platform in the system."); Console.WriteLine("Use --b, -b or /b to use in a batch, showing only the installed versions, without any extra informational lines."); return; } if (!ArgumentHelpers.IsBatchArgument(args)) { Console.WriteLine("Currently installed \"classic\" .NET Versions in the system:"); } Get1To45VersionFromRegistry(); Get45PlusFromRegistry(); }
public static Path Extend(this Path firstPath, Path secondPath, DirectedGraph graph) { ArgumentHelpers.ThrowIfNull(() => firstPath); ArgumentHelpers.ThrowIfNull(() => secondPath); ArgumentHelpers.ThrowIfNull(() => graph); var overlap = OverlapFinder.FindOverlap(firstPath, secondPath); if (!Path.IsEmpty(overlap)) { return(overlap); } var shortestPath = BreadthFirst.FindPath(graph, firstPath.End, node => node == secondPath.Start); if (Path.IsEmpty(shortestPath)) { return(Path.Empty); } return(Path.Join(firstPath, Path.Of(shortestPath.GetRange(1, shortestPath.Length - 2)), secondPath)); }
public static bool DescribesCycle(this IEnumerable <DirectedEdge> edges) { ArgumentHelpers.ThrowIfNull(() => edges); return(edges.Count() >= 1 && edges.First().From == edges.Last().To); }
public static bool Reaches(this IEnumerable <DirectedEdge> edges, NodeIdentity node) { ArgumentHelpers.ThrowIfNull(() => edges); ArgumentHelpers.ThrowIfNull(() => node); return(edges.Any(e => e.To == node)); }
public IEnumerable <Path> FindPrimePaths(DirectedGraph graph) { ArgumentHelpers.ThrowIfNull(() => graph); var pathsOfLength = new Dictionary <int, List <List <DirectedEdge> > >(); var pathsToConsider = new Queue <List <DirectedEdge> >(); foreach (var edge in graph.Edges) { pathsToConsider.Enqueue(new List <DirectedEdge> { edge }); } void markAsCandidate(List <DirectedEdge> path) { var length = path.Count(); if (!pathsOfLength.TryGetValue(length, out var paths)) { paths = new List <List <DirectedEdge> >(); pathsOfLength.Add(length, paths); } paths.Add(path); } while (pathsToConsider.Any()) { var p = pathsToConsider.Dequeue(); if (p.DescribesCycle()) { markAsCandidate(p); continue; } var possibleExtensions = graph.EdgesFrom(p.Last().To) .Where(ext => !p.Reaches(ext.To)); if (!possibleExtensions.Any()) { markAsCandidate(p); continue; } foreach (var possibleExtension in possibleExtensions) { var next = new List <DirectedEdge>(p); next.Add(possibleExtension); pathsToConsider.Enqueue(next); } } var primePaths = new List <Path>(); foreach (var possiblyPrime in pathsOfLength.OrderByDescending(kv => kv.Key).SelectMany(kv => kv.Value)) { var possiblyPrimePath = Path.Of(possiblyPrime); if (primePaths.All(prime => !prime.Contains(possiblyPrimePath))) { primePaths.Add(Path.Of(possiblyPrime)); } } return(primePaths); }
public void DoesNotThrowIfExpressionIsNotNull() { typeof(ArgumentHelpers).Invoking(_ => ArgumentHelpers.ThrowIfNull(() => "not null")) .Should().NotThrow(); }