rcx

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

decls.h (2245B)


      1 #define DICT_SPEC &(RDictTableSpec){ \
      2 		.ksize = sizeof(DICT_K), \
      3 		.vsize = sizeof(DICT_V), \
      4 		.eq = (RTableEqFunc)(DICT_EQ), \
      5 		.hash = (RTableHashFunc)(DICT_HASH), \
      6 		.allocz = (RTableAlloczFunc)(DICT_ALLOCZ), \
      7 		.free = (RTableFreeFunc)(DICT_FREE), \
      8 	}
      9 
     10 static inline UNUSED int
     11 DICT_METHOD(init)(DICT_NAME *d, usize hint) {
     12 	memset(&d->default_val, 0, sizeof(DICT_V));
     13 	return r_dict_table_init(&d->table, hint, DICT_SPEC);
     14 }
     15 
     16 static inline UNUSED void
     17 DICT_METHOD(free)(DICT_NAME *d) {
     18 	r_dict_table_free(&d->table, DICT_SPEC);
     19 }
     20 
     21 static inline UNUSED usize
     22 DICT_METHOD(len)(DICT_NAME *d) {
     23 	return r_dict_table_len(&d->table, DICT_SPEC);
     24 }
     25 
     26 static inline UNUSED void
     27 DICT_METHOD(set_default)(DICT_NAME *d, DICT_V v) {
     28 	d->default_val = v;
     29 }
     30 
     31 static inline UNUSED DICT_V
     32 DICT_METHOD(get_default)(DICT_NAME *d) {
     33 	return d->default_val;
     34 }
     35 
     36 static inline UNUSED DICT_V *
     37 DICT_METHOD(ptr)(DICT_NAME *d, DICT_K k) {
     38 	u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K));
     39 	void *vp = 0;
     40 	r_dict_table_acc(&d->table, &vp, &k, h, DICT_SPEC);
     41 	return vp;
     42 }
     43 
     44 static inline UNUSED bool
     45 DICT_METHOD(get)(DICT_NAME *d, DICT_V *v, DICT_K k) {
     46 	u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K));
     47 	void *vp;
     48 	int r = r_dict_table_acc(&d->table, &vp, &k, h, DICT_SPEC);
     49 	if (v) *v = r > 0 ? *(DICT_V *)vp : d->default_val;
     50 	return r > 0;
     51 }
     52 
     53 static inline UNUSED bool
     54 DICT_METHOD(set)(DICT_NAME *d, DICT_K k, DICT_V v) {
     55 	u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K));
     56 	void *vp;
     57 	int r = r_dict_table_acc(&d->table, &vp, &k, h, DICT_SPEC);
     58 	if (r > 0) *(DICT_V *)vp = v;
     59 	return r > 0;
     60 }
     61 
     62 static inline UNUSED int
     63 DICT_METHOD(put)(DICT_NAME *d, DICT_K k, DICT_V v) {
     64 	u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K));
     65 	void *vp;
     66 	int r = r_dict_table_ins(&d->table, &vp, &k, h, DICT_SPEC);
     67 	if (r >= 0) *(DICT_V *)vp = v;
     68 	return r;
     69 }
     70 
     71 static inline UNUSED bool
     72 DICT_METHOD(del)(DICT_NAME *d, DICT_V *v, DICT_K k) {
     73 	u64 h = DICT_HASH(&k, d->table.seed, sizeof(DICT_K));
     74 	void *vp;
     75 	int r = r_dict_table_del(&d->table, &vp, &k, h, DICT_SPEC);
     76 	if (v) *v = r > 0 ? *(DICT_V *)vp : d->default_val;
     77 	return r > 0;
     78 }
     79 
     80 static inline UNUSED void
     81 DICT_METHOD(clr)(DICT_NAME *d) {
     82 	r_dict_table_clr(&d->table, DICT_SPEC);
     83 }
     84 
     85 #undef DICT_SPEC