Types phase 4 — containers. A splice-on-demand open-addressing hash table (runtime/native/dict.ludic, FNV-1a, linear probing, tombstones, grow at 0.7) behind two namespaces: - Dict.* — string -> int map: new/set/get/get_or/has/remove/size/clear/keys. Resource counts, id/name registries. O(1) average vs a linear list scan. - Set.* — set of strings: new/add/has/remove/size/clear/members. Tags, unlocked achievements, visited tiles. Shares the same table. Values are int (also an entity handle / small id); Value.* covers richer maps. [T; N] inline fixed arrays remain future work — typed buffers and []T slices already cover heap-backed arrays. Wired: parser splice trigger (g_uses_dict), emit_call dispatch, reseeded seed, a self-asserting example (examples/library/containers.ludic + feat_case), and per-symbol docs + inventory. All suites green incl. golden renders byte- identical and the bootstrap fixpoint. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
39 lines
1.6 KiB
Text
39 lines
1.6 KiB
Text
# containers.ludic — Dict.* (string-keyed hash map) and Set.* (string set).
|
|
# Each assertion that holds prints its number, so a full run prints:
|
|
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
|
# One open-addressing hash table backs both (see runtime/native/dict.ludic).
|
|
program Containers {
|
|
entry {
|
|
# --- Dict: name -> int (resource counts, id registries) ---
|
|
let bank = Dict.new()
|
|
Dict.set(bank, "gold", 100)
|
|
Dict.set(bank, "wood", 50)
|
|
Dict.set(bank, "gold", 150) # update in place
|
|
if Dict.get(bank, "gold") == 150 { print(1) }
|
|
if Dict.get(bank, "wood") == 50 { print(2) }
|
|
if Dict.get(bank, "stone") == 0 { print(3) } # absent -> 0
|
|
if Dict.get_or(bank, "stone", 0 - 1) == 0 - 1 { print(4) }
|
|
if Dict.has(bank, "wood") { print(5) }
|
|
if Dict.size(bank) == 2 { print(6) }
|
|
Dict.remove(bank, "wood")
|
|
if not Dict.has(bank, "wood") { print(7) }
|
|
if Dict.size(bank) == 1 { print(8) }
|
|
|
|
# grow past the initial capacity, then read back
|
|
var i = 0
|
|
while i < 100 { Dict.set(bank, "k" + string(i), i * 2); i = i + 1 }
|
|
if Dict.get(bank, "k42") == 84 { print(9) }
|
|
if Dict.size(bank) == 101 { print(10) } # gold + k0..k99
|
|
if len(Dict.keys(bank)) == 101 { print(11) }
|
|
|
|
# --- Set: membership by name (tags, unlocked, visited) ---
|
|
let tags = Set.new()
|
|
Set.add(tags, "poison")
|
|
Set.add(tags, "fire")
|
|
Set.add(tags, "poison") # duplicate is ignored
|
|
if Set.has(tags, "fire") { print(12) }
|
|
if not Set.has(tags, "ice") { print(13) }
|
|
Set.remove(tags, "fire")
|
|
if Set.size(tags) == 1 { print(14) }
|
|
}
|
|
}
|