comparison src/protocols/jabber/xhash.c @ 3127:4e7cefc55971

[gaim-migrate @ 3142] Upgraded jabber to most recent stable version committer: Tailor Script <tailor@pidgin.im>
author Sean Egan <seanegan@gmail.com>
date Thu, 04 Apr 2002 03:04:57 +0000
parents
children
comparison
equal deleted inserted replaced
3126:e883f604174e 3127:4e7cefc55971
1 /* --------------------------------------------------------------------------
2 *
3 * License
4 *
5 * The contents of this file are subject to the Jabber Open Source License
6 * Version 1.0 (the "JOSL"). You may not copy or use this file, in either
7 * source code or executable form, except in compliance with the JOSL. You
8 * may obtain a copy of the JOSL at http://www.jabber.org/ or at
9 * http://www.opensource.org/.
10 *
11 * Software distributed under the JOSL is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the JOSL
13 * for the specific language governing rights and limitations under the
14 * JOSL.
15 *
16 * Copyrights
17 *
18 * Portions created by or assigned to Jabber.com, Inc. are
19 * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact
20 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
21 *
22 * Portions Copyright (c) 1998-1999 Jeremie Miller.
23 *
24 * Acknowledgements
25 *
26 * Special thanks to the Jabber Open Source Contributors for their
27 * suggestions and support of Jabber.
28 *
29 * Alternatively, the contents of this file may be used under the terms of the
30 * GNU General Public License Version 2 or later (the "GPL"), in which case
31 * the provisions of the GPL are applicable instead of those above. If you
32 * wish to allow use of your version of this file only under the terms of the
33 * GPL and not to allow others to use your version of this file under the JOSL,
34 * indicate your decision by deleting the provisions above and replace them
35 * with the notice and other provisions required by the GPL. If you do not
36 * delete the provisions above, a recipient may use your version of this file
37 * under either the JOSL or the GPL.
38 *
39 *
40 * --------------------------------------------------------------------------*/
41
42 #include "lib.h"
43
44
45 /* Generates a hash code for a string.
46 * This function uses the ELF hashing algorithm as reprinted in
47 * Andrew Binstock, "Hashing Rehashed," Dr. Dobb's Journal, April 1996.
48 */
49 int _xhasher(const char *s)
50 {
51 /* ELF hash uses unsigned chars and unsigned arithmetic for portability */
52 const unsigned char *name = (const unsigned char *)s;
53 unsigned long h = 0, g;
54
55 while (*name)
56 { /* do some fancy bitwanking on the string */
57 h = (h << 4) + (unsigned long)(*name++);
58 if ((g = (h & 0xF0000000UL))!=0)
59 h ^= (g >> 24);
60 h &= ~g;
61
62 }
63
64 return (int)h;
65 }
66
67
68 xhn _xhash_node_new(xht h, int index)
69 {
70 xhn n;
71 int i = index % h->prime;
72
73 /* get existing empty one */
74 for(n = &h->zen[i]; n != NULL; n = n->next)
75 if(n->key == NULL)
76 return n;
77
78 /* overflowing, new one! */
79 n = pmalloco(h->p, sizeof(_xhn));
80 n->next = h->zen[i].next;
81 h->zen[i].next = n;
82 return n;
83 }
84
85
86 xhn _xhash_node_get(xht h, const char *key, int index)
87 {
88 xhn n;
89 int i = index % h->prime;
90 for(n = &h->zen[i]; n != NULL; n = n->next)
91 if(j_strcmp(key, n->key) == 0)
92 return n;
93 return NULL;
94 }
95
96
97 xht xhash_new(int prime)
98 {
99 xht xnew;
100 pool p;
101
102 /* log_debug(ZONE,"creating new hash table of size %d",prime); */
103
104 p = pool_heap(sizeof(_xhn)*prime + sizeof(_xht));
105 xnew = pmalloco(p, sizeof(_xht));
106 xnew->prime = prime;
107 xnew->p = p;
108 xnew->zen = pmalloco(p, sizeof(_xhn)*prime); /* array of xhn size of prime */
109 return xnew;
110 }
111
112
113 void xhash_put(xht h, const char *key, void *val)
114 {
115 int index;
116 xhn n;
117
118 if(h == NULL || key == NULL)
119 return;
120
121 index = _xhasher(key);
122
123 /* if existing key, replace it */
124 if((n = _xhash_node_get(h, key, index)) != NULL)
125 {
126 /* log_debug(ZONE,"replacing %s with new val %X",key,val); */
127
128 n->key = key;
129 n->val = val;
130 return;
131 }
132
133 /* log_debug(ZONE,"saving %s val %X",key,val); */
134
135 /* new node */
136 n = _xhash_node_new(h, index);
137 n->key = key;
138 n->val = val;
139 }
140
141
142 void *xhash_get(xht h, const char *key)
143 {
144 xhn n;
145
146 if(h == NULL || key == NULL || (n = _xhash_node_get(h, key, _xhasher(key))) == NULL)
147 {
148 /* log_debug(ZONE,"failed lookup of %s",key); */
149 return NULL;
150 }
151
152 /* log_debug(ZONE,"found %s returning %X",key,n->val); */
153 return n->val;
154 }
155
156
157 void xhash_zap(xht h, const char *key)
158 {
159 xhn n;
160
161 if(h == NULL || key == NULL || (n = _xhash_node_get(h, key, _xhasher(key))) == NULL)
162 return;
163
164 /* log_debug(ZONE,"zapping %s",key); */
165
166 /* kill an entry by zeroing out the key */
167 n->key = NULL;
168 }
169
170
171 void xhash_free(xht h)
172 {
173 /* log_debug(ZONE,"hash free %X",h); */
174
175 if(h != NULL)
176 pool_free(h->p);
177 }
178
179 void xhash_walk(xht h, xhash_walker w, void *arg)
180 {
181 int i;
182 xhn n;
183
184 if(h == NULL || w == NULL)
185 return;
186
187 /* log_debug(ZONE,"walking %X",h); */
188
189 for(i = 0; i < h->prime; i++)
190 for(n = &h->zen[i]; n != NULL; n = n->next)
191 if(n->key != NULL && n->val != NULL)
192 (*w)(h, n->key, n->val, arg);
193 }
194