Exemplo n.º 1
0
    public virtual object Clone()
    {
        Conversation c = (Conversation)this.MemberwiseClone();

        c.conversationId   = (conversationId != null ? conversationId : null);
        c.conversationType = conversationType;

        Dictionary <ConversationNode, ConversationNode> clonedNodes =
            new Dictionary <ConversationNode, ConversationNode>();

        c.root = (root != null ? (ConversationNode)root.Clone() : null);

        clonedNodes.Add(root, c.root);
        List <ConversationNode> nodes   = new List <ConversationNode>();
        List <ConversationNode> visited = new List <ConversationNode>();

        nodes.Add(root);

        while (nodes.Count > 0)
        {
            ConversationNode temp   = nodes[0];
            ConversationNode cloned = clonedNodes[temp];
            nodes.RemoveAt(0);
            visited.Add(temp);

            for (int i = 0; i < temp.getChildCount(); i++)
            {
                ConversationNode tempCloned = clonedNodes[temp.getChild(i)];
                if (tempCloned == null)
                {
                    tempCloned = (ConversationNode)temp.getChild(i).Clone();
                    clonedNodes.Add(temp.getChild(i), tempCloned);
                }
                cloned.addChild(tempCloned);

                if (!visited.Contains(temp.getChild(i)) && !nodes.Contains(temp.getChild(i)))
                {
                    nodes.Add(temp.getChild(i));
                }
            }
        }
        return(c);
    }