xref: /onnv-gate/usr/src/lib/libast/common/hash/hashlib.h (revision 4887:feebf9260c2e)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin /*
24*4887Schin  * Glenn Fowler
25*4887Schin  * AT&T Research
26*4887Schin  *
27*4887Schin  * hash table library private definitions
28*4887Schin  */
29*4887Schin 
30*4887Schin #ifndef _HASHLIB_H
31*4887Schin #define _HASHLIB_H
32*4887Schin 
33*4887Schin #include <ast.h>
34*4887Schin 
35*4887Schin #define hash_info	_hash_info_
36*4887Schin 
37*4887Schin typedef void*		(*Hash_alloc_f)(size_t);
38*4887Schin typedef int		(*Hash_compare_f)(const char*, const char*, ...);
39*4887Schin typedef unsigned int	(*Hash_hash_f)(const char*, ...);
40*4887Schin typedef void		(*Hash_free_f)(void*);
41*4887Schin typedef void*		(*Hash_region_f)(void*, void*, size_t, int);
42*4887Schin 
43*4887Schin typedef struct				/* root local pointers		*/
44*4887Schin {
45*4887Schin 	Hash_hash_f	hash;		/* name hash routine		*/
46*4887Schin 	Hash_compare_f	compare;	/* name comparision routine	*/
47*4887Schin 	Hash_alloc_f	alloc;		/* value allocation routine	*/
48*4887Schin 	Hash_free_f	free;		/* value free routine		*/
49*4887Schin 	Hash_region_f	region;		/* region alloc/free routine	*/
50*4887Schin 	void*		handle;		/* region handle arg		*/
51*4887Schin } Hash_local_t;
52*4887Schin 
53*4887Schin #define _HASH_POSITION_PRIVATE_ \
54*4887Schin 	Hash_table_t*	tab;		/* table pointer		*/ \
55*4887Schin 	int		flags;		/* scan flags			*/ \
56*4887Schin 	Hash_bucket_t**	slot;		/* table slot			*/ \
57*4887Schin 	Hash_bucket_t**	limit;		/* slot limit			*/
58*4887Schin 
59*4887Schin #define _HASH_LAST_PRIVATE_ \
60*4887Schin 	const char*	name;		/* last lookup name		*/ \
61*4887Schin 	unsigned int	hash;		/* last lookup hash		*/
62*4887Schin 
63*4887Schin #define _HASH_ROOT_PRIVATE_ \
64*4887Schin 	int		namesize;	/* fixed name size: 0 => string	*/ \
65*4887Schin 	int		meanchain;	/* resize mean chain length	*/ \
66*4887Schin 	Hash_local_t*	local;		/* root local pointers		*/ \
67*4887Schin 	Hash_root_t*	next;		/* next in list	of all roots	*/ \
68*4887Schin 	Hash_table_t*	references;	/* referencing table list	*/
69*4887Schin 
70*4887Schin #define _HASH_TABLE_PRIVATE_ \
71*4887Schin 	unsigned char	frozen;		/* table freeze nesting		*/ \
72*4887Schin 	unsigned char	bucketsize;	/* min bucket size in char*'s	*/ \
73*4887Schin 	Hash_bucket_t**	table;		/* hash slot table		*/ \
74*4887Schin 	Hash_table_t*	next;		/* root reference list link	*/
75*4887Schin 
76*4887Schin #include <hash.h>
77*4887Schin 
78*4887Schin #define HASHMINSIZE	(1<<4)		/* min table slots (power of 2)	*/
79*4887Schin #define HASHMEANCHAIN	2		/* def resize mean chain len	*/
80*4887Schin 
81*4887Schin #define HASHMOD(t,h)	(h &= (t->size - 1))
82*4887Schin #define HASHVAL(x)	((x)&~HASH_FLAGS)
83*4887Schin 
84*4887Schin #define HASH(r,n,h)	if (r->local->hash) h = r->namesize ? (*r->local->hash)(n, r->namesize) : (*r->local->hash)(n);\
85*4887Schin 			else\
86*4887Schin 			{\
87*4887Schin 				register const char*	_hash_s1 = n;\
88*4887Schin 				h = 0;\
89*4887Schin 				if (r->namesize)\
90*4887Schin 				{\
91*4887Schin 					register const char*	_hash_s2 = _hash_s1 + r->namesize;\
92*4887Schin 					while (_hash_s1 < _hash_s2) HASHPART(h, *_hash_s1++);\
93*4887Schin 				}\
94*4887Schin 				else while (*_hash_s1) HASHPART(h, *_hash_s1++);\
95*4887Schin 			}
96*4887Schin 
97*4887Schin typedef struct				/* library private info		*/
98*4887Schin {
99*4887Schin 	Hash_root_t*	list;		/* root table list		*/
100*4887Schin } Hash_info_t;
101*4887Schin 
102*4887Schin extern Hash_info_t	hash_info;
103*4887Schin 
104*4887Schin #endif
105