root/trunk/fl_hash.c

Revision 17, 3.5 kB (checked in by shughes, 9 years ago)

whitespace... uses tabs not spaces

Line 
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 4                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2003 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 2.02 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available at through the world-wide-web at                           |
10   | http://www.php.net/license/2_02.txt.                                 |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Thies C. Arntzen <thies@php.net>                             |
16   |         Sterling Hughes <sterling@php.net>                           |
17   +----------------------------------------------------------------------+
18
19   $Id: fl_hash.c,v 1.1.1.1 2004/02/17 23:31:44 sterling Exp $
20 */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include "php_fastxsl.h"
25 #include "fl_hash.h"
26
27 FL_Hash *fl_hash_new(FL_Allocator *mems, FL_HashDtor dtor)
28 {
29         FL_Hash *table;
30
31         table = mems->calloc_func(1, sizeof(FL_Hash));
32         table->dtor = dtor;
33         table->mems = mems;
34
35         return table;
36 }
37
38 void fl_hash_free(FL_Hash *table)
39 {
40         FL_Bucket *cur;
41         FL_Bucket *next;
42         FL_Allocator *mems;
43         int     i;
44
45         mems = table->mems;
46        
47         for (i = 0; i < FL_HASH_SIZE; i++) {
48                 cur = table->buckets[i];
49                 while (cur) {
50                         next = cur->next;
51
52                         if (table->dtor) table->dtor(cur->data);
53                         mems->free_func(cur->key);
54                         mems->free_func(cur);
55
56                         cur = next;
57                 }
58         }
59         mems->free_func(table);
60 }
61
62 static unsigned int hash_hash(const char *key, int length)
63 {
64         unsigned int hash = 0;
65
66         while (--length)
67                 hash = hash * 65599 + *key++;
68
69         return hash;
70 }
71
72 void *fl_hash_find(FL_Hash *table, const void *key, int length)
73 {
74         FL_Bucket       *b;
75         unsigned int  hash = hash_hash(key, length) % FL_HASH_SIZE;
76
77         for (b = table->buckets[ hash ]; b; b = b->next) {
78                 if (hash != b->hash) continue;
79                 if (length != b->length) continue;
80                 if (memcmp(key, b->key, length)) continue;
81
82                 return b->data;
83         }
84
85         return NULL;
86 }
87
88 void fl_hash_add(FL_Hash *table, char *key, int length, void *data)
89 {
90         FL_Bucket       *b;
91         unsigned int  hash;
92        
93         hash = hash_hash(key, length) % FL_HASH_SIZE;
94
95         b = table->mems->malloc_func(sizeof(FL_Bucket));
96         b->key = table->mems->malloc_func(length + 1);
97         memcpy(b->key, key, length);
98         b->key[length] = 0;
99         b->length = length;
100         b->hash = hash;
101         b->data = data;
102
103         b->next = table->buckets[ hash ];
104
105         table->buckets[ hash ] = b;
106         table->nElements++;
107 }
108
109 void fl_hash_delete(FL_Hash *table, char *key, int length)
110 {
111         FL_Bucket       *b;
112         FL_Bucket       *prev = NULL;
113         unsigned int  hash;
114
115         hash = hash_hash(key, length) % FL_HASH_SIZE;
116         for (b = table->buckets[ hash ]; b; b = b->next) {
117                 if (hash != b->hash || length != b->length || memcmp(key, b->key, length)) {
118                         prev = b;
119                         continue;
120                 }
121
122                 /* unlink */
123                 if (prev) {
124                         prev->next = b->next;
125                 } else {
126                         table->buckets[hash] = b->next;
127                 }
128                
129                 if (table->dtor) table->dtor(b->data);
130                 table->mems->free_func(b->key);
131                 table->mems->free_func(b);
132
133                 break;
134         }
135 }
136
137 /*
138 Local variables:
139 tab-width: 4
140 c-basic-offset: 4
141 End:
142 vim600: noet sw=4 ts=4 fdm=marker
143 vim<600: noet sw=4 ts=4
144 */
Note: See TracBrowser for help on using the browser.