-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathint-ht-seq.c
More file actions
212 lines (176 loc) · 4.05 KB
/
int-ht-seq.c
File metadata and controls
212 lines (176 loc) · 4.05 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
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
#include "int_ht_seq.h"
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
static int64_t
mix(int64_t n) {
n = (~n) + (n << 21); // n = (n << 21) - n - 1;
n = n ^ (n >> 24);
n = (n + (n << 3)) + (n << 8); // n * 265
n = n ^ (n >> 14);
n = (n + (n << 2)) + (n << 4); // n * 21
n = n ^ (n >> 28);
n = n + (n << 31);
return n;
}
int_ht_seq_t *
int_ht_seq_new(int64_t size) {
int_ht_seq_t * rtn = malloc(sizeof(int_ht_seq_t));
return int_ht_seq_init(rtn, size);
}
int_ht_seq_t *
int_ht_seq_init(int_ht_seq_t * ht, int64_t size) {
size <<= 1;
int64_t pow = 16;
while(pow < size) {
pow <<= 1;
}
ht->size = pow;
ht->mask = pow-1;
ht->elements = 0;
ht->keys = malloc(sizeof(int64_t) * pow);
for(uint64_t i = 0; i < ht->size; i++) {
ht->keys[i] = INT_HT_SEQ_EMPTY;
}
return ht;
}
int_ht_seq_t *
int_ht_seq_empty(int_ht_seq_t * ht) {
for(uint64_t i = 0; i < ht->size; i++) {
ht->keys[i] = INT_HT_SEQ_EMPTY;
}
ht->elements = 0;
return ht;
}
int_ht_seq_t *
int_ht_seq_free_internal(int_ht_seq_t * ht) {
if(ht->keys) {
free(ht->keys);
}
return ht;
}
int_ht_seq_t *
int_ht_seq_free(int_ht_seq_t * ht) {
if(ht) {
int_ht_seq_free_internal(ht);
free(ht);
}
return NULL;
}
void
int_ht_seq_expand(int_ht_seq_t * ht, int64_t new_size) {
int_ht_seq_t tmp;
int_ht_seq_init(&tmp, new_size);
for(uint64_t i = 0; i < ht->size; i++) {
if(ht->keys[i] != INT_HT_SEQ_EMPTY) {
int_ht_seq_insert(&tmp, ht->keys[i]);
}
}
free(ht->keys);
*ht = tmp;
}
int_ht_seq_t *
int_ht_seq_insert(int_ht_seq_t * ht, int64_t k) {
if(((double)ht->elements) > (0.7f) * ((double)ht->size)) {
int_ht_seq_expand(ht, ht->size);
}
int64_t i = mix(k) & ht->mask;
while(ht->keys[i] != INT_HT_SEQ_EMPTY && ht->keys[i] != k) {
i = (i+1) & ht->mask;
}
if(ht->keys[i] == INT_HT_SEQ_EMPTY) {
ht->keys[i] = k;
ht->elements++;
}
return ht;
}
int
int_ht_seq_exists(int_ht_seq_t * ht, int64_t k) {
int64_t i = mix(k) & ht->mask;
while(ht->keys[i] != INT_HT_SEQ_EMPTY && ht->keys[i] != k) {
i = (i+1) & ht->mask;
}
if(ht->keys[i] == k) {
return 1;
} else {
return 0;
}
}
void
int_ht_seq_print_contents(int_ht_seq_t * ht) {
printf("[");
int first = 1;
for(uint64_t i = 0; i < ht->size; i++) {
if(ht->keys[i] != INT_HT_SEQ_EMPTY) {
printf("%s%" PRId64, first ? "" : ", ", ht->keys[i]);
first = 0;
}
}
printf("]\n");
}
#if defined(INT_HT_SEQ_TEST)
int main(int argc, char *argv[]) {
int_ht_seq_t * ht = int_ht_seq_new(10);
if(ht->size != 32) {
printf("Initialization failed.\n");
return -1;
}
for(uint64_t i = 1; i < 50; i += 2) {
int_ht_seq_insert(ht, i);
if(!int_ht_seq_exists(ht, i)) {
printf("Insertion of %ld failed.\n", i);
return -1;
}
}
for(uint64_t i = 0; i < 50; i++) {
if(i % 2 == 1) {
if(!int_ht_seq_exists(ht, i)) {
printf("%ld does not exist, but was inserted.\n", i);
return -1;
}
} else {
if(int_ht_seq_exists(ht, i)) {
printf("%ld does exist, but was not inserted.\n", i);
return -1;
}
}
}
int_ht_seq_empty(ht);
for(uint64_t i = 0; i < 50; i += 2) {
int_ht_seq_insert(ht, i);
if(!int_ht_seq_exists(ht, i)) {
printf("Insertion of %ld failed.\n", i);
return -1;
}
}
for(uint64_t i = 0; i < 50; i++) {
if(i % 2 != 1) {
if(!int_ht_seq_exists(ht, i)) {
printf("%ld does not exist, but was inserted.\n", i);
return -1;
}
} else {
if(int_ht_seq_exists(ht, i)) {
printf("%ld does exist, but was not inserted.\n", i);
return -1;
}
}
}
int_ht_seq_free(ht);
ht = int_ht_seq_new(1);
uint64_t count = 100000000;
tic();
for(uint64_t i = 0; i < count; i++) {
int_ht_seq_insert(ht, i);
}
double timecount = toc();
printf("time for count %ld %lf\n", count, timecount);
printf("updates per sedc %lf\n", ((double)count)/ timecount);
for(uint64_t i = 0; i < count; i++) {
if(!int_ht_seq_exists(ht, i)) {
printf("the value does not exist\n");
}
}
int_ht_seq_free(ht);
}
#endif