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
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent 0623093498
commit 3f173c4b81
  1. 2
      lua/msg.c
  2. 2
      python/map.c
  3. 8
      upb/collections/map.c
  4. 9
      upb/collections/map.h
  5. 2
      upb/collections/map_gencode_util.h
  6. 6
      upb/collections/map_internal.h

@ -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));

@ -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;
}

@ -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,

@ -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:
//

@ -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) {

@ -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,

Loading…
Cancel
Save