rcx

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

commit f24c14b23fd8acb7346e51470c82dab2ace6d447
parent f90b972f752d868d43167ee3d9cc0b56952d7d31
Author: Robert Russell <robertrussell.72001@gmail.com>
Date:   Sun, 24 Sep 2023 23:33:12 -0700

Add generic set implementation

Basically the same as dict, but VSIZE is specialized to 0.

Diffstat:
MMakefile | 2++
Ainc/set/declare.h | 10++++++++++
Ainc/set/define.h | 10++++++++++
Ainc/set/impl/declare.h | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ainc/set/impl/macro.h | 27+++++++++++++++++++++++++++
Ainc/set/impl/table.h | 5+++++
Ainc/set/impl/unmacro.h | 7+++++++
Ainc/set/static.h | 10++++++++++
Asrc/set/impl/table.c | 7+++++++
9 files changed, 132 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -9,6 +9,7 @@ SRC =\ src/dict/impl/table.c\ src/log.c\ src/rand.c\ + src/set/impl/table.c\ src/str.c\ src/string.c\ src/unicode.c\ @@ -48,6 +49,7 @@ src/dict/impl/table.o: src/dict/impl/table.c inc/dict/impl/table.h $(TABLE_DEPS) src/log.o: src/log.c inc/def.h inc/log.h inc/rcx.h config.mk src/opt.o: src/opt.c inc/def.h inc/opt.h inc/rcx.h config.mk src/rand.o: src/rand.c inc/bits.h inc/def.h inc/rand.h inc/rcx.h inc/unix.h config.mk +src/set/impl/table.o: src/set/impl/table.c inc/set/impl/table.h $(TABLE_DEPS) config.mk src/str.o: src/str.c inc/alloc.h inc/debug.h inc/def.h inc/log.h inc/rcx.h inc/str.h config.mk src/string.o: src/string.c inc/alloc.h inc/debug.h inc/def.h inc/rcx.h inc/string.h inc/utf8.h config.mk src/unicode.o: src/unicode.c inc/def.h inc/rcx.h gen/ucattab.inc config.mk diff --git a/inc/set/declare.h b/inc/set/declare.h @@ -0,0 +1,10 @@ +#include <stdbool.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/set/define.h b/inc/set/define.h @@ -0,0 +1,10 @@ +#include <stdbool.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/set/impl/declare.h b/inc/set/impl/declare.h @@ -0,0 +1,54 @@ +typedef struct { + RTable table; +} SET_NAME; + +#define SET_SPEC &(RSetTableSpec){ \ + .ksize = sizeof(SET_T), \ + .eq = (RTableEqFunc)(SET_EQ), \ + .hash = (RTableHashFunc)(SET_HASH), \ + .allocz = (RTableAlloczFunc)(SET_ALLOCZ), \ + .free = (RTableFreeFunc)(SET_FREE), \ + } + +static inline UNUSED int +SET_METHOD(init)(SET_NAME *s, usize hint) { + return r_set_table_init(&s->table, hint, SET_SPEC); +} + +static inline UNUSED void +SET_METHOD(free)(SET_NAME *s) { + r_set_table_free(&s->table, SET_SPEC); +} + +static inline UNUSED usize +SET_METHOD(len)(SET_NAME *s) { + return r_set_table_len(&s->table, SET_SPEC); +} + +static inline UNUSED bool +SET_METHOD(has)(SET_NAME *s, SET_T k) { + u64 h = SET_HASH(&k, s->table.seed, sizeof(SET_T)); + void *vp; + return r_set_table_acc(&s->table, &vp, &k, h, SET_SPEC) > 0; +} + +static inline UNUSED int +SET_METHOD(put)(SET_NAME *s, SET_T k) { + u64 h = SET_HASH(&k, s->table.seed, sizeof(SET_T)); + void *vp; + return r_set_table_ins(&s->table, &vp, &k, h, SET_SPEC); +} + +static inline UNUSED bool +SET_METHOD(del)(SET_NAME *s, SET_T k) { + u64 h = SET_HASH(&k, s->table.seed, sizeof(SET_T)); + void *vp; + return r_set_table_del(&s->table, &vp, &k, h, SET_SPEC) > 0; +} + +static inline UNUSED void +SET_METHOD(clr)(SET_NAME *s) { + r_set_table_clr(&s->table, SET_SPEC); +} + +#undef SET_SPEC diff --git a/inc/set/impl/macro.h b/inc/set/impl/macro.h @@ -0,0 +1,27 @@ +#ifndef SET_NAME +#error "rcx/set: SET_NAME must be defined" +#endif + +#ifndef SET_T +#error "rcx/set: SET_T must be defined" +#endif + +#ifndef SET_EQ +#define SET_EQ r_table_mem_eq +#endif + +#ifndef SET_HASH +#define SET_HASH r_table_mem_hash +#endif + +#ifndef SET_ALLOCZ +#define SET_ALLOCZ r_eallocz +#endif + +#ifndef SET_FREE +#define SET_FREE free +#endif + +#ifndef SET_METHOD +#define SET_METHOD(name) name +#endif diff --git a/inc/set/impl/table.h b/inc/set/impl/table.h @@ -0,0 +1,4 @@ +#define TABLE_VSIZE 0 +#define TABLE_SPEC_NAME RSetTableSpec +#define TABLE_METHOD(name) r_set_table_##name +#include "../../table/declare.h" +\ No newline at end of file diff --git a/inc/set/impl/unmacro.h b/inc/set/impl/unmacro.h @@ -0,0 +1,7 @@ +#undef SET_METHOD +#undef SET_FREE +#undef SET_ALLOCZ +#undef SET_HASH +#undef SET_EQ +#undef SET_T +#undef SET_NAME diff --git a/inc/set/static.h b/inc/set/static.h @@ -0,0 +1,10 @@ +#include <stdbool.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/src/set/impl/table.c b/src/set/impl/table.c @@ -0,0 +1,6 @@ +#include "set/impl/table.h" + +#define TABLE_VSIZE 0 +#define TABLE_SPEC_NAME RSetTableSpec +#define TABLE_METHOD(name) r_set_table_##name +#include "table/define.h" +\ No newline at end of file