예제 #1
0
void TR::ILValidator::checkSoundness(TR::TreeTop *start, TR::TreeTop *stop)
   {
   soundnessRule(start, start != NULL, "Start tree must exist");
   soundnessRule(stop, !stop || stop->getNode() != NULL, "Stop tree must have a node");

   TR::NodeChecklist treetopNodes(comp()), ancestorNodes(comp()), visitedNodes(comp());

   // Can't use iterators here, because those presuppose the IL is sound.  Walk trees the old-fashioned way.
   //
   for (TR::TreeTop *currentTree = start; currentTree != stop; currentTree = currentTree->getNextTreeTop())
      {
      soundnessRule(currentTree, currentTree->getNode() != NULL, "Tree must have a node");
      soundnessRule(currentTree, !treetopNodes.contains(currentTree->getNode()), "Treetop node n%dn encountered twice", currentTree->getNode()->getGlobalIndex());

      treetopNodes.add(currentTree->getNode());

      TR::TreeTop *next = currentTree->getNextTreeTop();
      if (next)
         {
         soundnessRule(currentTree, next->getNode() != NULL, "Tree after n%dn must have a node", currentTree->getNode()->getGlobalIndex());
         soundnessRule(currentTree, next->getPrevTreeTop() == currentTree, "Doubly-linked treetop list must be consistent: n%dn->n%dn<-n%dn", currentTree->getNode()->getGlobalIndex(), next->getNode()->getGlobalIndex(), next->getPrevTreeTop()->getNode()->getGlobalIndex());
         }
      else
         {
         soundnessRule(currentTree, stop == NULL, "Reached the end of the trees after n%dn without encountering the stop tree n%dn", currentTree->getNode()->getGlobalIndex(), stop? stop->getNode()->getGlobalIndex() : 0);
         checkNodeSoundness(currentTree, currentTree->getNode(), ancestorNodes, visitedNodes);
         }
      }
   }
예제 #2
0
std::vector<int> primPrincipal(Graph & G, float & length, int v)
{
    int size = G->GetNodes();
    std::vector<float> distances (size,inf);
    std::vector<bool> visitedNodes (size,false);
    std::vector<int> parents (size,-1);
    std::deque<QueueItem> queue;
    for(int i=0; i<size; i++)
    {
        QueueItem q;
        q.VertexID = i+1;
        q.distance = inf;
        queue.push_back(q);
    }
    distances[v-1] = 0;
    visitedNodes[v-1] = true;
    while(!queue.empty())
    {
        std::sort(queue.begin(), queue.end(), sortQueue);
        QueueItem u = queue.front();
        queue.pop_front();
        SnapEdge EI;
        if(parents[u.VertexID-1] != -1)
        {
            TNodeEDatNet<TInt, TFlt>::TEdgeI EI = G->GetEI(parents[u.VertexID-1],u.VertexID);
            length+=(float)EI.GetDat();
        }
        visitedNodes[u.VertexID-1] = true;
        SnapNode NI = G->GetNI(u.VertexID);
        for (int e = 0; e < NI.GetOutDeg(); e++)
        {
            SnapEdge EI = G->GetEI(u.VertexID,NI.GetOutNDat(e));
            int outNode = NI.GetOutNDat(e);
            float edgeWeight = (float)EI.GetDat();
            if(!visitedNodes[outNode-1] && distances[outNode-1] > edgeWeight)
            {
                parents[outNode-1] = u.VertexID;
                distances[outNode-1] = edgeWeight;
                for(int i=0; i< queue.size(); i++)
                {
                    if(queue[i].VertexID == outNode)
                        queue[i].distance = edgeWeight;
                }
            }
        }
    }
    return parents;
}
예제 #3
0
void TR::SoundnessRule::validate(TR::ResolvedMethodSymbol *methodSymbol)
   {
   TR::TreeTop *start = methodSymbol->getFirstTreeTop();
   TR::TreeTop *stop = methodSymbol->getLastTreeTop();
   checkSoundnessCondition(start, start != NULL, "Start tree must exist");
   checkSoundnessCondition(stop, !stop || stop->getNode() != NULL,
                           "Stop tree must have a node");

   TR::NodeChecklist treetopNodes(comp()), ancestorNodes(comp()), visitedNodes(comp());

   /* NOTE: Can't use iterators here, because iterators presuppose that the IL is sound. */
   for (TR::TreeTop *currentTree = start; currentTree != stop;
        currentTree = currentTree->getNextTreeTop())
      {
      checkSoundnessCondition(currentTree, currentTree->getNode() != NULL,
                              "Tree must have a node");
      checkSoundnessCondition(currentTree, !treetopNodes.contains(currentTree->getNode()),
                              "Treetop node n%dn encountered twice",
                              currentTree->getNode()->getGlobalIndex());

      treetopNodes.add(currentTree->getNode());

      TR::TreeTop *next = currentTree->getNextTreeTop();
      if (next)
         {
         checkSoundnessCondition(currentTree, next->getNode() != NULL,
                                 "Tree after n%dn must have a node",
                                 currentTree->getNode()->getGlobalIndex());
         checkSoundnessCondition(currentTree, next->getPrevTreeTop() == currentTree,
                                 "Doubly-linked treetop list must be consistent: n%dn->n%dn<-n%dn",
                                 currentTree->getNode()->getGlobalIndex(),
                                 next->getNode()->getGlobalIndex(),
                                 next->getPrevTreeTop()->getNode()->getGlobalIndex());
         }
      else
         {
         checkSoundnessCondition(currentTree, stop == NULL,
                                 "Reached the end of the trees after n%dn without encountering the stop tree n%dn",
                                 currentTree->getNode()->getGlobalIndex(),
                                 stop? stop->getNode()->getGlobalIndex() : 0);
         checkNodeSoundness(currentTree, currentTree->getNode(),
                            ancestorNodes, visitedNodes);
         }
      }
   }
예제 #4
0
    vector<string> binaryTreePaths(TreeNode* root)
    {
        vector<string> paths;
        // If root is NULL, return an empty path vector immediately.
        if (root == nullptr)
        {
            return paths;
        }

        // pathVal is a vector of TreeNode values along a path 
        // starting from root.
        vector<int> pathVal({ root->val });

        // st is the stack used for the depth-first search.
        stack<TreeNode*> st;
        st.push(root);

        // visitedNodes keeps all the nodes which have been visited 
        // during the depth-first search. In other words, it keeps 
        // all the nodes which have ever been pushed into the stack.
        unordered_set<TreeNode*> visitedNodes({ root });

        // Do the depth-first search until the stack is empty.
        while (!st.empty())
        {
            TreeNode *curr = st.top();

            if ((curr->left == nullptr) && (curr->right == nullptr))
            {
                // curr is a leaf, so the current path is a path from 
                // root to a leaf and add it to paths.
                paths.push_back(GetPathStringFromNums(pathVal));

                // Remove the leaf node from pathVal which will end at 
                // the parent node of curr.
                pathVal.pop_back();

                st.pop();
            }
            else
            {
                // curr is not leaf, so we need to go down at least one 
                // level.

                // First we try pushing the left child if it hasn't 
                // been visited.
                if (curr->left != nullptr)
                {
                    auto itLeft = visitedNodes.find(curr->left);
                    if (itLeft == visitedNodes.end())
                    {
                        st.push(curr->left);
                        pathVal.push_back(curr->left->val);

                        visitedNodes.insert(curr->left);
                        continue;
                    }
                }

                // We reach here because either the left child doesn't 
                // exist or the left child has been visited. Then we try 
                // pushing the right child if it hasn't been visited.
                if (curr->right != nullptr)
                {
                    auto itRight = visitedNodes.find(curr->right);
                    if (itRight == visitedNodes.end())
                    {
                        st.push(curr->right);
                        pathVal.push_back(curr->right->val);

                        visitedNodes.insert(curr->right);
                        continue;
                    }
                }

                // The nodes in the subtree below curr have all been 
                // visited, so remove curr from pathVal which will end 
                // at the parent node of curr.
                pathVal.pop_back();
                st.pop();
            }
        }

        return paths;
    }