diff --git a/include/extra/map.h b/include/extra/map.h index 88514d3..458d75e 100644 --- a/include/extra/map.h +++ b/include/extra/map.h @@ -6,15 +6,12 @@ #ifndef LIBCDSB_EXTRA_MAP_H #define LIBCDSB_EXTRA_MAP_H -typedef int (*map_foreach_callback)(const void* key, vtype key_type, void* value, vtype value_type, void* data); - - #define map_foreach(x, data, callback) libcdsb_map_foreach(x, data, callback, 0) -extern _Bool libcdsb_map_find (vtype_value* x, vtype_map* s, const void* key, vtype key_type, _Bool cut) LIBCDSB_nt__ LIBCDSB_nn23__; extern _Bool libcdsb_map_update(vtype_map* x, const void* k, vtype kt, const void* v, vtype vt) LIBCDSB_nt__ LIBCDSB_nn124__; -extern int libcdsb_map_foreach(vtype_map* x, void* data, map_foreach_callback, _Bool flush) LIBCDSB_nt__ LIBCDSB_nn13__; +extern int libcdsb_map_find (vtype_map* x, const void* key, vtype key_type, void* data, map_access_callback, _Bool cut) LIBCDSB_nt__ LIBCDSB_nn1__; +extern int libcdsb_map_foreach(vtype_map* x, void* data, map_access_callback, _Bool flush) LIBCDSB_nt__ LIBCDSB_nn13__; #endif /* LIBCDSB_EXTRA_MAP_H */ diff --git a/include/map.h b/include/map.h index c86e441..8e204a3 100644 --- a/include/map.h +++ b/include/map.h @@ -7,6 +7,8 @@ #ifndef LIBCDSB_MAP_H #define LIBCDSB_MAP_H +typedef int (*map_access_callback)(const void* key, vtype key_type, void* value, vtype value_type, void* data); + extern void map_init(vtype_map* x, vtype key_type); #define map_pop(x, s, key) _LIBCDSB_Generic (libcdsb_map, find, key)(x, s, key, 1) @@ -14,25 +16,25 @@ extern void map_init(vtype_map* x, vtype key_type); #define map_update(x, key, value) _LIBCDSB_Generic2(libcdsb_map, update, key, value)(x, key, value) #define map_remove(x, key) map_pop(0, x, key) -extern _Bool libcdsb_map_find_pointer(vtype_value* x, vtype_map* s, const void* key, _Bool cut); -extern _Bool libcdsb_map_find_cstring(vtype_value* x, vtype_map* s, const char* key, _Bool cut); -extern _Bool libcdsb_map_find_string (vtype_value* x, vtype_map* s, const vtype_string* key, _Bool cut); -extern _Bool libcdsb_map_find_array (vtype_value* x, vtype_map* s, const vtype_array* key, _Bool cut); -extern _Bool libcdsb_map_find_list (vtype_value* x, vtype_map* s, const vtype_list* key, _Bool cut); -extern _Bool libcdsb_map_find_map (vtype_value* x, vtype_map* s, const vtype_map* key, _Bool cut); -extern _Bool libcdsb_map_find_vset (vtype_value* x, vtype_map* s, const vtype_set* key, _Bool cut); -extern _Bool libcdsb_map_find_boolean(vtype_value* x, vtype_map* s, vtype_bool key, _Bool cut); -extern _Bool libcdsb_map_find_int8 (vtype_value* x, vtype_map* s, vtype_int8 key, _Bool cut); -extern _Bool libcdsb_map_find_int16 (vtype_value* x, vtype_map* s, vtype_int16 key, _Bool cut); -extern _Bool libcdsb_map_find_int32 (vtype_value* x, vtype_map* s, vtype_int32 key, _Bool cut); -extern _Bool libcdsb_map_find_int64 (vtype_value* x, vtype_map* s, vtype_int64 key, _Bool cut); -extern _Bool libcdsb_map_find_uint8 (vtype_value* x, vtype_map* s, vtype_uint8 key, _Bool cut); -extern _Bool libcdsb_map_find_uint16 (vtype_value* x, vtype_map* s, vtype_uint16 key, _Bool cut); -extern _Bool libcdsb_map_find_uint32 (vtype_value* x, vtype_map* s, vtype_uint32 key, _Bool cut); -extern _Bool libcdsb_map_find_uint64 (vtype_value* x, vtype_map* s, vtype_uint64 key, _Bool cut); -extern _Bool libcdsb_map_find_float (vtype_value* x, vtype_map* s, vtype_float key, _Bool cut); -extern _Bool libcdsb_map_find_double (vtype_value* x, vtype_map* s, vtype_double key, _Bool cut); -extern _Bool libcdsb_map_find_ldouble(vtype_value* x, vtype_map* s, vtype_ldouble key, _Bool cut); +extern int libcdsb_map_find_pointer(vtype_map* x, const void* key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_cstring(vtype_map* x, const char* key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_string (vtype_map* x, const vtype_string* key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_array (vtype_map* x, const vtype_array* key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_list (vtype_map* x, const vtype_list* key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_map (vtype_map* x, const vtype_map* key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_vset (vtype_map* x, const vtype_set* key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_boolean(vtype_map* x, vtype_bool key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_int8 (vtype_map* x, vtype_int8 key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_int16 (vtype_map* x, vtype_int16 key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_int32 (vtype_map* x, vtype_int32 key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_int64 (vtype_map* x, vtype_int64 key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_uint8 (vtype_map* x, vtype_uint8 key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_uint16 (vtype_map* x, vtype_uint16 key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_uint32 (vtype_map* x, vtype_uint32 key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_uint64 (vtype_map* x, vtype_uint64 key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_float (vtype_map* x, vtype_float key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_double (vtype_map* x, vtype_double key, void* data, map_access_callback, _Bool cut); +extern int libcdsb_map_find_ldouble(vtype_map* x, vtype_ldouble key, void* data, map_access_callback, _Bool cut); extern _Bool libcdsb_map_update_pointer_pointer(vtype_map* x, const void* key, const void* value); extern _Bool libcdsb_map_update_pointer_cstring(vtype_map* x, const void* key, const char* value); diff --git a/src/map/extra.c b/src/map/extra.c index f395d68..d9cb163 100644 --- a/src/map/extra.c +++ b/src/map/extra.c @@ -3,36 +3,6 @@ #include "include.h" - -_Bool libcdsb_map_find(val_t* x, map_t* s, const void* k, vtype t, _Bool cut) { - mnode_t* c; - int cmp; - - c = s->root; - - while (!mnode_is_empty(c)) { - - cmp = vtype_compare(vnode_peek(&c->key, s->type), s->type, k, t); - - if (cmp == 0) { - if (cut) { - c = mnode_delete(&s->root, c); - if (!is_null(x)) { - value_set(x, c->value, c->type, VF_WRITEABLE|VF_REMOVABLE); - } else vnode_free(&c->value, c->type); - vnode_free(&c->key, s->type); - free(c); - } else if (!is_null(x)) value_set(x, &c->value, c->type, VF_WRITEABLE|VF_CHANGEABLE); - - return true; - } - - c = (cmp < 0) ? c->right : c->left; - } - return false; -} - - _Bool libcdsb_map_update(map_t* x, const void* k, vtype kt, const void* v, vtype vt) { int cmp; mnode_t* n; @@ -80,7 +50,37 @@ _Bool libcdsb_map_update(map_t* x, const void* k, vtype kt, const void* v, vtype } -int libcdsb_map_foreach(map_t* x, void* dt, map_foreach_callback callback, _Bool flush) { +int libcdsb_map_find(map_t* x, const void* k, vtype t, void* _, map_access_callback callback, _Bool cut) { + mnode_t* c; + void *key; + int cmp; + + c = x->root; + + while (!mnode_is_empty(c)) { + key = vnode_peek(&c->key, x->type); + cmp = vtype_compare(key, x->type, k, t); + + if (cmp == 0) { + cmp = (callback) ? callback(key, x->type, vnode_peek(&c->value, c->type), c->type, _) : 0; + + if (cut) { + c = mnode_delete(&x->root, c); + vnode_free(&c->key, x->type); + vnode_free(&c->value, c->type); + free(c); + } + + return cmp; + } + } + + return -1; + +} + + +int libcdsb_map_foreach(map_t* x, void* dt, map_access_callback callback, _Bool flush) { stack_t z = { .prev = 0, .value = x->root }; int r = 0; mnode_t* c; diff --git a/src/map/generics.c b/src/map/generics.c index 69774a4..72a98b2 100644 --- a/src/map/generics.c +++ b/src/map/generics.c @@ -3,25 +3,25 @@ #include "include.h" -_Bool libcdsb_map_find_pointer(val_t* x, map_t* s, const void* k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_cstring(val_t* x, map_t* s, const char* k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_string (val_t* x, map_t* s, const str_t* k, _Bool cut) { return libcdsb_map_find(x, s, k, vtypeof( k), cut); } -_Bool libcdsb_map_find_array (val_t* x, map_t* s, const arr_t* k, _Bool cut) { return libcdsb_map_find(x, s, k, vtypeof( k), cut); } -_Bool libcdsb_map_find_list (val_t* x, map_t* s, const list_t* k, _Bool cut) { return libcdsb_map_find(x, s, k, vtypeof( k), cut); } -_Bool libcdsb_map_find_map (val_t* x, map_t* s, const map_t* k, _Bool cut) { return libcdsb_map_find(x, s, k, vtypeof( k), cut); } -_Bool libcdsb_map_find_vset (val_t* x, map_t* s, const set_t* k, _Bool cut) { return libcdsb_map_find(x, s, k, vtypeof( k), cut); } -_Bool libcdsb_map_find_boolean(val_t* x, map_t* s, _Bool k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_int8 (val_t* x, map_t* s, s8_t k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_int16 (val_t* x, map_t* s, s16_t k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_int32 (val_t* x, map_t* s, s32_t k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_int64 (val_t* x, map_t* s, s64_t k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_uint8 (val_t* x, map_t* s, u8_t k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_uint16 (val_t* x, map_t* s, u16_t k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_uint32 (val_t* x, map_t* s, u32_t k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_uint64 (val_t* x, map_t* s, u64_t k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_float (val_t* x, map_t* s, fl_t k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_double (val_t* x, map_t* s, dbl_t k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } -_Bool libcdsb_map_find_ldouble(val_t* x, map_t* s, ldbl_t k, _Bool cut) { return libcdsb_map_find(x, s, &k, vtypeof(&k), cut); } +int libcdsb_map_find_pointer(map_t* x, const void* k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_cstring(map_t* x, const char* k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_string (map_t* x, const str_t* k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, k, vtypeof( k), _, cb, cut); } +int libcdsb_map_find_array (map_t* x, const arr_t* k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, k, vtypeof( k), _, cb, cut); } +int libcdsb_map_find_list (map_t* x, const list_t* k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, k, vtypeof( k), _, cb, cut); } +int libcdsb_map_find_map (map_t* x, const map_t* k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, k, vtypeof( k), _, cb, cut); } +int libcdsb_map_find_vset (map_t* x, const set_t* k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, k, vtypeof( k), _, cb, cut); } +int libcdsb_map_find_boolean(map_t* x, _Bool k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_int8 (map_t* x, s8_t k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_int16 (map_t* x, s16_t k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_int32 (map_t* x, s32_t k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_int64 (map_t* x, s64_t k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_uint8 (map_t* x, u8_t k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_uint16 (map_t* x, u16_t k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_uint32 (map_t* x, u32_t k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_uint64 (map_t* x, u64_t k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_float (map_t* x, fl_t k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_double (map_t* x, dbl_t k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } +int libcdsb_map_find_ldouble(map_t* x, ldbl_t k, void* _, map_access_callback cb, _Bool cut) { return libcdsb_map_find(x, &k, vtypeof(&k), _, cb, cut); } _Bool libcdsb_map_update_pointer_pointer(map_t* x, const void* k, const void* v) { return libcdsb_map_update(x, &k, vtypeof(&k), &v, vtypeof(&v)); } _Bool libcdsb_map_update_pointer_cstring(map_t* x, const void* k, const char* v) { return libcdsb_map_update(x, &k, vtypeof(&k), &v, vtypeof(&v)); }