summaryrefslogtreecommitdiff
path: root/hw/xquartz/xpr/x-hash.c
blob: 7c6a67bd10685d43e0c21fe60bd81e025854f4f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* x-hash.c - basic hash tables

   Copyright (c) 2002 Apple Computer, 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. */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include "x-hash.h"
#include "x-list.h"
#include <stdlib.h>
#include <assert.h>

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;
};

#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)

#define SPLIT_THRESHOLD_FACTOR 2

/* http://planetmath.org/?op=getobj&from=objects&name=GoodHashTablePrimes */
static const unsigned int bucket_sizes[] = {
    29, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157,
    98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 12582917,
    25165843, 50331653, 100663319, 201326611, 402653189, 805306457,
    1610612741
};

#define N_BUCKET_SIZES (sizeof (bucket_sizes) / sizeof (bucket_sizes[0]))

static inline unsigned int
hash_table_total_buckets (x_hash_table *h)
{
    return bucket_sizes[h->bucket_index];
}

static inline void
hash_table_destroy_item (x_hash_table *h, void *k, void *v)
{
    if (h->destroy_key != 0)
        (*h->destroy_key) (k);

    if (h->destroy_value != 0)
        (*h->destroy_value) (v);
}

static inline size_t
hash_table_hash_key (x_hash_table *h, void *k)
{
    if (h->hash_key != 0)
        return (*h->hash_key) (k);
    else
        return (size_t) k;
}

static inline int
hash_table_compare_keys (x_hash_table *h, void *k1, void *k2)
{
    if (h->compare_keys == 0)
        return k1 == k2;
    else
        return (*h->compare_keys) (k1, k2) == 0;
}

static void
hash_table_split (x_hash_table *h)
{
    x_list **new, **old;
    x_list *node, *item, *next;
    int new_size, old_size;
    size_t b;
    int i;

    if (h->bucket_index == N_BUCKET_SIZES - 1)
        return;

    old_size = hash_table_total_buckets (h);
    old = h->buckets;

    h->bucket_index++;

    new_size = hash_table_total_buckets (h);
    new = calloc (new_size, sizeof (x_list *));

    if (new == 0)
    {
        h->bucket_index--;
        return;
    }

    for (i = 0; i < old_size; i++)
    {
        for (node = old[i]; node != 0; node = next)
        {
            next = node->next;
            item = node->data;

            b = hash_table_hash_key (h, ITEM_KEY (item)) % new_size;

            node->next = new[b];
            new[b] = node;
        }
    }

    h->buckets = new;
    free (old);
}

X_EXTERN x_hash_table *
X_PFX (hash_table_new) (x_hash_fun *hash,
                        x_compare_fun *compare,
                        x_destroy_fun *key_destroy,
                        x_destroy_fun *value_destroy)
{
    x_hash_table *h;

    h = calloc (1, sizeof (x_hash_table));
    if (h == 0)
        return 0;

    h->bucket_index = 0;
    h->buckets = calloc (hash_table_total_buckets (h), sizeof (x_list *));

    if (h->buckets == 0)
    {
        free (h);
        return 0;
    }
    
    h->hash_key = hash;
    h->compare_keys = compare;
    h->destroy_key = key_destroy;
    h->destroy_value = value_destroy;

    return h;
}

X_EXTERN void
X_PFX (hash_table_free) (x_hash_table *h)
{
    int n, i;
    x_list *node, *item;

    assert (h != NULL);

    n = hash_table_total_buckets (h);

    for (i = 0; i < n; i++)
    {
        for (node = h->buckets[i]; node != 0; node = node->next)
        {
            item = node->data;
            hash_table_destroy_item (h, ITEM_KEY (item), ITEM_VALUE (item));
            ITEM_FREE (item);
        }
        X_PFX (list_free) (h->buckets[i]);
    }

    free (h->buckets);
    free (h);
}

X_EXTERN unsigned int
X_PFX (hash_table_size) (x_hash_table *h)
{
    assert (h != NULL);

    return h->total_keys;
}

static void
hash_table_modify (x_hash_table *h, void *k, void *v, int replace)
{
    size_t hash_value;
    x_list *node, *item;

    assert (h != NULL);

    hash_value = hash_table_hash_key (h, k);

    for (node = h->buckets[hash_value % hash_table_total_buckets (h)];
         node != 0; node = node->next)
    {
        item = node->data;

        if (hash_table_compare_keys (h, ITEM_KEY (item), k))
        {
            if (replace)
            {
                hash_table_destroy_item (h, ITEM_KEY (item),
                                         ITEM_VALUE (item));
                item->next = k;
                ITEM_VALUE (item) = v;
            }
            else
            {
                hash_table_destroy_item (h, k, ITEM_VALUE (item));
                ITEM_VALUE (item) = v;
            }
            return;
        }
    }

    /* Key isn't already in the table. Insert it. */

    if (h->total_keys + 1
        > hash_table_total_buckets (h) * SPLIT_THRESHOLD_FACTOR)
    {
        hash_table_split (h);
    }

    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));
    h->total_keys++;
}

X_EXTERN void
X_PFX (hash_table_insert) (x_hash_table *h, void *k, void *v)
{
    hash_table_modify (h, k, v, 0);
}

X_EXTERN void
X_PFX (hash_table_replace) (x_hash_table *h, void *k, void *v)
{
    hash_table_modify (h, k, v, 1);
}

X_EXTERN void
X_PFX (hash_table_remove) (x_hash_table *h, void *k)
{
    size_t hash_value;
    x_list **ptr, *item;

    assert (h != NULL);

    hash_value = hash_table_hash_key (h, k);

    for (ptr = &h->buckets[hash_value % hash_table_total_buckets (h)];
         *ptr != 0; ptr = &((*ptr)->next))
    {
        item = (*ptr)->data;

        if (hash_table_compare_keys (h, ITEM_KEY (item), k))
        {
            hash_table_destroy_item (h, ITEM_KEY (item), ITEM_VALUE (item));
            ITEM_FREE (item);
            item = *ptr;
            *ptr = item->next;
            X_PFX (list_free_1) (item);
            h->total_keys--;
            return;
        }
    }
}

X_EXTERN void *
X_PFX (hash_table_lookup) (x_hash_table *h, void *k, void **k_ret)
{
    size_t hash_value;
    x_list *node, *item;

    assert (h != NULL);

    hash_value = hash_table_hash_key (h, k);

    for (node = h->buckets[hash_value % hash_table_total_buckets (h)];
         node != 0; node = node->next)
    {
        item = node->data;

        if (hash_table_compare_keys (h, ITEM_KEY (item), k))
        {
            if (k_ret != 0)
            *k_ret = ITEM_KEY (item);

            return ITEM_VALUE (item);
        }
    }

    if (k_ret != 0)
        *k_ret = 0;

    return 0;
}

X_EXTERN void
X_PFX (hash_table_foreach) (x_hash_table *h,
                            x_hash_foreach_fun *fun, void *data)
{
    int i, n;
    x_list *node, *item;

    assert (h != NULL);

    n = hash_table_total_buckets (h);

    for (i = 0; i < n; i++)
    {
        for (node = h->buckets[i]; node != 0; node = node->next)
        {
            item = node->data;
            (*fun) (ITEM_KEY (item), ITEM_VALUE (item), data);
        }
    }
}