1706f2543Smrg/* x-hash.h -- basic hash table class
2706f2543Smrg
3706f2543Smrg   Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
4706f2543Smrg
5706f2543Smrg   Permission is hereby granted, free of charge, to any person
6706f2543Smrg   obtaining a copy of this software and associated documentation files
7706f2543Smrg   (the "Software"), to deal in the Software without restriction,
8706f2543Smrg   including without limitation the rights to use, copy, modify, merge,
9706f2543Smrg   publish, distribute, sublicense, and/or sell copies of the Software,
10706f2543Smrg   and to permit persons to whom the Software is furnished to do so,
11706f2543Smrg   subject to the following conditions:
12706f2543Smrg
13706f2543Smrg   The above copyright notice and this permission notice shall be
14706f2543Smrg   included in all copies or substantial portions of the Software.
15706f2543Smrg
16706f2543Smrg   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17706f2543Smrg   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18706f2543Smrg   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19706f2543Smrg   NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
20706f2543Smrg   HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21706f2543Smrg   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22706f2543Smrg   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23706f2543Smrg   DEALINGS IN THE SOFTWARE.
24706f2543Smrg
25706f2543Smrg   Except as contained in this notice, the name(s) of the above
26706f2543Smrg   copyright holders shall not be used in advertising or otherwise to
27706f2543Smrg   promote the sale, use or other dealings in this Software without
28706f2543Smrg   prior written authorization. */
29706f2543Smrg
30706f2543Smrg#ifndef X_HASH_H
31706f2543Smrg#define X_HASH_H 1
32706f2543Smrg
33706f2543Smrg#include <stdlib.h>
34706f2543Smrg#include <assert.h>
35706f2543Smrg
36706f2543Smrgtypedef struct x_hash_table_struct x_hash_table;
37706f2543Smrg
38706f2543Smrgtypedef int (x_compare_fun) (const void *a, const void *b);
39706f2543Smrgtypedef unsigned int (x_hash_fun) (const void *k);
40706f2543Smrgtypedef void (x_destroy_fun) (void *x);
41706f2543Smrgtypedef void (x_hash_foreach_fun) (void *k, void *v, void *data);
42706f2543Smrg
43706f2543Smrg/* for X_PFX and X_EXTERN */
44706f2543Smrg#include "x-list.h"
45706f2543Smrg
46706f2543SmrgX_EXTERN x_hash_table *X_PFX (hash_table_new) (x_hash_fun *hash,
47706f2543Smrg					       x_compare_fun *compare,
48706f2543Smrg					       x_destroy_fun *key_destroy,
49706f2543Smrg					       x_destroy_fun *value_destroy);
50706f2543SmrgX_EXTERN void X_PFX (hash_table_free) (x_hash_table *h);
51706f2543Smrg
52706f2543SmrgX_EXTERN unsigned int X_PFX (hash_table_size) (x_hash_table *h);
53706f2543Smrg
54706f2543SmrgX_EXTERN void X_PFX (hash_table_insert) (x_hash_table *h, void *k, void *v);
55706f2543SmrgX_EXTERN void X_PFX (hash_table_replace) (x_hash_table *h, void *k, void *v);
56706f2543SmrgX_EXTERN void X_PFX (hash_table_remove) (x_hash_table *h, void *k);
57706f2543SmrgX_EXTERN void *X_PFX (hash_table_lookup) (x_hash_table *h,
58706f2543Smrg					  void *k, void **k_ret);
59706f2543SmrgX_EXTERN void X_PFX (hash_table_foreach) (x_hash_table *h,
60706f2543Smrg					  x_hash_foreach_fun *fun,
61706f2543Smrg					  void *data);
62706f2543Smrg
63706f2543Smrg/* Conversion between unsigned int (e.g. xp_resource_id) and void pointer */
64706f2543Smrg
65706f2543Smrg/* Forward declarations */
66706f2543Smrgstatic __inline__ void *
67706f2543SmrgX_PFX (cvt_uint_to_vptr) (unsigned int val) __attribute__((always_inline));
68706f2543Smrgstatic __inline__ unsigned int
69706f2543SmrgX_PFX (cvt_vptr_to_uint) (void * val) __attribute__((always_inline));
70706f2543Smrg
71706f2543Smrg/* Implementations */
72706f2543Smrgstatic __inline__ void *
73706f2543SmrgX_PFX (cvt_uint_to_vptr) (unsigned int val)
74706f2543Smrg{
75706f2543Smrg	return (void*)((unsigned long)(val));
76706f2543Smrg}
77706f2543Smrg
78706f2543Smrgstatic __inline__ unsigned int
79706f2543SmrgX_PFX (cvt_vptr_to_uint) (void * val)
80706f2543Smrg{
81706f2543Smrg	size_t sv = (size_t)val;
82706f2543Smrg	unsigned int uv = (unsigned int)sv;
83706f2543Smrg
84706f2543Smrg	/* If this assert fails, chances are val actually is a pointer,
85706f2543Smrg	   or there's been memory corruption */
86706f2543Smrg	assert(sv == uv);
87706f2543Smrg
88706f2543Smrg	return uv;
89706f2543Smrg}
90706f2543Smrg
91706f2543Smrg#endif /* X_HASH_H */
92