Skip to content

Commit

Permalink
replace upb_Map_Delete()
Browse files Browse the repository at this point in the history
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 protocolbuffers#3: Give the new footprint to the original function and switch back to it.

Since we're already touching map.h, also mark UPB_API as appropriate.

PiperOrigin-RevId: 498398474
  • Loading branch information
ericsalo authored and copybara-github committed Dec 29, 2022
1 parent 4664089 commit 0c6b72d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lua/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_Delete2(map, key, NULL);
upb_Map_Delete(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));
Expand Down
2 changes: 1 addition & 1 deletion python/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_Delete2(map, u_key, NULL)) {
if (!upb_Map_Delete(map, u_key, NULL)) {
PyErr_Format(PyExc_KeyError, "Key not present in map");
return -1;
}
Expand Down
3 changes: 1 addition & 2 deletions upb/collections/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key,
map->val_size, arena);
}

bool upb_Map_Delete2(upb_Map* map, upb_MessageValue key,
upb_MessageValue* val) {
bool upb_Map_Delete(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;
Expand Down
67 changes: 35 additions & 32 deletions upb/collections/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,50 +39,53 @@
extern "C" {
#endif

/* Creates a new map on the given arena with the given key/value size. */
upb_Map* upb_Map_New(upb_Arena* a, upb_CType key_type, upb_CType value_type);
// Creates a new map on the given arena with the given key/value size.
UPB_API upb_Map* upb_Map_New(upb_Arena* a, upb_CType key_type,
upb_CType value_type);

/* Returns the number of entries in the map. */
size_t upb_Map_Size(const upb_Map* map);
// Returns the number of entries in the map.
UPB_API size_t upb_Map_Size(const upb_Map* map);

/* Stores a value for the given key into |*val| (or the zero value if the key is
* not present). Returns whether the key was present. The |val| pointer may be
* NULL, in which case the function tests whether the given key is present. */
bool upb_Map_Get(const upb_Map* map, upb_MessageValue key,
upb_MessageValue* val);
// Stores a value for the given key into |*val| (or the zero value if the key is
// not present). Returns whether the key was present. The |val| pointer may be
// NULL, in which case the function tests whether the given key is present.
UPB_API bool upb_Map_Get(const upb_Map* map, upb_MessageValue key,
upb_MessageValue* val);

/* Removes all entries in the map. */
void upb_Map_Clear(upb_Map* map);
// Removes all entries in the map.
UPB_API void upb_Map_Clear(upb_Map* map);

typedef enum {
kUpb_MapInsertStatus_Inserted = 0,
kUpb_MapInsertStatus_Replaced = 1,
kUpb_MapInsertStatus_OutOfMemory = 2,
} upb_MapInsertStatus;

/* Sets the given key to the given value, returning whether the key was inserted
* or replaced. If the key was inserted, then any existing iterators will be
* invalidated. */
upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key,
upb_MessageValue val, upb_Arena* arena);

/* Sets the given key to the given value. Returns false if memory allocation
* failed. If the key is newly inserted, then any existing iterators will be
* invalidated. */
UPB_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key,
upb_MessageValue val, upb_Arena* arena) {
// Sets the given key to the given value, returning whether the key was inserted
// or replaced. If the key was inserted, then any existing iterators will be
// invalidated.
UPB_API upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key,
upb_MessageValue val,
upb_Arena* arena);

// Sets the given key to the given value. Returns false if memory allocation
// failed. If the key is newly inserted, then any existing iterators will be
// invalidated.
UPB_API_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key,
upb_MessageValue val, upb_Arena* arena) {
return upb_Map_Insert(map, key, val, arena) !=
kUpb_MapInsertStatus_OutOfMemory;
}

// Deletes this key from the table. Returns true if the key was present.
// If present and |val| is non-NULL, stores the deleted value.
bool upb_Map_Delete2(upb_Map* map, upb_MessageValue key, upb_MessageValue* val);
UPB_API bool upb_Map_Delete(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);
UPB_INLINE bool upb_Map_Delete2(upb_Map* map, upb_MessageValue key,
upb_MessageValue* val) {
return upb_Map_Delete(map, key, val);
}

// Map iteration:
Expand All @@ -97,8 +100,8 @@ UPB_INLINE bool upb_Map_Delete(upb_Map* map, upb_MessageValue key) {

// Advances to the next entry. Returns false if no more entries are present.
// Otherwise returns true and populates both *key and *value.
bool upb_Map_Next(const upb_Map* map, upb_MessageValue* key,
upb_MessageValue* val, size_t* iter);
UPB_API bool upb_Map_Next(const upb_Map* map, upb_MessageValue* key,
upb_MessageValue* val, size_t* iter);

// DEPRECATED iterator, slated for removal.

Expand All @@ -114,12 +117,12 @@ bool upb_Map_Next(const upb_Map* map, upb_MessageValue* key,
// Advances to the next entry. Returns false if no more entries are present.
bool upb_MapIterator_Next(const upb_Map* map, size_t* iter);

/* Returns true if the iterator still points to a valid entry, or false if the
* iterator is past the last element. It is an error to call this function with
* kUpb_Map_Begin (you must call next() at least once first). */
// Returns true if the iterator still points to a valid entry, or false if the
// iterator is past the last element. It is an error to call this function with
// kUpb_Map_Begin (you must call next() at least once first).
bool upb_MapIterator_Done(const upb_Map* map, size_t iter);

/* Returns the key and value for this entry of the map. */
// Returns the key and value for this entry of the map.
upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter);
upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter);

Expand Down

0 comments on commit 0c6b72d

Please sign in to comment.