Exemplo n.º 1
0
        /// <summary>
        /// Main method of merge algorithm.
        ///
        /// in first phase prepares new empty structure and data collections. Then collects all root memory locations
        /// and prepares their operations. As the final step process all merge operations which traverses the memory tree
        /// and creates new memory locations in target structure with the data from all source indexes.
        /// </summary>
        internal void Merge()
        {
            ContainerOperations[] collectVariables = new ContainerOperations[targetSnapshotCallLevel + 1];
            ContainerOperations[] collectControl   = new ContainerOperations[targetSnapshotCallLevel + 1];
            MergeOperation        returnOperation  = new MergeOperation();

            // Prepares empty structure for target snapshot
            for (int x = 0; x <= targetSnapshotCallLevel; x++)
            {
                writeableStrucure.AddLocalLevel();

                IWriteableIndexContainer variables = writeableStrucure.GetWriteableStackContext(x).WriteableVariables;
                collectVariables[x] = new ContainerOperations(this, variables, variables.UnknownIndex, variables.UnknownIndex);

                IWriteableIndexContainer control = writeableStrucure.GetWriteableStackContext(x).WriteableControllVariables;
                collectControl[x] = new ContainerOperations(this, control, control.UnknownIndex, control.UnknownIndex);
            }

            // Collects all objects and root locations from the source objects
            foreach (Snapshot snapshot in sourceSnapshots)
            {
                collectObjects(snapshot);

                for (int sourceLevel = 0, targetLevel = 0; targetLevel <= targetSnapshotCallLevel; sourceLevel++, targetLevel++)
                {
                    // Local levels of snaphot has to be merged together no matter to call level of each snapshot.
                    if (sourceLevel == snapshot.CallLevel && snapshot.CallLevel != targetSnapshotCallLevel)
                    {
                        if (isCallMerge)
                        {
                            // When this is the call merge the local level is forgotten
                            break;
                        }
                        else
                        {
                            targetLevel = targetSnapshotCallLevel;
                        }
                    }

                    // Gets all root locations
                    IWriteableIndexContainer targetVariables = writeableStrucure.GetWriteableStackContext(targetLevel).WriteableVariables;
                    IReadonlyIndexContainer  sourceVariables = snapshot.Structure.Readonly.GetReadonlyStackContext(sourceLevel).ReadonlyVariables;
                    collectVariables[targetLevel].CollectIndexes(snapshot, targetVariables.UnknownIndex, sourceVariables);

                    IWriteableIndexContainer targetControlls = writeableStrucure.GetWriteableStackContext(targetLevel).WriteableControllVariables;
                    IReadonlyIndexContainer  sourceControlls = snapshot.Structure.Readonly.GetReadonlyStackContext(sourceLevel).ReadonlyControllVariables;
                    collectControl[targetLevel].CollectIndexes(snapshot, targetControlls.UnknownIndex, sourceControlls);
                    collectTemporary(snapshot, sourceLevel, targetLevel);
                }

                foreach (var name in snapshot.Structure.Readonly.GetFunctions())
                {
                    foreach (var decl in snapshot.Structure.Readonly.GetFunction(name))
                    {
                        writeableStrucure.AddFunctiondeclaration(name, decl);
                    }
                }

                foreach (var name in snapshot.Structure.Readonly.GetClasses())
                {
                    foreach (var decl in snapshot.Structure.Readonly.GetClass(name))
                    {
                        writeableStrucure.AddClassDeclaration(name, decl);
                    }
                }

                // When is it call merge remember which arrays was forgotten in order to support arrays in returns
                if (isCallMerge)
                {
                    foreach (AssociativeArray array in snapshot.Structure.Readonly.ReadonlyLocalContext.ReadonlyArrays)
                    {
                        writeableStrucure.AddCallArray(array, snapshot);
                    }
                }
            }

            mergeObjects();

            // Prepares operations for all root locations
            for (int x = 0; x <= targetSnapshotCallLevel; x++)
            {
                collectVariables[x].MergeContainers();
                collectControl[x].MergeContainers();
                mergeTemporary(x);
            }

            processMerge();

            // Build aliases
            foreach (var alias in memoryAliases)
            {
                writeableStrucure.SetAlias(alias.Key, alias.Value.Build(writeableStrucure));
            }
        }