bool hash_add(hash_table *table, tc_pool_t *pool, uint64_t key, void *data) { hash_node *hn, *tmp; link_list *l; p_link_node ln; ln = hash_find_node(table, key); if (ln == NULL) { tmp = hash_node_malloc(pool, key, data); if (tmp != NULL) { l = get_link_list(table, key); ln = link_node_malloc(pool, tmp); if (ln != NULL) { link_list_push(l, ln); table->total++; return true; } else { return false; } } else { return false; } } else { hn = (hash_node *) ln->data; hn->data = data; return false; } }
static p_link_node hash_find_node(hash_table *table, uint64_t key) { hash_node *hn; link_list *l = get_link_list(table, key); p_link_node ln = link_list_first(l); table->total_visit++; while (ln) { hn = (hash_node *)ln->data; table->total_key_compared++; if (hn->key == key) { hn->access_time = time(0); hn->visit_cnt++; /* Put the lastest item to the head of the linked list */ (void)link_list_remove(l, ln); link_list_push(l, ln); return ln; } ln = link_list_get_next(l, ln); } return NULL; }
static p_link_node hash_find_node(hash_table *table, uint64_t key) { bool first = true; hash_node *hn; link_list *l = get_link_list(table, key); p_link_node ln = link_list_first(l); while (ln) { hn = (hash_node *) ln->data; if (hn->key == key) { if (!first) { /* put the lastest item to the head of the linked list */ link_list_remove(l, ln); link_list_push(l, ln); } return ln; } ln = link_list_get_next(l, ln); first = false; } return NULL; }