hash_table.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 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 
00040 #include <adt/hash_table.h>
00041 #include <adt/list.h>
00042 #include <typedefs.h>
00043 #include <arch/types.h>
00044 #include <debug.h>
00045 #include <mm/slab.h>
00046 #include <memstr.h>
00047 
00055 void hash_table_create(hash_table_t *h, count_t m, count_t max_keys, hash_table_operations_t *op)
00056 {
00057         int i;
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), 0);
00064         if (!h->entry) {
00065                 panic("cannot allocate memory for hash table\n");
00066         }
00067         memsetb((__address) h->entry, m * sizeof(link_t), 0);
00068         
00069         for (i = 0; i < m; i++)
00070                 list_initialize(&h->entry[i]);
00071         
00072         h->entries = m;
00073         h->max_keys = max_keys;
00074         h->op = op;
00075 }
00076 
00083 void hash_table_insert(hash_table_t *h, __native key[], link_t *item)
00084 {
00085         index_t chain;
00086 
00087         ASSERT(item);
00088         ASSERT(h && h->op && h->op->hash && h->op->compare);
00089 
00090         chain = h->op->hash(key);
00091         ASSERT(chain < h->entries);
00092         
00093         list_append(item, &h->entry[chain]);
00094 }
00095 
00103 link_t *hash_table_find(hash_table_t *h, __native key[])
00104 {
00105         link_t *cur;
00106         index_t chain;
00107 
00108         ASSERT(h && h->op && h->op->hash && h->op->compare);
00109 
00110         chain = h->op->hash(key);
00111         ASSERT(chain < h->entries);
00112         
00113         for (cur = h->entry[chain].next; cur != &h->entry[chain]; cur = cur->next) {
00114                 if (h->op->compare(key, h->max_keys, cur)) {
00115                         /*
00116                          * The entry is there.
00117                          */
00118                         return cur;
00119                 }
00120         }
00121         
00122         return NULL;
00123 }
00124 
00133 void hash_table_remove(hash_table_t *h, __native key[], count_t keys)
00134 {
00135         index_t chain;
00136         link_t *cur;
00137 
00138         ASSERT(h && h->op && h->op->hash && h->op->compare && h->op->remove_callback);
00139         ASSERT(keys <= h->max_keys);
00140         
00141         if (keys == h->max_keys) {
00142 
00143                 /*
00144                  * All keys are known, hash_table_find() can be used to find the entry.
00145                  */
00146         
00147                 cur = hash_table_find(h, key);
00148                 if (cur) {
00149                         list_remove(cur);
00150                         h->op->remove_callback(cur);
00151                 }
00152                 return;
00153         }
00154         
00155         /*
00156          * Fewer keys were passed.
00157          * Any partially matching entries are to be removed.
00158          */
00159         for (chain = 0; chain < h->entries; chain++) {
00160                 for (cur = h->entry[chain].next; cur != &h->entry[chain]; cur = cur->next) {
00161                         if (h->op->compare(key, keys, cur)) {
00162                                 link_t *hlp;
00163                                 
00164                                 hlp = cur;
00165                                 cur = cur->prev;
00166                                 
00167                                 list_remove(hlp);
00168                                 h->op->remove_callback(hlp);
00169                                 
00170                                 continue;
00171                         }
00172                 }
00173         }
00174 }
00175 

Generated on Sun Jun 18 17:17:05 2006 for HelenOS Kernel (ppc32) by  doxygen 1.4.6