rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rhashtable.h
Go to the documentation of this file.
1/* RLIB - Convenience library for useful things
2 * Copyright (C) 2016-2017 Haakon Sporsheim <haakon.sporsheim@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3.0 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library.
16 * See the COPYING file at the root of the source repository.
17 */
18#ifndef __R_HASH_TABLE_H__
19#define __R_HASH_TABLE_H__
20
21#if !defined(__RLIB_H_INCLUDE_GUARD__) && !defined(RLIB_COMPILATION)
22#error "#include <rlib.h> only please."
23#endif
24
52#include <rlib/rtypes.h>
53#include <rlib/rref.h>
54
56
58
60typedef struct RHashTable RHashTable;
61
77#define R_HASH_TABLE_IS_SUCCESS(err) (err <= R_HASH_TABLE_OK)
79#define R_HASH_TABLE_IS_ERROR(err) (err > R_HASH_TABLE_OK)
80
85#define r_hash_table_new(hash, equal) r_hash_table_new_full (hash, equal, NULL, NULL)
96 RDestroyNotify keynotify, RDestroyNotify valuenotify) R_ATTR_MALLOC;
98#define r_hash_table_ref r_ref_ref
100#define r_hash_table_unref r_ref_unref
101
109
118 rpointer key, rpointer value);
119
129 rpointer * keyout, rpointer * valueout);
132
148 rpointer * keyout, rpointer * valueout);
155 rpointer * keyout, rpointer * valueout);
156
166 rpointer user);
174 RKeyValueFuncReturn func, rpointer user);
179#define r_hash_table_remove_all_values(ht, val) \
180 r_hash_table_remove_with_func (ht, r_hash_table_remove_func_value, val)
181
189
190R_END_DECLS
191
194#endif /* __R_HASH_TABLE_H__ */
195
RHashTableError r_hash_table_remove_with_func(RHashTable *ht, RKeyValueFuncReturn func, rpointer user)
Remove all entries for which func returns TRUE.
RHashTable * r_hash_table_new_full(RHashFunc hash, REqualFunc equal, RDestroyNotify keynotify, RDestroyNotify valuenotify)
Construct a hash table with custom hash / equality and optional per-side destroy notifiers.
RHashTableError r_hash_table_remove(RHashTable *ht, rconstpointer key)
Remove the entry for key; destroy notifiers run.
RHashTableError r_hash_table_steal(RHashTable *ht, rconstpointer key, rpointer *keyout, rpointer *valueout)
Remove the entry for key without running its destroy notifiers, handing ownership of the key / value ...
void r_hash_table_remove_all(RHashTable *ht)
Remove every entry, invoking destroy notifiers.
RHashTableError r_hash_table_foreach(RHashTable *ht, RKeyValueFunc func, rpointer user)
Iterate every (key, value) pair in the table.
RHashTableError r_hash_table_remove_full(RHashTable *ht, rconstpointer key, rpointer *keyout, rpointer *valueout)
Remove the entry for key, running its destroy notifiers (as r_hash_table_remove), and also report the...
rsize r_hash_table_current_alloc_size(RHashTable *ht)
Allocated bucket count; useful for sizing diagnostics.
RHashTableError r_hash_table_insert(RHashTable *ht, rpointer key, rpointer value)
Insert or replace (key, value).
RHashTableError r_hash_table_lookup_full(RHashTable *ht, rconstpointer key, rpointer *keyout, rpointer *valueout)
Look up key, returning both the stored key and value.
struct RHashTable RHashTable
Opaque refcounted hash table.
Definition rhashtable.h:60
RHashTableError
Result code for hash-table operations.
Definition rhashtable.h:69
rsize r_hash_table_size(RHashTable *ht)
Number of entries currently in the table.
rpointer r_hash_table_lookup(RHashTable *ht, rconstpointer key)
Return the value associated with key, or NULL.
rsize r_hash_table_max_probe(RHashTable *ht)
Worst-case unsuccessful-probe length; diagnostic for home distribution (small means keys are well spr...
rboolean r_hash_table_remove_func_value(rpointer key, rpointer value, rpointer user)
Filter callback that matches by value identity.
RHashTableError r_hash_table_contains(RHashTable *ht, rconstpointer key)
R_HASH_TABLE_OK iff key is present, R_HASH_TABLE_NOT_FOUND otherwise.
@ R_HASH_TABLE_NOT_FOUND
Definition rhashtable.h:73
@ R_HASH_TABLE_REPLACE
Definition rhashtable.h:70
@ R_HASH_TABLE_OK
Definition rhashtable.h:71
@ R_HASH_TABLE_INVAL
Definition rhashtable.h:72
@ R_HASH_TABLE_ERROR
Definition rhashtable.h:74
#define R_API
Public-API decoration: resolves to R_API_EXPORT while building rlib and R_API_IMPORT for consumers.
Definition rmacros.h:115
#define R_BEGIN_DECLS
Open an extern "C" block under C++ (no-op in C).
Definition rmacros.h:196
rboolean(* REqualFunc)(rconstpointer a, rconstpointer b)
Equality predicate.
Definition rtypes.h:407
rboolean(* RKeyValueFuncReturn)(rpointer key, rpointer value, rpointer user)
Key-value iteration callback that can short-circuit.
Definition rtypes.h:421
int rboolean
Boolean type (typedef'd to int).
Definition rtypes.h:59
void(* RDestroyNotify)(rpointer ptr)
Destructor callback: free / release the value at ptr.
Definition rtypes.h:401
void(* RKeyValueFunc)(rpointer key, rpointer value, rpointer user)
Iteration callback over (key, value, user) triples.
Definition rtypes.h:411
void * rpointer
Generic pointer (alias for void *).
Definition rtypes.h:327
const void * rconstpointer
Generic const pointer (alias for const void *).
Definition rtypes.h:329
unsigned long rsize
Unsigned pointer-sized size type (like size_t).
Definition rtypes.h:290
rsize(* RHashFunc)(rconstpointer key)
Hash function over an opaque key.
Definition rtypes.h:409
Pre-built hash / equality function pairs for the common key types (pointer-identity,...
Refcount base struct shared by every refcounted type in rlib.
Foundational type aliases used by every rlib header.