Skip to content

Commit 5457d8a

Browse files
committed
more primes
1 parent 7c4c37c commit 5457d8a

File tree

2 files changed

+81
-82
lines changed

2 files changed

+81
-82
lines changed

ctl/ust.h

+58-34
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ typedef struct A
2828
B** bucket;
2929
size_t size;
3030
size_t bucket_count;
31-
int saturated;
3231
}
3332
A;
3433

@@ -185,21 +184,50 @@ JOIN(A, swap)(A* self, A* other)
185184
}
186185

187186
static inline size_t
188-
JOIN(A, closest_prime)(size_t number, int* saturated)
189-
{
190-
static size_t primes[] = {
191-
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
192-
103, 109, 113, 127, 137, 139, 149, 157, 167, 179, 193, 199, 211, 227, 241, 257, 277, 293, 313, 337,
193-
359, 383, 409, 439, 467, 503, 541, 577, 619, 661, 709, 761, 823, 887, 953, 1031, 1109, 1193, 1289, 1381,
194-
1493, 1613, 1741, 1879, 2029, 2179, 2357, 2549, 2753, 2971, 3209, 3469, 3739, 4027, 4349,
195-
4703, 5087, 5503, 5953, 6427, 6949, 7517, 8123, 8783, 9497, 10273, 11113, 12011, 12983, 14033,
196-
15173, 16411, 17749, 19183, 20753, 22447, 24281, 26267, 28411, 30727, 33223, 35933, 38873, 42043, 45481,
197-
49201, 53201, 57557, 62233, 67307, 72817, 78779, 85229, 92203, 99733, 107897, 116731, 126271, 136607, 147793,
198-
159871, 172933, 187091, 202409, 218971, 236897, 256279, 277261, 299951, 324503, 351061, 379787, 410857, 444487,
199-
480881, 520241, 562841, 608903, 658753, 712697, 771049, 834181, 902483, 976369, 1056323, 1142821, 1236397, 1337629,
200-
1447153, 1565659, 1693859, 1832561, 1982627, 2144977, 2320627, 2510653, 2716249, 2938679, 3179303, 3439651, 3721303,
201-
4026031, 4355707, 4712381, 5098259, 5515729, 5967347, 6456007, 6984629, 7556579, 8175383, 8844859, 9569143, 10352717,
202-
11200489, 12117689, 13109983, 14183539, 15345007, 16601593, 17961079, 19431899, 21023161, 22744717, 24607243
187+
JOIN(A, closest_prime)(size_t number)
188+
{
189+
static uint32_t primes[] = {
190+
2, 3, 5, 7, 11,
191+
13, 17, 19, 23, 29, 31,
192+
37, 41, 43, 47, 53, 59,
193+
61, 67, 71, 73, 79, 83,
194+
89, 97, 103, 109, 113, 127,
195+
137, 139, 149, 157, 167, 179,
196+
193, 199, 211, 227, 241, 257,
197+
277, 293, 313, 337, 359, 383,
198+
409, 439, 467, 503, 541, 577,
199+
619, 661, 709, 761, 823, 887,
200+
953, 1031, 1109, 1193, 1289, 1381,
201+
1493, 1613, 1741, 1879, 2029, 2179,
202+
2357, 2549, 2753, 2971, 3209, 3469,
203+
3739, 4027, 4349, 4703, 5087, 5503,
204+
5953, 6427, 6949, 7517, 8123, 8783,
205+
9497, 10273, 11113, 12011, 12983, 14033,
206+
15173, 16411, 17749, 19183, 20753, 22447,
207+
24281, 26267, 28411, 30727, 33223, 35933,
208+
38873, 42043, 45481, 49201, 53201, 57557,
209+
62233, 67307, 72817, 78779, 85229, 92203,
210+
99733, 107897, 116731, 126271, 136607, 147793,
211+
159871, 172933, 187091, 202409, 218971, 236897,
212+
256279, 277261, 299951, 324503, 351061, 379787,
213+
410857, 444487, 480881, 520241, 562841, 608903,
214+
658753, 712697, 771049, 834181, 902483, 976369,
215+
1056323, 1142821, 1236397, 1337629, 1447153, 1565659,
216+
1693859, 1832561, 1982627, 2144977, 2320627, 2510653,
217+
2716249, 2938679, 3179303, 3439651, 3721303, 4026031,
218+
4355707, 4712381, 5098259, 5515729, 5967347, 6456007,
219+
6984629, 7556579, 8175383, 8844859, 9569143, 10352717,
220+
11200489, 12117689, 13109983, 14183539, 15345007, 16601593,
221+
17961079, 19431899, 21023161, 22744717, 24607243, 26622317,
222+
28802401, 31160981, 33712729, 36473443, 39460231, 42691603,
223+
46187573, 49969847, 54061849, 58488943, 63278561, 68460391,
224+
74066549, 80131819, 86693767, 93793069, 101473717, 109783337,
225+
118773397, 128499677, 139022417, 150406843, 162723577, 176048909,
226+
190465427, 206062531, 222936881, 241193053, 260944219, 282312799,
227+
305431229, 330442829, 357502601, 386778277, 418451333, 452718089,
228+
489790921, 529899637, 573292817, 620239453, 671030513, 725980837,
229+
785430967, 849749479, 919334987, 994618837, 1076067617, 1164186217,
230+
1259520799, 1362662261, 1474249943, 1594975441, 1725587117,
203231
};
204232
size_t min = primes[0];
205233
if(number < min)
@@ -212,8 +240,6 @@ JOIN(A, closest_prime)(size_t number, int* saturated)
212240
if(number >= a && number <= b)
213241
return number == a ? a : b;
214242
}
215-
if(saturated)
216-
*saturated = 1;
217243
return primes[size - 1];
218244
}
219245

@@ -272,31 +298,29 @@ JOIN(A, rehash)(A* self, size_t desired_count);
272298
static inline void
273299
JOIN(A, reserve)(A* self, size_t desired_count)
274300
{
275-
if(!self->saturated)
301+
if(self->size > 0)
302+
JOIN(A, rehash)(self, desired_count);
303+
else
276304
{
277-
if(self->size > 0)
278-
JOIN(A, rehash)(self, desired_count);
279-
else
280-
{
281-
size_t bucket_count = JOIN(A, closest_prime)(desired_count, &self->saturated);
282-
B** temp = (B**) calloc(bucket_count, sizeof(B*));
283-
for(size_t i = 0; i < self->bucket_count; i++)
284-
temp[i] = self->bucket[i];
285-
free(self->bucket);
286-
self->bucket = temp;
287-
self->bucket_count = bucket_count;
288-
}
305+
size_t bucket_count = JOIN(A, closest_prime)(desired_count);
306+
B** temp = (B**) calloc(bucket_count, sizeof(B*));
307+
for(size_t i = 0; i < self->bucket_count; i++)
308+
temp[i] = self->bucket[i];
309+
free(self->bucket);
310+
self->bucket = temp;
311+
self->bucket_count = bucket_count;
289312
}
290313
}
291314

292315
static inline void
293316
JOIN(A, rehash)(A* self, size_t desired_count)
294317
{
295-
if(!self->saturated)
318+
if(desired_count <= self->size)
319+
desired_count = self->size + 1;
320+
size_t expected = JOIN(A, closest_prime)(desired_count);
321+
if(expected != self->bucket_count)
296322
{
297323
A rehashed = JOIN(A, init)(self->hash, self->equal);
298-
if(desired_count <= self->size)
299-
desired_count = self->size + 1;
300324
JOIN(A, reserve)(&rehashed, desired_count);
301325
foreach(A, self, it)
302326
{

tests/func/test_c11.c

+23-48
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#define T int
1010
#include <ust.h>
1111

12+
#define P
13+
#define T float
14+
#include <ust.h>
15+
1216
size_t
1317
int_hash(int* x)
1418
{ return abs(*x); }
@@ -17,6 +21,14 @@ int
1721
int_equal(int* a, int* b)
1822
{ return *a == *b; }
1923

24+
size_t
25+
float_hash(float* x)
26+
{ return abs((int) *x); }
27+
28+
int
29+
float_equal(float* a, float* b)
30+
{ return *a == *b; }
31+
2032
#define P
2133
#define T int
2234
#include <stk.h>
@@ -199,55 +211,18 @@ main(void)
199211
ust_int_insert(&a, -5);
200212
ust_int_insert(&a, -6);
201213
ust_int_insert(&a, -7);
202-
ust_int_insert(&a, -8);
203-
ust_int_insert(&a, -9);
204-
ust_int_insert(&a, -10);
205-
ust_int_insert(&a, -11);
206-
ust_int_insert(&a, -12);
207-
ust_int_insert(&a, -13);
208-
ust_int_insert(&a, -14);
209-
ust_int_insert(&a, -15);
210-
ust_int_insert(&a, -16);
211-
ust_int_insert(&a, -17);
212-
ust_int_insert(&a, -18);
213-
ust_int_insert(&a, -19);
214-
ust_int_insert(&a, -20);
215-
ust_int_insert(&a, -21);
216-
ust_int_insert(&a, -22);
217-
ust_int_insert(&a, -23);
218-
ust_int_insert(&a, -24);
219-
ust_int_insert(&a, -25);
220-
ust_int_insert(&a, -26);
221-
ust_int_insert(&a, -27);
222-
ust_int_insert(&a, 0);
223-
ust_int_insert(&a, 1);
224-
ust_int_insert(&a, 2);
225-
ust_int_insert(&a, 3);
226-
ust_int_insert(&a, 4);
227-
ust_int_insert(&a, 5);
228-
ust_int_insert(&a, 6);
229-
ust_int_insert(&a, 7);
230-
ust_int_insert(&a, 8);
231-
ust_int_insert(&a, 9);
232-
ust_int_insert(&a, 10);
233-
ust_int_insert(&a, 11);
234-
ust_int_insert(&a, 12);
235-
ust_int_insert(&a, 13);
236-
ust_int_insert(&a, 14);
237-
ust_int_insert(&a, 15);
238-
ust_int_insert(&a, 16);
239-
ust_int_insert(&a, 17);
240-
ust_int_insert(&a, 18);
241-
ust_int_insert(&a, 19);
242-
ust_int_insert(&a, 20);
243-
ust_int_insert(&a, 21);
244-
ust_int_insert(&a, 22);
245-
ust_int_insert(&a, 23);
246-
ust_int_insert(&a, 24);
247-
ust_int_insert(&a, 25);
248-
ust_int_insert(&a, 26);
249-
ust_int_insert(&a, 27);
250214
ust_int_free(&a);
215+
}{
216+
ust_float a = ust_float_init(float_hash, float_equal);
217+
ust_float_insert(&a, -0);
218+
ust_float_insert(&a, -1);
219+
ust_float_insert(&a, -2);
220+
ust_float_insert(&a, -3);
221+
ust_float_insert(&a, -4);
222+
ust_float_insert(&a, -5);
223+
ust_float_insert(&a, -6);
224+
ust_float_insert(&a, -7);
225+
ust_float_free(&a);
251226
}
252227
TEST_PASS(__FILE__);
253228
}

0 commit comments

Comments
 (0)