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.

Usage example (Cross-references)

Usage examples (Cross-references)
        }
    
        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");
    
        CityGraph graph = GraphInitWithDeepCopy(StrInitCopy, StrDeinit);
        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");
    
        typedef Graph(int) IntGraph;
        GraphAddEdge(&graph, a, b);
    
        GraphNodeForeachNeighbor(GraphGetNode(&graph, a), neighbor) {
            (void)neighbor;
            (void)GraphAddEdge(&graph, a, c);
        GraphForeachNode(&graph, node) {
            if (GraphNodeGetId(node) == a) {
                GraphNodeForeachNeighbor(node, neighbor) {
                    if (GraphNodeGetId(neighbor) == b) {
                        (void)GraphMarkEdgeForRemoval(&graph, GraphNodeGetId(node), GraphNodeGetId(neighbor));
Last updated on