From 3f173c4b810dd36c1ed24f4ffc82c1b2fb3eb60c Mon Sep 17 00:00:00 2001 From: Eric Salo Date: Thu, 22 Dec 2022 18:26:48 -0800 Subject: [PATCH] add upb_Map_Delete2() We would like for upb_Map_Delete() to optionally return the deleted value. Unfortunately this will require several steps since we are crossing repos. Step #1: Add a new version of the function and point all local uses at it. PiperOrigin-RevId: 497275930 --- lua/msg.c | 2 +- python/map.c | 2 +- upb/collections/map.c | 8 ++++++-- upb/collections/map.h | 9 ++++++++- upb/collections/map_gencode_util.h | 2 +- upb/collections/map_internal.h | 6 +++--- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lua/msg.c b/lua/msg.c index 010671b597..8a6f5cc04c 100644 --- a/lua/msg.c +++ b/lua/msg.c @@ -579,7 +579,7 @@ static int lupb_Map_Newindex(lua_State* L) { upb_MessageValue key = lupb_tomsgval(L, lmap->key_type, 2, 1, LUPB_REF); if (lua_isnil(L, 3)) { - upb_Map_Delete(map, key); + upb_Map_Delete2(map, key, NULL); } else { upb_MessageValue val = lupb_tomsgval(L, lmap->value_type, 3, 1, LUPB_COPY); upb_Map_Set(map, key, val, lupb_Arenaget(L, 1)); diff --git a/python/map.c b/python/map.c index b3a8fff11f..a058856016 100644 --- a/python/map.c +++ b/python/map.c @@ -182,7 +182,7 @@ int PyUpb_MapContainer_AssignSubscript(PyObject* _self, PyObject* key, if (!PyUpb_PyToUpb(val, val_f, &u_val, arena)) return -1; if (!PyUpb_MapContainer_Set(self, map, u_key, u_val, arena)) return -1; } else { - if (!upb_Map_Delete(map, u_key)) { + if (!upb_Map_Delete2(map, u_key, NULL)) { PyErr_Format(PyExc_KeyError, "Key not present in map"); return -1; } diff --git a/upb/collections/map.c b/upb/collections/map.c index e27e8b8980..3f5eb3b709 100644 --- a/upb/collections/map.c +++ b/upb/collections/map.c @@ -71,8 +71,12 @@ upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key, map->val_size, arena); } -bool upb_Map_Delete(upb_Map* map, upb_MessageValue key) { - return _upb_Map_Delete(map, &key, map->key_size); +bool upb_Map_Delete2(upb_Map* map, upb_MessageValue key, + upb_MessageValue* val) { + upb_value v; + const bool ok = _upb_Map_Delete(map, &key, map->key_size, &v); + if (val) val->uint64_val = v.val; + return ok; } bool upb_Map_Next(const upb_Map* map, upb_MessageValue* key, diff --git a/upb/collections/map.h b/upb/collections/map.h index ae2cf05d23..4e398dc554 100644 --- a/upb/collections/map.h +++ b/upb/collections/map.h @@ -76,7 +76,14 @@ UPB_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key, } // Deletes this key from the table. Returns true if the key was present. -bool upb_Map_Delete(upb_Map* map, upb_MessageValue key); +// If present and |val| is non-NULL, stores the deleted value. +bool upb_Map_Delete2(upb_Map* map, upb_MessageValue key, upb_MessageValue* val); + +// Deletes this key from the table. Returns true if the key was present. +// (DEPRECATED and going away soon. Do not use.) +UPB_INLINE bool upb_Map_Delete(upb_Map* map, upb_MessageValue key) { + return upb_Map_Delete2(map, key, NULL); +} // Map iteration: // diff --git a/upb/collections/map_gencode_util.h b/upb/collections/map_gencode_util.h index 59764c96b2..ca764c7557 100644 --- a/upb/collections/map_gencode_util.h +++ b/upb/collections/map_gencode_util.h @@ -76,7 +76,7 @@ UPB_INLINE bool _upb_msg_map_delete(upb_Message* msg, size_t ofs, const void* key, size_t key_size) { upb_Map* map = *UPB_PTR_AT(msg, ofs, upb_Map*); if (!map) return false; - return _upb_Map_Delete(map, key, key_size); + return _upb_Map_Delete(map, key, key_size, NULL); } UPB_INLINE void _upb_msg_map_clear(upb_Message* msg, size_t ofs) { diff --git a/upb/collections/map_internal.h b/upb/collections/map_internal.h index f235cb3ecd..d7ca1fc48f 100644 --- a/upb/collections/map_internal.h +++ b/upb/collections/map_internal.h @@ -111,10 +111,10 @@ UPB_INLINE void _upb_Map_Clear(upb_Map* map) { upb_strtable_clear(&map->table); } -UPB_INLINE bool _upb_Map_Delete(upb_Map* map, const void* key, - size_t key_size) { +UPB_INLINE bool _upb_Map_Delete(upb_Map* map, const void* key, size_t key_size, + upb_value* val) { upb_StringView k = _upb_map_tokey(key, key_size); - return upb_strtable_remove2(&map->table, k.data, k.size, NULL); + return upb_strtable_remove2(&map->table, k.data, k.size, val); } UPB_INLINE bool _upb_Map_Get(const upb_Map* map, const void* key,