예제 #1
0
        protected List <PatternNodePPA> Compute(PatternNodePPA node, IPermutationsCollection avoidedPermutations,
                                                int depthComputed, ResultPPA result)
        {
            List <PatternNodePPA> nodes    = new List <PatternNodePPA>();
            List <PatternNodePPA> newNodes = new List <PatternNodePPA>();
            IPermutationDictionary <ExtensionMap> extensionMaps = new PermutationDictionary <ExtensionMap>();

            nodes.Add(node);

            for (int i = 0; i < depthComputed; i++)
            {
                extensionMaps = ExtractExtensionMaps(nodes);

                foreach (var nodeProcessed in nodes)
                {
                    newNodes.AddRange(nodeProcessed.ComputeChildren(extensionMaps, avoidedPermutations));
                }

                result.ProcessNodes(newNodes);

                nodes    = newNodes;
                newNodes = new List <PatternNodePPA>();
            }

            return(nodes);
        }
예제 #2
0
        protected void ComputeStepDefault(PatternNodePPA node, ResultPPA result)
        {
            PatternNodePPA parent = node.Parent;
            IPermutationDictionary <ExtensionMap> extensionMaps;

            parent.TryGetExtensionMapsDescendants(out extensionMaps);
            List <PatternNodePPA>[] descendants;
            parent.TryGetDescendants(out descendants);
            List <PatternNodePPA> nodes = descendants[node.PositionPrecedingLetters(node.Permutation.Length - 1)];

            IPermutationDictionary <ExtensionMap> newExtensionMaps;

            List <PatternNodePPA>[] newDescendants = new List <PatternNodePPA> [node.Permutation.Length + 1];
            List <PatternNodePPA>   newNodes;
            List <PatternNodePPA>   allNodes = new List <PatternNodePPA>();

            for (int i = 0; i < newDescendants.Length; i++)
            {
                newDescendants[i] = new List <PatternNodePPA>();
            }

            foreach (var nodeProcessed in nodes)
            {
                newNodes = nodeProcessed.ComputeChildren(extensionMaps, avoidedPermutations);
                allNodes.AddRange(newNodes);
                AddDescendants(newDescendants, newNodes, parent.DescendantsDepthFromNode,
                               node.Permutation.Length);
            }

            result.ProcessNodes(allNodes);
            newExtensionMaps = ExtractExtensionMaps(allNodes);

            node.SetDescendants(newDescendants, parent.DescendantsDepthFromNode);
            node.SetExtensionMapsDescendants(newExtensionMaps);
        }
예제 #3
0
        public ResultPPA Compute(PatternNodePPA node, IPermutationsCollection avoidedPermutations,
                                 int maximalDepthComputed, ResultPPA result, int numThreads, int descendantsDepth)
        {
            SetFields(maximalDepthComputed, avoidedPermutations, descendantsDepth);
            ComputeParallelHandler(node, result, numThreads);

            return(result);
        }
예제 #4
0
        public void Compute(string command, int n, StreamReader reader, int numThreads)
        {
            IPermutationsCollection avoidedPermutations = ProcessPermutations(reader, n);
            ResultPPA result = resultFactories[command].CreateResultPPA(n);
            IAvoidersPPAComputationHandler computationHandler = computationHandlers[command];

            result = computationHandler.Compute(avoidedPermutations, n, result, numThreads);
            ProcessResult(result);
        }
예제 #5
0
 protected void ComputeParallelUnSufficientThreadsChildrenProcessing
     (List <PatternNodePPA> children, int startIndex, int length,
     ResultPPA result)
 {
     for (int i = startIndex; i < startIndex + length; i++)
     {
         ComputeStep(children[i], result);
         ComputeNonParallel(children[i], result);
     }
 }
예제 #6
0
        protected virtual PatternNodePPA ComputeNotMaximalDepth(PatternNodePPA node,
                                                                IPermutationsCollection avoidedPermutations,
                                                                int depthComputed, ResultPPA result)
        {
            List <PatternNodePPA> nodes = Compute(node, avoidedPermutations, depthComputed, result);

            node.SetDescendants(new[] { nodes }, depthComputed);
            node.SetExtensionMapsDescendants(ExtractExtensionMaps(nodes));

            return(node);
        }
예제 #7
0
        public ResultPPA Compute(IPermutationsCollection avoidedPatterns, int maximalLengthAvoiders,
                                 ResultPPA result, int numThreads)
        {
            PatternNodePPA node = computationSequential.Compute(avoidedPatterns, maximalLengthAvoiders,
                                                                avoidedPatterns.LengthLongestPermutation - 1,
                                                                result);

            return(computationTreeTraverse.Compute(node, avoidedPatterns,
                                                   maximalLengthAvoiders, result,
                                                   numThreads, avoidedPatterns.LengthLongestPermutation - 1));
        }
 protected override void ComputeStep(PatternNodePPA node, ResultPPA result)
 {
     if (node.Permutation.Length + node.Parent.DescendantsDepthFromNode == depthComputed)
     {
         ComputeStepProcessPermutations(node, result);
     }
     else
     {
         ComputeStepDefault(node, result);
     }
 }
        protected override PatternNodePPA ComputeMaximalDepth(PatternNodePPA node,
                                                              IPermutationsCollection avoidedPermutations,
                                                              int depthComputed, ResultPPA result)
        {
            List <PatternNodePPA> nodes = Compute(node, avoidedPermutations, depthComputed - 1, result);

            foreach (var nodeProcessed in nodes)
            {
                result.TryProcessNodeChildren(nodeProcessed);
            }

            return(node);
        }
        protected void ComputeStepProcessChildren(PatternNodePPA node, ResultPPA result)
        {
            PatternNodePPA parent = node.Parent;

            List <PatternNodePPA>[] descendants;
            parent.TryGetDescendants(out descendants);
            List <PatternNodePPA> nodes = descendants[node.PositionPrecedingLetters(node.Permutation.Length - 1)];

            foreach (var nodeProcessed in nodes)
            {
                result.TryProcessNodeChildren(nodeProcessed);
            }
        }
예제 #11
0
        protected override PatternNodePPA ComputeMaximalDepth(PatternNodePPA node,
                                                              IPermutationsCollection avoidedPermutations,
                                                              int depthComputed, ResultPPA result)
        {
            List <PatternNodePPA> nodes = Compute(node, avoidedPermutations, depthComputed - 1, result);

            foreach (var nodeProcessed in nodes)
            {
                result.ProcessPermutations(
                    successorsComputation.Successors(nodeProcessed.Permutation,
                                                     node.ExtensionMap, avoidedPermutations));
            }


            return(node);
        }
        protected void ComputeStepProcessPermutations(PatternNodePPA node, ResultPPA result)
        {
            PatternNodePPA        parent = node.Parent;
            List <PatternNodePPA> nodes;

            List <PatternNodePPA>[] descendants;
            parent.TryGetDescendants(out descendants);

            nodes = descendants[node.PositionPrecedingLetters(node.Permutation.Length - 1)];

            foreach (var nodeProcessed in nodes)
            {
                result.ProcessPermutations(permSuccessorsComputation.Successors(nodeProcessed.Permutation,
                                                                                nodeProcessed.ExtensionMap,
                                                                                avoidedPermutations));
            }
        }
예제 #13
0
        protected void ComputeNonParallel(PatternNodePPA node, ResultPPA result)
        {
            if (depthComputed > node.Permutation.Length + descendantsDepth)
            {
                List <PatternNodePPA> children;
                node.TryGetChildren(out children);
                foreach (var child in children)
                {
                    ComputeStep(child, result);

                    ComputeNonParallel(child, result);
                }

                node.DisposeChildren();
                node.DisposeDescendants();
            }
        }
예제 #14
0
        protected void ComputeParallelUnSufficientThreads(PatternNodePPA node,
                                                          ResultPPA result, int numThreads)
        {
            int[]    childrenPerThread = DivideThreadsChildren(node.CountChildren, numThreads);
            Thread[] threads           = new Thread[childrenPerThread.Length - 1];
            List <PatternNodePPA> children;

            node.TryGetChildren(out children);

            int index = 0;


            for (int i = 0; i < threads.Length; i++)
            {
                int       startIndex       = index;
                ResultPPA clone            = result.GetClone();
                int       childrenToThread = childrenPerThread[i];
                threads[i] = new Thread(() => ComputeParallelUnSufficientThreadsChildrenProcessing(children,
                                                                                                   startIndex, childrenToThread, clone));

                index += childrenPerThread[i];
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            ComputeParallelUnSufficientThreadsChildrenProcessing(children,
                                                                 index, childrenPerThread[childrenPerThread.Length - 1],
                                                                 result.GetClone());

            foreach (var thread in threads)
            {
                thread.Join();
            }

            result.FetchDataAllClones();
            result.DisposeClones();
        }
예제 #15
0
        protected void ComputeParallelHandler(PatternNodePPA node, ResultPPA result, int numThreads)
        {
            if (depthComputed > node.Permutation.Length + descendantsDepth && node.CountChildren > 0)
            {
                if (numThreads == 1)
                {
                    ComputeNonParallel(node, result);
                }
                else
                {
                    if (numThreads < node.CountChildren)
                    {
                        ComputeParallelUnSufficientThreads(node, result, numThreads);
                    }
                    else
                    {
                        ComputeParallelSufficientThreads(node, result, numThreads);
                    }

                    node.DisposeChildren();
                    node.DisposeDescendants();
                }
            }
        }
예제 #16
0
        protected void ComputeParallelSufficientThreads(PatternNodePPA node,
                                                        ResultPPA result, int numThreads)
        {
            int[]    numThreadsChildren = ComputeThreadsCountChildren(node.CountChildren, numThreads);
            Thread[] threads            = new Thread[node.CountChildren - 1];
            List <PatternNodePPA> children;

            node.TryGetChildren(out children);


            for (int i = 0; i < threads.Length; i++)
            {
                ResultPPA      clone           = result.GetClone();
                PatternNodePPA child           = children[i];
                int            numThreadsChild = numThreadsChildren[i];
                threads[i] = new Thread(() => ComputeParallel(child,
                                                              clone, numThreadsChild));
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }


            ComputeParallel(children[children.Count - 1],
                            result.GetClone(), numThreadsChildren[children.Count - 1]);

            foreach (var thread in threads)
            {
                thread.Join();
            }

            result.FetchDataAllClones();
            result.DisposeClones();
        }
예제 #17
0
        public virtual PatternNodePPA Compute(IPermutationsCollection avoidedPermutations,
                                              int maximalLengthAvoiders, int maximalDepthComputed, ResultPPA result)
        {
            PatternNodePPA node = GetStartingNode(avoidedPermutations, maximalLengthAvoiders);

            int depth = Math.Min(maximalLengthAvoiders, maximalDepthComputed);

            if (depth == maximalLengthAvoiders)
            {
                return(ComputeMaximalDepth(node, avoidedPermutations, depth, result));
            }
            else
            {
                return(ComputeNotMaximalDepth(node, avoidedPermutations, depth, result));
            }
        }
예제 #18
0
 protected abstract PatternNodePPA ComputeMaximalDepth(PatternNodePPA node,
                                                       IPermutationsCollection avoidedPermutations,
                                                       int depthComputed, ResultPPA result);
예제 #19
0
 protected abstract void ComputeStep(PatternNodePPA node, ResultPPA result);
예제 #20
0
 protected void ComputeParallel(PatternNodePPA node, ResultPPA result, int numThreads)
 {
     ComputeStep(node, result);
     ComputeParallelHandler(node, result, numThreads);
 }