Ignore:
Timestamp:
2011-11-06T22:21:05Z (15 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/fix-logger-deadlock, topic/msim-upgrade, topic/simplify-dev-export
Children:
898e847
Parents:
2bdf8313 (diff), 7b5f4c9 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/adt/hash_table.c

    r2bdf8313 rb0f00a9  
    5454 *
    5555 */
    56 int hash_table_create(hash_table_t *h, hash_count_t m, hash_count_t max_keys,
     56bool hash_table_create(hash_table_t *h, hash_count_t m, hash_count_t max_keys,
    5757    hash_table_operations_t *op)
    5858{
     
    6161        assert(max_keys > 0);
    6262       
    63         h->entry = malloc(m * sizeof(link_t));
     63        h->entry = malloc(m * sizeof(list_t));
    6464        if (!h->entry)
    6565                return false;
    6666       
    67         memset((void *) h->entry, 0,  m * sizeof(link_t));
     67        memset((void *) h->entry, 0,  m * sizeof(list_t));
    6868       
    6969        hash_count_t i;
     
    7878}
    7979
     80/** Remove all elements from the hash table
     81 *
     82 * @param h Hash table to be cleared
     83 */
     84void hash_table_clear(hash_table_t *h)
     85{
     86        for (hash_count_t chain = 0; chain < h->entries; ++chain) {
     87                link_t *cur;
     88                link_t *next;
     89               
     90                for (cur = h->entry[chain].head.next;
     91                    cur != &h->entry[chain].head;
     92                    cur = next) {
     93                        next = cur->next;
     94                        list_remove(cur);
     95                        h->op->remove_callback(cur);
     96                }
     97        }
     98}
     99
    80100/** Destroy a hash table instance.
    81101 *
     
    123143        assert(chain < h->entries);
    124144       
    125         link_t *cur;
    126         for (cur = h->entry[chain].next; cur != &h->entry[chain];
    127             cur = cur->next) {
     145        list_foreach(h->entry[chain], cur) {
    128146                if (h->op->compare(key, h->max_keys, cur)) {
    129147                        /*
     
    152170            h->op->remove_callback);
    153171        assert(keys <= h->max_keys);
    154        
    155         link_t *cur;
    156172       
    157173        if (keys == h->max_keys) {
     
    161177                 */
    162178               
    163                 cur = hash_table_find(h, key);
     179                link_t *cur = hash_table_find(h, key);
    164180                if (cur) {
    165181                        list_remove(cur);
     
    176192        hash_index_t chain;
    177193        for (chain = 0; chain < h->entries; chain++) {
    178                 for (cur = h->entry[chain].next; cur != &h->entry[chain];
     194                for (link_t *cur = h->entry[chain].head.next;
     195                    cur != &h->entry[chain].head;
    179196                    cur = cur->next) {
    180197                        if (h->op->compare(key, keys, cur)) {
     
    193210}
    194211
    195 /** Apply fucntion to all items in hash table.
     212/** Apply function to all items in hash table.
    196213 *
    197214 * @param h   Hash table.
     
    201218 */
    202219void hash_table_apply(hash_table_t *h, void (*f)(link_t *, void *), void *arg)
    203 {
    204         hash_index_t bucket;
    205         link_t *cur;
    206        
    207         for (bucket = 0; bucket < h->entries; bucket++) {
    208                 for (cur = h->entry[bucket].next; cur != &h->entry[bucket];
    209                     cur = cur->next) {
     220{       
     221        for (hash_index_t bucket = 0; bucket < h->entries; bucket++) {
     222                link_t *cur;
     223                link_t *next;
     224
     225                for (cur = h->entry[bucket].head.next; cur != &h->entry[bucket].head;
     226                    cur = next) {
     227                        /*
     228                         * The next pointer must be stored prior to the functor
     229                         * call to allow using destructor as the functor (the
     230                         * free function could overwrite the cur->next pointer).
     231                         */
     232                        next = cur->next;
    210233                        f(cur, arg);
    211234                }
Note: See TracChangeset for help on using the changeset viewer.