Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/adt/hash_table.c

    ra35b458 r1b20da0  
    9696        assert(h);
    9797        assert(op && op->hash && op->key_hash && op->key_equal);
    98 
     98       
    9999        /* Check for compulsory ops. */
    100100        if (!op || !op->hash || !op->key_hash || !op->key_equal)
    101101                return false;
    102 
     102       
    103103        h->bucket_cnt = round_up_size(init_size);
    104 
     104       
    105105        if (!alloc_table(h->bucket_cnt, &h->bucket))
    106106                return false;
    107 
     107       
    108108        h->max_load = (max_load == 0) ? HT_MAX_LOAD : max_load;
    109109        h->item_cnt = 0;
     
    115115                h->op->remove_callback = nop_remove_callback;
    116116        }
    117 
     117       
    118118        return true;
    119119}
     
    128128        assert(h && h->bucket);
    129129        assert(!h->apply_ongoing);
    130 
     130       
    131131        clear_items(h);
    132 
     132       
    133133        free(h->bucket);
    134134
     
    159159        assert(h && h->bucket);
    160160        assert(!h->apply_ongoing);
    161 
     161       
    162162        clear_items(h);
    163 
     163       
    164164        /* Shrink the table to its minimum size if possible. */
    165165        if (HT_MIN_BUCKETS < h->bucket_cnt) {
     
    173173        if (h->item_cnt == 0)
    174174                return;
    175 
     175       
    176176        for (size_t idx = 0; idx < h->bucket_cnt; ++idx) {
    177177                list_foreach_safe(h->bucket[idx], cur, next) {
    178178                        assert(cur);
    179179                        ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
    180 
     180                       
    181181                        list_remove(cur);
    182182                        h->op->remove_callback(cur_link);
    183183                }
    184184        }
    185 
     185       
    186186        h->item_cnt = 0;
    187187}
     
    197197        assert(h && h->bucket);
    198198        assert(!h->apply_ongoing);
    199 
     199       
    200200        size_t idx = h->op->hash(item) % h->bucket_cnt;
    201 
     201       
    202202        list_append(&item->link, &h->bucket[idx]);
    203203        ++h->item_cnt;
     
    220220        assert(h->op && h->op->hash && h->op->equal);
    221221        assert(!h->apply_ongoing);
    222 
     222       
    223223        size_t idx = h->op->hash(item) % h->bucket_cnt;
    224 
     224       
    225225        /* Check for duplicates. */
    226226        list_foreach(h->bucket[idx], link, ht_link_t, cur_link) {
     
    232232                        return false;
    233233        }
    234 
     234       
    235235        list_append(&item->link, &h->bucket[idx]);
    236236        ++h->item_cnt;
    237237        grow_if_needed(h);
    238 
     238       
    239239        return true;
    240240}
     
    251251{
    252252        assert(h && h->bucket);
    253 
     253       
    254254        size_t idx = h->op->key_hash(key) % h->bucket_cnt;
    255255
     
    264264                }
    265265        }
    266 
     266       
    267267        return NULL;
    268268}
     
    305305        assert(h && h->bucket);
    306306        assert(!h->apply_ongoing);
    307 
     307       
    308308        size_t idx = h->op->key_hash(key) % h->bucket_cnt;
    309309
    310310        size_t removed = 0;
    311 
     311       
    312312        list_foreach_safe(h->bucket[idx], cur, next) {
    313313                ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
    314 
     314               
    315315                if (h->op->key_equal(key, cur_link)) {
    316316                        ++removed;
     
    322322        h->item_cnt -= removed;
    323323        shrink_if_needed(h);
    324 
     324       
    325325        return removed;
    326326}
     
    352352        assert(f);
    353353        assert(h && h->bucket);
    354 
     354       
    355355        if (h->item_cnt == 0)
    356356                return;
    357 
     357       
    358358        h->apply_ongoing = true;
    359 
     359       
    360360        for (size_t idx = 0; idx < h->bucket_cnt; ++idx) {
    361361                list_foreach_safe(h->bucket[idx], cur, next) {
     
    371371out:
    372372        h->apply_ongoing = false;
    373 
     373       
    374374        shrink_if_needed(h);
    375375        grow_if_needed(h);
     
    380380{
    381381        size_t rounded_size = HT_MIN_BUCKETS;
    382 
     382       
    383383        while (rounded_size < size) {
    384384                rounded_size = 2 * rounded_size + 1;
    385385        }
    386 
     386       
    387387        return rounded_size;
    388388}
     
    392392{
    393393        assert(pbuckets && HT_MIN_BUCKETS <= bucket_cnt);
    394 
     394               
    395395        list_t *buckets = malloc(bucket_cnt * sizeof(list_t), FRAME_ATOMIC);
    396396        if (!buckets)
    397397                return false;
    398 
     398       
    399399        for (size_t i = 0; i < bucket_cnt; i++)
    400400                list_initialize(&buckets[i]);
     
    434434        assert(h && h->bucket);
    435435        assert(HT_MIN_BUCKETS <= new_bucket_cnt);
    436 
     436       
    437437        /* We are traversing the table and resizing would mess up the buckets. */
    438438        if (h->apply_ongoing)
    439439                return;
    440 
     440       
    441441        list_t *new_buckets;
    442442
     
    444444        if (!alloc_table(new_bucket_cnt, &new_buckets))
    445445                return;
    446 
     446       
    447447        if (0 < h->item_cnt) {
    448448                /* Rehash all the items to the new table. */
     
    457457                }
    458458        }
    459 
     459       
    460460        free(h->bucket);
    461461        h->bucket = new_buckets;
Note: See TracChangeset for help on using the changeset viewer.