using NodeDev.Core.Connections; namespace NodeDev.Core; /// /// Represent a list of paths in a graph. /// Each path is saved as the list of every outputExec connection in the path. /// public class NodePaths { public List Paths { get; } = []; public int CountPossiblePaths => Paths.Count; public NodePaths() { } public NodePaths(NodePath path) { Paths.Add(path); } /// /// Get a new NodePaths containing all the possible paths starting from the . /// This will remove all the paths that didn't go through the . /// public NodePaths GetPathsFromOutput(Connection connection) { var newPaths = new NodePaths(); foreach (var path in Paths) { var newPath = path.CloneThenRemoveUntil(connection); // Check if the path is not null and if it's not already in the list if (newPath != null && !newPaths.HasSamePath(newPath)) newPaths.Paths.Add(newPath); } return newPaths; } /// /// Get a new NodePaths containing all the possible paths leading to the . /// The is the last connection in each path. /// public NodePaths GetPathsLeadingToOutput(Connection connection) { var newPaths = new NodePaths(); foreach (var path in Paths) { var newPath = path.CloneThenRemoveEverythingAfter(connection); // Check if the path is not null and if it's not already in the list if (newPath != null && !newPaths.HasSamePath(newPath)) newPaths.Paths.Add(newPath); } return newPaths; } /// /// Check if the is contained in any of the paths. /// The path is contained if it is the exact same sequence of connections. /// public bool HasSamePath(NodePath path) { foreach (var localPath in Paths) { if (localPath.Connections.SequenceEqual(path.Connections)) return true; } return false; } /// /// Check if the is contained in any of the paths. /// public bool Contains(Connection connection) { foreach (var path in Paths) { if (path.Contains(connection)) return true; } return false; } /// /// Add the paths to the current list of possible paths. /// /// Paths to add to the current list of possible paths public void AddNewIndependantBranch(NodePaths path) { Paths.AddRange(path.Paths); } /// /// Add the path to the current list of possible paths. /// /// Path to add to the current list of possible paths public void AddNewIndependantBranch(NodePath path) { Paths.Add(path); } /// /// Add the path at the end of each possible path. /// public void AppendPath(NodePath path) { foreach (var localPath in Paths) localPath.AppendPath(path); } /// /// Add the connection at the beginning of each possible path. /// public void PrependPath(NodePath path) { foreach (var localPath in Paths) localPath.Connections.InsertRange(0, path.Connections); } } public class NodePath { public List Connections { get; } = []; /// /// Get the length of the path. (Amount of connections in the path) /// public int Length => Connections.Count; public NodePath() { } public NodePath(IEnumerable connections) { Connections.AddRange(connections); } public NodePath(List connections) { Connections = connections; } /// /// Check if the is contained in the path. /// public bool Contains(Connection connection) { return Connections.Contains(connection); } /// /// Create a clone of the path and crop the beginning up to the . /// Return null if the connection is not in the path. /// public NodePath? CloneThenRemoveUntil(Connection connection) { var index = Connections.IndexOf(connection); if (index == -1) return null; var newPath = new NodePath(Connections[index..]); return newPath; } /// /// Create a clone of the path and crop the end after the . /// /// /// public NodePath? CloneThenRemoveEverythingAfter(Connection connection) { var index = Connections.IndexOf(connection); if (index == -1) return null; var newPath = new NodePath(Connections[..index]); return newPath; } /// /// Extend the current path by adding the at the end of the path. /// /// Path to add to the end of this public void AppendPath(NodePath path) { Connections.AddRange(path.Connections); } /// /// Extend the current path by adding the at the end of the path. /// /// Connection to add to this public void AppendPath(Connection connection) { Connections.Add(connection); } }