-
Notifications
You must be signed in to change notification settings - Fork 0
/
fl_hash.c
150 lines (125 loc) · 3.65 KB
/
fl_hash.c
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
/*
+----------------------------------------------------------------------+
| PHP Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2003 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Thies C. Arntzen <thies@php.net> |
| Sterling Hughes <sterling@php.net> |
+----------------------------------------------------------------------+
$Id: fl_hash.c,v 1.1.1.1 2004/02/17 23:31:44 sterling Exp $
*/
#include <stdlib.h>
#include <string.h>
#include "php_fastxsl.h"
#include "fl_hash.h"
FL_Hash *fl_hash_new(FL_Allocator *mems, FL_HashDtor dtor)
{
FL_Hash *table;
table = mems->calloc_func(1, sizeof(FL_Hash));
table->dtor = dtor;
table->mems = mems;
return table;
}
void fl_hash_free(FL_Hash *table)
{
FL_Bucket *cur;
FL_Bucket *next;
FL_Allocator *mems;
int i;
mems = table->mems;
for (i = 0; i < FL_HASH_SIZE; i++) {
cur = table->buckets[i];
while (cur) {
next = cur->next;
if (table->dtor) table->dtor(cur->data);
mems->free_func(cur->key);
mems->free_func(cur);
cur = next;
}
}
mems->free_func(table);
}
static unsigned int hash_hash(const char *key, int length)
{
unsigned int hash = 0;
while (--length)
hash = hash * 65599 + *key++;
return hash;
}
void *fl_hash_find(FL_Hash *table, const void *key, int length)
{
FL_Bucket *b;
unsigned int hash = hash_hash(key, length) % FL_HASH_SIZE;
for (b = table->buckets[ hash ]; b; b = b->next) {
if (hash != b->hash) continue;
if (length != b->length) continue;
if (memcmp(key, b->key, length)) continue;
return b->data;
}
return NULL;
}
int fl_hash_add(FL_Hash *table, char *key, int length, void *data)
{
FL_Bucket *b, *ptr;
unsigned int hash;
hash = hash_hash(key, length) % FL_HASH_SIZE;
/* check hash for dupes */
ptr = table->buckets[ hash ];
while(ptr) {
if(!strcmp(ptr->key, key)) {
return 0;
}
ptr = ptr->next;
}
b = table->mems->malloc_func(sizeof(FL_Bucket));
b->key = table->mems->malloc_func(length + 1);
memcpy(b->key, key, length);
b->key[length] = 0;
b->length = length;
b->hash = hash;
b->data = data;
b->next = table->buckets[ hash ];
table->buckets[ hash ] = b;
table->nElements++;
return 1;
}
void fl_hash_delete(FL_Hash *table, char *key, int length)
{
FL_Bucket *b;
FL_Bucket *prev = NULL;
unsigned int hash;
hash = hash_hash(key, length) % FL_HASH_SIZE;
for (b = table->buckets[ hash ]; b; b = b->next) {
if (hash != b->hash || length != b->length || memcmp(key, b->key, length)) {
prev = b;
continue;
}
/* unlink */
if (prev) {
prev->next = b->next;
} else {
table->buckets[hash] = b->next;
}
if (table->dtor) table->dtor(b->data);
table->mems->free_func(b->key);
table->mems->free_func(b);
break;
}
}
/*
Local variables:
tab-width: 4
c-basic-offset: 4
End:
vim600: noet sw=4 ts=4 fdm=marker
vim<600: noet sw=4 ts=4
*/