From b7b6d4ad12f2cbacfd368bd100d5b940a492ea01 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Thu, 12 May 2022 18:12:37 +0900 Subject: [PATCH] [hash] Clean up some duplicate code Hash_Add and Hash_AddElement differed by only one line: the hash calculation. --- libs/util/hash.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/libs/util/hash.c b/libs/util/hash.c index 082672c7d..f1d3d3884 100644 --- a/libs/util/hash.c +++ b/libs/util/hash.c @@ -242,10 +242,9 @@ Hash_FlushTable (hashtab_t *tab) tab->num_ele = 0; } -VISIBLE int -Hash_Add (hashtab_t *tab, void *ele) +static int +hash_add_element (hashtab_t *tab, uintptr_t h, void *ele) { - uintptr_t h = Hash_String (tab->get_key(ele, tab->user_data)); size_t ind = get_index (h, tab->tab_size, tab->size_bits); hashlink_t *lnk = new_hashlink (tab->hashlink_freelist); @@ -261,23 +260,18 @@ Hash_Add (hashtab_t *tab, void *ele) return 0; } +VISIBLE int +Hash_Add (hashtab_t *tab, void *ele) +{ + uintptr_t h = Hash_String (tab->get_key(ele, tab->user_data)); + return hash_add_element (tab, h, ele); +} + VISIBLE int Hash_AddElement (hashtab_t *tab, void *ele) { uintptr_t h = tab->get_hash (ele, tab->user_data); - size_t ind = get_index (h, tab->tab_size, tab->size_bits); - hashlink_t *lnk = new_hashlink (tab->hashlink_freelist); - - if (!lnk) - return -1; - if (tab->tab[ind]) - tab->tab[ind]->prev = &lnk->next; - lnk->next = tab->tab[ind]; - lnk->prev = &tab->tab[ind]; - lnk->data = ele; - tab->tab[ind] = lnk; - tab->num_ele++; - return 0; + return hash_add_element (tab, h, ele); } VISIBLE void *