|
13 | 13 | static void **HTNAME##_lookup_bp_r(htable_t *h, void *key, void *ctx) \ |
14 | 14 | { \ |
15 | 15 | uint_t hv; \ |
16 | | - size_t i, orig, index, iter; \ |
| 16 | + size_t i, orig, index, iter, empty_slot; \ |
17 | 17 | size_t newsz, sz = hash_size(h); \ |
18 | 18 | size_t maxprobe = max_probe(sz); \ |
19 | 19 | void **tab = h->table; \ |
20 | 20 | void **ol; \ |
21 | 21 | \ |
22 | 22 | hv = HFUNC((uintptr_t)key, ctx); \ |
23 | | - retry_bp: \ |
24 | | - iter = 0; \ |
25 | | - index = (size_t)(hv & (sz-1)) * 2; \ |
26 | | - sz *= 2; \ |
27 | | - orig = index; \ |
28 | | - \ |
29 | | - do { \ |
30 | | - if (tab[index+1] == HT_NOTFOUND) { \ |
31 | | - tab[index] = key; \ |
32 | | - return &tab[index+1]; \ |
| 23 | + while (1) { \ |
| 24 | + iter = 0; \ |
| 25 | + index = (size_t)(hv & (sz-1)) * 2; \ |
| 26 | + sz *= 2; \ |
| 27 | + orig = index; \ |
| 28 | + empty_slot = -1; \ |
| 29 | + \ |
| 30 | + do { \ |
| 31 | + if (tab[index] == HT_NOTFOUND) { \ |
| 32 | + if (empty_slot == -1) \ |
| 33 | + empty_slot = index; \ |
| 34 | + break; \ |
| 35 | + } \ |
| 36 | + if (tab[index+1] == HT_NOTFOUND) { \ |
| 37 | + if (empty_slot == -1) \ |
| 38 | + empty_slot = index; \ |
| 39 | + } \ |
| 40 | + \ |
| 41 | + if (EQFUNC(key, tab[index], ctx)) \ |
| 42 | + return &tab[index+1]; \ |
| 43 | + \ |
| 44 | + index = (index+2) & (sz-1); \ |
| 45 | + iter++; \ |
| 46 | + if (iter > maxprobe) \ |
| 47 | + break; \ |
| 48 | + } while (index != orig); \ |
| 49 | + \ |
| 50 | + if (empty_slot != -1) { \ |
| 51 | + tab[empty_slot] = key; \ |
| 52 | + return &tab[empty_slot+1]; \ |
33 | 53 | } \ |
34 | 54 | \ |
35 | | - if (EQFUNC(key, tab[index], ctx)) \ |
36 | | - return &tab[index+1]; \ |
37 | | - \ |
38 | | - index = (index+2) & (sz-1); \ |
39 | | - iter++; \ |
40 | | - if (iter > maxprobe) \ |
41 | | - break; \ |
42 | | - } while (index != orig); \ |
43 | | - \ |
44 | | - /* table full */ \ |
45 | | - /* quadruple size, rehash, retry the insert */ \ |
46 | | - /* it's important to grow the table really fast; otherwise we waste */ \ |
47 | | - /* lots of time rehashing all the keys over and over. */ \ |
48 | | - sz = h->size; \ |
49 | | - ol = h->table; \ |
50 | | - if (sz < HT_N_INLINE) \ |
51 | | - newsz = HT_N_INLINE; \ |
52 | | - else if (sz >= (1<<19) || (sz <= (1<<8))) \ |
53 | | - newsz = sz<<1; \ |
54 | | - else \ |
55 | | - newsz = sz<<2; \ |
56 | | - /*printf("trying to allocate %d words.\n", newsz); fflush(stdout);*/ \ |
57 | | - tab = (void**)LLT_ALLOC(newsz*sizeof(void*)); \ |
58 | | - if (tab == NULL) \ |
59 | | - return NULL; \ |
60 | | - for(i=0; i < newsz; i++) \ |
61 | | - tab[i] = HT_NOTFOUND; \ |
62 | | - h->table = tab; \ |
63 | | - h->size = newsz; \ |
64 | | - for(i=0; i < sz; i+=2) { \ |
65 | | - if (ol[i+1] != HT_NOTFOUND) { \ |
66 | | - (*HTNAME##_lookup_bp_r(h, ol[i], ctx)) = ol[i+1]; \ |
| 55 | + /* table full */ \ |
| 56 | + /* quadruple size, rehash, retry the insert */ \ |
| 57 | + /* it's important to grow the table really fast; otherwise we waste */ \ |
| 58 | + /* lots of time rehashing all the keys over and over. */ \ |
| 59 | + sz = h->size; \ |
| 60 | + ol = h->table; \ |
| 61 | + if (sz < HT_N_INLINE) \ |
| 62 | + newsz = HT_N_INLINE; \ |
| 63 | + else if (sz >= (1<<19) || (sz <= (1<<8))) \ |
| 64 | + newsz = sz<<1; \ |
| 65 | + else \ |
| 66 | + newsz = sz<<2; \ |
| 67 | + /*printf("trying to allocate %d words.\n", newsz); fflush(stdout);*/ \ |
| 68 | + tab = (void**)LLT_ALLOC(newsz*sizeof(void*)); \ |
| 69 | + if (tab == NULL) \ |
| 70 | + return NULL; \ |
| 71 | + for (i = 0; i < newsz; i++) \ |
| 72 | + tab[i] = HT_NOTFOUND; \ |
| 73 | + h->table = tab; \ |
| 74 | + h->size = newsz; \ |
| 75 | + for (i = 0; i < sz; i += 2) { \ |
| 76 | + if (ol[i+1] != HT_NOTFOUND) { \ |
| 77 | + (*HTNAME##_lookup_bp_r(h, ol[i], ctx)) = ol[i+1]; \ |
| 78 | + } \ |
67 | 79 | } \ |
68 | | - } \ |
69 | | - if (ol != &h->_space[0]) \ |
70 | | - LLT_FREE(ol); \ |
| 80 | + if (ol != &h->_space[0]) \ |
| 81 | + LLT_FREE(ol); \ |
71 | 82 | \ |
72 | | - sz = hash_size(h); \ |
73 | | - maxprobe = max_probe(sz); \ |
74 | | - tab = h->table; \ |
75 | | - \ |
76 | | - goto retry_bp; \ |
| 83 | + sz = hash_size(h); \ |
| 84 | + maxprobe = max_probe(sz); \ |
| 85 | + tab = h->table; \ |
| 86 | + } \ |
77 | 87 | \ |
78 | 88 | return NULL; \ |
79 | 89 | } \ |
|
0 commit comments