rcx

library of miscellaneous bits of C code
git clone git://git.rr3.xyz/rcx
Log | Files | Refs | README | LICENSE

commit 5ff13c3ba6589900dc348dedddfbe6903a124296
parent f248b095549af9fa7ab32d3ef6b282f74ee56146
Author: Robert Russell <robertrussell.72001@gmail.com>
Date:   Mon, 25 Sep 2023 23:41:24 -0700

Update dict

See one of the previous commits.

Diffstat:
Dinc/dict/declare.h | 11-----------
Dinc/dict/define.h | 11-----------
Ainc/dict/impl/clean.h | 12++++++++++++
Dinc/dict/impl/declare.h | 90-------------------------------------------------------------------------------
Ainc/dict/impl/decls.h | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rinc/dict/impl/macro.h -> inc/dict/impl/macros.h | 0
Ainc/dict/impl/types.h | 4++++
Dinc/dict/impl/unmacro.h | 8--------
Ainc/dict/main.h | 17+++++++++++++++++
Ainc/dict/priv-hdr.h | 3+++
Ainc/dict/priv-src.h | 4++++
Ainc/dict/pub-hdr.h | 3+++
Ainc/dict/pub-src.h | 2++
Minc/dict/static.h | 16+++++-----------
Minc/table/main.h | 5+----
15 files changed, 136 insertions(+), 135 deletions(-)

diff --git a/inc/dict/declare.h b/inc/dict/declare.h @@ -1,11 +0,0 @@ -#include <stdbool.h> -#include <string.h> - -#include "../alloc.h" -#include "../def.h" -#include "../rand.h" - -#include "impl/macro.h" -#include "impl/table.h" -#include "impl/declare.h" -#include "impl/unmacro.h" diff --git a/inc/dict/define.h b/inc/dict/define.h @@ -1,11 +0,0 @@ -#include <stdbool.h> -#include <string.h> - -#include "../alloc.h" -#include "../def.h" -#include "../rand.h" - -#include "impl/macro.h" -#include "impl/table.h" -#include "impl/declare.h" -#include "impl/unmacro.h" diff --git a/inc/dict/impl/clean.h b/inc/dict/impl/clean.h @@ -0,0 +1,12 @@ +#undef DICT_METHOD +#undef DICT_FREE +#undef DICT_ALLOCZ +#undef DICT_HASH +#undef DICT_EQ +#undef DICT_V +#undef DICT_K +#undef DICT_NAME +#undef DICT_STATIC +#undef DICT_DEFS +#undef DICT_DECLS +#undef DICT_TYPES diff --git a/inc/dict/impl/declare.h b/inc/dict/impl/declare.h @@ -1,90 +0,0 @@ -typedef struct { - RTable table; - DICT_V default_val; -} DICT_NAME; - -#define DICT_SPEC &(RDictTableSpec){ \ - .ksize = sizeof(DICT_K), \ - .vsize = sizeof(DICT_V), \ - .eq = (RTableEqFunc)(DICT_EQ), \ - .hash = (RTableHashFunc)(DICT_HASH), \ - .allocz = (RTableAlloczFunc)(DICT_ALLOCZ), \ - .free = (RTableFreeFunc)(DICT_FREE), \ - } - -static inline UNUSED int -DICT_METHOD(init)(DICT_NAME *d, usize hint) { - memset(&d->default_val, 0, sizeof(DICT_V)); - return r_dict_table_init(&d->table, hint, DICT_SPEC); -} - -static inline UNUSED void -DICT_METHOD(free)(DICT_NAME *d) { - r_dict_table_free(&d->table, DICT_SPEC); -} - -static inline UNUSED usize -DICT_METHOD(len)(DICT_NAME *d) { - return r_dict_table_len(&d->table, DICT_SPEC); -} - -static inline UNUSED void -DICT_METHOD(set_default)(DICT_NAME *d, DICT_V v) { - d->default_val = v; -} - -static inline UNUSED DICT_V -DICT_METHOD(get_default)(DICT_NAME *d) { - return d->default_val; -} - -static inline UNUSED DICT_V * -DICT_METHOD(ptr)(DICT_NAME *d, DICT_K k) { - u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K)); - void *vp = 0; - r_dict_table_acc(&d->table, &vp, &k, h, DICT_SPEC); - return vp; -} - -static inline UNUSED bool -DICT_METHOD(get)(DICT_NAME *d, DICT_V *v, DICT_K k) { - u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K)); - void *vp; - int r = r_dict_table_acc(&d->table, &vp, &k, h, DICT_SPEC); - if (v) *v = r > 0 ? *(DICT_V *)vp : d->default_val; - return r > 0; -} - -static inline UNUSED bool -DICT_METHOD(set)(DICT_NAME *d, DICT_K k, DICT_V v) { - u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K)); - void *vp; - int r = r_dict_table_acc(&d->table, &vp, &k, h, DICT_SPEC); - if (r > 0) *(DICT_V *)vp = v; - return r > 0; -} - -static inline UNUSED int -DICT_METHOD(put)(DICT_NAME *d, DICT_K k, DICT_V v) { - u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K)); - void *vp; - int r = r_dict_table_ins(&d->table, &vp, &k, h, DICT_SPEC); - if (r >= 0) *(DICT_V *)vp = v; - return r; -} - -static inline UNUSED bool -DICT_METHOD(del)(DICT_NAME *d, DICT_V *v, DICT_K k) { - u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K)); - void *vp; - int r = r_dict_table_del(&d->table, &vp, &k, h, DICT_SPEC); - if (v) *v = r > 0 ? *(DICT_V *)vp : d->default_val; - return r > 0; -} - -static inline UNUSED void -DICT_METHOD(clr)(DICT_NAME *d) { - r_dict_table_clr(&d->table, DICT_SPEC); -} - -#undef DICT_SPEC diff --git a/inc/dict/impl/decls.h b/inc/dict/impl/decls.h @@ -0,0 +1,85 @@ +#define DICT_SPEC &(RDictTableSpec){ \ + .ksize = sizeof(DICT_K), \ + .vsize = sizeof(DICT_V), \ + .eq = (RTableEqFunc)(DICT_EQ), \ + .hash = (RTableHashFunc)(DICT_HASH), \ + .allocz = (RTableAlloczFunc)(DICT_ALLOCZ), \ + .free = (RTableFreeFunc)(DICT_FREE), \ + } + +static inline UNUSED int +DICT_METHOD(init)(DICT_NAME *d, usize hint) { + memset(&d->default_val, 0, sizeof(DICT_V)); + return r_dict_table_init(&d->table, hint, DICT_SPEC); +} + +static inline UNUSED void +DICT_METHOD(free)(DICT_NAME *d) { + r_dict_table_free(&d->table, DICT_SPEC); +} + +static inline UNUSED usize +DICT_METHOD(len)(DICT_NAME *d) { + return r_dict_table_len(&d->table, DICT_SPEC); +} + +static inline UNUSED void +DICT_METHOD(set_default)(DICT_NAME *d, DICT_V v) { + d->default_val = v; +} + +static inline UNUSED DICT_V +DICT_METHOD(get_default)(DICT_NAME *d) { + return d->default_val; +} + +static inline UNUSED DICT_V * +DICT_METHOD(ptr)(DICT_NAME *d, DICT_K k) { + u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K)); + void *vp = 0; + r_dict_table_acc(&d->table, &vp, &k, h, DICT_SPEC); + return vp; +} + +static inline UNUSED bool +DICT_METHOD(get)(DICT_NAME *d, DICT_V *v, DICT_K k) { + u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K)); + void *vp; + int r = r_dict_table_acc(&d->table, &vp, &k, h, DICT_SPEC); + if (v) *v = r > 0 ? *(DICT_V *)vp : d->default_val; + return r > 0; +} + +static inline UNUSED bool +DICT_METHOD(set)(DICT_NAME *d, DICT_K k, DICT_V v) { + u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K)); + void *vp; + int r = r_dict_table_acc(&d->table, &vp, &k, h, DICT_SPEC); + if (r > 0) *(DICT_V *)vp = v; + return r > 0; +} + +static inline UNUSED int +DICT_METHOD(put)(DICT_NAME *d, DICT_K k, DICT_V v) { + u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K)); + void *vp; + int r = r_dict_table_ins(&d->table, &vp, &k, h, DICT_SPEC); + if (r >= 0) *(DICT_V *)vp = v; + return r; +} + +static inline UNUSED bool +DICT_METHOD(del)(DICT_NAME *d, DICT_V *v, DICT_K k) { + u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K)); + void *vp; + int r = r_dict_table_del(&d->table, &vp, &k, h, DICT_SPEC); + if (v) *v = r > 0 ? *(DICT_V *)vp : d->default_val; + return r > 0; +} + +static inline UNUSED void +DICT_METHOD(clr)(DICT_NAME *d) { + r_dict_table_clr(&d->table, DICT_SPEC); +} + +#undef DICT_SPEC diff --git a/inc/dict/impl/macro.h b/inc/dict/impl/macros.h diff --git a/inc/dict/impl/types.h b/inc/dict/impl/types.h @@ -0,0 +1,4 @@ +typedef struct { + RTable table; + DICT_V default_val; +} DICT_NAME; diff --git a/inc/dict/impl/unmacro.h b/inc/dict/impl/unmacro.h @@ -1,8 +0,0 @@ -#undef DICT_METHOD -#undef DICT_FREE -#undef DICT_ALLOCZ -#undef DICT_HASH -#undef DICT_EQ -#undef DICT_V -#undef DICT_K -#undef DICT_NAME diff --git a/inc/dict/main.h b/inc/dict/main.h @@ -0,0 +1,17 @@ +#if defined(DICT_DECLS) || defined(DICT_DEFS) +#include <stdbool.h> +#include <string.h> +#include "../alloc.h" +#include "../def.h" +#include "../rand.h" +#endif + +#include "impl/macros.h" +#include "impl/table.h" +#ifdef DICT_TYPES +#include "impl/types.h" +#endif +#if defined(DICT_DECLS) || defined(DICT_DEFS) +#include "impl/decls.h" +#endif +#include "impl/clean.h" diff --git a/inc/dict/priv-hdr.h b/inc/dict/priv-hdr.h @@ -0,0 +1,3 @@ +#define DICT_TYPES +#define DICT_STATIC +#include "main.h" diff --git a/inc/dict/priv-src.h b/inc/dict/priv-src.h @@ -0,0 +1,4 @@ +#define DICT_DECLS +#define DICT_DEFS +#define DICT_STATIC +#include "main.h" diff --git a/inc/dict/pub-hdr.h b/inc/dict/pub-hdr.h @@ -0,0 +1,3 @@ +#define DICT_TYPES +#define DICT_DECLS +#include "main.h" diff --git a/inc/dict/pub-src.h b/inc/dict/pub-src.h @@ -0,0 +1,2 @@ +#define DICT_DEFS +#include "main.h" diff --git a/inc/dict/static.h b/inc/dict/static.h @@ -1,11 +1,5 @@ -#include <stdbool.h> -#include <string.h> - -#include "../alloc.h" -#include "../def.h" -#include "../rand.h" - -#include "impl/macro.h" -#include "impl/table.h" -#include "impl/declare.h" -#include "impl/unmacro.h" +#define DICT_TYPES +#define DICT_DECLS +#define DICT_DEFS +#define DICT_STATIC +#include "main.h" diff --git a/inc/table/main.h b/inc/table/main.h @@ -1,13 +1,10 @@ #include <stdbool.h> #include <string.h> -#if defined(TABLE_DECLS) || defined(TABLE_DEFS) -#include <errno.h> -#endif - #include "../def.h" #include "../rand.h" #include "../string.h" #if defined(TABLE_DECLS) || defined(TABLE_DEFS) +#include <errno.h> #include "../alloc.h" #endif #ifdef TABLE_DEFS