mirror of
https://github.com/json-c/json-c.git
synced 2026-04-26 15:49:06 +08:00
Add lh_table_delete_entry_to_tail() and array_list_set_idx() functions.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 *));
|
||||
|
||||
@@ -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;
|
||||
|
||||
12
linkhash.c
12
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);
|
||||
|
||||
11
linkhash.h
11
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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user