--- libelwix/src/index.c 2022/01/04 22:56:53 1.1.2.2 +++ libelwix/src/index.c 2022/01/05 23:03:40 1.1.2.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: index.c,v 1.1.2.2 2022/01/04 22:56:53 misho Exp $ +* $Id: index.c,v 1.1.2.3 2022/01/05 23:03:40 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -81,7 +81,7 @@ index_FreeList(index_list_t __restrict lst) * index_FreeLists() - Free linked lists with data * * @idx = index - * return: no result + * return: none */ void index_FreeLists(index_t *idx) @@ -97,7 +97,7 @@ index_FreeLists(index_t *idx) * index_Destroy() - Destroy index * * @idx = index - * return: no result + * return: none */ void index_Destroy(index_t **idx) @@ -126,11 +126,11 @@ index_getArray(index_t *idx, u_short key) if (!idx) return NULL; - for (n = 0, lst = index_get(idx, key); lst; n++, lst = lst->il_next); + for (n = 0, lst = index_getList(idx, key); lst; n++, lst = lst->il_next); arr = array_Init(n); if (!arr) return NULL; - for (n = 0, lst = index_get(idx, key); lst; n++, lst = lst->il_next) + for (n = 0, lst = index_getList(idx, key); lst; n++, lst = lst->il_next) array_Set(arr, n, lst); return arr; @@ -239,6 +239,48 @@ index_delList(index_t *idx, u_short key) } /* + * index_del2() - Dels item with index key and hash + * + * @idx = index + * @key = hash key + * @hash = calculated hash of item when its added to index + * return: -1 error, 0 nothing deleted and 1 item deleted + */ +int +index_del2(index_t *idx, u_short key, u_int hash) +{ + index_list_t lst, prev; + + if (!idx) + return -1; + + lst = idx->i_hash[key]; + if (!lst) + return 0; + + if (lst->il_hash == hash) { + idx->i_hash[key] = lst->il_next; + e_free(lst); + return 1; /* deleted item */ + } else { + prev = lst; + lst = lst->il_next; + } + while (lst) { + if (lst->il_hash == hash) { + prev->il_next = lst->il_next; + e_free(lst); + return 1; /* deleted item */ + } else { + prev = lst; + lst = lst->il_next; + } + } + + return 0; +} + +/* * index_get2() - Get item by key and hash * * @idx = index @@ -291,4 +333,26 @@ index_getVar(index_t *idx, u_short key, u_int hash) AIT_SET_PTR(v, lst->il_ptr, lst->il_len); return v; +} + +/* + * index_dump() - Debug routine about index hashes + * + * @idx = index + * return: none + */ +void +index_dump(index_t *idx) +{ + index_list_t lst; + register int i; + + for (i = 0; i < 65536; i++) { + lst = idx->i_hash[i]; + while (lst) { + printf("index[%hu/0x%x]=%s length=%lu\n", (u_short) i, index_Hash(lst), + (const char*) index_Ptr(lst), index_Len(lst)); + lst = lst->il_next; + } + } }