worlds

Closed arena worlds

Build a graph with stable, branded IDs. The graph grows after both the edge and the map entry are recorded, then an arena lookup uses the ID loaded from the edge. Recursive calls get independent arenas.

14

struct Node(I: type) {
    value: i64
    label: String
    edges: Vec(I)
}

#[no_alloc]
#[no_panic]
fn value(comptime W: world, graph: WorldArena(Node(WorldId(W)), W), id: WorldId(W)) -> i64 {
    graph.get(id).value
}

fn graph_score(depth: i64) -> i64 {
    world W (graph: Node(WorldId(W))) {
        let x = graph.add(Node(WorldId(W)) {
            value: depth, label: String.from("root"), edges: Vec(WorldId(W)).new()
        })
        let y = graph.add(Node(WorldId(W)) {
            value: 7, label: String.from("leaf"), edges: Vec(WorldId(W)).new()
        })
        graph.get_mut(x).edges.push(y)
        var weights = Map(WorldId(W), i64).new()
        weights.insert(y, 7)
        if depth < 0 { return value(graph, x) }
        for i in 0..80 {
            graph.add(Node(WorldId(W)) {
                value: i, label: String.new(), edges: Vec(WorldId(W)).new()
            })
        }
        let tail = if depth == 0 { 0 } else { graph_score(depth - 1) }
        let next: WorldId(W) = graph.get(x).edges[0]
        assert(weights.get(next).unwrap() == value(graph, next), "edge and map agree")
        tail + value(graph, next)
    }
}

fn main() {
    assert(graph_score(-1) == -1, "early return drops graph")
    println(graph_score(1))
}

View on GitHub

Esc