Skip to content

Commit 0c6b72d

Browse files
ericsalocopybara-github
authored andcommitted
replace upb_Map_Delete()
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
1 parent 4664089 commit 0c6b72d

File tree

4 files changed

+38
-36
lines changed

4 files changed

+38
-36
lines changed

lua/msg.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ static int lupb_Map_Newindex(lua_State* L) {
579579
upb_MessageValue key = lupb_tomsgval(L, lmap->key_type, 2, 1, LUPB_REF);
580580

581581
if (lua_isnil(L, 3)) {
582-
upb_Map_Delete2(map, key, NULL);
582+
upb_Map_Delete(map, key, NULL);
583583
} else {
584584
upb_MessageValue val = lupb_tomsgval(L, lmap->value_type, 3, 1, LUPB_COPY);
585585
upb_Map_Set(map, key, val, lupb_Arenaget(L, 1));

python/map.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ int PyUpb_MapContainer_AssignSubscript(PyObject* _self, PyObject* key,
182182
if (!PyUpb_PyToUpb(val, val_f, &u_val, arena)) return -1;
183183
if (!PyUpb_MapContainer_Set(self, map, u_key, u_val, arena)) return -1;
184184
} else {
185-
if (!upb_Map_Delete2(map, u_key, NULL)) {
185+
if (!upb_Map_Delete(map, u_key, NULL)) {
186186
PyErr_Format(PyExc_KeyError, "Key not present in map");
187187
return -1;
188188
}

upb/collections/map.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key,
7070
map->val_size, arena);
7171
}
7272

73-
bool upb_Map_Delete2(upb_Map* map, upb_MessageValue key,
74-
upb_MessageValue* val) {
73+
bool upb_Map_Delete(upb_Map* map, upb_MessageValue key, upb_MessageValue* val) {
7574
upb_value v;
7675
const bool ok = _upb_Map_Delete(map, &key, map->key_size, &v);
7776
if (val) val->uint64_val = v.val;

upb/collections/map.h

+35-32
Original file line numberDiff line numberDiff line change
@@ -39,50 +39,53 @@
3939
extern "C" {
4040
#endif
4141

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

45-
/* Returns the number of entries in the map. */
46-
size_t upb_Map_Size(const upb_Map* map);
46+
// Returns the number of entries in the map.
47+
UPB_API size_t upb_Map_Size(const upb_Map* map);
4748

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

54-
/* Removes all entries in the map. */
55-
void upb_Map_Clear(upb_Map* map);
55+
// Removes all entries in the map.
56+
UPB_API void upb_Map_Clear(upb_Map* map);
5657

5758
typedef enum {
5859
kUpb_MapInsertStatus_Inserted = 0,
5960
kUpb_MapInsertStatus_Replaced = 1,
6061
kUpb_MapInsertStatus_OutOfMemory = 2,
6162
} upb_MapInsertStatus;
6263

63-
/* Sets the given key to the given value, returning whether the key was inserted
64-
* or replaced. If the key was inserted, then any existing iterators will be
65-
* invalidated. */
66-
upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key,
67-
upb_MessageValue val, upb_Arena* arena);
68-
69-
/* Sets the given key to the given value. Returns false if memory allocation
70-
* failed. If the key is newly inserted, then any existing iterators will be
71-
* invalidated. */
72-
UPB_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key,
73-
upb_MessageValue val, upb_Arena* arena) {
64+
// Sets the given key to the given value, returning whether the key was inserted
65+
// or replaced. If the key was inserted, then any existing iterators will be
66+
// invalidated.
67+
UPB_API upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key,
68+
upb_MessageValue val,
69+
upb_Arena* arena);
70+
71+
// Sets the given key to the given value. Returns false if memory allocation
72+
// failed. If the key is newly inserted, then any existing iterators will be
73+
// invalidated.
74+
UPB_API_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key,
75+
upb_MessageValue val, upb_Arena* arena) {
7476
return upb_Map_Insert(map, key, val, arena) !=
7577
kUpb_MapInsertStatus_OutOfMemory;
7678
}
7779

7880
// Deletes this key from the table. Returns true if the key was present.
7981
// If present and |val| is non-NULL, stores the deleted value.
80-
bool upb_Map_Delete2(upb_Map* map, upb_MessageValue key, upb_MessageValue* val);
82+
UPB_API bool upb_Map_Delete(upb_Map* map, upb_MessageValue key,
83+
upb_MessageValue* val);
8184

82-
// Deletes this key from the table. Returns true if the key was present.
8385
// (DEPRECATED and going away soon. Do not use.)
84-
UPB_INLINE bool upb_Map_Delete(upb_Map* map, upb_MessageValue key) {
85-
return upb_Map_Delete2(map, key, NULL);
86+
UPB_INLINE bool upb_Map_Delete2(upb_Map* map, upb_MessageValue key,
87+
upb_MessageValue* val) {
88+
return upb_Map_Delete(map, key, val);
8689
}
8790

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

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

103106
// DEPRECATED iterator, slated for removal.
104107

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

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

122-
/* Returns the key and value for this entry of the map. */
125+
// Returns the key and value for this entry of the map.
123126
upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter);
124127
upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter);
125128

0 commit comments

Comments
 (0)