示例#1
0
        /// <summary>
        /// Breadth First search of the document tree
        /// </summary>
        public JsonValue FindValueByKey(string key, eTraversalStrategy strategy = eTraversalStrategy.BreadthFirst, eTraversalFlowDirection direction = eTraversalFlowDirection.ThroughChildren)
        {
            JsonValue found = TreeTraversal <JsonValue> .GetFirstChildWhichPasses(this, _TestIsDocumentWithKey, direction, strategy);

            if (found != null)
            {
                return(((JsonDocument)found).ValueFor(key));
            }

            return(null);

            bool _TestIsDocumentWithKey(JsonValue value)
            {
                return(value.IsDocument && ((JsonDocument)value).ContainsKey(key));
            }
        }
示例#2
0
 public TreeIter <TTreeNode> IteratorAt(TreeNodePath nodePath, Predicate <TTreeNode> predicate = null, eTraversalFlowDirection flowDirection = TreeTraversal <TTreeNode> .DefaultTraversalFlowDirection, eTraversalStrategy strategy = TreeTraversal <TTreeNode> .DefaultTraversalStrategy, bool includeRoot = true, LevelRestriction depthLimits = null)
 {
     return(TreeTraversal <TTreeNode> .IteratorAt(this.Root, nodePath, predicate, flowDirection, strategy, includeRoot, depthLimits, this.GetChildrenOverride, this.GetParentOverride));
 }
示例#3
0
 /// <summary>
 /// Returns an enumerable of all nodes in the tree
 /// </summary>
 /// <param name="startNode">The node on the tree to start at</param>
 /// <param name="flowDirection">The traversal direction</param>
 /// <param name="strategy">The traversal strategy (BFS or DFS)</param>
 /// <param name="includeRoot">Should the root node be included</param>
 /// <returns></returns>
 public IEnumerable <TTreeNode> All(TTreeNode startNode, eTraversalFlowDirection flowDirection = TreeTraversal <TTreeNode> .DefaultTraversalFlowDirection, eTraversalStrategy strategy = TreeTraversal <TTreeNode> .DefaultTraversalStrategy, bool includeRoot = true)
 {
     return(TreeTraversal <TTreeNode> .All(startNode ?? this.Root, flowDirection, strategy, includeRoot, this.GetChildrenOverride, this.GetParentOverride));
 }
示例#4
0
 /// <summary>
 /// Returns an enumerable of all nodes in the tree
 /// </summary>
 /// <param name="flowDirection">The traversal direction</param>
 /// <param name="strategy">The traversal strategy (BFS or DFS)</param>
 /// <param name="includeRoot">Should the root node be included</param>
 /// <returns></returns>
 public IEnumerable <TTreeNode> All(eTraversalFlowDirection flowDirection = TreeTraversal <TTreeNode> .DefaultTraversalFlowDirection, eTraversalStrategy strategy = TreeTraversal <TTreeNode> .DefaultTraversalStrategy, bool includeRoot = true)
 {
     return(this.All(this.Root, flowDirection, strategy, includeRoot));
 }
示例#5
0
        // -----------------------------------------------------------------------------------------------------------------------------------------------------
        // =====================================[ Public Instance Functions of TreeTraverser Utilities ] =======================================================
        // -----------------------------------------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Creates a basic tree iterator
        /// </summary>
        /// <param name="startNode">The node of the tree to start at (or null to start at the root)</param>
        /// <param name="flowDirection">The direction to flow through the tree</param>
        /// <param name="strategy">The traversal strategy to use</param>
        /// <returns>The iterator that was created</returns>
        public TreeIter <TTreeNode> CreateIterator(TTreeNode startNode = null, eTraversalFlowDirection flowDirection = TreeTraversal <TTreeNode> .DefaultTraversalFlowDirection, eTraversalStrategy strategy = TreeTraversal <TTreeNode> .DefaultTraversalStrategy)
        {
            return(TreeTraversal <TTreeNode> .CreateIterator(this.Root, startNode, flowDirection, strategy, this.GetChildrenOverride, this.GetParentOverride));
        }
示例#6
0
        public static T GetFirstChildOfType <T> (TTreeNode root, TTreeNode start, Predicate <T> predicate = null, eTraversalFlowDirection flowDirection = DefaultTraversalFlowDirection, eTraversalStrategy strategy = DefaultTraversalStrategy, bool includeSelf = true, bool canTypeBeAncestor = true, LevelRestriction depthLimits = null, GetTreeNodeChildren <TTreeNode> getChildrenMethodOverride = null, GetTreeNodeParent <TTreeNode> getParentMethodOverride = null)
            where T : class
        {
            var iter = IterateOverNodesOfType(root, start, predicate, flowDirection, strategy, includeSelf, canTypeBeAncestor, depthLimits, getChildrenMethodOverride, getParentMethodOverride);

            if (iter != TreeIter <TTreeNode> .End)
            {
                return(iter.Node as T);
            }

            return(null);
        }
示例#7
0
 /// <summary>
 /// Creates a basic tree iterator
 /// </summary>
 /// <param name="start">The starting point (also the acting root of the tree)</param>
 /// <param name="strategy">The traversal strategy to use</param>
 /// <returns>The iterator that was created</returns>
 public static TreeIter <TTreeNode> CreateIterator(TTreeNode start, eTraversalFlowDirection flowDirection = DefaultTraversalFlowDirection, eTraversalStrategy strategy = DefaultTraversalStrategy, GetTreeNodeChildren <TTreeNode> getChildrenMethodOverride = null, GetTreeNodeParent <TTreeNode> getParentMethodOverride = null)
 {
     return(CreateIterator(null, start, flowDirection, strategy, getChildrenMethodOverride, getParentMethodOverride));
 }
        public static int CompareTreeNodePaths(TreeNodePath left, TreeNodePath right, eTraversalFlowDirection flowDirection = eTraversalFlowDirection.ThroughChildren, eTraversalStrategy traversalStrategy = eTraversalStrategy.Default)
        {
            switch (flowDirection)
            {
            case eTraversalFlowDirection.ThroughParents:
                return(CompareTreeNodePaths_ThroughParents(left, right));

            case eTraversalFlowDirection.ThroughChildren:
                if (traversalStrategy == eTraversalStrategy.BreadthFirst)
                {
                    return(CompareTreeNodePaths_BreadthFirst(left, right));
                }
                return(CompareTreeNodePaths_DepthFirst(left, right));

            case eTraversalFlowDirection.ReversedThroughChildren:
                if (traversalStrategy == eTraversalStrategy.BreadthFirst)
                {
                    return(CompareTreeNodePaths_ReverseBreadthFirst(left, right));
                }
                return(CompareTreeNodePaths_ReverseDepthFirst(left, right));
            }

            throw new InvalidOperationException("Traversal flow and/or strategy was invalid, flowDirection was casted to something invalid");
        }
示例#9
0
 public static TTreeNode GetFirstChildWhichPasses(TTreeNode start, Predicate <TTreeNode> predicate, eTraversalFlowDirection flowDirection = DefaultTraversalFlowDirection, eTraversalStrategy strategy = DefaultTraversalStrategy, bool includeSelf = true, LevelRestriction depthLimits = null, GetTreeNodeChildren <TTreeNode> getChildrenMethodOverride = null, GetTreeNodeParent <TTreeNode> getParentMethodOverride = null)
 {
     return(GetFirstChildWhichPasses(null, start, predicate, flowDirection, strategy, includeSelf, depthLimits, getChildrenMethodOverride, getParentMethodOverride));
 }
示例#10
0
        public static TreeIter <TTreeNode> IteratorAt(TTreeNode root, TreeNodePath nodePath, Predicate <TTreeNode> predicate = null, eTraversalFlowDirection flowDirection = DefaultTraversalFlowDirection, eTraversalStrategy strategy = DefaultTraversalStrategy, bool includeSelf = true, LevelRestriction depthLimits = null, GetTreeNodeChildren <TTreeNode> getChildrenMethodOverride = null, GetTreeNodeParent <TTreeNode> getParentMethodOverride = null)
        {
            var traversalParams = new TreeTraversalParameters <TTreeNode>(root, flowDirection, strategy, getChildrenMethodOverride, getParentMethodOverride)
            {
                Predicate   = predicate,
                DepthLimits = depthLimits,
            };

            var iter = TreeIter <TTreeNode> .CreateIteratorAtNodePath(root, nodePath, traversalParams);

            return(FinalizeAndReturnIter(iter, includeSelf, traversalParams.Predicate, start: false));
        }
示例#11
0
        /// <summary>
        /// Iterate over all the nodes of a particaulr type
        /// </summary>
        /// <typeparam name="T">Tree node type</typeparam>
        /// <param name="root">The tree's root node</param>
        /// <param name="start">The starting point</param>
        /// <param name="predicate">The predicate</param>
        /// <param name="flowDirection">The direction the traversal flows through the tree</param>
        /// <param name="strategy">The traversal strategy</param>
        /// <param name="includeSelf">Should the start node be included</param>
        /// <param name="canTypeBeAncestor">Can the type be derivitive of the passed in type, or must it be an exact match (default is true)</param>
        /// <param name="depthLimits">Depth limits</param>
        /// <param name="getChildrenMethodOverride">The override to the default GetTreeNodeChildren method specified in <see cref="TreeTraversal{TTreeNode}"/>.SetupDefaults, or null to use the default</param>
        /// <param name="getParentMethodOverride">The override to the default GetTreeNodeParents method specified in <see cref="TreeTraversal{TTreeNode}"/>.SetupDefaults, or null to use the default</param>
        /// <returns></returns>
        public static TreeIter <TTreeNode> IterateOverNodesOfType <T> (TTreeNode root, TTreeNode start, Predicate <T> predicate = null, eTraversalFlowDirection flowDirection = DefaultTraversalFlowDirection, eTraversalStrategy strategy = DefaultTraversalStrategy, bool includeSelf = true, bool canTypeBeAncestor = true, LevelRestriction depthLimits = null, GetTreeNodeChildren <TTreeNode> getChildrenMethodOverride = null, GetTreeNodeParent <TTreeNode> getParentMethodOverride = null)
            where T : class
        {
            var traversalParams = new TreeTraversalParameters <TTreeNode>(root ?? FindRoot(start, getParentMethodOverride), flowDirection, strategy, getChildrenMethodOverride, getParentMethodOverride)
            {
                TypeMatching        = new TypeEval(typeof(T), canTypeBeAncestor),
                PruneAfterFirstFind = false,
                DepthLimits         = depthLimits,
            };

            if (predicate != null)
            {
                traversalParams.Predicate = (_obj) => predicate(_obj as T);
            }

            var iter = CreateIterator(start, traversalParams);

            return(FinalizeAndReturnIter(iter, includeSelf, traversalParams.Predicate));
        }
示例#12
0
        /// <summary>
        /// Creates an iterator for iterating over all nodes which pass a predicate
        /// </summary>
        /// <param name="root">The tree's root node</param>
        /// <param name="start">The starting point</param>
        /// <param name="predicate">The predicate</param>
        /// <param name="flowDirection">The direction the traversal flows through the tree</param>
        /// <param name="strategy">The traversal strategy</param>
        /// <param name="includeSelf">Should the start node be included</param>
        /// <param name="depthLimits">Depth limits</param>
        /// <param name="getChildrenMethodOverride">The override to the default GetTreeNodeChildren method specified in <see cref="TreeTraversal{TTreeNode}"/>.SetupDefaults, or null to use the default</param>
        /// <param name="getParentMethodOverride">The override to the default GetTreeNodeParents method specified in <see cref="TreeTraversal{TTreeNode}"/>.SetupDefaults, or null to use the default</param>
        /// <returns>The iterator created</returns>
        public static TreeIter <TTreeNode> IterateOverAllNodesWhichPass(TTreeNode root, TTreeNode start, Predicate <TTreeNode> predicate, eTraversalFlowDirection flowDirection = DefaultTraversalFlowDirection, eTraversalStrategy strategy = DefaultTraversalStrategy, bool includeSelf = true, LevelRestriction depthLimits = null, GetTreeNodeChildren <TTreeNode> getChildrenMethodOverride = null, GetTreeNodeParent <TTreeNode> getParentMethodOverride = null)
        {
            var iter = CreateIterator(start, new TreeTraversalParameters <TTreeNode>(root ?? FindRoot(start, getParentMethodOverride), flowDirection, strategy, getChildrenMethodOverride, getParentMethodOverride)
            {
                Predicate   = predicate,
                DepthLimits = depthLimits
            });

            return(FinalizeAndReturnIter(iter, includeSelf, predicate));
        }
示例#13
0
        public static IEnumerable <TTreeNode> All(TTreeNode root, TTreeNode start, eTraversalFlowDirection flowDirection = DefaultTraversalFlowDirection, eTraversalStrategy strategy = DefaultTraversalStrategy, bool includeSelf = true, GetTreeNodeChildren <TTreeNode> getChildrenMethodOverride = null, GetTreeNodeParent <TTreeNode> getParentMethodOverride = null)
        {
            TreeIter <TTreeNode> iter = CreateIterator(root, start, flowDirection, strategy, getChildrenMethodOverride, getParentMethodOverride);

            if (!includeSelf)
            {
                ++iter;
            }

            while (iter != TreeIter <TTreeNode> .End)
            {
                yield return(iter.Node);

                ++iter;
            }
        }
示例#14
0
 public static IEnumerable <TTreeNode> All(TTreeNode start, eTraversalFlowDirection flowDirection = DefaultTraversalFlowDirection, eTraversalStrategy strategy = DefaultTraversalStrategy, bool includeSelf = true, GetTreeNodeChildren <TTreeNode> getChildrenMethodOverride = null, GetTreeNodeParent <TTreeNode> getParentMethodOverride = null)
 {
     return(All(FindRoot(start), start, flowDirection, strategy, includeSelf, getChildrenMethodOverride, getParentMethodOverride));
 }
示例#15
0
 /// <summary>
 /// Creates a basic tree iterator
 /// </summary>
 /// <param name="root">The tree root, will search from start up if not specified - if this is undesired, pass in start for root (or call overloaded function without root which uses start for root)</param>
 /// <param name="start">The starting point</param>
 /// <param name="strategy">The traversal strategy to use</param>
 /// <param name="flowDirection">How the traversal flows through the tree</param>
 /// <param name="getChildrenMethodOverride">The override to the default GetTreeNodeChildren method specified in <see cref="TreeTraversal{TTreeNode}"/>.SetupDefaults, or null to use the default</param>
 /// <param name="getParentMethodOverride">The override to the default GetTreeNodeParents method specified in <see cref="TreeTraversal{TTreeNode}"/>.SetupDefaults, or null to use the default</param>
 /// <returns>The iterator that was created</returns>
 public static TreeIter <TTreeNode> CreateIterator(TTreeNode root, TTreeNode start, eTraversalFlowDirection flowDirection = DefaultTraversalFlowDirection, eTraversalStrategy strategy = DefaultTraversalStrategy, GetTreeNodeChildren <TTreeNode> getChildrenMethodOverride = null, GetTreeNodeParent <TTreeNode> getParentMethodOverride = null)
 {
     return(CreateIterator(start,
                           new TreeTraversalParameters <TTreeNode>(root ?? FindRoot(start, getParentMethodOverride), flowDirection, strategy, getChildrenMethodOverride, getParentMethodOverride)
                           ));
 }
示例#16
0
 public TTreeNode GetFirstChildWhichPasses(TTreeNode start, Predicate <TTreeNode> predicate, eTraversalFlowDirection flowDirection = TreeTraversal <TTreeNode> .DefaultTraversalFlowDirection, eTraversalStrategy strategy = TreeTraversal <TTreeNode> .DefaultTraversalStrategy, bool includeRoot = true, LevelRestriction depthLimits = null)
 {
     return(TreeTraversal <TTreeNode> .GetFirstChildWhichPasses(this.Root, start, predicate, flowDirection, strategy, includeRoot, depthLimits, this.GetChildrenOverride, this.GetParentOverride));
 }
示例#17
0
 public T GetFirstChildOfType <T>(TTreeNode start, Predicate <T> predicate = null, bool includeRoot = true, eTraversalFlowDirection flowDirection = TreeTraversal <TTreeNode> .DefaultTraversalFlowDirection, eTraversalStrategy strategy = TreeTraversal <TTreeNode> .DefaultTraversalStrategy, bool canTypeBeAncestor = true, LevelRestriction depthLimits = null)
     where T : class, TTreeNode
 {
     return(TreeTraversal <TTreeNode> .GetFirstChildOfType(this.Root, start, predicate, flowDirection, strategy, includeRoot, canTypeBeAncestor, depthLimits, this.GetChildrenOverride, this.GetParentOverride));
 }
示例#18
0
        public static TTreeNode GetFirstChildWhichPasses(TTreeNode root, TTreeNode start, Predicate <TTreeNode> predicate, eTraversalFlowDirection flowDirection = DefaultTraversalFlowDirection, eTraversalStrategy strategy = DefaultTraversalStrategy, bool includeSelf = true, LevelRestriction depthLimits = null, GetTreeNodeChildren <TTreeNode> getChildrenMethodOverride = null, GetTreeNodeParent <TTreeNode> getParentMethodOverride = null)
        {
            var iter = IterateOverAllNodesWhichPass(root, start, predicate, flowDirection, strategy, includeSelf, depthLimits, getChildrenMethodOverride, getParentMethodOverride);

            if (iter != TreeIter <TTreeNode> .End)
            {
                return(iter.Node);
            }

            return(null);
        }
示例#19
0
        /// <summary>
        /// Create parameters to the tree traversal
        /// </summary>
        /// <param name="root">The tree's root</param>
        /// <param name="flowDirection">The direction to traverse</param>
        /// <param name="strategy">The traversal strategy</param>
        /// <param name="getChildrenMethodOverride">The get children method to use (or null to use default)</param>
        /// <param name="getParentMethodOverride">The get parent method to use (or null to use default)</param>
        public TreeTraversalParameters(TTreeNode root, eTraversalFlowDirection flowDirection, eTraversalStrategy strategy, GetTreeNodeChildren <TTreeNode> getChildrenMethodOverride = null, GetTreeNodeParent <TTreeNode> getParentMethodOverride = null)
        {
            this.Root                   = root;
            this.FlowDirection          = flowDirection;
            this.ChildTraversalStrategy = strategy;
            this.TypeMatching           = null;
            this.Predicate              = null;
            this.PruneAfterFirstFind    = false;
            this.MaxNumberOfEvaluations = -1; // All
            this.TrackReentrancy        = false;
            this.DepthLimits            = null;

            m_getChildrenMethodOverride = getChildrenMethodOverride;
            m_getParentMethodOverride   = getParentMethodOverride;
        }
示例#20
0
 public static T GetFirstChildOfType <T> (TTreeNode start, Predicate <T> predicate = null, eTraversalFlowDirection flowDirection = DefaultTraversalFlowDirection, eTraversalStrategy strategy = DefaultTraversalStrategy, bool includeSelf = true, bool canTypeBeAncestor = true, LevelRestriction depthLimits = null, GetTreeNodeChildren <TTreeNode> getChildrenMethodOverride = null, GetTreeNodeParent <TTreeNode> getParentMethodOverride = null)
     where T : class
 {
     return(GetFirstChildOfType <T>(null, start, predicate, flowDirection, strategy, includeSelf, canTypeBeAncestor, depthLimits, getChildrenMethodOverride, getParentMethodOverride));
 }