WPILibC++ 2027.0.0-alpha-3
Loading...
Searching...
No Matches
map_sorter.h
Go to the documentation of this file.
1// Protocol Buffers - Google's data interchange format
2// Copyright 2023 Google LLC. All rights reserved.
3//
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file or at
6// https://developers.google.com/open-source/licenses/bsd
7
8// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
9
10#ifndef UPB_MESSAGE_INTERNAL_MAP_SORTER_H_
11#define UPB_MESSAGE_INTERNAL_MAP_SORTER_H_
12
13#include <stdint.h>
14#include <stdlib.h>
15#include <string.h>
16
19#include "upb/hash/common.h"
20#include "upb/mem/alloc.h"
25
26// Must be last.
27#include "upb/port/def.inc"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33// _upb_mapsorter sorts maps and provides ordered iteration over the entries.
34// Since maps can be recursive (map values can be messages which contain other
35// maps), _upb_mapsorter can contain a stack of maps.
36
37typedef struct {
38 void const** entries;
39 int size;
40 int cap;
42
43typedef struct {
44 int start;
45 int pos;
46 int end;
48
50 s->entries = NULL;
51 s->size = 0;
52 s->cap = 0;
53}
54
58
60 const struct upb_Map* map,
61 _upb_sortedmap* sorted, upb_MapEntry* ent) {
62 if (sorted->pos == sorted->end) return false;
63 const upb_tabent* tabent = (const upb_tabent*)s->entries[sorted->pos++];
64 if (map->UPB_PRIVATE(is_strtable)) {
65 upb_StringView key = upb_key_strview(tabent->key);
66 _upb_map_fromkey(key, &ent->k, map->key_size);
67 } else {
68 uintptr_t key = tabent->key.num;
69 memcpy(&ent->k, &key, map->key_size);
70 }
71 upb_value val = {tabent->val.val};
72 _upb_map_fromvalue(val, &ent->v, map->val_size);
73 return true;
74}
75
77 _upb_sortedmap* sorted,
78 const upb_Extension** ext) {
79 if (sorted->pos == sorted->end) return false;
80 *ext = (const upb_Extension*)s->entries[sorted->pos++];
81 return true;
82}
83
85 _upb_sortedmap* sorted) {
86 s->size = sorted->start;
87}
88
90 const struct upb_Map* map, _upb_sortedmap* sorted);
91
93 _upb_sortedmap* sorted);
94
95#ifdef __cplusplus
96} /* extern "C" */
97#endif
98
99#include "upb/port/undef.inc"
100
101#endif /* UPB_MESSAGE_INTERNAL_MAP_SORTER_H_ */
UPB_INLINE void upb_gfree(void *ptr)
Definition alloc.h:94
#define UPB_INLINE
Definition def.inc:144
upb_FieldType
Definition descriptor_constants.h:40
UPB_INLINE upb_StringView upb_key_strview(upb_key key)
Definition common.h:119
UPB_INLINE void _upb_map_fromvalue(upb_value val, void *out, size_t size)
Definition map.h:104
UPB_INLINE void _upb_map_fromkey(upb_StringView key, void *out, size_t size)
Definition map.h:83
UPB_INLINE void _upb_mapsorter_init(_upb_mapsorter *s)
Definition map_sorter.h:49
bool _upb_mapsorter_pushmap(_upb_mapsorter *s, upb_FieldType key_type, const struct upb_Map *map, _upb_sortedmap *sorted)
bool _upb_mapsorter_pushexts(_upb_mapsorter *s, const upb_Message_Internal *in, _upb_sortedmap *sorted)
UPB_INLINE bool _upb_sortedmap_nextext(_upb_mapsorter *s, _upb_sortedmap *sorted, const upb_Extension **ext)
Definition map_sorter.h:76
UPB_INLINE void _upb_mapsorter_popmap(_upb_mapsorter *s, _upb_sortedmap *sorted)
Definition map_sorter.h:84
UPB_INLINE void _upb_mapsorter_destroy(_upb_mapsorter *s)
Definition map_sorter.h:55
UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter *s, const struct upb_Map *map, _upb_sortedmap *sorted, upb_MapEntry *ent)
Definition map_sorter.h:59
Definition map_sorter.h:37
int size
Definition map_sorter.h:39
void const ** entries
Definition map_sorter.h:38
int cap
Definition map_sorter.h:40
Definition map_sorter.h:43
int end
Definition map_sorter.h:46
int pos
Definition map_sorter.h:45
int start
Definition map_sorter.h:44
Definition common.h:125
upb_key key
Definition common.h:127
upb_value val
Definition common.h:126
Definition extension.h:32
Definition map_entry.h:24
union upb_MapEntry::@70 k
union upb_MapEntry::@71 v
Definition map.h:38
Definition message.h:110
Definition string_view.h:23
Definition common.h:44
uint64_t val
Definition common.h:45
uintptr_t num
Definition common.h:115