]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
Add lh_table_delete_entry_to_tail() and array_list_set_idx() functions.
authorEric Hawicz <erh+git@nimenees.com>
Fri, 24 Apr 2026 16:55:46 +0000 (12:55 -0400)
committerEric Hawicz <erh+git@nimenees.com>
Mon, 4 May 2026 00:44:08 +0000 (20:44 -0400)
arraylist.c
arraylist.h
json-c.sym
linkhash.c
linkhash.h

index bfc14251978105009926907afadaf34af0adf0ff..88c6d230bc0955a356426e6283144e660e990f3f 100644 (file)
@@ -172,6 +172,14 @@ int array_list_put_idx(struct array_list *arr, size_t idx, void *data)
        return 0;
 }
 
+int array_list_set_idx(struct array_list *arr, size_t idx, void *data)
+{
+       if (idx >= arr->length)
+               return -1;
+       arr->array[idx] = data;
+       return 0;
+}
+
 int array_list_add(struct array_list *arr, void *data)
 {
        /* Repeat some of array_list_put_idx() so we can skip several
index a12f27f54fc741925eab879edc08f311a269277e..5b794100c6664f837c640ec7fbc08811baeabc37 100644 (file)
@@ -68,6 +68,12 @@ extern int array_list_put_idx(struct array_list *al, size_t i, void *data);
 
 extern int array_list_add(struct array_list *al, void *data);
 
+/**
+ * Set the value at index i.  Caller is responsible for freeing the previous value.
+ * To automatically free the existing value, use array_list_put_idx() instead.
+ */
+extern int array_list_set_idx(struct array_list *al, size_t i, void  *data);
+
 extern size_t array_list_length(struct array_list *al);
 
 extern void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *));
index a41901373233c03693f490183e0d8c24215e2eb9..7a20758dd7f38e9bfd935c25723078fdd3230765 100644 (file)
@@ -17,6 +17,7 @@ JSONC_PRIVATE {
     array_list_free;
     array_list_new;
     array_list_put_idx;
+    array_list_set_idx;
     array_list_sort;
     json_hex_chars;
     json_parse_double;
@@ -24,6 +25,7 @@ JSONC_PRIVATE {
     json_parse_uint64;
     lh_table_delete;
     lh_table_delete_entry;
+    lh_table_delete_entry_to_tail;
     lh_table_free;
     lh_table_insert;
     lh_table_insert_w_hash;
index 58e13130914dc84900163599a716692edae5b574..1856569650d2ca07a0773ce125b5464f62ad3576 100644 (file)
@@ -704,6 +704,18 @@ int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
        return 0;
 }
 
+int lh_table_delete_entry_to_tail(struct lh_table *t, struct lh_entry *first_entry)
+{
+       struct lh_entry *del_entry = t->tail;
+       do
+       {
+               // Could probably micro-optimize this, but better to avoid code duplication for now
+               if (lh_table_delete_entry(t, del_entry) != 0)
+                       return -1;
+       } while (del_entry != first_entry);
+       return 0;
+}
+
 int lh_table_delete(struct lh_table *t, const void *k)
 {
        struct lh_entry *e = lh_table_lookup_entry(t, k);
index 65c4909683856c53f93479a71fcf5ef7f6771d30..5ad17cf2d24b3a2ad2c5e2e510e8b59379ec03cb 100644 (file)
@@ -303,6 +303,17 @@ extern json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v)
  */
 extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
 
+/**
+ * Delete all entries from the specified one to the tail of the list.
+ * Same as calling lh_table_delete_entry() on each of them.
+ *
+ * @param t the table to delete from.
+ * @param e a pointer to the first entry to delete.
+ * @return 0 if the item was deleted.
+ * @return -1 if it was not found.
+ */
+extern int lh_table_delete_entry_to_tail(struct lh_table *t, struct lh_entry *e);
+
 /**
  * Delete a record from the table.
  *