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 "upb/hash/common.h" 12 13 // Must be last. 14 #include "upb/port/def.inc" 15 16 typedef struct { 17 upb_table t; // For entries that don't fit in the array part. 18 const upb_tabval* array; // Array part of the table. See const note above. 19 size_t array_size; // Array part size. 20 size_t array_count; // Array part number of elements. 21 } upb_inttable; 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 // Initialize a table. If memory allocation failed, false is returned and 28 // the table is uninitialized. 29 bool upb_inttable_init(upb_inttable* table, upb_Arena* a); 30 31 // Returns the number of values in the table. 32 size_t upb_inttable_count(const upb_inttable* t); 33 34 // Inserts the given key into the hashtable with the given value. 35 // The key must not already exist in the hash table. 36 // The value must not be UINTPTR_MAX. 37 // 38 // If a table resize was required but memory allocation failed, false is 39 // returned and the table is unchanged. 40 bool upb_inttable_insert(upb_inttable* t, uintptr_t key, upb_value val, 41 upb_Arena* a); 42 43 // Looks up key in this table, returning "true" if the key was found. 44 // If v is non-NULL, copies the value for this key into *v. 45 bool upb_inttable_lookup(const upb_inttable* t, uintptr_t key, upb_value* v); 46 47 // Removes an item from the table. Returns true if the remove was successful, 48 // and stores the removed item in *val if non-NULL. 49 bool upb_inttable_remove(upb_inttable* t, uintptr_t key, upb_value* val); 50 51 // Updates an existing entry in an inttable. 52 // If the entry does not exist, returns false and does nothing. 53 // Unlike insert/remove, this does not invalidate iterators. 54 bool upb_inttable_replace(upb_inttable* t, uintptr_t key, upb_value val); 55 56 // Optimizes the table for the current set of entries, for both memory use and 57 // lookup time. Client should call this after all entries have been inserted; 58 // inserting more entries is legal, but will likely require a table resize. 59 void upb_inttable_compact(upb_inttable* t, upb_Arena* a); 60 61 // Iteration over inttable: 62 // 63 // intptr_t iter = UPB_INTTABLE_BEGIN; 64 // uintptr_t key; 65 // upb_value val; 66 // while (upb_inttable_next(t, &key, &val, &iter)) { 67 // // ... 68 // } 69 70 #define UPB_INTTABLE_BEGIN -1 71 72 bool upb_inttable_next(const upb_inttable* t, uintptr_t* key, upb_value* val, 73 intptr_t* iter); 74 void upb_inttable_removeiter(upb_inttable* t, intptr_t* iter); 75 76 #ifdef __cplusplus 77 } /* extern "C" */ 78 #endif 79 80 #include "upb/port/undef.inc" 81 82 #endif /* UPB_HASH_INT_TABLE_H_ */ 83