Ignore:
Timestamp:
2008-03-06T22:18:23Z (17 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
3298ddc
Parents:
7b6d98b
Message:

Add hash_table_destroy().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/libadt/hash_table.c

    r7b6d98b r739d00a  
    11/*
    2  * Copyright (c) 2006 Jakub Jermar
     2 * Copyright (c) 2008 Jakub Jermar
    33 * All rights reserved.
    44 *
     
    4747/** Create chained hash table.
    4848 *
    49  * @param h Hash table structure. Will be initialized by this call.
    50  * @param m Number of slots in the hash table.
    51  * @param max_keys Maximal number of keys needed to identify an item.
    52  * @param op Hash table operations structure.
    53  * @return true on success
     49 * @param h             Hash table structure. Will be initialized by this call.
     50 * @param m             Number of hash table buckets.
     51 * @param max_keys      Maximal number of keys needed to identify an item.
     52 * @param op            Hash table operations structure.
     53 * @return              True on success
    5454 */
    55 int hash_table_create(hash_table_t *h, hash_count_t m, hash_count_t max_keys, hash_table_operations_t *op)
     55int hash_table_create(hash_table_t *h, hash_count_t m, hash_count_t max_keys,
     56    hash_table_operations_t *op)
    5657{
    5758        hash_count_t i;
     
    7778}
    7879
    79 /** Insert item into hash table.
     80/** Destroy a hash table instance.
    8081 *
    81  * @param h Hash table.
    82  * @param key Array of all keys necessary to compute hash index.
    83  * @param item Item to be inserted into the hash table.
     82 * @param h             Hash table to be destroyed.
     83 */
     84void hash_table_destroy(hash_table_t *h)
     85{
     86        assert(h);
     87        assert(h->entry);
     88        free(h->entry);
     89}
     90
     91/** Insert item into a hash table.
     92 *
     93 * @param h             Hash table.
     94 * @param key           Array of all keys necessary to compute hash index.
     95 * @param item          Item to be inserted into the hash table.
    8496 */
    8597void hash_table_insert(hash_table_t *h, unsigned long key[], link_t *item)
     
    98110/** Search hash table for an item matching keys.
    99111 *
    100  * @param h Hash table.
    101  * @param key Array of all keys needed to compute hash index.
     112 * @param h             Hash table.
     113 * @param key           Array of all keys needed to compute hash index.
    102114 *
    103  * @return Matching item on success, NULL if there is no such item.
     115 * @return              Matching item on success, NULL if there is no such item.
    104116 */
    105117link_t *hash_table_find(hash_table_t *h, unsigned long key[])
     
    113125        assert(chain < h->entries);
    114126       
    115         for (cur = h->entry[chain].next; cur != &h->entry[chain]; cur = cur->next) {
     127        for (cur = h->entry[chain].next; cur != &h->entry[chain];
     128            cur = cur->next) {
    116129                if (h->op->compare(key, h->max_keys, cur)) {
    117130                        /*
     
    129142 * For each removed item, h->remove_callback() is called.
    130143 *
    131  * @param h Hash table.
    132  * @param key Array of keys that will be compared against items of the hash table.
    133  * @param keys Number of keys in the 'key' array.
     144 * @param h             Hash table.
     145 * @param key           Array of keys that will be compared against items of
     146 *                      the hash table.
     147 * @param keys          Number of keys in the 'key' array.
    134148 */
    135149void hash_table_remove(hash_table_t *h, unsigned long key[], hash_count_t keys)
     
    138152        link_t *cur;
    139153
    140         assert(h && h->op && h->op->hash && h->op->compare && h->op->remove_callback);
     154        assert(h && h->op && h->op->hash && h->op->compare &&
     155            h->op->remove_callback);
    141156        assert(keys <= h->max_keys);
    142157       
     
    144159
    145160                /*
    146                  * All keys are known, hash_table_find() can be used to find the entry.
     161                 * All keys are known, hash_table_find() can be used to find the
     162                 * entry.
    147163                 */
    148164       
     
    160176         */
    161177        for (chain = 0; chain < h->entries; chain++) {
    162                 for (cur = h->entry[chain].next; cur != &h->entry[chain]; cur = cur->next) {
     178                for (cur = h->entry[chain].next; cur != &h->entry[chain];
     179                    cur = cur->next) {
    163180                        if (h->op->compare(key, keys, cur)) {
    164181                                link_t *hlp;
Note: See TracChangeset for help on using the changeset viewer.