Add lh_table_delete_entry_to_tail() and array_list_set_idx() functions.

This commit is contained in:
Eric Hawicz
2026-04-24 12:55:46 -04:00
parent 52ddfb35f1
commit fe30bc7e66
5 changed files with 39 additions and 0 deletions

View File

@@ -172,6 +172,14 @@ int array_list_put_idx(struct array_list *arr, size_t idx, void *data)
return 0; 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) int array_list_add(struct array_list *arr, void *data)
{ {
/* Repeat some of array_list_put_idx() so we can skip several /* Repeat some of array_list_put_idx() so we can skip several

View 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); 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 size_t array_list_length(struct array_list *al);
extern void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *)); extern void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *));

View File

@@ -17,6 +17,7 @@ JSONC_PRIVATE {
array_list_free; array_list_free;
array_list_new; array_list_new;
array_list_put_idx; array_list_put_idx;
array_list_set_idx;
array_list_sort; array_list_sort;
json_hex_chars; json_hex_chars;
json_parse_double; json_parse_double;
@@ -24,6 +25,7 @@ JSONC_PRIVATE {
json_parse_uint64; json_parse_uint64;
lh_table_delete; lh_table_delete;
lh_table_delete_entry; lh_table_delete_entry;
lh_table_delete_entry_to_tail;
lh_table_free; lh_table_free;
lh_table_insert; lh_table_insert;
lh_table_insert_w_hash; lh_table_insert_w_hash;

View File

@@ -704,6 +704,18 @@ int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
return 0; 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) int lh_table_delete(struct lh_table *t, const void *k)
{ {
struct lh_entry *e = lh_table_lookup_entry(t, k); struct lh_entry *e = lh_table_lookup_entry(t, k);

View 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); 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. * Delete a record from the table.
* *