summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2024-10-11 19:10:59 +0200
committerJo-Philipp Wich <jo@mein.io>2024-12-02 15:07:26 +0100
commit47b54cf5a4b6166944efa4ee4cd023d73ebe6c3e (patch)
tree93f53b0374553d5647d6cef4797bf08daa105e12 /include
parentb0b5d93846a1fb9d1d94992d5fdf508ef345e87d (diff)
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 <idewolff@vincitech.nl> [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 <jo@mein.io>
Diffstat (limited to 'include')
-rw-r--r--include/ucode/types.h2
-rw-r--r--include/ucode/util.h28
2 files changed, 30 insertions, 0 deletions
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 */