-
Notifications
You must be signed in to change notification settings - Fork 1
/
tablec.c
167 lines (120 loc) · 4.7 KB
/
tablec.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tablec.h"
void tablec_ca_init(struct tablec_ca_ht *tablec, size_t max_capacity) {
tablec->buckets = malloc(sizeof(struct tablec_ca_buckets) * max_capacity);
tablec->capacity = max_capacity;
tablec->length = 0;
while (max_capacity--) {
tablec->buckets[max_capacity].capacity = 1;
tablec->buckets[max_capacity].length = 0;
tablec->buckets[max_capacity].array = malloc(sizeof(struct tablec_ca_bucket));
tablec->buckets[max_capacity].array[0].key = NULL;
tablec->buckets[max_capacity].array[0].value = NULL;
}
}
static size_t _tablec_ca_hash(struct tablec_ca_ht *tablec, char *key) {
size_t hash = 0, i = 0;
while (key[i] != '\0') hash = hash * 37 + (key[i++] & 255);
return hash % tablec->capacity;
}
void tablec_ca_resize(struct tablec_ca_ht *tablec, size_t new_max_capacity) {
size_t i = tablec->capacity;
struct tablec_ca_ht newHashtable;
tablec_ca_init(&newHashtable, new_max_capacity);
if (tablec->length != 0) while (i--) {
if (tablec->buckets[i].length == 0) continue;
if (tablec->buckets[i].length == 1) {
tablec_ca_set(&newHashtable, tablec->buckets[i].array[0].key, tablec->buckets[i].array[0].value);
continue;
}
while (tablec->buckets[i].capacity-- - 1) {
if (tablec->buckets[i].array[tablec->buckets[i].capacity - 1].key)
tablec_ca_set(&newHashtable, tablec->buckets[i].array[tablec->buckets[i].capacity - 1].key, tablec->buckets[i].array[tablec->buckets[i].capacity - 1].value);
}
}
tablec_ca_cleanup(tablec);
*tablec = newHashtable;
}
void tablec_ca_set(struct tablec_ca_ht *tablec, char *key, void *value) {
struct tablec_ca_bucket *newArray = NULL;
size_t hash = _tablec_ca_hash(tablec, key);
if (tablec->buckets[hash].length == 0) {
if (tablec->buckets[hash].array[0].key == NULL) {
tablec->buckets[hash].array[0].key = key;
tablec->buckets[hash].array[0].value = value;
tablec->length++;
tablec->buckets[hash].length = 1;
} else {
tablec->buckets[hash].capacity = 2;
newArray = malloc(sizeof(struct tablec_ca_bucket) * 2);
newArray[0].key = tablec->buckets[hash].array[0].key;
newArray[0].value = tablec->buckets[hash].array[0].value;
free(tablec->buckets[hash].array);
newArray[1].key = key;
newArray[1].value = value;
tablec->buckets[hash].array = newArray;
tablec->buckets[hash].length = 2;
tablec->length++;
}
} else {
size_t i = tablec->buckets[hash].capacity;
while (i--) {
if (tablec->buckets[hash].array[i].key) continue;
tablec->buckets[hash].array[i].key = key;
tablec->buckets[hash].array[i].value = value;
tablec->buckets[hash].length++;
tablec->length++;
return;
}
i = tablec->buckets[hash].capacity;
newArray = calloc(sizeof(struct tablec_ca_bucket) * (tablec->buckets[hash].capacity * 2), 1);
while (i--) {
if (!tablec->buckets[hash].array[i].key) {
newArray[i].key = NULL;
newArray[i].value = NULL;
continue;
}
newArray[i].key = tablec->buckets[hash].array[i].key;
newArray[i].value = tablec->buckets[hash].array[i].value;
}
free(tablec->buckets[hash].array);
newArray[tablec->buckets[hash].length].key = key;
newArray[tablec->buckets[hash].length].value = value;
tablec->buckets[hash].array = newArray;
tablec->buckets[hash].capacity *= 2;
tablec->buckets[hash].length++;
return;
}
}
struct tablec_ca_bucket *tablec_ca_get(struct tablec_ca_ht *tablec, char *key) {
size_t hash = _tablec_ca_hash(tablec, key), i = tablec->buckets[hash].capacity;
if (tablec->buckets[hash].length == 0) return NULL;
while (i--) {
if (tablec->buckets[hash].array[i].key != NULL && strcmp(tablec->buckets[hash].array[i].key, key) == 0)
return &tablec->buckets[hash].array[i];
}
return NULL;
}
void tablec_ca_del(struct tablec_ca_ht *tablec, char *key) {
size_t hash = _tablec_ca_hash(tablec, key), i = tablec->buckets[hash].capacity;
if (tablec->buckets[hash].length == 0) return;
while (i--) {
if (tablec->buckets[hash].array[i].key && strcmp(tablec->buckets[hash].array[i].key, key) == 0) {
tablec->buckets[hash].array[i].key = NULL;
tablec->buckets[hash].array[i].value = NULL;
tablec->length--;
tablec->buckets[hash].length--;
return;
}
}
}
int tablec_ca_full(struct tablec_ca_ht *tablec) {
return (int)(tablec->capacity == tablec->length) ? -1 : (int)(tablec->capacity - tablec->length);
}
void tablec_ca_cleanup(struct tablec_ca_ht *tablec) {
while (tablec->capacity--)
free(tablec->buckets[tablec->capacity].array);
free(tablec->buckets);
}