Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
3
All rights reserved.
4
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are
7
met:
8
9
- Redistributions of source code must retain the above copyright
10
notice, this list of conditions and the following disclaimer.
11
12
- Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in
14
the documentation and/or other materials provided with the
15
distribution.
16
17
- Neither the name of The Numerical ALgorithms Group Ltd. nor the
18
names of its contributors may be used to endorse or promote products
19
derived from this software without specific prior written permission.
20
21
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34
35
#include "openaxiom-c-macros.h"
36
#define _HASH_C
37
#include "debug.h"
38
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <string.h>
42
43
#include "halloc.h"
44
#include "hash.h"
45
46
/* initialize a hash table */
47
48
void
49
hash_init(HashTable *table, int size, EqualFunction equal,
50
HashcodeFunction hash_code)
51
{
52
int i;
53
54
table->table =
55
(HashEntry **) halloc(size * sizeof(HashEntry *), "HashEntry");
56
for (i = 0; i < size; i++)
57
table->table[i] = NULL;
58
table->size = size;
59
table->equal = equal;
60
table->hash_code = hash_code;
61
table->num_entries = 0;
62
}
63
64
void
65
free_hash(HashTable* table, FreeFunction free_fun)
66
{
67
if (table) {
68
int i;
69
70
for (i = 0; i < table->size; i++) {
71
HashEntry *e, *next;
72
73
for (e = table->table[i]; e != NULL;) {
74
next = e->next;
75
(*free_fun) ((char*) e->data);
76
(*e).data=0;
77
free(e);
78
e = next;
79
}
80
}
81
free(table->table);
82
}
83
}
84
85
/* insert an entry into a hash table */
86
87
void
88
hash_insert(HashTable* table, char* data, const char *key)
89
{
90
HashEntry *entry = (HashEntry *) halloc(sizeof(HashEntry), "HashEntry");
91
int code;
92
93
entry->data = data;
94
entry->key = key;
95
code = (*table->hash_code) (key, table->size) % table->size;
96
#ifdef DEBUG
97
fprintf(stderr, "Hash value = %d\n", code);
98
#endif
99
entry->next = table->table[code];
100
table->table[code] = entry;
101
table->num_entries++;
102
}
103
104
char *
105
hash_find(HashTable* table, const char *key)
106
{
107
HashEntry *entry;
108
int code = table->hash_code(key, table->size) % table->size;
109
110
for (entry = table->table[code]; entry != NULL; entry = entry->next)
111
if ((*table->equal) (entry->key, key))
112
return entry->data;
113
return NULL;
114
}
115
116
char *
117
hash_replace(HashTable* table, char* data, const char* key)
118
{
119
HashEntry *entry;
120
int code = table->hash_code(key, table->size) % table->size;
121
122
for (entry = table->table[code]; entry != NULL; entry = entry->next)
123
if ((*table->equal) (entry->key, key)) {
124
entry->data = data;
125
return entry->data;
126
}
127
return NULL;
128
}
129
130
void
131
hash_delete(HashTable* table, const char* key)
132
{
133
HashEntry **entry;
134
int code = table->hash_code(key, table->size) % table->size;
135
136
for (entry = &table->table[code]; *entry != NULL; entry = &((*entry)->next))
137
if ((*table->equal) ((*entry)->key, key)) {
138
*entry = (*entry)->next;
139
table->num_entries--;
140
return;
141
}
142
}
143
144
void
145
hash_map(HashTable* table, MappableFunction func)
146
{
147
int i;
148
HashEntry *e;
149
150
if (table == NULL)
151
return;
152
for (i = 0; i < table->size; i++)
153
for (e = table->table[i]; e != NULL; e = e->next)
154
(*func) (e->data);
155
}
156
157
HashEntry *
158
hash_copy_entry(HashEntry *e)
159
{
160
HashEntry *ne;
161
162
if (e == NULL)
163
return e;
164
ne = (HashEntry *) halloc(sizeof(HashEntry), "HashEntry");
165
ne->data = e->data;
166
ne->key = e->key;
167
ne->next = hash_copy_entry(e->next);
168
return ne;
169
}
170
171
/* copy a hash table */
172
HashTable *
173
hash_copy_table(HashTable* table)
174
{
175
HashTable *nt = (HashTable *) halloc(sizeof(HashTable), "copy hash table");
176
int i;
177
178
nt->size = table->size;
179
nt->num_entries = table->num_entries;
180
nt->equal = table->equal;
181
nt->hash_code = table->hash_code;
182
nt->table = (HashEntry **) halloc(nt->size * sizeof(HashEntry *),
183
"copy table");
184
for (i = 0; i < table->size; i++)
185
nt->table[i] = hash_copy_entry(table->table[i]);
186
return nt;
187
}
188
189
/* hash code function for strings */
190
int
191
string_hash(const char* s, int size)
192
{
193
int c = 0;
194
const char *p =s;
195
196
197
while (*p)
198
c += *p++;
199
return c % size;
200
}
201
202
/* test strings for equality */
203
204
int
205
string_equal(const char* s1, const char* s2)
206
{
207
return (strcmp(s1, s2) == 0);
208
}
209
210
/* make a fresh copy of the given string */
211
char *
212
alloc_string(const char* str)
213
{
214
char * result;
215
result = halloc(strlen(str)+1,"alloc_string");
216
strcpy(result,str);
217
return (result);
218
}
219
220