summaryrefslogtreecommitdiffhomepage
path: root/types.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2023-06-06 10:13:53 +0200
committerGitHub <noreply@github.com>2023-06-06 10:13:53 +0200
commitc7d84aae09691a99ae3db427c0b2463732ef84f4 (patch)
tree2b8a04d11ac4ecca6fad53dac87b5a2259df11a4 /types.c
parent3ffb046c59a690eb102b23f4539afd956f7e72d7 (diff)
parentd72eebeb168b8b350f3f9fb8cded8075464eef10 (diff)
Merge pull request #153 from jow-/lib-sort-object-support
lib: support object ordering in `uc_sort()`
Diffstat (limited to 'types.c')
-rw-r--r--types.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/types.c b/types.c
index 7bb67a6..5758f74 100644
--- a/types.c
+++ b/types.c
@@ -956,6 +956,48 @@ ucv_object_add(uc_value_t *uv, const char *key, uc_value_t *val)
return true;
}
+void
+ucv_object_sort(uc_value_t *uv, int (*cmp)(const void *, const void *))
+{
+ uc_object_t *object = (uc_object_t *)uv;
+ struct lh_table *t;
+ struct lh_entry *e;
+ size_t i;
+
+ struct {
+ struct lh_entry **entries;
+ size_t count;
+ } keys = { 0 };
+
+ if (ucv_type(uv) != UC_OBJECT || lh_table_length(object->table) <= 1)
+ return;
+
+ for (t = object->table, e = t->head; e; e = e->next)
+ uc_vector_push(&keys, e);
+
+ if (!keys.entries)
+ return;
+
+ qsort(keys.entries, keys.count, sizeof(keys.entries[0]), cmp);
+
+ for (i = 0; i < keys.count; i++) {
+ e = keys.entries[i];
+
+ if (i == 0) {
+ t->head = t->tail = e;
+ e->next = e->prev = NULL;
+ }
+ else {
+ t->tail->next = e;
+ e->prev = t->tail;
+ e->next = NULL;
+ t->tail = e;
+ }
+ }
+
+ uc_vector_clear(&keys);
+}
+
bool
ucv_object_delete(uc_value_t *uv, const char *key)
{