/
symtab.c
319 lines (286 loc) · 7.26 KB
/
symtab.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* symtab.c
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include "symtab.h"
#include "util.h"
#include "prng.h"
//no keys in hashtable should have count of 0
//if a key is not found in hashtable, indicate with 0
#define NOT_FOUND 0
// Define incomplete type from symtab.h
struct symtab {
c_a** bucket;
int size;
long long a,b;
};
// private functions
static void free_c_a( c_a* c );
static void free_chain( c_a* c );
static int hash( symtab* tab, int item);
static c_a* init_c_a( int key);
static int c_a_heap_cmp(Sample_type* p, Sample_type* q);
// -----------------------------------------------------
// Create symbol table
symtab* new_symtab(int k)
{
symtab* table = safe_malloc( sizeof *table );
prng_type* prng = prng_Init(12345, 2);
prng_int(prng);
prng_int(prng);
table->a = (long long) (prng_int(prng)% MOD);
table->b = (long long) (prng_int(prng)% MOD);
prng_Destroy(prng);
table->size = k;
table->bucket = (c_a**) safe_malloc(k * sizeof(c_a*));
for (int i=0; i<k; i++) {
table->bucket[ i ] = NULL;
}
return table;
}
// -----------------------------------------------------
// Free symbol table
void free_symtab( symtab* table )
{
for (int i=0; i<table->size; i++) {
free_chain( table->bucket[i] );
}
free(table->bucket);
free( table );
}
// -----------------------------------------------------
// Lookup key in symbol table
// If found, return the count of the counter for key
// If not found, return special value NOT_FOUND
int lookup( symtab* table, int key )
{
int bn = hash( table, key );
c_a* c = table->bucket[ bn ];
while (c != NULL) {
if(c->key == key) return c->count;
c = c->next;
}
return NOT_FOUND;
}
//decrements number of primary samplers of b. If b
//is not being processed and not being sampled, b is
//removed from hashtable. removes min from b->sample_heap
//and restores heap property to backup heap
void decrement_prim_samplers(symtab* table, c_a* b,
backup_heap* h, Sample_type* min)
{
b->num_prim_samplers--;
if(b->num_prim_samplers == 0)
{ //need to remove b from backup heap
delete_pos_bheap(h, b->backup_pos);
if(b->num_backup_samplers == b->processing &&
b->processing == 0)
{ //time to remove sampler from hash table
int bn = hash(table, b->key);
c_a* c = table->bucket[ bn ];
if(b == c)
{ //b is first element in bucket
table->bucket[bn] = b->next;
if(b->next != NULL) b->next->previous=NULL;
}
else
{ //b is not first element in bucket
b->previous->next = b->next;
if(b->next != NULL) b->next->previous=b->previous;
}
free_c_a(b);
return;
}
}
delete_pos_c_a_heap(b->sample_heap, min->c_s0_pos);
restore_bheap_property(h, b->backup_pos);
}
//decrements number of backup samplers sampling b
//if b is not being processed and no one is sampling b
//then b is removed from the hashtable
void decrement_backup_samplers(symtab* table, c_a* b)
{
b->num_backup_samplers--;
if(b->num_prim_samplers == b->num_backup_samplers &&
b->num_backup_samplers == b->processing &&
b->processing == 0)
{ //time to remove sampler from hash table
int bn = hash(table, b->key);
c_a* c = table->bucket[ bn ];
if(b == c)
{ //b is first element in bucket
table->bucket[bn] = b->next;
if(b->next != NULL) b->next->previous=NULL;
}
else
{ //b is not first element in bucket
b->previous->next = b->next;
if(b->next != NULL) b->next->previous=b->previous;
}
free_c_a(b);
}
}
//sets b's processing field to 0, if no sampler has b as a primary or
//backup sample, b is removed from hashtable to save space
void done_processing(symtab* table, c_a* b)
{
b->processing = 0;
if(b->num_prim_samplers == b->num_backup_samplers &&
b->num_backup_samplers == 0)
{ //time to remove sampler from hash table
int bn = hash(table, b->key);
c_a* c = table->bucket[ bn ];
if(b == c)
{ //b is first element in bucket
table->bucket[bn] = b->next;
if(b->next != NULL) b->next->previous=NULL;
}
else
{ //b is not first element in bucket
b->previous->next = b->next;
if(b->next != NULL) b->next->previous=b->previous;
}
free_c_a(b);
}
}
//precondition: min's wait times set properly
//postcondition: min is in proper place in backup_heap and in b's sample_heap
void increment_prim_samplers(c_a* b, backup_heap* h, Sample_type* min)
{
b->num_prim_samplers++;
insert_c_a_heap(b->sample_heap, min);
if(b->num_prim_samplers == 1)
{
insert_bheap(h, b);
}
else
{
restore_bheap_property(h, b->backup_pos);
}
}
void increment_backup_samplers(c_a* b)
{
b->num_backup_samplers++;
}
//increment count of key
//if key is not in table, insert it and
//set processing and count to 1, num_prim/backup_samplers to 0
//returns pointer to the key
//DOES NOT RESTORE HEAP PROPERTY IN BACKUP HEAP
c_a* increment_count(symtab* table, int key)
{
int bn = hash(table, key);
c_a* c = table->bucket[ bn ];
while (c != NULL) {
if(c->key == key)
{
c->count++;
c->processing = 1;
return c;
}
else c = c->next;
}
// key not found; create new cell
c_a* n = init_c_a(key);
n->next = table->bucket[bn];
if(table->bucket[bn] != NULL)
table->bucket[bn]->previous = n;
table->bucket[ bn ] = n;
return n;
}
//return the length of the longest bucket
//used for testing effectiveness of hash function
int max_row(symtab* tab)
{
int j = 0;
int max = 0;
c_a* iter;
for(int i = 0; i < tab->size; i++)
{
j = 0;
iter = tab->bucket[i];
while(iter != NULL)
{
j++;
iter = iter->next;
}
if(max < j) max = j;
}
return max;
}
int total_elements_tracked(symtab* tab)
{
int count = 0;
c_a* iter;
for(int i = 0; i < tab->size; i++)
{
iter = tab->bucket[i];
while(iter != NULL)
{
count++;
iter = iter->next;
}
}
return count;
}
int sizeof_symtab(symtab* tab)
{
int size = sizeof(struct symtab) + tab->size * sizeof(c_a**);
c_a* iter;
for(int i = 0; i < tab->size; i++)
{
iter = tab->bucket[i];
while(iter != NULL)
{
size += sizeof(struct c_a) + sizeof_c_a_heap(iter->sample_heap);
iter = iter->next;
}
}
return size;
}
// =====================================================
// Local functions
// -----------------------------------------------------
// Create a new cell
static c_a* init_c_a( int key)
{
c_a* value = (c_a*) safe_malloc(sizeof(c_a));
value->key = key;
value->next = value->previous = NULL;
value->count = value->processing= 1;
value->num_prim_samplers = value->num_backup_samplers = 0;
value->sample_heap = new_c_a_heap(c_a_heap_cmp, 10);
value->backup_pos = -1;
return value;
}
// -----------------------------------------------------
// Free a linked chain of cells
static void free_chain( c_a* c )
{
c_a* next;
while (c != NULL) {
next = c->next;
free_c_a( c );
c = next;
}
}
static void free_c_a( c_a* c )
{
free_c_a_heap(c->sample_heap);
free(c);
}
static int c_a_heap_cmp(Sample_type* p, Sample_type* q)
{
int a = p->backup_minus_delay;
int b = q->backup_minus_delay;
return (a==b) ? 0 : (a<b) ? -1 : 1;
}
// -----------------------------------------------------
// Compute hash value in range [0..nBuckets-1] from string s
static int hash( symtab* tab, int item)
{
return hash31(tab->a,tab->b,item) % tab->size;
}