-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasharray.c
89 lines (80 loc) · 1.95 KB
/
hasharray.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
#include <stdlib.h>
#include <string.h>
#include "hash.h"
#include "memarena.h"
#include "hasharray.h"
struct hasharray {
memarena *keyarena;
hash_t hash;
int count;
int alloc;
int itemsize;
void **itemarray;
};
hasharray * hasharray_create(void **itemarray, int itemsize) {
hasharray * a;
if ( (a = (hasharray*) malloc(sizeof(hasharray))) ) {
a->count = 0;
a->alloc = 0;
a->itemsize = itemsize;
a->itemarray = itemarray;
*(a->itemarray) = 0;
if ( ! ( a->keyarena = memarena_create() ) ) {
free((void*)a);
return 0;
}
hash_init(&(a->hash),0);
}
return a;
}
void hasharray_destroy(hasharray *a) {
if ( ! a ) return;
hash_destroy(&(a->hash));
memarena_destroy(a->keyarena);
if ( *(a->itemarray) ) {
free(*(a->itemarray));
*(a->itemarray) = 0;
}
free((void*)a);
}
int hasharray_insert(hasharray *a, const char *key) {
int i;
int new_alloc;
void *new_array;
char *s;
if ( ! a ) return HASHARRAY_FAIL;
i = hash_lookup(&(a->hash),key);
if ( i != HASH_FAIL ) return i;
i = a->count;
a->count++;
if ( a->count > a->alloc ) {
if ( a->alloc ) new_alloc = a->alloc * 2;
else new_alloc = 8;
new_array = realloc(*(a->itemarray), new_alloc * a->itemsize);
if ( new_array ) {
*(a->itemarray) = new_array;
a->alloc = new_alloc;
} else return HASHARRAY_FAIL;
}
if ( ! ( s = memarena_alloc(a->keyarena,strlen(key)+1) ) ) {
return HASHARRAY_FAIL;
}
strcpy(s,key);
hash_insert(&(a->hash),s,i);
return i;
}
int hasharray_delete(hasharray *a, const char *key) {
if (!a) return HASHARRAY_FAIL; /* I think this should be assert(a) */
return hash_delete(&(a->hash), key);
}
int hasharray_index(hasharray *a, const char *key) {
int i;
if ( ! a ) return HASHARRAY_FAIL;
i = hash_lookup(&(a->hash),key);
if ( i == HASH_FAIL ) i = HASHARRAY_FAIL;
return i;
}
int hasharray_count(hasharray *a) {
if ( ! a ) return HASHARRAY_FAIL;
return a->count;
}