Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • common/adt/hash_table.c

    r0db0df2 rad9178bf  
    247247        assert(h && h->bucket);
    248248
    249         size_t hash = h->op->key_hash(key);
    250         size_t idx = hash % h->bucket_cnt;
     249        size_t idx = h->op->key_hash(key) % h->bucket_cnt;
    251250
    252251        list_foreach(h->bucket[idx], link, ht_link_t, cur_link) {
    253                 if (h->op->key_equal(key, hash, cur_link))
     252                /*
     253                 * Is this is the item we are looking for? We could have first
     254                 * checked if the hashes match but op->key_equal() may very well be
     255                 * just as fast as op->hash().
     256                 */
     257                if (h->op->key_equal(key, cur_link)) {
    254258                        return cur_link;
     259                }
    255260        }
    256261
     
    260265/** Find the next item equal to item. */
    261266ht_link_t *
    262 hash_table_find_next(const hash_table_t *h, ht_link_t *item)
     267hash_table_find_next(const hash_table_t *h, ht_link_t *first, ht_link_t *item)
    263268{
    264269        assert(item);
     
    266271
    267272        size_t idx = h->op->hash(item) % h->bucket_cnt;
    268         list_t *list = &h->bucket[idx];
    269         link_t *cur = list_next(&item->link, list);
    270 
    271         /* Traverse the list until we reach its end. */
    272         for (; cur != NULL; cur = list_next(cur, list)) {
     273
     274        /* Traverse the circular list until we reach the starting item again. */
     275        for (link_t *cur = item->link.next; cur != &first->link;
     276            cur = cur->next) {
     277                assert(cur);
     278
     279                if (cur == &h->bucket[idx].head)
     280                        continue;
     281
    273282                ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
    274 
    275                 if (h->op->equal(cur_link, item))
     283                /*
     284                 * Is this is the item we are looking for? We could have first
     285                 * checked if the hashes match but op->equal() may very well be
     286                 * just as fast as op->hash().
     287                 */
     288                if (h->op->equal(cur_link, item)) {
    276289                        return cur_link;
     290                }
    277291        }
    278292
     
    295309        assert(!h->apply_ongoing);
    296310
    297         size_t hash = h->op->key_hash(key);
    298         size_t idx = hash % h->bucket_cnt;
     311        size_t idx = h->op->key_hash(key) % h->bucket_cnt;
    299312
    300313        size_t removed = 0;
     
    303316                ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
    304317
    305                 if (h->op->key_equal(key, hash, cur_link)) {
     318                if (h->op->key_equal(key, cur_link)) {
    306319                        ++removed;
    307320                        list_remove(cur);
Note: See TracChangeset for help on using the changeset viewer.