|
|
|
@ -102,14 +102,12 @@ static int (*const compar[kUpb_FieldType_SizeOf])(const void*, const void*) = { |
|
|
|
|
[kUpb_FieldType_Bytes] = _upb_mapsorter_cmpstr, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type, |
|
|
|
|
const upb_Map* map, _upb_sortedmap* sorted) { |
|
|
|
|
int map_size = _upb_Map_Size(map); |
|
|
|
|
static bool _upb_mapsorter_resize(_upb_mapsorter* s, _upb_sortedmap* sorted, |
|
|
|
|
int size) { |
|
|
|
|
sorted->start = s->size; |
|
|
|
|
sorted->pos = sorted->start; |
|
|
|
|
sorted->end = sorted->start + map_size; |
|
|
|
|
sorted->end = sorted->start + size; |
|
|
|
|
|
|
|
|
|
// Grow s->entries if necessary.
|
|
|
|
|
if (sorted->end > s->cap) { |
|
|
|
|
s->cap = upb_Log2CeilingSize(sorted->end); |
|
|
|
|
s->entries = realloc(s->entries, s->cap * sizeof(*s->entries)); |
|
|
|
@ -117,9 +115,17 @@ bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
s->size = sorted->end; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type, |
|
|
|
|
const upb_Map* map, _upb_sortedmap* sorted) { |
|
|
|
|
int map_size = _upb_Map_Size(map); |
|
|
|
|
|
|
|
|
|
if (!_upb_mapsorter_resize(s, sorted, map_size)) return false; |
|
|
|
|
|
|
|
|
|
// Copy non-empty entries from the table to s->entries.
|
|
|
|
|
upb_tabent const** dst = &s->entries[sorted->start]; |
|
|
|
|
const void** dst = &s->entries[sorted->start]; |
|
|
|
|
const upb_tabent* src = map->table.t.entries; |
|
|
|
|
const upb_tabent* end = src + upb_table_size(&map->table.t); |
|
|
|
|
for (; src < end; src++) { |
|
|
|
@ -135,3 +141,26 @@ bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type, |
|
|
|
|
compar[key_type]); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int _upb_mapsorter_cmpext(const void* _a, const void* _b) { |
|
|
|
|
const upb_Message_Extension* const* a = _a; |
|
|
|
|
const upb_Message_Extension* const* b = _b; |
|
|
|
|
uint32_t a_num = (*a)->ext->field.number; |
|
|
|
|
uint32_t b_num = (*b)->ext->field.number; |
|
|
|
|
assert(a_num != b_num); |
|
|
|
|
return a_num < b_num ? -1 : 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool _upb_mapsorter_pushexts(_upb_mapsorter* s, |
|
|
|
|
const upb_Message_Extension* exts, size_t count, |
|
|
|
|
_upb_sortedmap* sorted) { |
|
|
|
|
if (!_upb_mapsorter_resize(s, sorted, count)) return false; |
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++) { |
|
|
|
|
s->entries[sorted->start + i] = &exts[i]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
qsort(&s->entries[sorted->start], count, sizeof(*s->entries), |
|
|
|
|
_upb_mapsorter_cmpext); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|