Example #1
0
        // Copies tree branch (copies every node) back to the root that this Mapping is part of - this should always be a leaf node
        // it returns the new copy of the Mapping that is in the same spot in the tree as this
        // And also returns in the out variable the new copied root mapping
        public Mapping CopyTreeBranch(out Mapping newRootMapping)
        {
            Mapping newTree = Copy(false);

            int     previousChildIndex;
            Mapping previousNode = null;
            Mapping currentNode  = newTree;

            while (currentNode.parent != null)
            {
                previousChildIndex = currentNode.childIndex;
                previousNode       = currentNode;
                currentNode        = currentNode.parent;

                currentNode = currentNode.Copy(true);

                // Change the correct child node to the copy
                currentNode.children[previousChildIndex] = previousNode;
            }

            newRootMapping = currentNode;

            return(newTree);
        }
Example #2
0
        // Copies this tree from left to right until it gets to mapping - will include mapping in the returned copy
        public Mapping CopyTreeUpToMapping(ref Mapping targetMapping)
        {
            if (parent != null)
            {
                Debug.LogError("Trying to CopyTreeUpToMapping starting at a non-root Mapping! - " + this);
                return(null);
            }

            Mapping currentMapping = this;
            Mapping parentMapping  = null;
            Dictionary <Mapping, int> currentChildIndex = new Dictionary <Mapping, int>();

            while (true)
            {
                int index = 0;
                if (!currentChildIndex.TryGetValue(currentMapping, out index))
                {
                    if (currentMapping == targetMapping)
                    {
                        currentMapping        = currentMapping.Copy(true);
                        currentMapping.parent = parentMapping;
                        targetMapping         = currentMapping;
                    }
                    else
                    {
                        currentMapping        = currentMapping.Copy(true);
                        currentMapping.parent = parentMapping;
                    }
                    currentChildIndex.Add(currentMapping, 0);

                    if (currentMapping.children == null || currentMapping.children.Count == 0)
                    {
                        // Exit while loop when the targetMapping copy has no more children to copy
                        if (currentMapping == targetMapping)
                        {
                            break;
                        }

                        // No more children - go back up the tree looking for more children to copy
                        currentMapping = currentMapping.parent;
                        while (currentMapping != null && currentMapping.children != null && currentChildIndex[currentMapping] >= currentMapping.children.Count)
                        {
                            // Exit while loop when the targetMapping copy has no more children to copy
                            if (currentMapping == targetMapping)
                            {
                                break;
                            }
                            currentMapping = currentMapping.parent;
                        }

                        // The break above only breaks out of the small while loop
                        if (currentMapping == targetMapping)
                        {
                            break;
                        }

                        // This should never happen - means it never found the targetMapping
                        if (currentMapping == null)
                        {
                            Debug.LogError("CopyTreeUpToMapping ended up with a null currentMapping! - " + this);
                            return(null);
                        }
                    }
                    else
                    {
                        // currentMapping has children - goto first child
                        currentChildIndex[currentMapping] = 1;
                        parentMapping  = currentMapping;
                        currentMapping = currentMapping.children[0];
                    }
                }
                else if (index < currentMapping.children.Count)
                {
                    // Move currentMapping to next child
                    currentChildIndex[currentMapping]++;
                    parentMapping  = currentMapping;
                    currentMapping = currentMapping.children[currentChildIndex[currentMapping] - 1];
                }
                else
                {
                    // Exit while loop when the targetMapping copy has no more children to copy
                    if (currentMapping == targetMapping)
                    {
                        break;
                    }

                    // No more children - go back up the tree looking for more children to copy
                    currentMapping = currentMapping.parent;
                    while (currentMapping != null && currentMapping.children != null && currentChildIndex[currentMapping] >= currentMapping.children.Count)
                    {
                        // Exit while loop when the targetMapping copy has no more children to copy
                        if (currentMapping == targetMapping)
                        {
                            break;
                        }
                        currentMapping = currentMapping.parent;
                    }

                    // The break above only breaks out of the small while loop
                    if (currentMapping == targetMapping)
                    {
                        break;
                    }

                    // This should never happen - means it never found the targetMapping
                    if (currentMapping == null)
                    {
                        Debug.LogError("CopyTreeUpToMapping ended up with a null currentMapping! - " + this);
                        return(null);
                    }
                }
            }

            // Go up to rootMapping
            while (currentMapping.parent != null)
            {
                currentMapping = currentMapping.parent;
            }
            return(currentMapping);
        }