Skip to content
GraphNodeForeachNeighbor

GraphNodeForeachNeighbor

Description

Iterate over each outgoing neighbor of a node handle.

The neighbor loop variable is also a GraphNode handle. Structural graph mutation is not allowed while the iterator is active.

Parameters

Name Direction Description
node in Source node handle.
neighbor in Name of the GraphNode loop variable used for outgoing neighbors.

Success

Loop body runs once per outgoing neighbor of node in insertion order.

Failure

Aborts the traversal on a structural mutation (epoch mismatch) or when node carries no owning graph handle.

Usage example (Cross-references)

Usage examples (Cross-references)
        GraphForeachNode(&graph, node) {
            if (GraphNodeGetId(node) == a) {
                GraphNodeForeachNeighbor(node, neighbor) {
                    if (GraphNodeGetId(neighbor) == b) {
                        (void)GraphMarkEdgeForRemoval(&graph, GraphNodeGetId(node), GraphNodeGetId(neighbor));
        }
    
        GraphNodeForeachNeighbor(node, neighbor) {
            if (city_reachable_from(neighbor, goal_id)) {
                return true;
    
    static bool test_graph_city_reachability(void) {
        WriteFmt("Testing GraphForeachNode and GraphNodeForeachNeighbor for reachability\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        GraphForeachNode(&graph, node) {
            (void)MapEnsurePtr(&counts, GraphNodeGetId(node), 0);
            GraphNodeForeachNeighbor(node, neighbor) {
                u64 *count  = MapEnsurePtr(&counts, GraphNodeGetId(neighbor), 0);
                *count     += 1;
    
    static bool test_graph_neighbor_iteration_rejects_structural_mutation_deadend(void) {
        WriteFmt("Testing GraphNodeForeachNeighbor rejects structural mutation (should abort)\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        GraphAddEdge(&graph, a, b);
    
        GraphNodeForeachNeighbor(GraphGetNode(&graph, a), neighbor) {
            (void)neighbor;
            (void)GraphAddEdge(&graph, a, c);
Last updated on