From fe30bc7e660cef44be37a735c63ee1500bb83022 Mon Sep 17 00:00:00 2001 From: Eric Hawicz Date: Fri, 24 Apr 2026 12:55:46 -0400 Subject: [PATCH] Add lh_table_delete_entry_to_tail() and array_list_set_idx() functions. --- arraylist.c | 8 ++++++++ arraylist.h | 6 ++++++ json-c.sym | 2 ++ linkhash.c | 12 ++++++++++++ linkhash.h | 11 +++++++++++ 5 files changed, 39 insertions(+) diff --git a/arraylist.c b/arraylist.c index bfc1425..88c6d23 100644 --- a/arraylist.c +++ b/arraylist.c @@ -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 diff --git a/arraylist.h b/arraylist.h index a12f27f..5b79410 100644 --- a/arraylist.h +++ b/arraylist.h @@ -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 *)); diff --git a/json-c.sym b/json-c.sym index a419013..7a20758 100644 --- a/json-c.sym +++ b/json-c.sym @@ -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; diff --git a/linkhash.c b/linkhash.c index 58e1313..1856569 100644 --- a/linkhash.c +++ b/linkhash.c @@ -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); diff --git a/linkhash.h b/linkhash.h index 65c4909..5ad17cf 100644 --- a/linkhash.h +++ b/linkhash.h @@ -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. *