Add runtime.map_insert_and_check_for_previous

This commit is contained in:
gingerBill
2024-02-06 23:58:22 +00:00
parent 4035a226da
commit fa093d9b09
2 changed files with 65 additions and 0 deletions
+14
View File
@@ -824,6 +824,20 @@ map_insert :: proc(m: ^$T/map[$K]$V, key: K, value: V, loc := #caller_location)
return (^V)(__dynamic_map_set_without_hash((^Raw_Map)(m), map_info(T), rawptr(&key), rawptr(&value), loc))
}
// Explicitly inserts a key and value into a map `m`, the same as `map_insert`, but the return values differ.
// - `prev_key_ptr` will return the previous pointer of a key if it exists, and `nil` otherwise.
// - `value_ptr` will return the pointer of the memory where the insertion happens, and `nil` if the map failed to resize
// - `found_previous` will be true if `prev_key_ptr != nil`
@(require_results)
map_insert_and_check_for_previous :: proc(m: ^$T/map[$K]$V, key: K, value: V, loc := #caller_location) -> (prev_key_ptr: ^K, value_ptr: ^V, found_previous: bool) {
key, value := key, value
kp, vp := __dynamic_map_set_extra_without_hash((^Raw_Map)(m), map_info(T), rawptr(&key), rawptr(&value), loc)
prev_key_ptr = (^K)(kp)
value_ptr = (^V)(vp)
found_previous = kp != nil
return
}
@builtin
card :: proc "contextless" (s: $S/bit_set[$E; $U]) -> int {