@@ -64,7 +64,7 @@ size_t hash( unsigned int a)
64
64
hashtable_int *hashtableCreate (size_t size, size_t (*hashfunc)(unsigned int ))
65
65
{
66
66
hashtable_int *hashtbl;
67
-
67
+
68
68
if (size < 16 )
69
69
{
70
70
size = 16 ;
@@ -73,25 +73,25 @@ hashtable_int *hashtableCreate(size_t size, size_t (*hashfunc)(unsigned int))
73
73
{
74
74
size = (size_t )next_power_of_two ((unsigned int )size);
75
75
}
76
-
77
- hashtbl=(hashtable_int*)malloc (sizeof (hashtable_int));
76
+
77
+ hashtbl=(hashtable_int*)malloc (sizeof (hashtable_int));
78
78
if (!hashtbl)
79
79
return NULL ;
80
-
81
- hashtbl->nodes =(hashnode_i**)calloc (size, sizeof (struct hashnode_i *));
80
+
81
+ hashtbl->nodes =(hashnode_i**)calloc (size, sizeof (struct hashnode_i *));
82
82
if (!hashtbl->nodes )
83
83
{
84
84
free (hashtbl);
85
85
return NULL ;
86
86
}
87
-
87
+
88
88
hashtbl->size =size;
89
-
89
+
90
90
if (hashfunc)
91
91
hashtbl->hashfunc =hashfunc;
92
92
else
93
93
hashtbl->hashfunc =hash;
94
-
94
+
95
95
return hashtbl;
96
96
}
97
97
@@ -100,7 +100,7 @@ void hashtableDestroy(hashtable_int *hashtbl)
100
100
{
101
101
size_t n;
102
102
struct hashnode_i *node, *oldnode;
103
-
103
+
104
104
for (n=0 ; n<hashtbl->size ; ++n)
105
105
{
106
106
node=hashtbl->nodes [n];
@@ -120,10 +120,10 @@ int hashtableInsert(hashtable_int *hashtbl, KeyType key, void *data)
120
120
{
121
121
struct hashnode_i *node;
122
122
size_t hash=hashtbl->hashfunc (key)%hashtbl->size ;
123
-
124
-
123
+
124
+
125
125
/* fpruintf(stderr, "hashtbl_insert() key=%s, hash=%d, data=%s\n", key, hash, (char*)data);*/
126
-
126
+
127
127
node=hashtbl->nodes [hash];
128
128
while (node)
129
129
{
@@ -134,29 +134,29 @@ int hashtableInsert(hashtable_int *hashtbl, KeyType key, void *data)
134
134
}
135
135
node=node->next ;
136
136
}
137
-
138
-
139
- node=(hashnode_i*)malloc (sizeof (struct hashnode_i ));
137
+
138
+
139
+ node=(hashnode_i*)malloc (sizeof (struct hashnode_i ));
140
140
if (!node)
141
141
return -1 ;
142
142
node->key =key;
143
-
143
+
144
144
node->data =data;
145
145
node->next =hashtbl->nodes [hash];
146
146
hashtbl->nodes [hash]=node;
147
-
148
-
147
+
148
+
149
149
return 0 ;
150
150
}
151
151
152
152
int hashtableInsertHashed (hashtable_int *hashtbl, KeyType key, void *data)
153
153
{
154
154
struct hashnode_i *node;
155
155
size_t hash = key % hashtbl->size ;
156
-
157
-
156
+
157
+
158
158
/* fpruintf(stderr, "hashtbl_insert() key=%s, hash=%d, data=%s\n", key, hash, (char*)data);*/
159
-
159
+
160
160
node=hashtbl->nodes [hash];
161
161
while (node)
162
162
{
@@ -167,18 +167,18 @@ int hashtableInsertHashed(hashtable_int *hashtbl, KeyType key, void *data)
167
167
}
168
168
node=node->next ;
169
169
}
170
-
171
- node=(hashnode_i*)malloc (sizeof (struct hashnode_i ));
170
+
171
+ node=(hashnode_i*)malloc (sizeof (struct hashnode_i ));
172
172
if (!node)
173
173
return -1 ;
174
-
174
+
175
175
node->key =key;
176
-
176
+
177
177
node->data =data;
178
178
node->next =hashtbl->nodes [hash];
179
179
hashtbl->nodes [hash]=node;
180
-
181
-
180
+
181
+
182
182
return 0 ;
183
183
}
184
184
@@ -187,7 +187,7 @@ int hashtableRemove(hashtable_int *hashtbl, KeyType key)
187
187
{
188
188
struct hashnode_i *node, *prevnode=NULL ;
189
189
size_t hash=hashtbl->hashfunc (key)%hashtbl->size ;
190
-
190
+
191
191
node=hashtbl->nodes [hash];
192
192
while (node)
193
193
{
@@ -203,7 +203,7 @@ int hashtableRemove(hashtable_int *hashtbl, KeyType key)
203
203
prevnode=node;
204
204
node=node->next ;
205
205
}
206
-
206
+
207
207
return -1 ;
208
208
}
209
209
@@ -212,24 +212,24 @@ void *hashtableGet(hashtable_int *hashtbl, KeyType key)
212
212
{
213
213
struct hashnode_i *node;
214
214
size_t hash=hashtbl->hashfunc (key)%hashtbl->size ;
215
-
215
+
216
216
/* fprintf(stderr, "hashtbl_get() key=%s, hash=%d\n", key, hash);*/
217
-
217
+
218
218
node=hashtbl->nodes [hash];
219
219
while (node)
220
220
{
221
221
if (node->key ==key)
222
222
return node->data ;
223
223
node=node->next ;
224
224
}
225
-
225
+
226
226
return NULL ;
227
227
}
228
228
229
229
hashnode_i* hashtableGetBucketHashed (hashtable_int *hashtbl, KeyType key)
230
230
{
231
231
size_t hash = key % hashtbl->size ;
232
-
232
+
233
233
return hashtbl->nodes [hash];
234
234
}
235
235
@@ -238,29 +238,29 @@ int hashtableResize(hashtable_int *hashtbl, size_t size)
238
238
hashtable_int newtbl;
239
239
size_t n;
240
240
struct hashnode_i *node,*next;
241
-
241
+
242
242
newtbl.size =size;
243
243
newtbl.hashfunc =hashtbl->hashfunc ;
244
-
245
- newtbl.nodes =(hashnode_i**)calloc (size, sizeof (struct hashnode_i *));
244
+
245
+ newtbl.nodes =(hashnode_i**)calloc (size, sizeof (struct hashnode_i *));
246
246
if (!newtbl.nodes )
247
247
return -1 ;
248
-
248
+
249
249
for (n=0 ; n<hashtbl->size ; ++n)
250
250
{
251
251
for (node=hashtbl->nodes [n]; node; node=next)
252
252
{
253
253
next = node->next ;
254
254
hashtableInsert (&newtbl, node->key , node->data );
255
255
hashtableRemove (hashtbl, node->key );
256
-
256
+
257
257
}
258
258
}
259
-
259
+
260
260
free (hashtbl->nodes );
261
261
hashtbl->size =newtbl.size ;
262
262
hashtbl->nodes =newtbl.nodes ;
263
-
263
+
264
264
return 0 ;
265
265
}
266
266
@@ -270,24 +270,24 @@ int hashtableWrite(const hashtable_int * hashtbl, const size_t dataSize, FILE* f
270
270
size_t hashMagic=T_HASH_MAGIC;
271
271
size_t n=hashtbl->size ;
272
272
size_t i;
273
-
273
+
274
274
fwrite (&hashMagic, sizeof (size_t ),1 , f);
275
275
fwrite (&n, sizeof (size_t ),1 , f);
276
276
fwrite (&dataSize, sizeof (size_t ),1 , f);
277
-
277
+
278
278
for (i=0 ; i<hashtbl->size ; i++)
279
279
{
280
280
struct hashnode_i * node=hashtbl->nodes [i];
281
281
size_t noEl=0 ;
282
-
282
+
283
283
while (node)
284
284
{
285
285
noEl++;
286
286
node=node->next ;
287
287
}
288
-
288
+
289
289
fwrite (&noEl, sizeof (size_t ),1 , f);
290
-
290
+
291
291
node=hashtbl->nodes [i];
292
292
while (node)
293
293
{
@@ -296,7 +296,7 @@ int hashtableWrite(const hashtable_int * hashtbl, const size_t dataSize, FILE* f
296
296
node=node->next ;
297
297
}
298
298
}
299
-
299
+
300
300
return 1 ;
301
301
}
302
302
@@ -305,13 +305,13 @@ void hashtablePrint(hashtable_int *hashtbl)
305
305
{
306
306
size_t n;
307
307
struct hashnode_i *node,*next;
308
-
308
+
309
309
for (n=0 ; n<hashtbl->size ; ++n)
310
310
{
311
311
for (node=hashtbl->nodes [n]; node; node=next)
312
312
{
313
313
next = node->next ;
314
- std::cout<<" Key : " <<node->key <<" , Data : " <<node->data <<std::endl;
314
+ std::cout<<" Key : " <<node->key <<" , Data : " <<node->data <<std::endl;
315
315
}
316
316
}
317
317
}
@@ -321,28 +321,28 @@ hashtable_int *hashtableRead(FILE* f)
321
321
size_t hashMagic = 0 ;
322
322
size_t n = 0 , status;
323
323
hashtable_int *hashtbl = 0 ;
324
-
324
+
325
325
status = fread (&hashMagic, sizeof (size_t ),1 , f);
326
326
if (status && hashMagic==T_HASH_MAGIC)
327
327
{
328
328
size_t i;
329
329
size_t dataSize;
330
330
status = fread (&n, sizeof (size_t ),1 , f);
331
331
status = fread (&dataSize, sizeof (size_t ),1 , f);
332
-
332
+
333
333
hashtbl=hashtableCreate (n, hash);
334
-
334
+
335
335
for (i=0 ; i<hashtbl->size ; i++)
336
336
{
337
337
size_t j=0 ;
338
338
status = fread (&n, sizeof (size_t ),1 , f);
339
-
339
+
340
340
for (j=0 ; j<n; j++)
341
341
{
342
342
int key=0 ;
343
343
void * data=0 ;
344
344
status = fread (&key, sizeof (KeyType), 1 , f);
345
-
345
+
346
346
if (dataSize>sizeof (void *))
347
347
{
348
348
data=malloc (dataSize);
@@ -355,15 +355,15 @@ hashtable_int *hashtableRead(FILE* f)
355
355
}
356
356
else
357
357
status = fread (&data, dataSize, 1 , f);
358
-
358
+
359
359
hashtableInsert (hashtbl, key, data);
360
360
// free(key);
361
361
}
362
362
}
363
363
}
364
364
else
365
365
return 0 ;
366
-
366
+
367
367
return hashtbl;
368
368
}
369
369
0 commit comments