File: hash.c

package info (click to toggle)
s3d 0.2.2-10
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,544 kB
  • ctags: 5,243
  • sloc: ansic: 20,861; python: 488; perl: 98; makefile: 39; sh: 29
file content (323 lines) | stat: -rw-r--r-- 8,682 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * hash.h
 *
 * Copyright (C) 2004-2011  Simon Wunderlich <sw@simonwunderlich.de>
 * Copyright (C) 2004-2011  Marek Lindner <mareklindner@neomailbox.ch>
 *
 * This file is part of the s3d API, the API of s3d (the 3d network display server).
 * See http://s3d.berlios.de/ for more updates.
 *
 * The s3d API is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * The s3d API 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the s3d API; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


#include <stdio.h>  /* NULL */
#include <stdlib.h>  /* malloc(), free() */
#include "hash.h"


/* clears the hash */
void _s3d_hash_init(struct hashtable_t *hash)
{
	int i;
	hash->elements = 0;
	for (i = 0 ; i < hash->size ; i++) {
		hash->table[i] = NULL;
	}
}


/* remove the hash structure. if hashdata_free_cb != NULL,
 * this function will be called to remove the elements inside of the hash.
 * if you don't remove the elements, memory might be leaked. */
void _s3d_hash_delete(struct hashtable_t *hash, hashdata_free_cb free_cb)
{
	struct element_t *bucket, *last_bucket;
	int i;

	for (i = 0; i < hash->size; i++) {

		bucket = hash->table[i];
		while (bucket != NULL) {

			if (free_cb != NULL)
				free_cb(bucket->data);

			last_bucket = bucket;
			bucket = bucket->next;
			free(last_bucket);

		}

	}
	_s3d_hash_destroy(hash);
}



/* free only the hashtable and the hash itself. */
void _s3d_hash_destroy(struct hashtable_t *hash)
{

	free(hash->table);
	free(hash);

}




/* iterate though the hash. first element is selected with iter_in NULL.
 * use the returned iterator to access the elements until hash_it_t returns NULL. */
struct hash_it_t *_s3d_hash_iterate(struct hashtable_t *hash, struct hash_it_t *iter_in) {
	struct hash_it_t *iter;

	if (iter_in == NULL) {
		iter = (struct hash_it_t*)malloc(sizeof(struct hash_it_t));
		iter->index =  -1;
		iter->bucket = NULL;
		iter->prev_bucket = NULL;
	} else
		iter = iter_in;

	/* sanity checks first (if our bucket got deleted in the last iteration): */
	if (iter->bucket != NULL) {
		if (iter->first_bucket != NULL) {

			/* we're on the first element and it got removed after the last iteration. */
			if ((*iter->first_bucket) != iter->bucket) {

				/* there are still other elements in the list */
				if ((*iter->first_bucket) != NULL) {
					iter->prev_bucket = NULL;
					iter->bucket = (*iter->first_bucket);
					iter->first_bucket = &hash->table[ iter->index ];
					return iter;
				} else {
					iter->bucket = NULL;
				}

			}

		} else if (iter->prev_bucket != NULL) {

			/* we're not on the first element, and the bucket got removed after the last iteration.
			* the last bucket's next pointer is not pointing to our actual bucket anymore.
			* select the next. */
			if (iter->prev_bucket->next != iter->bucket)
				iter->bucket = iter->prev_bucket;

		}

	}

	/* now as we are sane, select the next one if there is some */
	if (iter->bucket != NULL) {
		if (iter->bucket->next != NULL) {
			iter->prev_bucket = iter->bucket;
			iter->bucket = iter->bucket->next;
			iter->first_bucket = NULL;
			return iter;
		}
	}
	/* if not returned yet, we've reached the last one on the index and have to search forward */

	iter->index++;
	while (iter->index < hash->size) {    /* go through the entries of the hash table */
		if ((hash->table[ iter->index ]) != NULL) {
			iter->prev_bucket = NULL;
			iter->bucket = hash->table[ iter->index ];
			iter->first_bucket = &hash->table[ iter->index ];
			return iter;      /* if this table entry is not null, return it */
		} else
			iter->index++;      /* else, go to the next */
	}
	/* nothing to iterate over anymore */
	free(iter);
	return NULL;
}


/* allocates and clears the hash */
struct hashtable_t *_s3d_hash_new(int size, hashdata_compare_cb compare, hashdata_choose_cb choose) {
	struct hashtable_t *hash;

	hash = (struct hashtable_t*)malloc(sizeof(struct hashtable_t));
	if (hash == NULL)      /* could not allocate the hash control structure */
		return NULL;

	hash->size = size;
	hash->table = (struct element_t **)malloc(sizeof(struct element_t *) * size);
	if (hash->table == NULL) {   /* could not allocate the table */
		free(hash);
		return NULL;
	}
	_s3d_hash_init(hash);
	hash->compare = compare;
	hash->choose = choose;
	return hash;
}


/* adds data to the hashtable. returns 0 on success, -1 on error */
int _s3d_hash_add(struct hashtable_t *hash, void *data)
{
	int index;
	struct element_t *bucket, *prev_bucket = NULL;

	index = hash->choose(data, hash->size);
	bucket = hash->table[index];

	while (bucket != NULL) {
		if (0 == hash->compare(bucket->data, data))
			return -1;

		prev_bucket = bucket;
		bucket = bucket->next;
	}

	/* found the tail of the list, add new element */
	if (NULL == (bucket = (struct element_t*)malloc(sizeof(struct element_t))))
		return -1; /* malloc failed */

	bucket->data = data;   /* init the new bucket */
	bucket->next = NULL;

	/* and link it */
	if (prev_bucket == NULL) {
		hash->table[index] = bucket;
	} else {
		prev_bucket->next = bucket;
	}

	hash->elements++;
	return 0;

}
/* finds data, based on the key in keydata. returns the found data on success, or NULL on error */
void *_s3d_hash_find(struct hashtable_t *hash, void *keydata)
{
	int index;
	struct element_t *bucket;

	index = hash->choose(keydata , hash->size);
	bucket = hash->table[index];

	while (bucket != NULL) {
		if (0 == hash->compare(bucket->data, keydata))
			return bucket->data;

		bucket = bucket->next;
	}

	return NULL;

}

/* remove bucket (this might be used in hash_iterate() if you already found the bucket
 * you want to delete and don't need the overhead to find it again with hash_remove().
 * But usually, you don't want to use this function, as it fiddles with hash-internals. */
void *_s3d_hash_remove_bucket(struct hashtable_t *hash, struct hash_it_t *hash_it_t)
{
	void *data_save;

	data_save = hash_it_t->bucket->data; /* save the pointer to the data */

	if (hash_it_t->prev_bucket != NULL) {
		hash_it_t->prev_bucket->next = hash_it_t->bucket->next;
	} else if (hash_it_t->first_bucket != NULL) {
		(*hash_it_t->first_bucket) = hash_it_t->bucket->next;
	}

	free(hash_it_t->bucket);

	hash->elements--;
	return data_save;

}


/* removes data from hash, if found. returns pointer do data on success,
 * so you can remove the used structure yourself, or NULL on error .
 * data could be the structure you use with just the key filled,
 * we just need the key for comparing. */
void *_s3d_hash_remove(struct hashtable_t *hash, void *data)
{
	struct hash_it_t hash_it_t;

	hash_it_t.index = hash->choose(data, hash->size);
	hash_it_t.bucket = hash->table[hash_it_t.index];
	hash_it_t.prev_bucket = NULL;

	while (hash_it_t.bucket != NULL) {
		if (0 == hash->compare(hash_it_t.bucket->data, data)) {
			hash_it_t.first_bucket = (hash_it_t.bucket == hash->table[hash_it_t.index] ? &hash->table[ hash_it_t.index ] : NULL);
			return _s3d_hash_remove_bucket(hash, &hash_it_t);
		}

		hash_it_t.prev_bucket = hash_it_t.bucket;
		hash_it_t.bucket = hash_it_t.bucket->next;
	}

	return NULL;

}


/* resize the hash, returns the pointer to the new hash or NULL on error. removes the old hash on success. */
struct hashtable_t *_s3d_hash_resize(struct hashtable_t *hash, int size) {
	struct hashtable_t *new_hash;
	struct element_t *bucket;
	int i;

	/* initialize a new hash with the new size */
	if (NULL == (new_hash = _s3d_hash_new(size, hash->compare, hash->choose)))
		return NULL;

	/* copy the elements */
	for (i = 0; i < hash->size; i++) {
		bucket = hash->table[i];
		while (bucket != NULL) {
			_s3d_hash_add(new_hash, bucket->data);
			bucket = bucket->next;
		}
	}
	_s3d_hash_delete(hash, NULL); /* remove hash and eventual overflow buckets but not the content itself. */

	return new_hash;
}


/* print the hash table for debugging */
void _s3d_hash_debug(struct hashtable_t *hash)
{
	int i;
	struct element_t *bucket;

	for (i = 0; i < hash->size; i++) {
		printf("[%d] ", i);
		bucket = hash->table[i];

		while (bucket != NULL) {
			printf("-> [%10p] ", (void *)bucket);
			bucket = bucket->next;
		}

		printf("\n");

	}
	printf("\n");
}