ucif/antlr3/src/antlr3collections.c
static void
DFS(pANTLR3_TOPO topo, ANTLR3_UINT32 node)
{
    pANTLR3_BITSET edges;

    // Guard against a revisit and check for cycles
    //
    if  (topo->hasCycle == ANTLR3_TRUE)
    {
        return; // We don't do anything else if we found a cycle
    }

    if  (topo->visited->isMember(topo->visited, node))
    {
        // Check to see if we found a cycle. To do this we search the
        // current cycle stack and see if we find this node already in the stack.
        //
        ANTLR3_UINT32   i;

        for (i=0; i<topo->cycleMark; i++)
        {
            if  (topo->cycle[i] == node)
            {
                // Stop! We found a cycle in the input, so rejig the cycle
                // stack so that it only contains the cycle and set the cycle flag
                // which will tell the caller what happened
                //
                ANTLR3_UINT32 l;

                for (l = i; l < topo->cycleMark; l++)
                {
                    topo->cycle[l - i] = topo->cycle[l];    // Move to zero base in the cycle list
                }

                // Recalculate the limit
                //
                topo->cycleMark -= i;

                // Signal disaster
                //
                topo->hasCycle = ANTLR3_TRUE;
            }
        }
        return;
    }

    // So far, no cycles have been found and we have not visited this node yet,
    // so this node needs to go into the cycle stack before we continue
    // then we will take it out of the stack once we have descended all its
    // dependencies.
    //
    topo->cycle[topo->cycleMark++] = node;

    // First flag that we have visited this node
    //
    topo->visited->add(topo->visited, node);

    // Now, if this node has edges, then we want to ensure we visit
    // them all before we drop through and add this node into the sorted
    // list.
    //
    edges = *((topo->edges) + node);
    if  (edges != NULL)
    {
        // We have some edges, so visit each of the edge nodes
        // that have not already been visited.
        //
        ANTLR3_UINT32   numBits;            // How many bits are in the set
        ANTLR3_UINT32   i;
        ANTLR3_UINT32   range;

        numBits = edges->numBits(edges);
        range   = edges->size(edges);   // Number of set bits

        // Stop if we exahust the bit list or have checked the
        // number of edges that this node refers to (so we don't
        // check bits at the end that cannot possibly be set).
        //
        for (i=0; i<= numBits && range > 0; i++)
        {
            if  (edges->isMember(edges, i))
            {
                range--;        // About to check another one

                // Found an edge, make sure we visit and descend it
                //
                DFS(topo, i);
            }
        }
    }

    // At this point we will have visited all the dependencies
    // of this node and they will be ordered (even if there are cycles)
    // So we just add the node into the sorted list at the
    // current index position.
    //
    topo->sorted[topo->limit++] = node;

    // Remove this node from the cycle list if we have not detected a cycle
    //
    if  (topo->hasCycle == ANTLR3_FALSE)
    {
        topo->cycleMark--;
    }

    return;
}
/**
 * Given a starting node, descend its dependent nodes (ones that it has edges
 * to) until we find one without edges. Having found a node without edges, we have
 * discovered the bottom of a depth first search, which we can then ascend, adding
 * the nodes in order from the bottom, which gives us the dependency order.
 */
Given a starting node, descend its dependent nodes (ones that it has edges
to) until we find one without edges. Having found a node without edges, we have
discovered the bottom of a depth first search, which we can then ascend, adding
the nodes in order from the bottom, which gives us the dependency order.
Given a starting node descend its dependent nodes ( ones that it has edges to ) until we find one without edges . Having found a node without edges we have discovered the bottom of a depth first search which we can then ascend adding the nodes in order from the bottom which gives us the dependency order .
static void
DFS(pANTLR3_TOPO topo, ANTLR3_UINT32 node)
{
    pANTLR3_BITSET edges;
    if  (topo->hasCycle == ANTLR3_TRUE)
    {
        return;  We don't do anything else if we found a cycle
    }
    if  (topo->visited->isMember(topo->visited, node))
    {
         Check to see if we found a cycle. To do this we search the
         current cycle stack and see if we find this node already in the stack.
        ANTLR3_UINT32   i;
        for (i=0; i<topo->cycleMark; i++)
        {
            if  (topo->cycle[i] == node)
            {
                 Stop! We found a cycle in the input, so rejig the cycle
                 stack so that it only contains the cycle and set the cycle flag
                 which will tell the caller what happened
                ANTLR3_UINT32 l;
                for (l = i; l < topo->cycleMark; l++)
                {
                    topo->cycle[l - i] = topo->cycle[l];     Move to zero base in the cycle list
                }
                 Recalculate the limit
                topo->cycleMark -= i;
                 Signal disaster
                topo->hasCycle = ANTLR3_TRUE;
            }
        }
        return;
    }
     So far, no cycles have been found and we have not visited this node yet,
     so this node needs to go into the cycle stack before we continue
     then we will take it out of the stack once we have descended all its
     dependencies.
    topo->cycle[topo->cycleMark++] = node;
     First flag that we have visited this node
    topo->visited->add(topo->visited, node);
     Now, if this node has edges, then we want to ensure we visit
     them all before we drop through and add this node into the sorted
     list.
    edges = *((topo->edges) + node);
    if  (edges != NULL)
    {
         We have some edges, so visit each of the edge nodes
         that have not already been visited.
        ANTLR3_UINT32   numBits;             How many bits are in the set
        ANTLR3_UINT32   i;
        ANTLR3_UINT32   range;
        numBits = edges->numBits(edges);
        range   = edges->size(edges);    Number of set bits
         Stop if we exahust the bit list or have checked the
         number of edges that this node refers to (so we don't
         check bits at the end that cannot possibly be set).
        for (i=0; i<= numBits && range > 0; i++)
        {
            if  (edges->isMember(edges, i))
            {
                range--;         About to check another one
                 Found an edge, make sure we visit and descend it
                DFS(topo, i);
            }
        }
    }
     At this point we will have visited all the dependencies
     of this node and they will be ordered (even if there are cycles)
     So we just add the node into the sorted list at the
     current index position.
    topo->sorted[topo->limit++] = node;
     Remove this node from the cycle list if we have not detected a cycle
    if  (topo->hasCycle == ANTLR3_FALSE)
    {
        topo->cycleMark--;
    }
    return;
}
static void DFS ( pANTLR3_TOPO topo , ANTLR3_UINT32 node ) { pANTLR3_BITSET edges ; if ( topo -> hasCycle == ANTLR3_TRUE ) { return ; } if ( topo -> visited -> isMember ( topo -> visited , node ) ) { ANTLR3_UINT32 i ; for ( i = 0 ; i < topo -> cycleMark ; i ++ ) { if ( topo -> cycle [ i ] == node ) { ANTLR3_UINT32 l ; for ( l = i ; l < topo -> cycleMark ; l ++ ) { topo -> cycle [ l - i ] = topo -> cycle [ l ] ; } topo -> cycleMark -= i ; topo -> hasCycle = ANTLR3_TRUE ; } } return ; } topo -> cycle [ topo -> cycleMark ++ ] = node ; topo -> visited -> add ( topo -> visited , node ) ; edges = * ( ( topo -> edges ) + node ) ; if ( edges != NULL ) { ANTLR3_UINT32 numBits ; ANTLR3_UINT32 i ; ANTLR3_UINT32 range ; numBits = edges -> numBits ( edges ) ; range = edges -> size ( edges ) ; for ( i = 0 ; i <= numBits && range > 0 ; i ++ ) { if ( edges -> isMember ( edges , i ) ) { range -- ; DFS ( topo , i ) ; } } } topo -> sorted [ topo -> limit ++ ] = node ; if ( topo -> hasCycle == ANTLR3_FALSE ) { topo -> cycleMark -- ; } return ; }
Given a starting node, descend its dependent nodes (ones that it has edges
 to) until we find one without edges.
Given a starting node descend its dependent nodes ( ones that it has edges to ) until we find one without edges .
// Guard against a revisit and check for cycles // // We don't do anything else if we found a cycle // Check to see if we found a cycle. To do this we search the // current cycle stack and see if we find this node already in the stack. // // Stop! We found a cycle in the input, so rejig the cycle // stack so that it only contains the cycle and set the cycle flag // which will tell the caller what happened // // Move to zero base in the cycle list // Recalculate the limit // // Signal disaster // // So far, no cycles have been found and we have not visited this node yet, // so this node needs to go into the cycle stack before we continue // then we will take it out of the stack once we have descended all its // dependencies. // // First flag that we have visited this node // // Now, if this node has edges, then we want to ensure we visit // them all before we drop through and add this node into the sorted // list. // // We have some edges, so visit each of the edge nodes // that have not already been visited. // // How many bits are in the set // Number of set bits // Stop if we exahust the bit list or have checked the // number of edges that this node refers to (so we don't // check bits at the end that cannot possibly be set). // // About to check another one // Found an edge, make sure we visit and descend it // // At this point we will have visited all the dependencies // of this node and they will be ordered (even if there are cycles) // So we just add the node into the sorted list at the // current index position. // // Remove this node from the cycle list if we have not detected a cycle //