|
1 | | -// std.collections — collection conveniences in pure With. |
| 1 | +// std.collections — collection type surface imported by the prelude. |
2 | 2 | // |
3 | | -// Core collection types (Vec, HashMap, HashSet) are language built-ins. |
4 | | -// This module adds generic helper functions over those built-ins. |
5 | | - |
6 | | -pub fn update(map: HashMap[str, i32], key: str, default_value: i32, f: fn(i32) -> i32) -> void: |
7 | | - map.update(key, default_value, f) |
8 | | - |
9 | | -pub fn increment(map: HashMap[str, i32], key: str) -> void: |
10 | | - map.increment(key) |
11 | | - |
12 | | -pub fn decrement(map: HashMap[str, i32], key: str) -> void: |
13 | | - map.decrement(key) |
14 | | - |
15 | | -pub fn append(map: HashMap[str, Vec[i32]], key: str, value: i32) -> void: |
16 | | - map.append(key, value) |
17 | | - |
18 | | -pub fn sequence_option(xs: Vec[?i32]) -> ?Vec[i32]: |
19 | | - xs.sequence() |
20 | | - |
21 | | -pub fn sequence_result(xs: Vec[Result[i32, i32]]) -> Result[Vec[i32], i32]: |
22 | | - xs.sequence() |
23 | | - |
24 | | -// --- Additional collections (concrete scaffolding) --- |
25 | | - |
26 | | -extern fn with_i32_to_str(n: i32) -> str |
27 | | - |
28 | | -type Handle = { |
29 | | - key: str, |
30 | | - generation: i32, |
| 3 | +// Keep this module intentionally minimal for selfhost compatibility. |
| 4 | +// The compiler still owns the lowering/runtime behavior for these |
| 5 | +// collection types; this module provides the user-facing names so they |
| 6 | +// resolve through normal imports instead of hardcoded sema allowlists. |
| 7 | + |
| 8 | +type Vec[T] = { |
| 9 | + ptr: *const T, |
| 10 | + len: i64, |
| 11 | + cap: i64, |
| 12 | + elem_size: i64, |
31 | 13 | } |
32 | 14 |
|
33 | | -type SlotMap = { |
34 | | - generations: HashMap[str, i32], |
35 | | - values: HashMap[str, i32], |
| 15 | +type HashMap[K, V] = { |
| 16 | + ptr: *const i8, |
36 | 17 | } |
37 | 18 |
|
38 | | -pub fn slotmap_new -> SlotMap: |
39 | | - SlotMap { |
40 | | - generations: HashMap.new(), |
41 | | - values: HashMap.new(), |
42 | | - } |
43 | | - |
44 | | -pub fn slotmap_insert(map: SlotMap, value: i32) -> (SlotMap, Handle): |
45 | | - let key = map.generations.len() as i32 |> with_i32_to_str |
46 | | - let generation_value = map.generations.get(key).unwrap_or(0) + 1 |
47 | | - map.generations.insert(key, generation_value) |
48 | | - map.values.insert(key, value) |
49 | | - (map, Handle { key: key, generation: generation_value }) |
50 | | - |
51 | | -pub fn slotmap_get(map: SlotMap, h: Handle) -> ?i32: |
52 | | - let g = map.generations.get(h.key) |
53 | | - if g.is_none() then map.values.get("__with_slotmap_absent__") |
54 | | - else if g.unwrap() != h.generation then map.values.get("__with_slotmap_absent__") |
55 | | - else map.values.get(h.key) |
56 | | - |
57 | | -pub fn slotmap_contains(map: SlotMap, h: Handle) -> bool: |
58 | | - let g = map.generations.get(h.key) |
59 | | - if g.is_none() then false |
60 | | - else if g.unwrap() != h.generation then false |
61 | | - else map.values.get(h.key).is_some() |
62 | | - |
63 | | -pub fn slotmap_remove(map: SlotMap, h: Handle) -> (SlotMap, bool): |
64 | | - let g = map.generations.get(h.key) |
65 | | - if g.is_none() then (map, false) |
66 | | - else if g.unwrap() != h.generation then (map, false) |
67 | | - else |
68 | | - let removed = map.values.remove(h.key) |
69 | | - if removed: |
70 | | - map.generations.insert(h.key, h.generation + 1) |
71 | | - (map, removed) |
72 | | - |
73 | | -pub fn slotmap_len(map: SlotMap) -> i32: |
74 | | - map.values.len() as i32 |
75 | | - |
76 | | -type BTreeMap = { |
77 | | - inner: HashMap[str, i32], |
| 19 | +type HashSet[T] = { |
| 20 | + ptr: *const i8, |
78 | 21 | } |
79 | | - |
80 | | -pub fn btree_new -> BTreeMap: |
81 | | - BTreeMap { inner: HashMap.new() } |
82 | | - |
83 | | -pub fn btree_insert(map: BTreeMap, key: str, value: i32) -> BTreeMap: |
84 | | - map.inner.insert(key, value) |
85 | | - map |
86 | | - |
87 | | -pub fn btree_get(map: BTreeMap, key: str) -> ?i32: |
88 | | - map.inner.get(key) |
89 | | - |
90 | | -pub fn btree_contains(map: BTreeMap, key: str) -> bool: |
91 | | - map.inner.contains(key) |
92 | | - |
93 | | -pub fn btree_remove(map: BTreeMap, key: str) -> (BTreeMap, bool): |
94 | | - let removed = map.inner.remove(key) |
95 | | - (map, removed) |
96 | | - |
97 | | -pub fn btree_len(map: BTreeMap) -> i64: |
98 | | - map.inner.len() |
0 commit comments