-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.c
More file actions
63 lines (55 loc) · 1.33 KB
/
Copy pathtable.c
File metadata and controls
63 lines (55 loc) · 1.33 KB
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
#include <stdint.h>
#include <op.h>
#include <table.h>
#include <stdlib.h>
#include <string.h>
void reallocate_table(table t) {
t->buckets = malloc(t->bucket_length * sizeof(op));
memset(t->buckets, 0, t->bucket_length * sizeof(op));
}
void resize_table(table t)
{
int old_length = t->bucket_length;
op *old_buckets = t->buckets;
t->bucket_length *= 2;
t->count = 0;
reallocate_table(t);
for (int i=0; i<old_length; i++) {
op k = 0;
for (op j =old_buckets[i]; j; j= k){
k = j->next;
insert(t, j->tag, j);
}
}
}
void insert(table t, uint64_t tag, op o) {
o->tag = tag;
if (t->count > t->bucket_length)
resize_table(t);
o->next = t->buckets[o->tag % t->bucket_length];
t->buckets[o->tag % t->bucket_length] = o;
t->count++;
}
op *search(table t, uint64_t tag)
{
op *j = t->buckets + tag % t->bucket_length;
while(*j && ((*j)->tag != tag)) j = &(*j)->next;
return(j);
}
op get(table t, uint64_t tag) {
op *j = search(t, tag);
return(*j);
}
void delete(table t, uint64_t tag) {
op *j = search(t, tag);
t->count--;
*j = (*j)->next;
}
table allocate_table(int count)
{
table t = malloc(sizeof(struct table));
t->bucket_length = count;
t->count = 0;
reallocate_table(t);
return(t);
}