Пример #1
0
 /// <summary>
 /// Returns a breadth first parent edge iterator.
 /// </summary>
 /// <param name="treeEdge">The tree edge.</param>
 /// <param name="treeIterationMode">The tree iteration mode.</param>
 /// <returns>A breadth first parent edge iterator.</returns>
 public static IEnumerable <ITaxonRelationsTreeEdge> AsBreadthFirstParentEdgeIterator(
     this ITaxonRelationsTreeEdge treeEdge,
     TaxonRelationsTreeParentsIterationMode treeIterationMode)
 {
     return(AsBreadthFirstParentEdgeIterator(new List <ITaxonRelationsTreeEdge> {
         treeEdge
     }, treeIterationMode));
 }
        /// <summary>
        /// Gets the tree edge.
        /// </summary>
        /// <param name="parentTaxonId">The parent taxon identifier.</param>
        /// <param name="childTaxonId">The child taxon identifier.</param>
        /// <returns>A tree edge or null if not found.</returns>
        public ITaxonRelationsTreeEdge GetTreeEdge(int parentTaxonId, int childTaxonId)
        {
            var parentNode = GetTreeNode(parentTaxonId);
            var childNode  = GetTreeNode(childTaxonId);

            ITaxonRelationsTreeEdge edge = null;

            //foreach (ITaxonRelationsTreeEdge treeEdge in parentNode.AllChildEdges)
            if (parentNode.AllEdges != null)
            {
                foreach (ITaxonRelationsTreeEdge treeEdge in parentNode.AllEdges)
                {
                    if (treeEdge.Child.Taxon.Id == childTaxonId)
                    {
                        edge = treeEdge;
                        break;
                    }
                }
            }

            return(edge);
        }
Пример #3
0
 /// <summary>
 /// Determines whether the specified <see cref="TaxonRelationsTreeEdge" />, is equal to this instance.
 /// </summary>
 /// <param name="other">The <see cref="TaxonRelationsTreeEdge" /> to compare with this instance.</param>
 /// <returns><c>true</c> if the specified <see cref="TaxonRelationsTreeEdge" /> is equal to this instance; otherwise, <c>false</c>.</returns>
 protected bool Equals(ITaxonRelationsTreeEdge other)
 {
     return(Equals(TaxonRelation.Id, other.TaxonRelation.Id));
 }
Пример #4
0
        /// <summary>
        /// Creates an iterator that traverses a list of tree nodes parents top first.
        /// </summary>
        public static IEnumerable <ITaxonRelationsTreeEdge> AsBreadthFirstParentEdgeIterator(
            this ICollection <ITaxonRelationsTreeEdge> treeEdges,
            TaxonRelationsTreeParentsIterationMode treeIterationMode)
        {
            HashSet <ITaxonRelationsTreeNode> visitedNodes = new HashSet <ITaxonRelationsTreeNode>();
            HashSet <ITaxonRelationsTreeEdge> visitedEdges = new HashSet <ITaxonRelationsTreeEdge>();
            Queue <ITaxonRelationsTreeEdge>   queue        = new Queue <ITaxonRelationsTreeEdge>();

            foreach (ITaxonRelationsTreeEdge edge in treeEdges)
            {
                visitedNodes.Add(edge.Child);
                queue.Enqueue(edge);
            }

            while (queue.Any())
            {
                ITaxonRelationsTreeEdge current = queue.Dequeue();
                if (current != null)
                {
                    if (treeIterationMode == TaxonRelationsTreeParentsIterationMode.Everything)
                    {
                        if (current.Parent.AllParentEdges != null)
                        {
                            for (int i = current.Parent.AllParentEdges.Count - 1; i >= 0; i--)
                            {
                                queue.Enqueue(current.Parent.AllParentEdges[i]);
                            }
                        }
                    }
                    else
                    {
                        if (current.Parent.ValidMainParents != null)
                        {
                            for (int i = current.Parent.ValidMainParents.Count - 1; i >= 0; i--)
                            {
                                queue.Enqueue(current.Parent.ValidMainParents[i]);
                            }
                        }

                        if (treeIterationMode == TaxonRelationsTreeParentsIterationMode.BothValidMainAndSecondaryParents)
                        {
                            if (current.Parent.ValidSecondaryParents != null)
                            {
                                for (int i = current.Parent.ValidSecondaryParents.Count - 1; i >= 0; i--)
                                {
                                    queue.Enqueue(current.Parent.ValidSecondaryParents[i]);
                                }
                            }
                        }
                    }

                    // Avoid cycles. Just return not visited nodes.
                    if (!visitedNodes.Contains(current.Parent))
                    {
                        visitedNodes.Add(current.Parent);
                    }

                    // Avoid cycles. Just return not visited edges.
                    if (!visitedEdges.Contains(current))
                    {
                        visitedEdges.Add(current);
                        yield return(current);
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Returns a breadth first child edge iterator.
        /// </summary>
        /// <param name="treeNodes">The tree nodes.</param>
        /// <param name="treeIterationMode">The tree iteration mode.</param>
        /// <returns>A breadth first child edge iterator.</returns>
        public static IEnumerable <ITaxonRelationsTreeEdge> AsBreadthFirstChildEdgeIterator(
            this ICollection <ITaxonRelationsTreeNode> treeNodes,
            TaxonRelationsTreeChildrenIterationMode treeIterationMode)
        {
            HashSet <ITaxonRelationsTreeNode> visitedNodes = new HashSet <ITaxonRelationsTreeNode>();
            HashSet <ITaxonRelationsTreeEdge> visitedEdges = new HashSet <ITaxonRelationsTreeEdge>();
            Queue <ITaxonRelationsTreeEdge>   queue        = new Queue <ITaxonRelationsTreeEdge>();

            foreach (var treeNode in treeNodes)
            {
                visitedNodes.Add(treeNode);

                if (treeIterationMode == TaxonRelationsTreeChildrenIterationMode.Everything)
                {
                    if (treeNode.AllChildEdges != null)
                    {
                        for (int i = 0; i < treeNode.AllChildEdges.Count; i++)
                        {
                            queue.Enqueue(treeNode.AllChildEdges[i]);
                        }
                    }
                }
                else
                {
                    if (treeNode.ValidMainChildren != null)
                    {
                        for (int i = 0; i < treeNode.ValidMainChildren.Count; i++)
                        {
                            queue.Enqueue(treeNode.ValidMainChildren[i]);
                        }
                    }

                    if (treeIterationMode == TaxonRelationsTreeChildrenIterationMode.BothValidMainAndSecondaryChildren)
                    {
                        if (treeNode.ValidSecondaryChildren != null)
                        {
                            for (int i = 0; i < treeNode.ValidSecondaryChildren.Count; i++)
                            {
                                queue.Enqueue(treeNode.ValidSecondaryChildren[i]);
                            }
                        }
                    }
                }
            }

            while (queue.Any())
            {
                ITaxonRelationsTreeEdge current = queue.Dequeue();
                if (current != null)
                {
                    if (treeIterationMode == TaxonRelationsTreeChildrenIterationMode.Everything)
                    {
                        if (current.Child.AllChildEdges != null)
                        {
                            for (int i = 0; i < current.Child.AllChildEdges.Count; i++)
                            {
                                queue.Enqueue(current.Child.AllChildEdges[i]);
                            }
                        }
                    }
                    else
                    {
                        if (current.Child.ValidMainChildren != null)
                        {
                            for (int i = 0; i < current.Child.ValidMainChildren.Count; i++)
                            {
                                queue.Enqueue(current.Child.ValidMainChildren[i]);
                            }
                        }

                        if (current.Child.ValidSecondaryChildren != null)
                        {
                            for (int i = 0; i < current.Child.ValidSecondaryChildren.Count; i++)
                            {
                                queue.Enqueue(current.Child.ValidSecondaryChildren[i]);
                            }
                        }
                    }

                    // Avoid cycles. Just return not visited nodes.
                    if (!visitedNodes.Contains(current.Child))
                    {
                        visitedNodes.Add(current.Child);
                    }

                    // Avoid cycles. Just return not visited edges.
                    if (!visitedEdges.Contains(current))
                    {
                        visitedEdges.Add(current);
                        yield return(current);
                    }
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Returns a depth first child edge iterator.
        /// </summary>
        /// <param name="treeNodes">The tree nodes.</param>
        /// <param name="treeIterationMode">The tree iteration mode.</param>
        /// <returns>A depth first child edge iterator.</returns>
        public static IEnumerable <ITaxonRelationsTreeEdge> AsDepthFirstChildEdgeIterator(
            this ICollection <ITaxonRelationsTreeNode> treeNodes,
            TaxonRelationsTreeChildrenIterationMode treeIterationMode)
        {
            HashSet <ITaxonRelationsTreeNode> visitedNodes = new HashSet <ITaxonRelationsTreeNode>();
            HashSet <ITaxonRelationsTreeEdge> visitedEdges = new HashSet <ITaxonRelationsTreeEdge>();
            Stack <ITaxonRelationsTreeEdge>   stack        = new Stack <ITaxonRelationsTreeEdge>();

            foreach (var treeNode in treeNodes)
            {
                visitedNodes.Add(treeNode);

                if (treeNode.ValidMainChildren != null)
                {
                    for (int i = treeNode.ValidMainChildren.Count - 1; i >= 0; i--)
                    {
                        stack.Push(treeNode.ValidMainChildren[i]);
                    }
                }

                if (treeIterationMode == TaxonRelationsTreeChildrenIterationMode.BothValidMainAndSecondaryChildren)
                {
                    if (treeNode.ValidSecondaryChildren != null)
                    {
                        for (int i = treeNode.ValidSecondaryChildren.Count - 1; i >= 0; i--)
                        {
                            stack.Push(treeNode.ValidSecondaryChildren[i]);
                        }
                    }
                }
            }

            while (stack.Any())
            {
                ITaxonRelationsTreeEdge current = stack.Pop();
                if (current != null)
                {
                    if (current.Child.ValidMainChildren != null)
                    {
                        for (int i = current.Child.ValidMainChildren.Count - 1; i >= 0; i--)
                        {
                            stack.Push(current.Child.ValidMainChildren[i]);
                        }
                    }

                    if (current.Child.ValidSecondaryChildren != null)
                    {
                        for (int i = current.Child.ValidSecondaryChildren.Count - 1; i >= 0; i--)
                        {
                            stack.Push(current.Child.ValidSecondaryChildren[i]);
                        }
                    }

                    // Avoid cycles. Just return not visited nodes.
                    if (!visitedNodes.Contains(current.Child))
                    {
                        visitedNodes.Add(current.Child);
                    }

                    // Avoid cycles. Just return not visited edges.
                    if (!visitedEdges.Contains(current))
                    {
                        visitedEdges.Add(current);
                        yield return(current);
                    }
                }
            }
        }