WPILibC++ 2027.0.0-alpha-3
Loading...
Searching...
No Matches
str_table.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#ifndef UPB_HASH_STR_TABLE_H_
9#define UPB_HASH_STR_TABLE_H_
10
11#include <stddef.h>
12#include <stdint.h>
13#include <string.h>
14
16#include "upb/hash/common.h"
17#include "upb/mem/arena.h"
18
19// Must be last.
20#include "upb/port/def.inc"
21
22typedef struct {
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30// Initialize a table. If memory allocation failed, false is returned and
31// the table is uninitialized.
32bool upb_strtable_init(upb_strtable* table, size_t expected_size, upb_Arena* a);
33
34// Returns the number of values in the table.
36 return t->t.count;
37}
38
40
41// Inserts the given key into the hashtable with the given value.
42// The key must not already exist in the hash table. The key is not required
43// to be NULL-terminated, and the table will make an internal copy of the key.
44//
45// If a table resize was required but memory allocation failed, false is
46// returned and the table is unchanged. */
47bool upb_strtable_insert(upb_strtable* t, const char* key, size_t len,
48 upb_value val, upb_Arena* a);
49
50// Looks up key in this table, returning "true" if the key was found.
51// If v is non-NULL, copies the value for this key into *v.
52bool upb_strtable_lookup2(const upb_strtable* t, const char* key, size_t len,
53 upb_value* v);
54
55// For NULL-terminated strings.
56UPB_INLINE bool upb_strtable_lookup(const upb_strtable* t, const char* key,
57 upb_value* v) {
58 return upb_strtable_lookup2(t, key, strlen(key), v);
59}
60
61// Removes an item from the table. Returns true if the remove was successful,
62// and stores the removed item in *val if non-NULL.
63bool upb_strtable_remove2(upb_strtable* t, const char* key, size_t len,
64 upb_value* val);
65
67 upb_value* v) {
68 return upb_strtable_remove2(t, key, strlen(key), v);
69}
70
71// Exposed for testing only.
72bool upb_strtable_resize(upb_strtable* t, size_t size_lg2, upb_Arena* a);
73
74/* Iteration over strtable:
75 *
76 * intptr_t iter = UPB_STRTABLE_BEGIN;
77 * upb_StringView key;
78 * upb_value val;
79 * while (upb_strtable_next2(t, &key, &val, &iter)) {
80 * // ...
81 * }
82 */
83
84#define UPB_STRTABLE_BEGIN -1
85
87 upb_value* val, intptr_t* iter);
88void upb_strtable_removeiter(upb_strtable* t, intptr_t* iter);
90
91/* DEPRECATED iterators, slated for removal.
92 *
93 * Iterators for string tables. We are subject to some kind of unusual
94 * design constraints:
95 *
96 * For high-level languages:
97 * - we must be able to guarantee that we don't crash or corrupt memory even if
98 * the program accesses an invalidated iterator.
99 *
100 * For C++11 range-based for:
101 * - iterators must be copyable
102 * - iterators must be comparable
103 * - it must be possible to construct an "end" value.
104 *
105 * Iteration order is undefined.
106 *
107 * Modifying the table invalidates iterators. upb_{str,int}table_done() is
108 * guaranteed to work even on an invalidated iterator, as long as the table it
109 * is iterating over has not been freed. Calling next() or accessing data from
110 * an invalidated iterator yields unspecified elements from the table, but it is
111 * guaranteed not to crash and to return real table elements (except when done()
112 * is true). */
113/* upb_strtable_iter **********************************************************/
114
115/* upb_strtable_iter i;
116 * upb_strtable_begin(&i, t);
117 * for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
118 * const char *key = upb_strtable_iter_key(&i);
119 * const upb_value val = upb_strtable_iter_value(&i);
120 * // ...
121 * }
122 */
123
124typedef struct {
126 size_t index;
128
130 return &i->t->t.entries[i->index];
131}
132
140 const upb_strtable_iter* i2);
141
142#ifdef __cplusplus
143} /* extern "C" */
144#endif
145
146#include "upb/port/undef.inc"
147
148#endif /* UPB_HASH_STR_TABLE_H_ */
#define UPB_INLINE
Definition def.inc:144
bool upb_strtable_iter_isequal(const upb_strtable_iter *i1, const upb_strtable_iter *i2)
bool upb_strtable_next2(const upb_strtable *t, upb_StringView *key, upb_value *val, intptr_t *iter)
UPB_INLINE bool upb_strtable_remove(upb_strtable *t, const char *key, upb_value *v)
Definition str_table.h:66
void upb_strtable_setentryvalue(upb_strtable *t, intptr_t iter, upb_value v)
UPB_INLINE bool upb_strtable_lookup(const upb_strtable *t, const char *key, upb_value *v)
Definition str_table.h:56
UPB_INLINE const upb_tabent * str_tabent(const upb_strtable_iter *i)
Definition str_table.h:129
UPB_INLINE size_t upb_strtable_count(const upb_strtable *t)
Definition str_table.h:35
void upb_strtable_iter_setdone(upb_strtable_iter *i)
bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len, upb_value *v)
upb_StringView upb_strtable_iter_key(const upb_strtable_iter *i)
void upb_strtable_clear(upb_strtable *t)
bool upb_strtable_remove2(upb_strtable *t, const char *key, size_t len, upb_value *val)
void upb_strtable_removeiter(upb_strtable *t, intptr_t *iter)
void upb_strtable_next(upb_strtable_iter *i)
upb_value upb_strtable_iter_value(const upb_strtable_iter *i)
bool upb_strtable_init(upb_strtable *table, size_t expected_size, upb_Arena *a)
bool upb_strtable_insert(upb_strtable *t, const char *key, size_t len, upb_value val, upb_Arena *a)
void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t)
bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_Arena *a)
bool upb_strtable_done(const upb_strtable_iter *i)
Definition common.h:125
Definition arena.h:29
Definition string_view.h:23
Definition str_table.h:124
size_t index
Definition str_table.h:126
const upb_strtable * t
Definition str_table.h:125
Definition str_table.h:22
upb_table t
Definition str_table.h:23
Definition common.h:136
upb_tabent * entries
Definition common.h:137
Definition common.h:44