Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

Commit 03b1dee

Browse files
ericsalocopybara-github
authored andcommitted
implement upb_Map_Next() as the new upb_Map iterator
update upb to use upb_Map_Next() remove upb_MapIterator_SetValue(), which was declared but not actually implemented remove upb_MapIterator_Done(), which was implemented but not actually used PiperOrigin-RevId: 489989481
1 parent e932f79 commit 03b1dee

File tree

9 files changed

+63
-60
lines changed

9 files changed

+63
-60
lines changed

lua/msg.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,8 @@ static int lupb_MapIterator_Next(lua_State* L) {
596596
size_t* iter = lua_touserdata(L, lua_upvalueindex(1));
597597
lupb_map* lmap = lupb_map_check(L, map);
598598

599-
if (upb_MapIterator_Next(lmap->map, iter)) {
600-
upb_MessageValue key = upb_MapIterator_Key(lmap->map, *iter);
601-
upb_MessageValue val = upb_MapIterator_Value(lmap->map, *iter);
599+
upb_MessageValue key, val;
600+
if (upb_Map_Next(lmap->map, &key, &val, iter)) {
602601
lupb_pushmsgval(L, map, lmap->key_type, key);
603602
lupb_pushmsgval(L, map, lmap->value_type, val);
604603
return 2;

python/convert.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,8 @@ bool PyUpb_Map_IsEqual(const upb_Map* map1, const upb_Map* map2,
334334
const upb_FieldDef* val_f = upb_MessageDef_Field(entry_m, 1);
335335
size_t iter = kUpb_Map_Begin;
336336

337-
while (upb_MapIterator_Next(map1, &iter)) {
338-
upb_MessageValue key = upb_MapIterator_Key(map1, iter);
339-
upb_MessageValue val1 = upb_MapIterator_Value(map1, iter);
337+
upb_MessageValue key, val1;
338+
while (upb_Map_Next(map1, &key, &val1, &iter)) {
340339
upb_MessageValue val2;
341340
if (!upb_Map_Get(map2, key, &val2)) return false;
342341
if (!PyUpb_ValueEq(val1, val2, val_f)) return false;

python/map.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,10 @@ static PyObject* PyUpb_MapContainer_Repr(PyObject* _self) {
318318
const upb_FieldDef* key_f = upb_MessageDef_Field(entry_m, 0);
319319
const upb_FieldDef* val_f = upb_MessageDef_Field(entry_m, 1);
320320
size_t iter = kUpb_Map_Begin;
321-
while (upb_MapIterator_Next(map, &iter)) {
322-
PyObject* key =
323-
PyUpb_UpbToPy(upb_MapIterator_Key(map, iter), key_f, self->arena);
324-
PyObject* val =
325-
PyUpb_UpbToPy(upb_MapIterator_Value(map, iter), val_f, self->arena);
321+
upb_MessageValue map_key, map_val;
322+
while (upb_Map_Next(map, &map_key, &map_val, &iter)) {
323+
PyObject* key = PyUpb_UpbToPy(map_key, key_f, self->arena);
324+
PyObject* val = PyUpb_UpbToPy(map_val, val_f, self->arena);
326325
if (!key || !val) {
327326
Py_XDECREF(key);
328327
Py_XDECREF(val);
@@ -474,8 +473,8 @@ PyObject* PyUpb_MapIterator_IterNext(PyObject* _self) {
474473
}
475474
upb_Map* map = PyUpb_MapContainer_GetIfReified(self->map);
476475
if (!map) return NULL;
477-
if (!upb_MapIterator_Next(map, &self->iter)) return NULL;
478-
upb_MessageValue key = upb_MapIterator_Key(map, self->iter);
476+
upb_MessageValue key, val;
477+
if (!upb_Map_Next(map, &key, &val, &self->iter)) return NULL;
479478
const upb_FieldDef* f = PyUpb_MapContainer_GetField(self->map);
480479
const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(f);
481480
const upb_FieldDef* key_f = upb_MessageDef_Field(entry_m, 0);

upb/collections/map.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,20 @@ bool upb_Map_Delete(upb_Map* map, upb_MessageValue key) {
7676
return _upb_Map_Delete(map, &key, map->key_size);
7777
}
7878

79-
bool upb_MapIterator_Next(const upb_Map* map, size_t* iter) {
80-
return _upb_map_next(map, iter);
79+
bool upb_Map_Next(const upb_Map* map, upb_MessageValue* key,
80+
upb_MessageValue* val, size_t* iter) {
81+
upb_StringView k;
82+
upb_value v;
83+
const bool ok = upb_strtable_next2(&map->table, &k, &v, iter);
84+
if (ok) {
85+
_upb_map_fromkey(k, key, map->key_size);
86+
_upb_map_fromvalue(v, val, map->val_size);
87+
}
88+
return ok;
8189
}
8290

83-
bool upb_MapIterator_Done(const upb_Map* map, size_t iter) {
84-
upb_strtable_iter i;
85-
UPB_ASSERT(iter != kUpb_Map_Begin);
86-
i.t = &map->table;
87-
i.index = iter;
88-
return upb_strtable_done(&i);
91+
bool upb_MapIterator_Next(const upb_Map* map, size_t* iter) {
92+
return _upb_map_next(map, iter);
8993
}
9094

9195
// Returns the key and value for this entry of the map.

upb/collections/map.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,37 +78,39 @@ UPB_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key,
7878
// Deletes this key from the table. Returns true if the key was present.
7979
bool upb_Map_Delete(upb_Map* map, upb_MessageValue key);
8080

81+
// Map iteration:
82+
//
83+
// size_t iter = kUpb_Map_Begin;
84+
// upb_MessageValue key, val;
85+
// while (upb_Map_Next(map, &key, &val, &iter)) {
86+
// ...
87+
// }
88+
89+
#define kUpb_Map_Begin ((size_t)-1)
90+
91+
// Advances to the next entry. Returns false if no more entries are present.
92+
// Otherwise returns true and populates both *key and *value.
93+
bool upb_Map_Next(const upb_Map* map, upb_MessageValue* key,
94+
upb_MessageValue* val, size_t* iter);
95+
96+
// DEPRECATED iterator, slated for removal.
97+
8198
/* Map iteration:
8299
*
83100
* size_t iter = kUpb_Map_Begin;
84101
* while (upb_MapIterator_Next(map, &iter)) {
85102
* upb_MessageValue key = upb_MapIterator_Key(map, iter);
86103
* upb_MessageValue val = upb_MapIterator_Value(map, iter);
87-
*
88-
* // If mutating is desired.
89-
* upb_MapIterator_SetValue(map, iter, value2);
90104
* }
91105
*/
92106

93-
#define kUpb_Map_Begin ((size_t)-1)
94-
95107
// Advances to the next entry. Returns false if no more entries are present.
96108
bool upb_MapIterator_Next(const upb_Map* map, size_t* iter);
97109

98-
/* Returns true if the iterator still points to a valid entry, or false if the
99-
* iterator is past the last element. It is an error to call this function with
100-
* kUpb_Map_Begin (you must call next() at least once first). */
101-
bool upb_MapIterator_Done(const upb_Map* map, size_t iter);
102-
103110
/* Returns the key and value for this entry of the map. */
104111
upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter);
105112
upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter);
106113

107-
/* Sets the value for this entry. The iterator must not be done, and the
108-
* iterator must not have been initialized const. */
109-
void upb_MapIterator_SetValue(upb_Map* map, size_t iter,
110-
upb_MessageValue value);
111-
112114
#ifdef __cplusplus
113115
} /* extern "C" */
114116
#endif

upb/json/encode.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -470,20 +470,20 @@ static void jsonenc_fieldmask(jsonenc* e, const upb_Message* msg,
470470

471471
static void jsonenc_struct(jsonenc* e, const upb_Message* msg,
472472
const upb_MessageDef* m) {
473+
jsonenc_putstr(e, "{");
474+
473475
const upb_FieldDef* fields_f = upb_MessageDef_FindFieldByNumber(m, 1);
474476
const upb_Map* fields = upb_Message_Get(msg, fields_f).map_val;
475-
const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(fields_f);
476-
const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(entry_m, 2);
477-
size_t iter = kUpb_Map_Begin;
478-
bool first = true;
479-
480-
jsonenc_putstr(e, "{");
481477

482478
if (fields) {
483-
while (upb_MapIterator_Next(fields, &iter)) {
484-
upb_MessageValue key = upb_MapIterator_Key(fields, iter);
485-
upb_MessageValue val = upb_MapIterator_Value(fields, iter);
479+
const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(fields_f);
480+
const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(entry_m, 2);
481+
482+
size_t iter = kUpb_Map_Begin;
483+
bool first = true;
486484

485+
upb_MessageValue key, val;
486+
while (upb_Map_Next(fields, &key, &val, &iter)) {
487487
jsonenc_putsep(e, ",", &first);
488488
jsonenc_string(e, key.str_val);
489489
jsonenc_putstr(e, ":");
@@ -677,19 +677,21 @@ static void jsonenc_array(jsonenc* e, const upb_Array* arr,
677677
}
678678

679679
static void jsonenc_map(jsonenc* e, const upb_Map* map, const upb_FieldDef* f) {
680+
jsonenc_putstr(e, "{");
681+
680682
const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f);
681683
const upb_FieldDef* key_f = upb_MessageDef_FindFieldByNumber(entry, 1);
682684
const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(entry, 2);
683-
size_t iter = kUpb_Map_Begin;
684-
bool first = true;
685-
686-
jsonenc_putstr(e, "{");
687685

688686
if (map) {
689-
while (upb_MapIterator_Next(map, &iter)) {
687+
size_t iter = kUpb_Map_Begin;
688+
bool first = true;
689+
690+
upb_MessageValue key, val;
691+
while (upb_Map_Next(map, &key, &val, &iter)) {
690692
jsonenc_putsep(e, ",", &first);
691-
jsonenc_mapkey(e, upb_MapIterator_Key(map, iter), key_f);
692-
jsonenc_scalar(e, upb_MapIterator_Value(map, iter), val_f);
693+
jsonenc_mapkey(e, key, key_f);
694+
jsonenc_scalar(e, val, val_f);
693695
}
694696
}
695697

upb/reflection/message.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ bool _upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m,
299299

300300
if (!val_m) continue;
301301

302-
while (upb_MapIterator_Next(map, &iter)) {
303-
upb_MessageValue map_val = upb_MapIterator_Value(map, iter);
302+
upb_MessageValue map_key, map_val;
303+
while (upb_Map_Next(map, &map_key, &map_val, &iter)) {
304304
if (!_upb_Message_DiscardUnknown((upb_Message*)map_val.msg_val, val_m,
305305
depth)) {
306306
ret = false;

upb/text/encode.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,8 @@ static void txtenc_mapentry(txtenc* e, upb_MessageValue key,
282282
static void txtenc_map(txtenc* e, const upb_Map* map, const upb_FieldDef* f) {
283283
if (e->options & UPB_TXTENC_NOSORT) {
284284
size_t iter = kUpb_Map_Begin;
285-
while (upb_MapIterator_Next(map, &iter)) {
286-
upb_MessageValue key = upb_MapIterator_Key(map, iter);
287-
upb_MessageValue val = upb_MapIterator_Value(map, iter);
285+
upb_MessageValue key, val;
286+
while (upb_Map_Next(map, &key, &val, &iter)) {
288287
txtenc_mapentry(e, key, val, f);
289288
}
290289
} else {

upb/util/required_fields.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,8 @@ static void upb_util_FindUnsetRequiredInternal(upb_FindContext* ctx,
265265
if (!val_m) continue;
266266
const upb_Map* map = val.map_val;
267267
size_t iter = kUpb_Map_Begin;
268-
while (upb_MapIterator_Next(map, &iter)) {
269-
upb_MessageValue key = upb_MapIterator_Key(map, iter);
270-
upb_MessageValue map_val = upb_MapIterator_Value(map, iter);
268+
upb_MessageValue key, map_val;
269+
while (upb_Map_Next(map, &key, &map_val, &iter)) {
271270
upb_FindContext_Push(ctx, (upb_FieldPathEntry){.map_key = key});
272271
upb_util_FindUnsetRequiredInternal(ctx, map_val.msg_val, val_m);
273272
upb_FindContext_Pop(ctx);

0 commit comments

Comments
 (0)