1*0a6a1f1dSLionel Sambuc /* $NetBSD: hash.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc /*
37ebfedea0SLionel Sambuc * Hash table functions
38ebfedea0SLionel Sambuc */
39ebfedea0SLionel Sambuc
40ebfedea0SLionel Sambuc #include "gen_locl.h"
41ebfedea0SLionel Sambuc
42*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
43ebfedea0SLionel Sambuc
44ebfedea0SLionel Sambuc static Hashentry *_search(Hashtab * htab, /* The hash table */
45ebfedea0SLionel Sambuc void *ptr); /* And key */
46ebfedea0SLionel Sambuc
47ebfedea0SLionel Sambuc Hashtab *
hashtabnew(int sz,int (* cmp)(void *,void *),unsigned (* hash)(void *))48ebfedea0SLionel Sambuc hashtabnew(int sz,
49ebfedea0SLionel Sambuc int (*cmp) (void *, void *),
50ebfedea0SLionel Sambuc unsigned (*hash) (void *))
51ebfedea0SLionel Sambuc {
52ebfedea0SLionel Sambuc Hashtab *htab;
53ebfedea0SLionel Sambuc int i;
54ebfedea0SLionel Sambuc
55ebfedea0SLionel Sambuc assert(sz > 0);
56ebfedea0SLionel Sambuc
57ebfedea0SLionel Sambuc htab = (Hashtab *) malloc(sizeof(Hashtab) + (sz - 1) * sizeof(Hashentry *));
58ebfedea0SLionel Sambuc if (htab == NULL)
59ebfedea0SLionel Sambuc return NULL;
60ebfedea0SLionel Sambuc
61ebfedea0SLionel Sambuc for (i = 0; i < sz; ++i)
62ebfedea0SLionel Sambuc htab->tab[i] = NULL;
63ebfedea0SLionel Sambuc
64ebfedea0SLionel Sambuc htab->cmp = cmp;
65ebfedea0SLionel Sambuc htab->hash = hash;
66ebfedea0SLionel Sambuc htab->sz = sz;
67ebfedea0SLionel Sambuc return htab;
68ebfedea0SLionel Sambuc }
69ebfedea0SLionel Sambuc
70ebfedea0SLionel Sambuc /* Intern search function */
71ebfedea0SLionel Sambuc
72ebfedea0SLionel Sambuc static Hashentry *
_search(Hashtab * htab,void * ptr)73ebfedea0SLionel Sambuc _search(Hashtab * htab, void *ptr)
74ebfedea0SLionel Sambuc {
75ebfedea0SLionel Sambuc Hashentry *hptr;
76ebfedea0SLionel Sambuc
77ebfedea0SLionel Sambuc assert(htab && ptr);
78ebfedea0SLionel Sambuc
79ebfedea0SLionel Sambuc for (hptr = htab->tab[(*htab->hash) (ptr) % htab->sz];
80ebfedea0SLionel Sambuc hptr;
81ebfedea0SLionel Sambuc hptr = hptr->next)
82ebfedea0SLionel Sambuc if ((*htab->cmp) (ptr, hptr->ptr) == 0)
83ebfedea0SLionel Sambuc break;
84ebfedea0SLionel Sambuc return hptr;
85ebfedea0SLionel Sambuc }
86ebfedea0SLionel Sambuc
87ebfedea0SLionel Sambuc /* Search for element in hash table */
88ebfedea0SLionel Sambuc
89ebfedea0SLionel Sambuc void *
hashtabsearch(Hashtab * htab,void * ptr)90ebfedea0SLionel Sambuc hashtabsearch(Hashtab * htab, void *ptr)
91ebfedea0SLionel Sambuc {
92ebfedea0SLionel Sambuc Hashentry *tmp;
93ebfedea0SLionel Sambuc
94ebfedea0SLionel Sambuc tmp = _search(htab, ptr);
95ebfedea0SLionel Sambuc return tmp ? tmp->ptr : tmp;
96ebfedea0SLionel Sambuc }
97ebfedea0SLionel Sambuc
98ebfedea0SLionel Sambuc /* add element to hash table */
99ebfedea0SLionel Sambuc /* if already there, set new value */
100ebfedea0SLionel Sambuc /* !NULL if succesful */
101ebfedea0SLionel Sambuc
102ebfedea0SLionel Sambuc void *
hashtabadd(Hashtab * htab,void * ptr)103ebfedea0SLionel Sambuc hashtabadd(Hashtab * htab, void *ptr)
104ebfedea0SLionel Sambuc {
105ebfedea0SLionel Sambuc Hashentry *h = _search(htab, ptr);
106ebfedea0SLionel Sambuc Hashentry **tabptr;
107ebfedea0SLionel Sambuc
108ebfedea0SLionel Sambuc assert(htab && ptr);
109ebfedea0SLionel Sambuc
110ebfedea0SLionel Sambuc if (h)
111ebfedea0SLionel Sambuc free((void *) h->ptr);
112ebfedea0SLionel Sambuc else {
113ebfedea0SLionel Sambuc h = (Hashentry *) malloc(sizeof(Hashentry));
114ebfedea0SLionel Sambuc if (h == NULL) {
115ebfedea0SLionel Sambuc return NULL;
116ebfedea0SLionel Sambuc }
117ebfedea0SLionel Sambuc tabptr = &htab->tab[(*htab->hash) (ptr) % htab->sz];
118ebfedea0SLionel Sambuc h->next = *tabptr;
119ebfedea0SLionel Sambuc *tabptr = h;
120ebfedea0SLionel Sambuc h->prev = tabptr;
121ebfedea0SLionel Sambuc if (h->next)
122ebfedea0SLionel Sambuc h->next->prev = &h->next;
123ebfedea0SLionel Sambuc }
124ebfedea0SLionel Sambuc h->ptr = ptr;
125ebfedea0SLionel Sambuc return h;
126ebfedea0SLionel Sambuc }
127ebfedea0SLionel Sambuc
128ebfedea0SLionel Sambuc /* delete element with key key. Iff freep, free Hashentry->ptr */
129ebfedea0SLionel Sambuc
130ebfedea0SLionel Sambuc int
_hashtabdel(Hashtab * htab,void * ptr,int freep)131ebfedea0SLionel Sambuc _hashtabdel(Hashtab * htab, void *ptr, int freep)
132ebfedea0SLionel Sambuc {
133ebfedea0SLionel Sambuc Hashentry *h;
134ebfedea0SLionel Sambuc
135ebfedea0SLionel Sambuc assert(htab && ptr);
136ebfedea0SLionel Sambuc
137ebfedea0SLionel Sambuc h = _search(htab, ptr);
138ebfedea0SLionel Sambuc if (h) {
139ebfedea0SLionel Sambuc if (freep)
140ebfedea0SLionel Sambuc free(h->ptr);
141ebfedea0SLionel Sambuc if ((*(h->prev) = h->next))
142ebfedea0SLionel Sambuc h->next->prev = h->prev;
143ebfedea0SLionel Sambuc free(h);
144ebfedea0SLionel Sambuc return 0;
145ebfedea0SLionel Sambuc } else
146ebfedea0SLionel Sambuc return -1;
147ebfedea0SLionel Sambuc }
148ebfedea0SLionel Sambuc
149ebfedea0SLionel Sambuc /* Do something for each element */
150ebfedea0SLionel Sambuc
151ebfedea0SLionel Sambuc void
hashtabforeach(Hashtab * htab,int (* func)(void * ptr,void * arg),void * arg)152ebfedea0SLionel Sambuc hashtabforeach(Hashtab * htab, int (*func) (void *ptr, void *arg),
153ebfedea0SLionel Sambuc void *arg)
154ebfedea0SLionel Sambuc {
155ebfedea0SLionel Sambuc Hashentry **h, *g;
156ebfedea0SLionel Sambuc
157ebfedea0SLionel Sambuc assert(htab);
158ebfedea0SLionel Sambuc
159ebfedea0SLionel Sambuc for (h = htab->tab; h < &htab->tab[htab->sz]; ++h)
160ebfedea0SLionel Sambuc for (g = *h; g; g = g->next)
161ebfedea0SLionel Sambuc if ((*func) (g->ptr, arg))
162ebfedea0SLionel Sambuc return;
163ebfedea0SLionel Sambuc }
164ebfedea0SLionel Sambuc
165ebfedea0SLionel Sambuc /* standard hash-functions for strings */
166ebfedea0SLionel Sambuc
167ebfedea0SLionel Sambuc unsigned
hashadd(const char * s)168ebfedea0SLionel Sambuc hashadd(const char *s)
169ebfedea0SLionel Sambuc { /* Standard hash function */
170ebfedea0SLionel Sambuc unsigned i;
171ebfedea0SLionel Sambuc
172ebfedea0SLionel Sambuc assert(s);
173ebfedea0SLionel Sambuc
174ebfedea0SLionel Sambuc for (i = 0; *s; ++s)
175ebfedea0SLionel Sambuc i += *s;
176ebfedea0SLionel Sambuc return i;
177ebfedea0SLionel Sambuc }
178ebfedea0SLionel Sambuc
179ebfedea0SLionel Sambuc unsigned
hashcaseadd(const char * s)180ebfedea0SLionel Sambuc hashcaseadd(const char *s)
181ebfedea0SLionel Sambuc { /* Standard hash function */
182ebfedea0SLionel Sambuc unsigned i;
183ebfedea0SLionel Sambuc
184ebfedea0SLionel Sambuc assert(s);
185ebfedea0SLionel Sambuc
186ebfedea0SLionel Sambuc for (i = 0; *s; ++s)
187ebfedea0SLionel Sambuc i += toupper((unsigned char)*s);
188ebfedea0SLionel Sambuc return i;
189ebfedea0SLionel Sambuc }
190ebfedea0SLionel Sambuc
191ebfedea0SLionel Sambuc #define TWELVE (sizeof(unsigned))
192ebfedea0SLionel Sambuc #define SEVENTYFIVE (6*sizeof(unsigned))
193ebfedea0SLionel Sambuc #define HIGH_BITS (~((unsigned)(~0) >> TWELVE))
194ebfedea0SLionel Sambuc
195ebfedea0SLionel Sambuc unsigned
hashjpw(const char * ss)196ebfedea0SLionel Sambuc hashjpw(const char *ss)
197ebfedea0SLionel Sambuc { /* another hash function */
198ebfedea0SLionel Sambuc unsigned h = 0;
199ebfedea0SLionel Sambuc unsigned g;
200ebfedea0SLionel Sambuc const unsigned char *s = (const unsigned char *)ss;
201ebfedea0SLionel Sambuc
202ebfedea0SLionel Sambuc for (; *s; ++s) {
203ebfedea0SLionel Sambuc h = (h << TWELVE) + *s;
204ebfedea0SLionel Sambuc if ((g = h & HIGH_BITS))
205ebfedea0SLionel Sambuc h = (h ^ (g >> SEVENTYFIVE)) & ~HIGH_BITS;
206ebfedea0SLionel Sambuc }
207ebfedea0SLionel Sambuc return h;
208ebfedea0SLionel Sambuc }
209