2006-11-26 11:13:41 -07:00
|
|
|
/* x-hash.c - basic hash tables
|
2012-06-10 07:21:05 -06:00
|
|
|
*
|
|
|
|
* Copyright (c) 2002-2012 Apple Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
|
|
|
|
* HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* Except as contained in this notice, the name(s) of the above
|
|
|
|
* copyright holders shall not be used in advertising or otherwise to
|
|
|
|
* promote the sale, use or other dealings in this Software without
|
|
|
|
* prior written authorization.
|
|
|
|
*/
|
2008-11-02 08:26:08 -07:00
|
|
|
|
|
|
|
#ifdef HAVE_DIX_CONFIG_H
|
|
|
|
#include <dix-config.h>
|
2007-11-24 10:55:21 -07:00
|
|
|
#endif
|
2008-11-02 08:26:08 -07:00
|
|
|
|
2006-11-26 11:13:41 -07:00
|
|
|
#include "x-hash.h"
|
|
|
|
#include "x-list.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2019-07-27 01:57:06 -06:00
|
|
|
#define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
|
|
|
|
|
2006-11-26 11:13:41 -07:00
|
|
|
struct x_hash_table_struct {
|
|
|
|
unsigned int bucket_index;
|
|
|
|
unsigned int total_keys;
|
|
|
|
x_list **buckets;
|
|
|
|
|
|
|
|
x_hash_fun *hash_key;
|
|
|
|
x_compare_fun *compare_keys;
|
|
|
|
x_destroy_fun *destroy_key;
|
|
|
|
x_destroy_fun *destroy_value;
|
|
|
|
};
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
#define ITEM_NEW(k, v) X_PFX(list_prepend) ((x_list *)(k), v)
|
|
|
|
#define ITEM_FREE(i) X_PFX(list_free_1) (i)
|
|
|
|
#define ITEM_KEY(i) ((void *)(i)->next)
|
|
|
|
#define ITEM_VALUE(i) ((i)->data)
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
#define SPLIT_THRESHOLD_FACTOR 2
|
|
|
|
|
|
|
|
/* http://planetmath.org/?op=getobj&from=objects&name=GoodHashTablePrimes */
|
|
|
|
static const unsigned int bucket_sizes[] = {
|
2012-06-10 07:21:05 -06:00
|
|
|
29, 53, 97, 193, 389, 769, 1543,
|
|
|
|
3079, 6151, 12289, 24593, 49157,
|
|
|
|
98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
|
|
|
|
12582917,
|
2006-11-26 11:13:41 -07:00
|
|
|
25165843, 50331653, 100663319, 201326611, 402653189, 805306457,
|
|
|
|
1610612741
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline unsigned int
|
2012-06-10 07:21:05 -06:00
|
|
|
hash_table_total_buckets(x_hash_table *h)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
|
|
|
return bucket_sizes[h->bucket_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2012-06-10 07:21:05 -06:00
|
|
|
hash_table_destroy_item(x_hash_table *h, void *k, void *v)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
|
|
|
if (h->destroy_key != 0)
|
2012-06-10 07:21:05 -06:00
|
|
|
(*h->destroy_key)(k);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
if (h->destroy_value != 0)
|
2012-06-10 07:21:05 -06:00
|
|
|
(*h->destroy_value)(v);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2009-09-06 13:44:18 -06:00
|
|
|
static inline size_t
|
2012-06-10 07:21:05 -06:00
|
|
|
hash_table_hash_key(x_hash_table *h, void *k)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
|
|
|
if (h->hash_key != 0)
|
2012-06-10 07:21:05 -06:00
|
|
|
return (*h->hash_key)(k);
|
2006-11-26 11:13:41 -07:00
|
|
|
else
|
2012-06-10 07:21:05 -06:00
|
|
|
return (size_t)k;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2012-06-10 07:21:05 -06:00
|
|
|
hash_table_compare_keys(x_hash_table *h, void *k1, void *k2)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
|
|
|
if (h->compare_keys == 0)
|
|
|
|
return k1 == k2;
|
|
|
|
else
|
2012-06-10 07:21:05 -06:00
|
|
|
return (*h->compare_keys)(k1, k2) == 0;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-06-10 07:21:05 -06:00
|
|
|
hash_table_split(x_hash_table *h)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
|
|
|
x_list **new, **old;
|
|
|
|
x_list *node, *item, *next;
|
|
|
|
int new_size, old_size;
|
2009-09-06 13:44:18 -06:00
|
|
|
size_t b;
|
2006-11-26 11:13:41 -07:00
|
|
|
int i;
|
|
|
|
|
2019-07-27 01:57:06 -06:00
|
|
|
if (h->bucket_index == ARRAY_SIZE(bucket_sizes) - 1)
|
2006-11-26 11:13:41 -07:00
|
|
|
return;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
old_size = hash_table_total_buckets(h);
|
2006-11-26 11:13:41 -07:00
|
|
|
old = h->buckets;
|
|
|
|
|
|
|
|
h->bucket_index++;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
new_size = hash_table_total_buckets(h);
|
|
|
|
new = calloc(new_size, sizeof(x_list *));
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
if (new == 0) {
|
2006-11-26 11:13:41 -07:00
|
|
|
h->bucket_index--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
for (i = 0; i < old_size; i++) {
|
|
|
|
for (node = old[i]; node != 0; node = next) {
|
2006-11-26 11:13:41 -07:00
|
|
|
next = node->next;
|
|
|
|
item = node->data;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
b = hash_table_hash_key(h, ITEM_KEY(item)) % new_size;
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
node->next = new[b];
|
|
|
|
new[b] = node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
h->buckets = new;
|
2012-06-10 07:21:05 -06:00
|
|
|
free(old);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN x_hash_table *
|
2012-06-10 07:21:05 -06:00
|
|
|
X_PFX(hash_table_new) (x_hash_fun * hash,
|
|
|
|
x_compare_fun * compare,
|
|
|
|
x_destroy_fun * key_destroy,
|
|
|
|
x_destroy_fun * value_destroy) {
|
2006-11-26 11:13:41 -07:00
|
|
|
x_hash_table *h;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
h = calloc(1, sizeof(x_hash_table));
|
2006-11-26 11:13:41 -07:00
|
|
|
if (h == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
h->bucket_index = 0;
|
2012-06-10 07:21:05 -06:00
|
|
|
h->buckets = calloc(hash_table_total_buckets(h), sizeof(x_list *));
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
if (h->buckets == 0) {
|
|
|
|
free(h);
|
2006-11-26 11:13:41 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2012-06-10 07:21:05 -06:00
|
|
|
|
2006-11-26 11:13:41 -07:00
|
|
|
h->hash_key = hash;
|
|
|
|
h->compare_keys = compare;
|
|
|
|
h->destroy_key = key_destroy;
|
|
|
|
h->destroy_value = value_destroy;
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN void
|
2012-06-10 07:21:05 -06:00
|
|
|
X_PFX(hash_table_free) (x_hash_table * h) {
|
2006-11-26 11:13:41 -07:00
|
|
|
int n, i;
|
|
|
|
x_list *node, *item;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
assert(h != NULL);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
n = hash_table_total_buckets(h);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
for (node = h->buckets[i]; node != 0; node = node->next) {
|
2006-11-26 11:13:41 -07:00
|
|
|
item = node->data;
|
2012-06-10 07:21:05 -06:00
|
|
|
hash_table_destroy_item(h, ITEM_KEY(item), ITEM_VALUE(item));
|
|
|
|
ITEM_FREE(item);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
2012-06-10 07:21:05 -06:00
|
|
|
X_PFX(list_free) (h->buckets[i]);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
free(h->buckets);
|
|
|
|
free(h);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN unsigned int
|
2012-06-10 07:21:05 -06:00
|
|
|
X_PFX(hash_table_size) (x_hash_table * h) {
|
|
|
|
assert(h != NULL);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
return h->total_keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-06-10 07:21:05 -06:00
|
|
|
hash_table_modify(x_hash_table *h, void *k, void *v, int replace)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
2009-09-06 13:44:18 -06:00
|
|
|
size_t hash_value;
|
2006-11-26 11:13:41 -07:00
|
|
|
x_list *node, *item;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
assert(h != NULL);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
hash_value = hash_table_hash_key(h, k);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
for (node = h->buckets[hash_value % hash_table_total_buckets(h)];
|
|
|
|
node != 0; node = node->next) {
|
2006-11-26 11:13:41 -07:00
|
|
|
item = node->data;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
if (hash_table_compare_keys(h, ITEM_KEY(item), k)) {
|
|
|
|
if (replace) {
|
|
|
|
hash_table_destroy_item(h, ITEM_KEY(item),
|
|
|
|
ITEM_VALUE(item));
|
2006-11-26 11:13:41 -07:00
|
|
|
item->next = k;
|
2012-06-10 07:21:05 -06:00
|
|
|
ITEM_VALUE(item) = v;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
2012-06-10 07:21:05 -06:00
|
|
|
else {
|
|
|
|
hash_table_destroy_item(h, k, ITEM_VALUE(item));
|
|
|
|
ITEM_VALUE(item) = v;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Key isn't already in the table. Insert it. */
|
|
|
|
|
|
|
|
if (h->total_keys + 1
|
2012-06-10 07:21:05 -06:00
|
|
|
> hash_table_total_buckets(h) * SPLIT_THRESHOLD_FACTOR) {
|
|
|
|
hash_table_split(h);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
hash_value = hash_value % hash_table_total_buckets(h);
|
|
|
|
h->buckets[hash_value] = X_PFX(list_prepend) (h->buckets[hash_value],
|
|
|
|
ITEM_NEW(k, v));
|
2006-11-26 11:13:41 -07:00
|
|
|
h->total_keys++;
|
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN void
|
2012-06-10 07:21:05 -06:00
|
|
|
X_PFX(hash_table_insert) (x_hash_table * h, void *k, void *v) {
|
|
|
|
hash_table_modify(h, k, v, 0);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN void
|
2012-06-10 07:21:05 -06:00
|
|
|
X_PFX(hash_table_replace) (x_hash_table * h, void *k, void *v) {
|
|
|
|
hash_table_modify(h, k, v, 1);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN void
|
2012-06-10 07:21:05 -06:00
|
|
|
X_PFX(hash_table_remove) (x_hash_table * h, void *k) {
|
2009-09-06 13:44:18 -06:00
|
|
|
size_t hash_value;
|
2006-11-26 11:13:41 -07:00
|
|
|
x_list **ptr, *item;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
assert(h != NULL);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
hash_value = hash_table_hash_key(h, k);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
for (ptr = &h->buckets[hash_value % hash_table_total_buckets(h)];
|
|
|
|
*ptr != 0; ptr = &((*ptr)->next)) {
|
2006-11-26 11:13:41 -07:00
|
|
|
item = (*ptr)->data;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
if (hash_table_compare_keys(h, ITEM_KEY(item), k)) {
|
|
|
|
hash_table_destroy_item(h, ITEM_KEY(item), ITEM_VALUE(item));
|
|
|
|
ITEM_FREE(item);
|
2006-11-26 11:13:41 -07:00
|
|
|
item = *ptr;
|
|
|
|
*ptr = item->next;
|
2012-06-10 07:21:05 -06:00
|
|
|
X_PFX(list_free_1) (item);
|
2006-11-26 11:13:41 -07:00
|
|
|
h->total_keys--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN void *
|
2012-06-10 07:21:05 -06:00
|
|
|
X_PFX(hash_table_lookup) (x_hash_table * h, void *k, void **k_ret) {
|
2009-09-06 13:44:18 -06:00
|
|
|
size_t hash_value;
|
2006-11-26 11:13:41 -07:00
|
|
|
x_list *node, *item;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
assert(h != NULL);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
hash_value = hash_table_hash_key(h, k);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
for (node = h->buckets[hash_value % hash_table_total_buckets(h)];
|
|
|
|
node != 0; node = node->next) {
|
2006-11-26 11:13:41 -07:00
|
|
|
item = node->data;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
if (hash_table_compare_keys(h, ITEM_KEY(item), k)) {
|
2006-11-26 11:13:41 -07:00
|
|
|
if (k_ret != 0)
|
2012-06-10 07:21:05 -06:00
|
|
|
*k_ret = ITEM_KEY(item);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
return ITEM_VALUE(item);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k_ret != 0)
|
|
|
|
*k_ret = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN void
|
2012-06-10 07:21:05 -06:00
|
|
|
X_PFX(hash_table_foreach) (x_hash_table * h,
|
|
|
|
x_hash_foreach_fun * fun, void *data) {
|
2006-11-26 11:13:41 -07:00
|
|
|
int i, n;
|
|
|
|
x_list *node, *item;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
assert(h != NULL);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
n = hash_table_total_buckets(h);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
for (node = h->buckets[i]; node != 0; node = node->next) {
|
2006-11-26 11:13:41 -07:00
|
|
|
item = node->data;
|
2012-06-10 07:21:05 -06:00
|
|
|
(*fun)(ITEM_KEY(item), ITEM_VALUE(item), data);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|