Beispiel #1
0
        /// <summary>
        /// Returns the shallow copy (Morphemes and Patterns are not duplicated) of the adtree. The returned copy is on the same path.
        /// </summary>
        /// <param name="adTree"></param>
        /// <returns></returns>
        public static IAdTree MakeShallowCopy(this IAdTree adTree)
        {
            // Store the current position in the tree.
            AttachingPosition[] path = adTree.GetPath();

            IAdTree root = adTree.Root;

            // <original, copy>
            Stack <Tuple <IAdTree, IAdTree> > stack = new Stack <Tuple <IAdTree, IAdTree> >();
            IAdTree rootCopy = new AdTree(root.Morpheme, root.Pattern);

            stack.Push(Tuple.Create(root, rootCopy));

            while (stack.Count > 0)
            {
                Tuple <IAdTree, IAdTree> aThis = stack.Pop();

                if (aThis.Item1.Left != null)
                {
                    IAdTree leftCopy = new AdTree(aThis.Item1.Left.Morpheme, aThis.Item1.Left.Pattern);
                    aThis.Item2.Left = leftCopy;
                    stack.Push(Tuple.Create(aThis.Item1.Left, leftCopy));
                }

                if (aThis.Item1.Right != null)
                {
                    IAdTree rightCopy = new AdTree(aThis.Item1.Right.Morpheme, aThis.Item1.Right.Pattern);
                    aThis.Item2.Right = rightCopy;
                    stack.Push(Tuple.Create(aThis.Item1.Right, rightCopy));
                }
            }

            // Return the tree element in the copy which is on the same path as the input parameter.
            if (rootCopy.TryGetAdTree(path, out IAdTree result))
            {
                return(result);
            }

            throw new InvalidOperationException("Failed to properly copy the adtree.");
        }
Beispiel #2
0
        /// <summary>
        /// Returns the deep copy of the sub-adTree. (adTree will be the root of the returned adTree)
        /// </summary>
        /// <param name="adTree"></param>
        /// <returns></returns>
        public static IAdTree MakeDeepCopy(this IAdTree adTree)
        {
            var root = adTree;

            // <original, copy>
            Stack <Tuple <IAdTree, IAdTree> > stack = new Stack <Tuple <IAdTree, IAdTree> >();

            var     rootMorpheme = new Morpheme(root.Morpheme);
            var     rootPattern  = new Pattern(root.Pattern);
            IAdTree rootCopy     = new AdTree(rootMorpheme, rootPattern);

            stack.Push(Tuple.Create(root, rootCopy));

            while (stack.Count > 0)
            {
                Tuple <IAdTree, IAdTree> aThis = stack.Pop();

                if (aThis.Item1.Left != null)
                {
                    var     morpheme = new Morpheme(aThis.Item1.Left.Morpheme);
                    var     pattern  = new Pattern(aThis.Item1.Left.Pattern);
                    IAdTree leftCopy = new AdTree(morpheme, pattern);
                    aThis.Item2.Left = leftCopy;
                    stack.Push(Tuple.Create(aThis.Item1.Left, leftCopy));
                }

                if (aThis.Item1.Right != null)
                {
                    var     morpheme  = new Morpheme(aThis.Item1.Right.Morpheme);
                    var     pattern   = new Pattern(aThis.Item1.Right.Pattern);
                    IAdTree rightCopy = new AdTree(morpheme, pattern);
                    aThis.Item2.Right = rightCopy;
                    stack.Push(Tuple.Create(aThis.Item1.Right, rightCopy));
                }
            }

            return(rootCopy);
        }