public void TraversalTest() { var algorithm = new TraversalAlgorithm(); var node = new Node(); Assert.NotNull(algorithm.Traverse(node)); Assert.Fail("Write your test"); }
public DijkstraMultiDestinationShortestPath(Transaction tx, GraphStorage graphStorage, Node root, CancellationToken cancelToken) { _rootNode = root; _shortestPathVisitor = new DijkstraMultiDestinationShortestPathVisitor(root, (nodeFrom, nodeTo) => 0, (traversalInfo, adjacentNode) => adjacentNode.EdgeTo.Weight + traversalInfo.TotalEdgeWeightUpToNow); _bfs = new TraversalAlgorithm(tx, graphStorage, root, TraversalType.BFS, cancelToken) { Visitor = _shortestPathVisitor }; }
protected BaseSingleDestinationShortestPath(Transaction tx, GraphStorage graphStorage, Node root, Node targetNode, SingleDestinationShortestPathVisitor shortestPathVisitor, TraversalAlgorithm traversal, CancellationToken cancelToken) { _rootNode = root; _targetNode = targetNode; _shortestPathVisitor = shortestPathVisitor; _traversal = traversal; _traversal.Visitor = shortestPathVisitor; }
public static async Task<IEnumerable<Node>> FindAsync(this GraphStorage storage, Transaction tx, Node rootNode, Func<JObject, bool> searchPredicate, TraversalType algorithmType, CancellationToken cancelToken, int? take = null) { var searchVisitor = new SearchVisitor(searchPredicate, take ?? 0); var traversalAlgorithm = new TraversalAlgorithm(tx, storage, rootNode, algorithmType, cancelToken) { Visitor = searchVisitor }; await traversalAlgorithm.TraverseAsync(); return searchVisitor.Results; }
/// <summary> /// Gets a list containing this view and all children matching the predicate. /// </summary> public static List <T> HierarchyToList <T>(this T view, Func <T, bool> predicate = null, bool recursive = true, View parent = null, TraversalAlgorithm traversalAlgorithm = TraversalAlgorithm.DepthFirst) where T : View { var children = new List <T>(); if (predicate == null) { predicate = x => true; } if (predicate(view)) { children.Add(view); } view.ForEach <T>(x => { if (predicate(x)) { children.Add(x); } }, recursive, parent, traversalAlgorithm); return(children); }
/// <summary> /// Gets a list of all descendants. /// </summary> public static List <T> GetChildren <T>(this View view, bool recursive = true, View parent = null, TraversalAlgorithm traversalAlgorithm = TraversalAlgorithm.DepthFirst) where T : View { return(view.GetChildren <T>(x => true, recursive, parent, traversalAlgorithm)); }
/// <summary> /// Returns first view of type T with the specified ID. /// </summary> public static T Find <T>(this View view, string id, bool recursive = true, View parent = null, TraversalAlgorithm traversalAlgorithm = TraversalAlgorithm.DepthFirst) where T : View { return(view.Find <T>(x => String.Equals(x.Id, id, StringComparison.OrdinalIgnoreCase), recursive, parent, traversalAlgorithm)); }
/// <summary> /// Traverses the view object tree and returns the first view that matches the predicate. /// </summary> public static T Find <T>(this View view, Predicate <T> predicate, bool recursive = true, View parent = null, TraversalAlgorithm traversalAlgorithm = TraversalAlgorithm.DepthFirst) where T : View { T result = null; view.ForEach <T>(x => { if (predicate(x)) { result = x; return(false); } return(true); }, recursive, parent, traversalAlgorithm); return(result); }
/// <summary> /// Traverses the view object tree and performs an action on this view and its children until the action returns false. /// </summary> public static void ForThisAndEach <T>(this View view, Action <T> action, bool recursive = true, View parent = null, TraversalAlgorithm traversalAlgorithm = TraversalAlgorithm.DepthFirst) where T : View { var thisView = view as T; if (thisView != null) { action(thisView); } view.ForEach <T>(action, recursive, parent, traversalAlgorithm); }
/// <summary> /// Traverses the view object tree and performs an action on each child. /// </summary> public static void ForEach <T>(this View view, Action <T> action, bool recursive = true, View parent = null, TraversalAlgorithm traversalAlgorithm = TraversalAlgorithm.DepthFirst) where T : View { view.ForEach <T>(x => { action(x); return(true); }, recursive, parent, traversalAlgorithm); }
/// <summary> /// Traverses the view layout tree and performs an action on each child until the action returns false. /// </summary> public static void ForEach <T>(this View view, Func <T, bool> action, bool recursive = true, DependencyObject parent = null, TraversalAlgorithm traversalAlgorithm = TraversalAlgorithm.DepthFirst) where T : View { switch (traversalAlgorithm) { default: case TraversalAlgorithm.DepthFirst: foreach (View child in view.LayoutChildren) { bool skipChild = false; if (parent != null) { if (child.Parent != parent) { skipChild = true; } } if (!skipChild) { var matchedChild = child as T; if (matchedChild != null) { var result = action(matchedChild); if (!result) { // done traversing return; } } } if (recursive) { child.ForEach <T>(action, recursive, parent, traversalAlgorithm); } } break; case TraversalAlgorithm.BreadthFirst: Queue <View> queue = new Queue <View>(); foreach (View child in view.LayoutChildren) { bool skipChild = false; if (parent != null) { if (child.Parent != parent) { skipChild = true; } } if (!skipChild) { var matchedChild = child as T; if (matchedChild != null) { var result = action(matchedChild); if (!result) { // done traversing return; } } } if (recursive) { // add children to queue queue.Enqueue(child); } } foreach (var queuedView in queue) { queuedView.ForEach <T>(action, recursive, parent, traversalAlgorithm); } break; case TraversalAlgorithm.ReverseDepthFirst: foreach (View child in view.LayoutChildren) { if (recursive) { child.ForEach <T>(action, recursive, parent, traversalAlgorithm); } if (parent != null) { if (child.Parent != parent) { continue; } } var matchedChild = child as T; if (matchedChild != null) { var result = action(matchedChild); if (!result) { // done traversing return; } } } break; case TraversalAlgorithm.ReverseBreadthFirst: Stack <T> matchedChildStack = new Stack <T>(); Stack <View> childStack = new Stack <View>(); foreach (View child in view.LayoutChildren) { if (recursive) { childStack.Push(child); } if (parent != null) { if (child.Parent != parent) { continue; } } var matchedChild = child as T; if (matchedChild != null) { matchedChildStack.Push(matchedChild); } } foreach (var childStackView in childStack) { childStackView.ForEach <T>(action, recursive, parent, traversalAlgorithm); } foreach (T matchedChild in matchedChildStack) { var result = action(matchedChild); if (!result) { // done traversing return; } } break; } }
/// <summary> /// Gets a list of all descendants matching the predicate. /// </summary> public static List <T> GetChildren <T>(this GameObject gameObject, Func <T, bool> predicate = null, bool recursive = true, View parent = null, TraversalAlgorithm traversalAlgorithm = TraversalAlgorithm.DepthFirst) where T : View { var view = gameObject.GetComponent <View>(); if (view == null) { return(new List <T>()); } return(view.GetChildren <T>(predicate, recursive, parent, traversalAlgorithm)); }
/// <summary> /// Returns first view of type T found. /// </summary> public static T Find <T>(this GameObject gameObject, bool recursive = true, View parent = null, TraversalAlgorithm traversalAlgorithm = TraversalAlgorithm.DepthFirst) where T : View { var view = gameObject.GetComponent <View>(); if (view == null) { return(null); } return(view.Find <T>(x => true, recursive, parent, traversalAlgorithm)); }
/// <summary> /// Traverses the view object tree and performs an action on each child until the action returns false. /// </summary> public static void ForThisAndEachChild <T>(this GameObject gameObject, Action <T> action, bool recursive = true, View parent = null, TraversalAlgorithm traversalAlgorithm = TraversalAlgorithm.DepthFirst) where T : View { var view = gameObject.GetComponent <T>(); if (view != null) { action(view); view.ForEachChild <T>(action, recursive, parent, traversalAlgorithm); } }
/// <summary> /// Traverses the view object tree and performs an action on each child until the action returns false. /// </summary> public static void DoUntil <T>(this View view, Func <T, bool> action, bool recursive = true, View parent = null, TraversalAlgorithm traversalAlgorithm = TraversalAlgorithm.DepthFirst) where T : View { switch (traversalAlgorithm) { default: case TraversalAlgorithm.DepthFirst: foreach (Transform child in view.gameObject.transform) { bool skipChild = false; var childView = child.GetComponent <View>(); if (childView == null) { continue; } if (parent != null) { if (childView.Parent != parent) { skipChild = true; } } if (!skipChild) { var component = child.GetComponent <T>(); if (component != null) { var result = action(component); if (!result) { // done traversing return; } } } if (recursive) { childView.DoUntil <T>(action, recursive, parent, traversalAlgorithm); } } break; case TraversalAlgorithm.BreadthFirst: Queue <View> queue = new Queue <View>(); foreach (Transform child in view.gameObject.transform) { bool skipChild = false; var childView = child.GetComponent <View>(); if (childView == null) { continue; } if (parent != null) { if (childView.Parent != parent.gameObject) { skipChild = true; } } if (!skipChild) { var component = child.GetComponent <T>(); if (component != null) { var result = action(component); if (!result) { // done traversing return; } } } if (recursive) { // add children to queue queue.Enqueue(childView); } } foreach (var queuedView in queue) { queuedView.DoUntil <T>(action, recursive, parent, traversalAlgorithm); } break; case TraversalAlgorithm.ReverseDepthFirst: foreach (Transform child in view.gameObject.transform) { var childView = child.GetComponent <View>(); if (childView == null) { continue; } if (recursive) { childView.DoUntil <T>(action, recursive, parent, traversalAlgorithm); } if (parent != null) { if (childView.Parent != parent.gameObject) { continue; } } var component = child.GetComponent <T>(); if (component != null) { var result = action(component); if (!result) { // done traversing return; } } } break; case TraversalAlgorithm.ReverseBreadthFirst: Stack <T> componentStack = new Stack <T>(); Stack <View> childStack = new Stack <View>(); foreach (Transform child in view.gameObject.transform) { var childView = child.GetComponent <View>(); if (childView == null) { continue; } if (recursive) { childStack.Push(childView); } if (parent != null) { if (childView.Parent != parent.gameObject) { continue; } } var component = child.GetComponent <T>(); if (component != null) { componentStack.Push(component); } } foreach (var childStackView in childStack) { childStackView.DoUntil <T>(action, recursive, parent, traversalAlgorithm); } foreach (T component in componentStack) { var result = action(component); if (!result) { // done traversing return; } } break; } }
/// <summary> /// Gets a list of all descendants matching the predicate. /// </summary> public static List <T> GetChildren <T>(this View view, Func <T, bool> predicate = null, bool recursive = true, View parent = null, TraversalAlgorithm traversalAlgorithm = TraversalAlgorithm.DepthFirst) where T : View { var children = new List <T>(); view.ForEachChild <T>(x => { if (predicate(x)) { children.Add(x); } }, recursive, parent, traversalAlgorithm); return(children); }