2 musl license, hsearch.c originally written by Szabolcs Nagy
4 Copyright © 2005-2020 Rich Felker, et al.
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 open addressing hash table with 2^n table size
32 quadratic probing is used in case of hash collision
33 tab indices and hash are size_t
34 after resize fails with ENOMEM the state of tab is still usable
37 typedef struct htab_entry
{
54 #define MAXSIZE ((size_t)-1/2 + 1)
56 static size_t keyhash(char *k
)
58 unsigned char *p
= (void *)k
;
66 static int resize(struct htab
*htab
, size_t nel
)
70 struct elem
*e
, *newe
;
71 struct elem
*oldtab
= htab
->elems
;
72 struct elem
*oldend
= htab
->elems
+ htab
->mask
+ 1;
76 for (newsize
= MINSIZE
; newsize
< nel
; newsize
*= 2);
77 htab
->elems
= calloc(newsize
, sizeof *htab
->elems
);
82 htab
->mask
= newsize
- 1;
85 for (e
= oldtab
; e
< oldend
; e
++)
87 for (i
=e
->hash
,j
=1; ; i
+=j
++) {
88 newe
= htab
->elems
+ (i
& htab
->mask
);
98 static struct elem
*lookup(struct htab
*htab
, char *key
, size_t hash
)
103 for (i
=hash
,j
=1; ; i
+=j
++) {
104 e
= htab
->elems
+ (i
& htab
->mask
);
106 (e
->hash
==hash
&& strcmp(e
->item
.key
, key
)==0))
112 struct htab
*htab_create(size_t nel
)
114 struct htab
*r
= calloc(1, sizeof *r
);
115 if(r
&& !resize(r
, nel
)) {
122 void htab_destroy(struct htab
*htab
)
128 static htab_entry
*htab_find_item(struct htab
*htab
, char* key
)
130 size_t hash
= keyhash(key
);
131 struct elem
*e
= lookup(htab
, key
, hash
);
139 htab_value
* htab_find(struct htab
*htab
, char* key
)
141 htab_entry
*i
= htab_find_item(htab
, key
);
142 if(i
) return &i
->data
;
146 int htab_delete(struct htab
*htab
, char* key
)
148 htab_entry
*i
= htab_find_item(htab
, key
);
154 int htab_insert(struct htab
*htab
, char* key
, htab_value value
)
156 size_t hash
= keyhash(key
);
157 struct elem
*e
= lookup(htab
, key
, hash
);
159 /* it's not allowed to overwrite existing data */
164 e
->item
.data
= value
;
166 if (++htab
->used
> htab
->mask
- htab
->mask
/4) {
167 if (!resize(htab
, 2*htab
->used
)) {
176 size_t htab_next(struct htab
*htab
, size_t iterator
, char** key
, htab_value
**v
)
179 for(i
=iterator
;i
<htab
->mask
+1;++i
) {
180 struct elem
*e
= htab
->elems
+ i
;