| OLD | NEW |
| 1 #define _GNU_SOURCE | 1 #define _GNU_SOURCE |
| 2 #include <stdlib.h> | 2 #include <stdlib.h> |
| 3 #include <string.h> | 3 #include <string.h> |
| 4 #include <search.h> | 4 #include <search.h> |
| 5 #include "libc.h" | 5 #include "libc.h" |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 open addressing hash table with 2^n table size | 8 open addressing hash table with 2^n table size |
| 9 quadratic probing is used in case of hash collision | 9 quadratic probing is used in case of hash collision |
| 10 tab indices and hash are size_t | 10 tab indices and hash are size_t |
| 11 after resize fails with ENOMEM the state of tab is still usable | 11 after resize fails with ENOMEM the state of tab is still usable |
| 12 | 12 |
| 13 with the posix api items cannot be iterated and length cannot be queried | 13 with the posix api items cannot be iterated and length cannot be queried |
| 14 */ | 14 */ |
| 15 | 15 |
| 16 #define MINSIZE 8 | 16 #define MINSIZE 8 |
| 17 #define MAXSIZE ((size_t)-1/2 + 1) | 17 #define MAXSIZE ((size_t)-1 / 2 + 1) |
| 18 | 18 |
| 19 struct __tab { | 19 struct __tab { |
| 20 » ENTRY *entries; | 20 ENTRY* entries; |
| 21 » size_t mask; | 21 size_t mask; |
| 22 » size_t used; | 22 size_t used; |
| 23 }; | 23 }; |
| 24 | 24 |
| 25 static struct hsearch_data htab; | 25 static struct hsearch_data htab; |
| 26 | 26 |
| 27 int __hcreate_r(size_t, struct hsearch_data *); | 27 int __hcreate_r(size_t, struct hsearch_data*); |
| 28 void __hdestroy_r(struct hsearch_data *); | 28 void __hdestroy_r(struct hsearch_data*); |
| 29 int __hsearch_r(ENTRY, ACTION, ENTRY **, struct hsearch_data *); | 29 int __hsearch_r(ENTRY, ACTION, ENTRY**, struct hsearch_data*); |
| 30 | 30 |
| 31 static size_t keyhash(char *k) | 31 static size_t keyhash(char* k) { |
| 32 { | 32 unsigned char* p = (void*)k; |
| 33 » unsigned char *p = (void *)k; | 33 size_t h = 0; |
| 34 » size_t h = 0; | |
| 35 | 34 |
| 36 » while (*p) | 35 while (*p) |
| 37 » » h = 31*h + *p++; | 36 h = 31 * h + *p++; |
| 38 » return h; | 37 return h; |
| 39 } | 38 } |
| 40 | 39 |
| 41 static int resize(size_t nel, struct hsearch_data *htab) | 40 static int resize(size_t nel, struct hsearch_data* htab) { |
| 42 { | 41 size_t newsize; |
| 43 » size_t newsize; | 42 size_t i, j; |
| 44 » size_t i, j; | 43 ENTRY *e, *newe; |
| 45 » ENTRY *e, *newe; | 44 ENTRY* oldtab = htab->__tab->entries; |
| 46 » ENTRY *oldtab = htab->__tab->entries; | 45 ENTRY* oldend = htab->__tab->entries + htab->__tab->mask + 1; |
| 47 » ENTRY *oldend = htab->__tab->entries + htab->__tab->mask + 1; | |
| 48 | 46 |
| 49 » if (nel > MAXSIZE) | 47 if (nel > MAXSIZE) |
| 50 » » nel = MAXSIZE; | 48 nel = MAXSIZE; |
| 51 » for (newsize = MINSIZE; newsize < nel; newsize *= 2); | 49 for (newsize = MINSIZE; newsize < nel; newsize *= 2) |
| 52 » htab->__tab->entries = calloc(newsize, sizeof *htab->__tab->entries); | 50 ; |
| 53 » if (!htab->__tab->entries) { | 51 htab->__tab->entries = calloc(newsize, sizeof *htab->__tab->entries); |
| 54 » » htab->__tab->entries = oldtab; | 52 if (!htab->__tab->entries) { |
| 55 » » return 0; | 53 htab->__tab->entries = oldtab; |
| 56 » } | 54 return 0; |
| 57 » htab->__tab->mask = newsize - 1; | 55 } |
| 58 » if (!oldtab) | 56 htab->__tab->mask = newsize - 1; |
| 59 » » return 1; | 57 if (!oldtab) |
| 60 » for (e = oldtab; e < oldend; e++) | 58 return 1; |
| 61 » » if (e->key) { | 59 for (e = oldtab; e < oldend; e++) |
| 62 » » » for (i=keyhash(e->key),j=1; ; i+=j++) { | 60 if (e->key) { |
| 63 » » » » newe = htab->__tab->entries + (i & htab->__tab->
mask); | 61 for (i = keyhash(e->key), j = 1;; i += j++) { |
| 64 » » » » if (!newe->key) | 62 newe = htab->__tab->entries + (i & htab->__tab->mask); |
| 65 » » » » » break; | 63 if (!newe->key) |
| 66 » » » } | 64 break; |
| 67 » » » *newe = *e; | 65 } |
| 68 » » } | 66 *newe = *e; |
| 69 » free(oldtab); | 67 } |
| 70 » return 1; | 68 free(oldtab); |
| 69 return 1; |
| 71 } | 70 } |
| 72 | 71 |
| 73 int hcreate(size_t nel) | 72 int hcreate(size_t nel) { |
| 74 { | 73 return __hcreate_r(nel, &htab); |
| 75 » return __hcreate_r(nel, &htab); | |
| 76 } | 74 } |
| 77 | 75 |
| 78 void hdestroy(void) | 76 void hdestroy(void) { |
| 79 { | 77 __hdestroy_r(&htab); |
| 80 » __hdestroy_r(&htab); | |
| 81 } | 78 } |
| 82 | 79 |
| 83 static ENTRY *lookup(char *key, size_t hash, struct hsearch_data *htab) | 80 static ENTRY* lookup(char* key, size_t hash, struct hsearch_data* htab) { |
| 84 { | 81 size_t i, j; |
| 85 » size_t i, j; | 82 ENTRY* e; |
| 86 » ENTRY *e; | |
| 87 | 83 |
| 88 » for (i=hash,j=1; ; i+=j++) { | 84 for (i = hash, j = 1;; i += j++) { |
| 89 » » e = htab->__tab->entries + (i & htab->__tab->mask); | 85 e = htab->__tab->entries + (i & htab->__tab->mask); |
| 90 » » if (!e->key || strcmp(e->key, key) == 0) | 86 if (!e->key || strcmp(e->key, key) == 0) |
| 91 » » » break; | 87 break; |
| 92 » } | 88 } |
| 93 » return e; | 89 return e; |
| 94 } | 90 } |
| 95 | 91 |
| 96 ENTRY *hsearch(ENTRY item, ACTION action) | 92 ENTRY* hsearch(ENTRY item, ACTION action) { |
| 97 { | 93 ENTRY* e; |
| 98 » ENTRY *e; | |
| 99 | 94 |
| 100 » __hsearch_r(item, action, &e, &htab); | 95 __hsearch_r(item, action, &e, &htab); |
| 101 » return e; | 96 return e; |
| 102 } | 97 } |
| 103 | 98 |
| 104 int __hcreate_r(size_t nel, struct hsearch_data *htab) | 99 int __hcreate_r(size_t nel, struct hsearch_data* htab) { |
| 105 { | 100 int r; |
| 106 » int r; | |
| 107 | 101 |
| 108 » htab->__tab = calloc(1, sizeof *htab->__tab); | 102 htab->__tab = calloc(1, sizeof *htab->__tab); |
| 109 » if (!htab->__tab) | 103 if (!htab->__tab) |
| 110 » » return 0; | 104 return 0; |
| 111 » r = resize(nel, htab); | 105 r = resize(nel, htab); |
| 112 » if (r == 0) { | 106 if (r == 0) { |
| 113 » » free(htab->__tab); | 107 free(htab->__tab); |
| 114 » » htab->__tab = 0; | 108 htab->__tab = 0; |
| 115 » } | 109 } |
| 116 » return r; | 110 return r; |
| 117 } | 111 } |
| 118 weak_alias(__hcreate_r, hcreate_r); | 112 weak_alias(__hcreate_r, hcreate_r); |
| 119 | 113 |
| 120 void __hdestroy_r(struct hsearch_data *htab) | 114 void __hdestroy_r(struct hsearch_data* htab) { |
| 121 { | 115 if (htab->__tab) |
| 122 » if (htab->__tab) free(htab->__tab->entries); | 116 free(htab->__tab->entries); |
| 123 » free(htab->__tab); | 117 free(htab->__tab); |
| 124 » htab->__tab = 0; | 118 htab->__tab = 0; |
| 125 } | 119 } |
| 126 weak_alias(__hdestroy_r, hdestroy_r); | 120 weak_alias(__hdestroy_r, hdestroy_r); |
| 127 | 121 |
| 128 int __hsearch_r(ENTRY item, ACTION action, ENTRY **retval, struct hsearch_data *
htab) | 122 int __hsearch_r(ENTRY item, |
| 129 { | 123 ACTION action, |
| 130 » size_t hash = keyhash(item.key); | 124 ENTRY** retval, |
| 131 » ENTRY *e = lookup(item.key, hash, htab); | 125 struct hsearch_data* htab) { |
| 126 size_t hash = keyhash(item.key); |
| 127 ENTRY* e = lookup(item.key, hash, htab); |
| 132 | 128 |
| 133 » if (e->key) { | 129 if (e->key) { |
| 134 » » *retval = e; | 130 *retval = e; |
| 135 » » return 1; | 131 return 1; |
| 136 » } | 132 } |
| 137 » if (action == FIND) { | 133 if (action == FIND) { |
| 138 » » *retval = 0; | 134 *retval = 0; |
| 139 » » return 0; | 135 return 0; |
| 140 » } | 136 } |
| 141 » *e = item; | 137 *e = item; |
| 142 » if (++htab->__tab->used > htab->__tab->mask - htab->__tab->mask/4) { | 138 if (++htab->__tab->used > htab->__tab->mask - htab->__tab->mask / 4) { |
| 143 » » if (!resize(2*htab->__tab->used, htab)) { | 139 if (!resize(2 * htab->__tab->used, htab)) { |
| 144 » » » htab->__tab->used--; | 140 htab->__tab->used--; |
| 145 » » » e->key = 0; | 141 e->key = 0; |
| 146 » » » *retval = 0; | 142 *retval = 0; |
| 147 » » » return 0; | 143 return 0; |
| 148 » » } | 144 } |
| 149 » » e = lookup(item.key, hash, htab); | 145 e = lookup(item.key, hash, htab); |
| 150 » } | 146 } |
| 151 » *retval = e; | 147 *retval = e; |
| 152 » return 1; | 148 return 1; |
| 153 } | 149 } |
| 154 weak_alias(__hsearch_r, hsearch_r); | 150 weak_alias(__hsearch_r, hsearch_r); |
| OLD | NEW |