WPILibC++ 2027.0.0-alpha-3
Loading...
Searching...
No Matches
int_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_INT_TABLE_H_
9#define UPB_HASH_INT_TABLE_H_
10
11#include <stddef.h>
12#include <stdint.h>
13
14#include "upb/hash/common.h"
15#include "upb/mem/arena.h"
16
17// Must be last.
18#include "upb/port/def.inc"
19
20typedef struct {
21 upb_table t; // For entries that don't fit in the array part.
22 // Array part of the table.
23 // Pointers on this table are const so we can create static initializers for
24 // tables. We cast away const sometimes, but *only* when the containing
25 // upb_table is known to be non-const. This requires a bit of care, but
26 // the subtlety is confined to table.c.
28 // Track presence in the array part. Each bit at index (key % 8) at the
29 // presence_mask[key/8] indicates if the element is present in the array part.
30 const uint8_t* presence_mask;
31 uint32_t array_size; // Array part size.
32 uint32_t array_count; // Array part number of elements.
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39// Initialize a table. If memory allocation failed, false is returned and
40// the table is uninitialized.
42
43// Returns the number of values in the table.
45
46// Inserts the given key into the hashtable with the given value.
47// The key must not already exist in the hash table.
48// The value must not be UINTPTR_MAX.
49//
50// If a table resize was required but memory allocation failed, false is
51// returned and the table is unchanged.
52bool upb_inttable_insert(upb_inttable* t, uintptr_t key, upb_value val,
53 upb_Arena* a);
54
55// Looks up key in this table, returning "true" if the key was found.
56// If v is non-NULL, copies the value for this key into *v.
57bool upb_inttable_lookup(const upb_inttable* t, uintptr_t key, upb_value* v);
58
59// Removes an item from the table. Returns true if the remove was successful,
60// and stores the removed item in *val if non-NULL.
61bool upb_inttable_remove(upb_inttable* t, uintptr_t key, upb_value* val);
62
63// Updates an existing entry in an inttable.
64// If the entry does not exist, returns false and does nothing.
65// Unlike insert/remove, this does not invalidate iterators.
66bool upb_inttable_replace(upb_inttable* t, uintptr_t key, upb_value val);
67
68// Optimizes the table for the current set of entries, for both memory use and
69// lookup time. Client should call this after all entries have been inserted;
70// inserting more entries is legal, but will likely require a table resize.
71// Returns false if reallocation fails.
73
74// Clears the table.
76
77// Iteration over inttable:
78//
79// intptr_t iter = UPB_INTTABLE_BEGIN;
80// uintptr_t key;
81// upb_value val;
82// while (upb_inttable_next(t, &key, &val, &iter)) {
83// // ...
84// }
85
86#define UPB_INTTABLE_BEGIN -1
87
88bool upb_inttable_next(const upb_inttable* t, uintptr_t* key, upb_value* val,
89 intptr_t* iter);
90void upb_inttable_removeiter(upb_inttable* t, intptr_t* iter);
92bool upb_inttable_done(const upb_inttable* t, intptr_t i);
93uintptr_t upb_inttable_iter_key(const upb_inttable* t, intptr_t iter);
95
96UPB_INLINE bool upb_inttable_arrhas(const upb_inttable* t, uintptr_t key) {
97 return (t->presence_mask[key / 8] & (1 << (key % 8))) != 0;
98}
99
100#ifdef __cplusplus
101} /* extern "C" */
102#endif
103
104#include "upb/port/undef.inc"
105
106#endif /* UPB_HASH_INT_TABLE_H_ */
#define UPB_INLINE
Definition def.inc:144
void upb_inttable_clear(upb_inttable *t)
void upb_inttable_removeiter(upb_inttable *t, intptr_t *iter)
bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val)
void upb_inttable_setentryvalue(upb_inttable *t, intptr_t iter, upb_value v)
bool upb_inttable_done(const upb_inttable *t, intptr_t i)
bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v)
bool upb_inttable_init(upb_inttable *table, upb_Arena *a)
bool upb_inttable_insert(upb_inttable *t, uintptr_t key, upb_value val, upb_Arena *a)
upb_value upb_inttable_iter_value(const upb_inttable *t, intptr_t iter)
bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val)
uintptr_t upb_inttable_iter_key(const upb_inttable *t, intptr_t iter)
bool upb_inttable_next(const upb_inttable *t, uintptr_t *key, upb_value *val, intptr_t *iter)
bool upb_inttable_compact(upb_inttable *t, upb_Arena *a)
size_t upb_inttable_count(const upb_inttable *t)
UPB_INLINE bool upb_inttable_arrhas(const upb_inttable *t, uintptr_t key)
Definition int_table.h:96
Definition arena.h:29
Definition int_table.h:20
const upb_value * array
Definition int_table.h:27
uint32_t array_count
Definition int_table.h:32
uint32_t array_size
Definition int_table.h:31
const uint8_t * presence_mask
Definition int_table.h:30
upb_table t
Definition int_table.h:21
Definition common.h:136
Definition common.h:44