示例#1
0
        /// <summary>
        /// Merges two nodes recursivly by also merging all child nodes
        /// </summary>
        /// <param name="sameNode">The node to be intaken</param>
        /// <param name="replacements">If the node already exist, the id of the sameNode gets added
        /// to the replacement list together with the already existing node. Can be null if not needed</param>
        internal void Merge(Node sameNode)
        {
            if (!Valid || !sameNode.Valid)
            {
                throw new InvalidOperationException("Node is declared invalid");
            }

            Appearances = Appearances.Union(sameNode.Appearances).ToList();

            foreach (var nodeToAdd in sameNode.ChildNodes)
            {
                if (ChildNodes.TryGetValue(nodeToAdd.Key, out Node existingNode))
                {
                    existingNode.Merge(nodeToAdd.Value);
                }
                else
                {
                    var replacement = nodeToAdd.Value.CreateReplacement();
                    AddChild(replacement);
                }
            }

            sameNode.ReplacedBy = this;
            sameNode.Invalidate();
        }