blob: 521f048f1866fceedc0077636aaf6e59d3c1e2d7 [file]
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_HASH_INT_TABLE_H_
#define UPB_HASH_INT_TABLE_H_
#include <stddef.h>
#include <stdint.h>
#include "upb/hash/common.h"
#include "upb/mem/arena.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct {
upb_table t;
} upb_inttable;
#ifdef __cplusplus
extern "C" {
#endif
// Initialize a table. If memory allocation failed, false is returned and
// the table is uninitialized.
UPB_NODISCARD bool upb_inttable_init(upb_inttable* table, upb_Arena* a);
// Returns the number of values in the table.
size_t upb_inttable_count(const upb_inttable* t);
// Inserts the given key into the hashtable with the given value.
// The key must not already exist in the hash table.
//
// If a table resize was required but memory allocation failed, false is
// returned and the table is unchanged.
UPB_NODISCARD bool upb_inttable_insert(upb_inttable* t, uintptr_t key,
upb_value val, upb_Arena* a);
// Looks up key in this table, returning "true" if the key was found.
// If v is non-NULL, copies the value for this key into *v.
bool upb_inttable_lookup(const upb_inttable* t, uintptr_t key, upb_value* v);
// Removes an item from the table. Returns true if the remove was successful,
// and stores the removed item in *val if non-NULL.
bool upb_inttable_remove(upb_inttable* t, uintptr_t key, upb_value* val);
// Updates an existing entry in an inttable.
// If the entry does not exist, returns false and does nothing.
// Unlike insert/remove, this does not invalidate iterators.
bool upb_inttable_replace(upb_inttable* t, uintptr_t key, upb_value val);
// Clears the table.
void upb_inttable_clear(upb_inttable* t);
// Iteration over inttable:
//
// intptr_t iter = UPB_INTTABLE_BEGIN;
// uintptr_t key;
// upb_value val;
// while (upb_inttable_next(t, &key, &val, &iter)) {
// // ...
// }
#define UPB_INTTABLE_BEGIN -1
bool upb_inttable_next(const upb_inttable* t, uintptr_t* key, upb_value* val,
intptr_t* iter);
void upb_inttable_removeiter(upb_inttable* t, intptr_t* iter);
void upb_inttable_setentryvalue(upb_inttable* t, intptr_t iter, upb_value v);
bool upb_inttable_done(const upb_inttable* t, intptr_t i);
uintptr_t upb_inttable_iter_key(const upb_inttable* t, intptr_t iter);
upb_value upb_inttable_iter_value(const upb_inttable* t, intptr_t iter);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_HASH_INT_TABLE_H_ */