From a4be1a5e4c9d76b617fe17f564dc97e325e6fec8 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 4 Jul 2021 18:52:47 +0100 Subject: [PATCH] `delete_key` now returns the deleted key and deleted value (if found) --- core/runtime/core_builtin.odin | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/core/runtime/core_builtin.odin b/core/runtime/core_builtin.odin index 29c84a59e..946f326ca 100644 --- a/core/runtime/core_builtin.odin +++ b/core/runtime/core_builtin.odin @@ -270,11 +270,22 @@ reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int) { // The delete_key built-in procedure deletes the element with the specified key (m[key]) from the map. // If m is nil, or there is no such element, this procedure is a no-op @builtin -delete_key :: proc(m: ^$T/map[$K]$V, key: K) { +delete_key :: proc(m: ^$T/map[$K]$V, key: K) -> (deleted_key: K, deleted_value: V) { if m != nil { key := key; - __dynamic_map_delete_key(__get_map_header(m), __get_map_hash(&key)); + h := __get_map_header(m); + hash := __get_map_hash(&key); + fr := __dynamic_map_find(h, hash); + if fr.entry_index >= 0 { + entry := __dynamic_map_get_entry(h, fr.entry_index); + deleted_key = (^K)(uintptr(entry)+h.key_offset)^; + deleted_value = (^V)(uintptr(entry)+h.value_offset)^; + + __dynamic_map_erase(h, fr); + } } + + return; }