1 /* x-hash.c - basic hash tables
3 Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
5 Permission is hereby granted, free of charge, to any person
6 obtaining a copy of this software and associated documentation files
7 (the "Software"), to deal in the Software without restriction,
8 including without limitation the rights to use, copy, modify, merge,
9 publish, distribute, sublicense, and/or sell copies of the Software,
10 and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
20 HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name(s) of the above
26 copyright holders shall not be used in advertising or otherwise to
27 promote the sale, use or other dealings in this Software without
28 prior written authorization. */
29 #ifdef HAVE_XORG_CONFIG_H
30 #include <xorg-config.h>
37 struct x_hash_table_struct
{
38 unsigned int bucket_index
;
39 unsigned int total_keys
;
43 x_compare_fun
*compare_keys
;
44 x_destroy_fun
*destroy_key
;
45 x_destroy_fun
*destroy_value
;
48 #define ITEM_NEW(k, v) X_PFX (list_prepend) ((x_list *) (k), v)
49 #define ITEM_FREE(i) X_PFX (list_free_1) (i)
50 #define ITEM_KEY(i) ((void *) (i)->next)
51 #define ITEM_VALUE(i) ((i)->data)
53 #define SPLIT_THRESHOLD_FACTOR 2
55 /* http://planetmath.org/?op=getobj&from=objects&name=GoodHashTablePrimes */
56 static const unsigned int bucket_sizes
[] = {
57 29, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157,
58 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 12582917,
59 25165843, 50331653, 100663319, 201326611, 402653189, 805306457,
63 #define N_BUCKET_SIZES (sizeof (bucket_sizes) / sizeof (bucket_sizes[0]))
65 static inline unsigned int
66 hash_table_total_buckets (x_hash_table
*h
)
68 return bucket_sizes
[h
->bucket_index
];
72 hash_table_destroy_item (x_hash_table
*h
, void *k
, void *v
)
74 if (h
->destroy_key
!= 0)
75 (*h
->destroy_key
) (k
);
77 if (h
->destroy_value
!= 0)
78 (*h
->destroy_value
) (v
);
81 static inline unsigned int
82 hash_table_hash_key (x_hash_table
*h
, void *k
)
85 return (*h
->hash_key
) (k
);
87 return (unsigned int) k
;
91 hash_table_compare_keys (x_hash_table
*h
, void *k1
, void *k2
)
93 if (h
->compare_keys
== 0)
96 return (*h
->compare_keys
) (k1
, k2
) == 0;
100 hash_table_split (x_hash_table
*h
)
103 x_list
*node
, *item
, *next
;
104 int new_size
, old_size
;
108 if (h
->bucket_index
== N_BUCKET_SIZES
- 1)
111 old_size
= hash_table_total_buckets (h
);
116 new_size
= hash_table_total_buckets (h
);
117 new = calloc (new_size
, sizeof (x_list
*));
125 for (i
= 0; i
< old_size
; i
++)
127 for (node
= old
[i
]; node
!= 0; node
= next
)
132 b
= hash_table_hash_key (h
, ITEM_KEY (item
)) % new_size
;
143 X_EXTERN x_hash_table
*
144 X_PFX (hash_table_new
) (x_hash_fun
*hash
,
145 x_compare_fun
*compare
,
146 x_destroy_fun
*key_destroy
,
147 x_destroy_fun
*value_destroy
)
151 h
= calloc (1, sizeof (x_hash_table
));
156 h
->buckets
= calloc (hash_table_total_buckets (h
), sizeof (x_list
*));
165 h
->compare_keys
= compare
;
166 h
->destroy_key
= key_destroy
;
167 h
->destroy_value
= value_destroy
;
173 X_PFX (hash_table_free
) (x_hash_table
*h
)
180 n
= hash_table_total_buckets (h
);
182 for (i
= 0; i
< n
; i
++)
184 for (node
= h
->buckets
[i
]; node
!= 0; node
= node
->next
)
187 hash_table_destroy_item (h
, ITEM_KEY (item
), ITEM_VALUE (item
));
190 X_PFX (list_free
) (h
->buckets
[i
]);
197 X_EXTERN
unsigned int
198 X_PFX (hash_table_size
) (x_hash_table
*h
)
202 return h
->total_keys
;
206 hash_table_modify (x_hash_table
*h
, void *k
, void *v
, int replace
)
208 unsigned int hash_value
;
213 hash_value
= hash_table_hash_key (h
, k
);
215 for (node
= h
->buckets
[hash_value
% hash_table_total_buckets (h
)];
216 node
!= 0; node
= node
->next
)
220 if (hash_table_compare_keys (h
, ITEM_KEY (item
), k
))
224 hash_table_destroy_item (h
, ITEM_KEY (item
),
227 ITEM_VALUE (item
) = v
;
231 hash_table_destroy_item (h
, k
, ITEM_VALUE (item
));
232 ITEM_VALUE (item
) = v
;
238 /* Key isn't already in the table. Insert it. */
240 if (h
->total_keys
+ 1
241 > hash_table_total_buckets (h
) * SPLIT_THRESHOLD_FACTOR
)
243 hash_table_split (h
);
246 hash_value
= hash_value
% hash_table_total_buckets (h
);
247 h
->buckets
[hash_value
] = X_PFX (list_prepend
) (h
->buckets
[hash_value
],
253 X_PFX (hash_table_insert
) (x_hash_table
*h
, void *k
, void *v
)
255 hash_table_modify (h
, k
, v
, 0);
259 X_PFX (hash_table_replace
) (x_hash_table
*h
, void *k
, void *v
)
261 hash_table_modify (h
, k
, v
, 1);
265 X_PFX (hash_table_remove
) (x_hash_table
*h
, void *k
)
267 unsigned int hash_value
;
272 hash_value
= hash_table_hash_key (h
, k
);
274 for (ptr
= &h
->buckets
[hash_value
% hash_table_total_buckets (h
)];
275 *ptr
!= 0; ptr
= &((*ptr
)->next
))
279 if (hash_table_compare_keys (h
, ITEM_KEY (item
), k
))
281 hash_table_destroy_item (h
, ITEM_KEY (item
), ITEM_VALUE (item
));
285 X_PFX (list_free_1
) (item
);
293 X_PFX (hash_table_lookup
) (x_hash_table
*h
, void *k
, void **k_ret
)
295 unsigned int hash_value
;
300 hash_value
= hash_table_hash_key (h
, k
);
302 for (node
= h
->buckets
[hash_value
% hash_table_total_buckets (h
)];
303 node
!= 0; node
= node
->next
)
307 if (hash_table_compare_keys (h
, ITEM_KEY (item
), k
))
310 *k_ret
= ITEM_KEY (item
);
312 return ITEM_VALUE (item
);
323 X_PFX (hash_table_foreach
) (x_hash_table
*h
,
324 x_hash_foreach_fun
*fun
, void *data
)
331 n
= hash_table_total_buckets (h
);
333 for (i
= 0; i
< n
; i
++)
335 for (node
= h
->buckets
[i
]; node
!= 0; node
= node
->next
)
338 (*fun
) (ITEM_KEY (item
), ITEM_VALUE (item
), data
);