File: chash.c

package info (click to toggle)
chpp 0.3.4-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,228 kB
  • ctags: 5,154
  • sloc: ansic: 30,186; cpp: 575; sh: 500; makefile: 314; yacc: 265; asm: 261; lex: 64
file content (300 lines) | stat: -rw-r--r-- 5,946 bytes parent folder | download | duplicates (3)
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
/* -*- c -*- */

/*
 * chash.c
 *
 * chpp
 *
 * Copyright (C) 1995-1997 Herbert Poetzl
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <string.h>
#include <stdlib.h>

#include "memory.h"

#include "chash.h"

#ifndef TRUE
#define	TRUE 	(0==0)
#endif
#ifndef FALSE
#define	FALSE	(1==0)
#endif

#define	DELETED	((char *)-1)


char   *_copy_string(const char *string)
{
	char *copy = NULL;
	if (string) {
	    int len = strlen(string);
	    copy = (char *)memXAllocAtomic(len+1);
	    if (copy) {
	    	memcpy(copy, string, len+1);
	    }
	}
	return copy;
}


long	_hashcode_ascii(const char *key)
{  
	long h, c;
	h = 0;
	while ((c = *key++)) 
	{
	    h += c + (c<<5) + (h<<3);
	}
	return h;
}


struct 	hash_entry *_hash_find(hash table, const char *key)
{
	long pos, hashcode;
	struct hash_entry *place, *start;
	hashcode = _hashcode_ascii(key);
	pos = (table->mask & hashcode);
	place = &(table->data[pos]);
	start = place;
	while (place->key != NULL) {
	    if (hashcode == place->hashcode) {
		if (place->key != DELETED) {
		    if (strcmp(key, place->key) == 0) {
			return place;
		    }
		}
	    }
	    place++;
	    if (place == table->wall) place = table->data;
	    if (place == start) break;
	}
	return NULL;
}


void	_hash_place(hash table, struct hash_entry *entry, int check)
{
	long pos;
	struct hash_entry *place;
	pos = (table->mask & entry->hashcode);
	place = &(table->data[pos]);
	while (place->key != NULL) {
	    if (place->key == DELETED) break;
	    if (check) {
	    	if (entry->hashcode == place->hashcode) {
		    if (strcmp(entry->key, place->key) == 0) {
			place->value = entry->value;
			return;
		    }
		}
	    }
	    place++;
	    if (place == table->wall) place = table->data;
	}
	table->entries++;
	memcpy(place, entry, sizeof(struct hash_entry));
	return;
}

int	_hash_grow(hash table)
{
	struct hash_control *new;
	new = hash_new(2<<table->sizelog);
	if (new) {
	    long i,n;
	    for (i=0, n=1<<table->sizelog; i<n; i++) {
		if (table->data[i].key != NULL)
		    _hash_place(new, &(table->data[i]), FALSE); 
	    }
	    memFree(table->data);
	    memcpy(table, new, sizeof(struct hash_control));
	    memFree(new);			
	    return (TRUE);
	}
	return (FALSE);
}

int	_hash_cramped(hash table)
{
	long bound;
	bound = 101*(1 << table->sizelog)/143;
	if (table->entries > bound) return (TRUE);	
	return (FALSE);
}


hash	hash_new(long capacity)
{
	struct hash_control *new;
	long n,s;
	new = (struct hash_control *)
	    memXAlloc(sizeof(struct hash_control));
	if (new) {
	    for (n=0,s=1; s<capacity; n++,s <<= 1);
	    new->sizelog = n;
	    new->mask = s-1;
	    new->memsize = sizeof(struct hash_entry)*s;
	    new->data = (struct hash_entry *)
		memXAlloc(new->memsize);
	    if (new->data) {
	    	new->wall = new->data+s;
		hash_empty(new);
	    }
	    else {
	    	memFree(new);
		new = NULL;
	    }
	}	
	return (hash)new;
}

void	hash_free(hash table)
{
	if (table) {
	    memFree(table->data);
	    memFree(table);
	}
	return;
}

void	hash_empty(hash table)
{
	table->entries = 0;
	memset(table->data, 0, table->memsize);
	return;
}


int	hash_inject(hash table, const char *key, void *value)
{
	if (table && key) {
	    struct hash_entry place;
	    if (_hash_cramped(table)) {
		_hash_grow(table);
	    }
	    place.key = (char *)key;
	    place.value = value;
	    place.hashcode = _hashcode_ascii(key);
	    _hash_place(table, &place, (FALSE));
	    return (TRUE);
	}
	return (FALSE);
}

int	hash_insert(hash table, const char *key, void *value)
{
	if (table && key) {
	    struct hash_entry place;
	    if (_hash_cramped(table)) {
		_hash_grow(table);
	    }
	    place.key = _copy_string(key);
	    place.value = value;
	    place.hashcode = _hashcode_ascii(key);
	    _hash_place(table, &place, (TRUE));
	    return (TRUE);
	}
	return (FALSE);
}


void *	hash_replace(hash table, const char *key, void *value)
{
	void *oldvalue;
	struct hash_entry *entry;
	if (key) {
	    entry = _hash_find(table, key);
	    if (entry) {
	    	oldvalue = entry->value;
		entry->value = value;
		return oldvalue;
	    }
	}
	return NULL;	
}

void *	hash_delete(hash table, const char *key)
{
	void *oldvalue = NULL;
	struct hash_entry *entry;
	if (key) {
	    entry = _hash_find(table, key);
	    if (entry) {
		oldvalue = entry->value;
		memFree(entry->key);
		entry->key = DELETED;
		table->entries--;
	    }
	}
	return oldvalue;
}


void *	hash_lookup(hash table, const char *key)
{
	struct hash_entry *entry;
	if (key) {
	    entry = _hash_find(table, key);
	    if (entry) {
		return entry->value;
	    }
	}
	return NULL;
}


hstate	hash_state(hash table)
{
	hstate state;
	state.hashpos = 0;
	state.table = table;
	state.sflags.active = TRUE;
	return state;
}

void *	hash_next(hstate *state, char **key)
{
	long pos;
	char *lkey;
	void *value = NULL;
	if (state) {
	    hash table = state->table;
	    if (state->sflags.active) { 
		while ((pos = state->hashpos++) 
		    < (1 << table->sizelog)) {
		    lkey = table->data[pos].key;
		    if ((lkey != DELETED) && (lkey)) {
			value = table->data[pos].value;
			if (key) *key = lkey;
			break;
		    } 
		}
		if (pos == (1 << table->sizelog)) 
		    state->sflags.active = FALSE; 
	    }
	}
	return value;
}

int	hash_count(hash table)
{
	if (table) return table->entries;
	return 0;
}