示例#1
0
        private bool MakePass(ITree tree, ISimplificationOptions options)
        {
            bool removedAnything = false;

            IBranch[] branches = tree.Branches.ToArray();

            foreach (IBranch b in branches)
            {
                List <INode> nodesInBranch         = tree.EnumerateNodes(b).ToList();
                var          laterNodesInBranchSet = new HashSet <INode>(nodesInBranch);

                for (int index = 0; index < nodesInBranch.Count;)
                {
                    bool  removedNode = false;
                    INode currentNode = nodesInBranch[index];
                    laterNodesInBranchSet.Remove(currentNode);
                    INode nextNode = index < nodesInBranch.Count - 1 ? nodesInBranch[index + 1] : null;

                    // Remove edges that we got from cleaning up.
                    removedAnything |= CleanUpChildEdges(currentNode, nextNode, laterNodesInBranchSet, tree);

                    bool canRemoveTheNode = index > 0 || options.FirstBranchNodeMayBeRemoved;
                    if (canRemoveTheNode)
                    {
                        // Now, can we remove this node altogether?
                        removedNode = RemoveNodeIfItIsOnlyConnecting(
                            tree,
                            currentNode,
                            options.LeaveTails);
                    }

                    if (removedNode)
                    {
                        removedAnything = true;
                        nodesInBranch.RemoveAt(index);
                    }
                    else
                    {
                        index++;
                    }
                }
            }

            return(removedAnything);
        }
示例#2
0
        public void Simplify(ITree tree, ISimplificationOptions options)
        {
            // First, remove orphans, if applicable.
            if (!options.KeepAllOrphans)
            {
                RemoveOrphans(tree);
            }

            int  pass = 0;
            bool removedAnything;

            do
            {
                _log.Debug("Doing pass {0}.", pass + 1);
                pass++;
                removedAnything = MakePass(tree, options);
            } while (removedAnything);
        }