minor refactoring

This commit is contained in:
2026-07-25 00:23:52 +02:00
parent ad54a00893
commit c7f527e3c3
5 changed files with 49 additions and 40 deletions
+1 -1
View File
@@ -8,4 +8,4 @@ Io :: alias io.Io
EnumMap :: alias enums.EnumMap
ArrayList :: alias arraylist.ArrayList
StringHashMap :: alias hashmap.StringHashMap
StaticStringMap :: alias strmap.StaticStringMap
StringMap :: alias strmap.StringMap
+8 -7
View File
@@ -1,6 +1,6 @@
import "@std/mem"
StaticStringMap proc($V type) type {
StringMap proc($V type) type {
return struct {
keys [][]u8
values []V
@@ -14,7 +14,8 @@ hide Pair proc($V type) type {
return struct { []u8, V }
}
init proc($V type, $N usize, $entries [N]Pair(V)) StaticStringMap(V) {
#! initializes a static string map from a list of key-value pairs (constructed at compile-time).
init proc($V type, $N usize, $entries [N]Pair(V)) StringMap(V) {
if N > usize(maxval!(u32)) {
compile_error!("static string map has too many entries")
}
@@ -38,7 +39,7 @@ init proc($V type, $N usize, $entries [N]Pair(V)) StaticStringMap(V) {
if N == 0 {
len_indexes [0]u32 = undefined
return StaticStringMap(V){
return StringMap(V){
keys = keys[..],
values = values[..],
len_indexes = len_indexes[..],
@@ -47,7 +48,7 @@ init proc($V type, $N usize, $entries [N]Pair(V)) StaticStringMap(V) {
}
}
# fixme: insertion sort is compile-time O(N^2); replace if large maps affect builds
# fixme: insertion sort is compile-time O(N²); replace if large maps affect builds
for 1..N |i| {
key :: keys[i]
value :: values[i]
@@ -69,7 +70,7 @@ init proc($V type, $N usize, $entries [N]Pair(V)) StaticStringMap(V) {
len_indexes[length] = u32(entry_index)
}
return StaticStringMap(V) {
return StringMap(V) {
keys = keys[..],
values = values[..],
len_indexes = len_indexes[..],
@@ -78,7 +79,7 @@ init proc($V type, $N usize, $entries [N]Pair(V)) StaticStringMap(V) {
}
}
get proc($V type, map @StaticStringMap(V), key []u8) ?V {
get proc($V type, map @StringMap(V), key []u8) ?V {
if (map.keys.len == 0 or key.len > maxval!(u32)) return null
length u32 = u32(key.len)
@@ -87,7 +88,7 @@ get proc($V type, map @StaticStringMap(V), key []u8) ?V {
idx usize = usize(map.len_indexes[usize(length)])
while idx < map.keys.len : idx += 1 {
candidate :: map.keys[idx]
if (candidate.len != key.len) return null
if (candidate.len != key.len) return null # key not found
if mem.eql(u8, candidate, key) return map.values[idx]
}
return null