1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002 Cray Inc.
5 * Copyright (c) 2002 Eric Hoffman
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * a very simple hash table implementation with paramerterizable
30 * comparison and key generation functions. it does resize
31 * in order to accomidate more entries, but never collapses
35 static table_entry *table_lookup (table t,void *comparator,
37 int (*compare_function)(void *, void *),
40 unsigned int key=k%t->size;
43 for (i=&(t->entries[key]);*i;i=&((*i)->next)){
44 if (compare_function && ((*i)->key==k))
45 if ((*t->compare_function)((*i)->value,comparator)){
51 return(&(t->entries[key]));
55 static void resize_table(table t, int size)
58 table_entry *old_entries=t->entries;
61 table_entry *position;
65 t->entries=(table_entry *)malloc(sizeof(table_entry)*t->size);
66 memset(t->entries,0,sizeof(table_entry)*t->size);
68 for (i=0;i<old_size;i++)
69 for (j=old_entries[i];j;j=n){
71 position=table_lookup(t,0,j->key,0,&success);
79 /* Function: key_from_int
80 * Arguments: int i: value to compute the key of
83 unsigned int key_from_int(int i)
89 /* Function: key_from_string
90 * Arguments: char *s: the null terminated string
91 * to compute the key of
94 unsigned int key_from_string(char *s)
96 unsigned int result=0;
100 for (n=s,i=0;*n;n++,i++) result^=(*n*57)^*n*i;
105 /* Function: hash_create_table
106 * Arguments: compare_function: a function to compare
107 * a table instance with a correlator
108 * key_function: a function to generate a 32 bit
109 * hash key from a correlator
110 * Returns: a pointer to the new table
112 table hash_create_table (int (*compare_function)(void *, void *),
113 unsigned int (*key_function)(unsigned int *))
115 table new=(table)malloc(sizeof(struct table));
116 memset(new, 0, sizeof(struct table));
118 new->compare_function=compare_function;
119 new->key_function=key_function;
120 new->number_of_entries=0;
122 new->entries=(table_entry *)malloc(sizeof(table_entry)*new->size);
123 memset(new->entries,0,sizeof(table_entry)*new->size);
128 /* Function: hash_table_find
129 * Arguments: t: a table to look in
130 * comparator: a value to access the table entry
131 * Returns: the element references to by comparator, or null
133 void *hash_table_find (table t, void *comparator)
136 table_entry* entry=table_lookup(t,comparator,
137 (*t->key_function)(comparator),
140 if (success) return((*entry)->value);
145 /* Function: hash_table_insert
146 * Arguments: t: a table to insert the object
147 * value: the object to put in the table
148 * comparator: the value by which the object
152 void hash_table_insert (table t, void *value, void *comparator)
155 unsigned int k=(*t->key_function)(comparator);
156 table_entry *position=table_lookup(t,comparator,k,
157 t->compare_function,&success);
163 entry = (table_entry)malloc(sizeof(struct table_entry));
164 memset(entry, 0, sizeof(struct table_entry));
165 entry->next= *position;
167 t->number_of_entries++;
171 if (t->number_of_entries > t->size) resize_table(t,t->size*2);
174 /* Function: hash_table_remove
175 * Arguments: t: the table to remove the object from
176 * comparator: the index value of the object to remove
179 void hash_table_remove (table t, void *comparator)
183 table_entry *position=table_lookup(t,comparator,
184 (*t->key_function)(comparator),
185 t->compare_function,&success);
188 *position=(*position)->next;
189 free(temp); /* the value? */
190 t->number_of_entries--;
194 /* Function: hash_iterate_table_entries
195 * Arguments: t: the table to iterate over
196 * handler: a function to call with each element
197 * of the table, along with arg
198 * arg: the opaque object to pass to handler
201 void hash_iterate_table_entries(table t,
202 void (*handler)(void *,void *),
206 table_entry *j,*next;
208 for (i=0;i<t->size;i++)
209 for (j=t->entries+i;*j;j=next){
211 (*handler)(arg,(*j)->value);
215 /* Function: hash_filter_table_entries
216 * Arguments: t: the table to iterate over
217 * handler: a function to call with each element
218 * of the table, along with arg
219 * arg: the opaque object to pass to handler
221 * Notes: operations on the table inside handler are not safe
223 * filter_table_entires() calls the handler function for each
224 * item in the table, passing it and arg. The handler function
225 * returns 1 if it is to be retained in the table, and 0
226 * if it is to be removed.
228 void hash_filter_table_entries(table t, int (*handler)(void *, void *), void *arg)
231 table_entry *j,*next,v;
233 for (i=0;i<t->size;i++)
234 for (j=t->entries+i;*j;j=next){
236 if (!(*handler)(arg,(*j)->value)){
241 t->number_of_entries--;
246 /* Function: destroy_table
247 * Arguments: t: the table to free
248 * thunk: a function to call with each element,
252 void hash_destroy_table(table t,void (*thunk)(void *))
256 for (i=0;i<t->size;i++)
257 for (j=t->entries[i];j;j=next){
259 if (thunk) (*thunk)(j->value);