hash_table.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008 Jakub Jermar
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 /*
00036  * This is an implementation of generic chained hash table.
00037  */
00038 
00039 #include <adt/hash_table.h>
00040 #include <adt/list.h>
00041 #include <unistd.h>
00042 #include <malloc.h>
00043 #include <assert.h>
00044 #include <str.h>
00045 
00056 bool hash_table_create(hash_table_t *h, hash_count_t m, hash_count_t max_keys,
00057     hash_table_operations_t *op)
00058 {
00059         assert(h);
00060         assert(op && op->hash && op->compare);
00061         assert(max_keys > 0);
00062         
00063         h->entry = malloc(m * sizeof(link_t));
00064         if (!h->entry)
00065                 return false;
00066         
00067         memset((void *) h->entry, 0,  m * sizeof(link_t));
00068         
00069         hash_count_t i;
00070         for (i = 0; i < m; i++)
00071                 list_initialize(&h->entry[i]);
00072         
00073         h->entries = m;
00074         h->max_keys = max_keys;
00075         h->op = op;
00076         
00077         return true;
00078 }
00079 
00085 void hash_table_destroy(hash_table_t *h)
00086 {
00087         assert(h);
00088         assert(h->entry);
00089         
00090         free(h->entry);
00091 }
00092 
00099 void hash_table_insert(hash_table_t *h, unsigned long key[], link_t *item)
00100 {
00101         assert(item);
00102         assert(h && h->op && h->op->hash && h->op->compare);
00103         
00104         hash_index_t chain = h->op->hash(key);
00105         assert(chain < h->entries);
00106         
00107         list_append(item, &h->entry[chain]);
00108 }
00109 
00118 link_t *hash_table_find(hash_table_t *h, unsigned long key[])
00119 {
00120         assert(h && h->op && h->op->hash && h->op->compare);
00121         
00122         hash_index_t chain = h->op->hash(key);
00123         assert(chain < h->entries);
00124         
00125         link_t *cur;
00126         for (cur = h->entry[chain].next; cur != &h->entry[chain];
00127             cur = cur->next) {
00128                 if (h->op->compare(key, h->max_keys, cur)) {
00129                         /*
00130                          * The entry is there.
00131                          */
00132                         return cur;
00133                 }
00134         }
00135         
00136         return NULL;
00137 }
00138 
00149 void hash_table_remove(hash_table_t *h, unsigned long key[], hash_count_t keys)
00150 {
00151         assert(h && h->op && h->op->hash && h->op->compare &&
00152             h->op->remove_callback);
00153         assert(keys <= h->max_keys);
00154         
00155         link_t *cur;
00156         
00157         if (keys == h->max_keys) {
00158                 /*
00159                  * All keys are known, hash_table_find() can be used to find the
00160                  * entry.
00161                  */
00162                 
00163                 cur = hash_table_find(h, key);
00164                 if (cur) {
00165                         list_remove(cur);
00166                         h->op->remove_callback(cur);
00167                 }
00168                 
00169                 return;
00170         }
00171         
00172         /*
00173          * Fewer keys were passed.
00174          * Any partially matching entries are to be removed.
00175          */
00176         hash_index_t chain;
00177         for (chain = 0; chain < h->entries; chain++) {
00178                 for (cur = h->entry[chain].next; cur != &h->entry[chain];
00179                     cur = cur->next) {
00180                         if (h->op->compare(key, keys, cur)) {
00181                                 link_t *hlp;
00182                                 
00183                                 hlp = cur;
00184                                 cur = cur->prev;
00185                                 
00186                                 list_remove(hlp);
00187                                 h->op->remove_callback(hlp);
00188                                 
00189                                 continue;
00190                         }
00191                 }
00192         }
00193 }
00194 
00202 void hash_table_apply(hash_table_t *h, void (*f)(link_t *, void *), void *arg)
00203 {
00204         hash_index_t bucket;
00205         link_t *cur;
00206         
00207         for (bucket = 0; bucket < h->entries; bucket++) {
00208                 for (cur = h->entry[bucket].next; cur != &h->entry[bucket];
00209                     cur = cur->next) {
00210                         f(cur, arg);
00211                 }
00212         }
00213 }
00214 

Generated on Thu Jun 2 07:45:45 2011 for HelenOS/USB by  doxygen 1.4.7