From 47b54cf5a4b6166944efa4ee4cd023d73ebe6c3e Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 11 Oct 2024 19:10:59 +0200 Subject: types: introduce `ucv_array_sort_r()` and `ucv_object_sort_r()` Introduce two new functions for ordering arrays and object keys which utilize a different compare callback signature and allow passing a user provided pointer to the comparison callback. The main advantages of the `ucv_*_sort_r()` flavors are the ability to pass custom context to comparisons via the user data pointer and the invocation of the comparison callback with direct `uc_value_t *` pointers instead of opaque `const void *` arguments pointing to `uc_value_t *` or json-c internal `struct lh_entry *` pointers respectively. Suggested-by: Isaac de Wolff [align naming and whitespace with the rest of the codebase, rename some variables for clarity, group sort related changes into two commits, drop constness from `uc_value_t *` compare function arguments] Signed-off-by: Jo-Philipp Wich --- include/ucode/types.h | 2 ++ include/ucode/util.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) (limited to 'include') diff --git a/include/ucode/types.h b/include/ucode/types.h index bf8b9f2..9cd7cfc 100644 --- a/include/ucode/types.h +++ b/include/ucode/types.h @@ -388,6 +388,7 @@ uc_value_t *ucv_array_push(uc_value_t *, uc_value_t *); uc_value_t *ucv_array_shift(uc_value_t *); uc_value_t *ucv_array_unshift(uc_value_t *, uc_value_t *); void ucv_array_sort(uc_value_t *, int (*)(const void *, const void *)); +void ucv_array_sort_r(uc_value_t *, int (*)(uc_value_t *, uc_value_t *, void *), void *); bool ucv_array_delete(uc_value_t *, size_t, size_t); bool ucv_array_set(uc_value_t *, size_t, uc_value_t *); size_t ucv_array_length(uc_value_t *); @@ -396,6 +397,7 @@ uc_value_t *ucv_object_new(uc_vm_t *); uc_value_t *ucv_object_get(uc_value_t *, const char *, bool *); bool ucv_object_add(uc_value_t *, const char *, uc_value_t *); void ucv_object_sort(uc_value_t *, int (*)(const void *, const void *)); +void ucv_object_sort_r(uc_value_t *, int (*)(const char *, uc_value_t *, const char *, uc_value_t *, void *), void *); bool ucv_object_delete(uc_value_t *, const char *); size_t ucv_object_length(uc_value_t *); diff --git a/include/ucode/util.h b/include/ucode/util.h index cc92e33..1360d28 100644 --- a/include/ucode/util.h +++ b/include/ucode/util.h @@ -267,4 +267,32 @@ uc_vector_extend_(char **base, size_t itemsize, size_t count, size_t add) iter != NULL && iter >= (vec)->entries; \ iter--) +#ifdef __APPLE__ + +# define uc_vector_sort_cb(fn_, optype_, udtype_, ...) \ + int fn_(void *ud, const void *k1, const void *k2) { \ + optype_ v1 = *(optype_ *)k1; \ + optype_ v2 = *(optype_ *)k2; \ + udtype_ ctx = (udtype_)ud; \ + __VA_ARGS__ \ + } + +# define uc_vector_sort(v_, cmp_, ud_) \ + qsort_r((v_)->entries, (v_)->count, sizeof((v_)->entries[0]), ud_, cmp_) + +#else + +# define uc_vector_sort_cb(fn_, optype_, udtype_, ...) \ + int fn_(const void *k1, const void *k2, void *ud) { \ + optype_ v1 = *(optype_ *)k1; \ + optype_ v2 = *(optype_ *)k2; \ + udtype_ ctx = (udtype_)ud; \ + __VA_ARGS__ \ + } + +# define uc_vector_sort(v_, cmp_, ud_) \ + qsort_r((v_)->entries, (v_)->count, sizeof((v_)->entries[0]), cmp_, ud_) + +#endif + #endif /* UCODE_UTIL_H */ -- cgit v1.2.3