-
Notifications
You must be signed in to change notification settings - Fork 0
/
jak_ht.c
167 lines (105 loc) · 2.56 KB
/
jak_ht.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "jak_ht.h"
#include "jak_da.h"
#include "jak_dbg.h"
#include <stdlib.h>
struct jak_ht {
int (*compare_keys)( void *, void * );
uint32_t (*key_to_hash)( void * );
void (*destructor)( void *, void * );
float factor;
unsigned int buckets_c;
jak_da_t * * buckets;
};
struct buck {
void * key;
void * val;
};
struct buck * find_buck( jak_ht_t * ht, void * key, int * i ) {
unsigned int j;
struct buck * cur;
*i = ht->key_to_hash( key ) % ht->buckets_c;
if( ht->buckets[ *i ] ) {
for( j = 0; j < ht->buckets[ *i ]->len; j++ ) {
cur = (struct buck *)jak_da_get( ht->buckets[ *i ], j );
if( cur && ht->compare_keys( cur->key, key ) ) {
return cur;
}
}
}
return NULL;
}
// public functions
jak_ht_t * jak_ht_new(
unsigned int initial_buckets_count,
float growth_factor,
int (*compare_keys)( void * key1, void * key2 ),
uint32_t (*key_to_hash)( void * key ),
void (*destructor)( void * key, void * value )
) {
jak_ht_t * ht;
check( initial_buckets_count > 0 && compare_keys && key_to_hash, final_cleanup );
ht = malloc( sizeof( jak_ht_t ) );
check( ht, final_cleanup );
ht->buckets = calloc( initial_buckets_count, sizeof( jak_da_t * ) );
check( ht->buckets, ht_cleanup );
ht->buckets_c = initial_buckets_count;
ht->factor = growth_factor;
ht->compare_keys = compare_keys;
ht->key_to_hash = key_to_hash;
ht->destructor = destructor;
return ht;
ht_cleanup:
free( ht );
final_cleanup:
return NULL;
}
void jak_ht_free( jak_ht_t * ht ) {
struct buck * cur;
unsigned int i;
unsigned int j;
for( i = 0; i < ht->buckets_c; i++ ) {
if( ! ht->buckets[i] ) continue;
if( ht->destructor ) {
for( j = 0; j < ht->buckets[i]->len; j++ ) {
cur = (struct buck *)jak_da_get( ht->buckets[i], j );
if( ! cur ) continue;
ht->destructor( cur->key, cur->val );
}
}
jak_da_free( ht->buckets[i] );
}
free( ht->buckets );
free( ht );
}
int jak_ht_set( jak_ht_t * ht, void * key, void * value ) {
int rc;
struct buck * cur;
int i;
struct buck new;
cur = find_buck( ht, key, &i );
if( cur ) {
cur->val = value;
} else {
if( ! ht->buckets[i] ) {
ht->buckets[i] = jak_da_new( 1, 2, sizeof( struct buck ) );
check( ht->buckets[i], final_cleanup );
}
new.key = key;
new.val = value;
rc = jak_da_push( ht->buckets[i], &new );
check( rc == 0, final_cleanup );
}
return 0;
final_cleanup:
return -1;
}
void * jak_ht_get( jak_ht_t * ht, void * key ) {
int i;
struct buck * cur;
cur = find_buck( ht, key, &i );
if( cur ) {
return cur->val;
} else {
return NULL;
}
}