// only copy a complete independent tree // when node name exists void ComputationNetwork::CopySubTree(const ComputationNetwork& fromNet, const std::wstring fromName, std::wstring toNamePrefix, const CopyNodeFlags flags) { InvalidateCompiledNetwork(); if (!(flags & CopyNodeFlags::copyNodeValue)) LogicError("CopySubTree: you cannot copy a tree without copying the node values."); ComputationNodeBasePtr fromRoot = fromNet.GetNodeFromName(fromName); for (const auto& fromNode : GetEvalOrder(fromRoot)) { wstring fromNodeName = fromNode->NodeName(); wstring toNodeName = toNamePrefix + fromNodeName; ComputationNodeBasePtr toNode = CopyNode(fromNet, fromNodeName, toNodeName, CopyNodeFlags::copyNodeValue); if (flags & CopyNodeFlags::copyNodeChildren) { // copy the children structure but use the new nodes generated for (int i = 0; i < fromNode->GetNumInputs(); i++) toNode->SetInput(i, GetNodeFromName(toNamePrefix + fromNode->GetInputs()[i]->NodeName())); } } }
// only copy a complete independent tree // when node name exists void ComputationNetwork::CopySubTree(const ComputationNetwork& fromNet, const std::wstring fromName, std::wstring toNamePrefix, const CopyNodeFlags flags) { InvalidateCompiledNetwork(); if (!(flags & CopyNodeFlags::copyNodeValue)) LogicError("CopySubTree: you cannot copy a tree without copying the node values."); ComputationNodeBasePtr fromRoot = fromNet.GetNodeFromName(fromName); if (!fromNet.EvalOrderExists(fromRoot)) const_cast<ComputationNetwork&>(fromNet).FormEvalOrder(fromRoot); for (const auto& fromNode : fromNet.GetEvalOrder(fromRoot)) // BUGBUG: This probably will fail because the precomputed eval orders are invalid at this point. { wstring fromNodeName = fromNode->NodeName(); wstring toNodeName = toNamePrefix + fromNodeName; ComputationNodeBasePtr toNode = CopyNode(fromNet, fromNodeName, toNodeName, CopyNodeFlags::copyNodeValue); if (flags & CopyNodeFlags::copyNodeInputLinks) { // copy the children structure but use the new nodes generated for (int i = 0; i < fromNode->GetNumInputs(); i++) toNode->SetInput(i, GetNodeFromName(toNamePrefix + fromNode->GetInputs()[i]->NodeName())); } } }
ComputationNodeBasePtr ComputationNetwork::CopyNode(const ComputationNetwork& fromNet, const std::wstring fromName, std::wstring toName, const CopyNodeFlags flags) { InvalidateCompiledNetwork(); if (toName == L"") toName = fromName; ComputationNodeBasePtr pFromNode = fromNet.GetNodeFromName(fromName); ComputationNodeBasePtr pToNode; // don't allow cross network child copy unless caller explicity handles children fixup if ((flags & CopyNodeFlags::copyNodeChildren) && this != &fromNet && !(flags & CopyNodeFlags::copyNodeChildrenCrossNetwork)) { LogicError("CopyNode: Copying node children across network is invalid."); } if (!NodeNameExists(toName)) { pToNode = pFromNode->Duplicate(toName, flags); AddNodeToNet(pToNode); } else { // node already exists pToNode = GetNodeFromName(toName); // same node. no copy needed if (pFromNode == pToNode) LogicError("CopyNode: You are copying the node to the same network with same node name."); else pFromNode->CopyTo(pToNode, toName, flags); // blast it over the existing node } return pToNode; }