stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddn
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent aa482fc · Browse files · 1 file·+191 added−66 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+191 added−66 removed
- other:+191 added−66 removed
Provenance
- Commit
d693c61- Tree
9931465- Author
Sean Barrett <sean@nothings.org>- Committer
Sean Barrett <sean@nothings.org>- Parent
aa482fc
1 file changed+191 added−66 removed
modified:stb_ds.h+191 added-66 removed
1+#include <stdio.h>1-/* stb_ds.h - v0.62 - public domain data structures - Sean Barrett 20192+/* stb_ds.h - v0.62b - public domain data structures - Sean Barrett 20193 4 This is a single-header-file library that provides easy-to-use5 dynamic arrays and hash tables for C (also works in C++).6 101 unmodified lines108 void arrins(T* a, int p, int n);109 Inserts n uninitialized items into array a starting at a[p],110 moving the rest of the array over.111 112+ arraddn:113+ T* arraddn(T* a, int n)114+ Appends n uninitialized items onto array at the end.115+ Returns a pointer to the first uninitialized item added.116+117 arrdel:118 void arrdel(T* a, int p);119 Deletes the element at a[p], moving the rest of the array over.120 75 unmodified lines196 Returns the number of elements in the hashmap.197 198 hmgeti199 shgeti200+ hmgeti_ts201 ptrdiff_t hmgeti(T*, TK key)202 ptrdiff_t shgeti(T*, char* key)203+ ptrdiff_t hmgeti_ts(T*, TK key, ptrdiff_t tempvar)204 Returns the index in the hashmap which has the key 'key', or -1205 if the key is not present.206 207 hmget208+ hmget_ts209 shget210 TV hmget(T*, TK key)211 TV shget(T*, char* key)212+ TV hmget_ts(T*, TK key, ptrdiff_t tempvar)213 Returns the value corresponding to 'key' in the hashmap.214 The structure must have a 'value' field215 216 hmgets1 unmodified line218 T hmgets(T*, TK key)219 T shgets(T*, char* key)220 Returns the structure corresponding to 'key' in the hashmap.221 222+ hmgetp223+ shgetp224+ hmgetp_ts225+ hmgetp_null226+ shgetp_null227+ T* hmgetp(T*, TK key)228+ T* shgetp(T*, char* key)229+ T* hmgetp_ts(T*, TK key, ptrdiff_t tempvar)230+ T* hmgetp_null(T*, TK key)231+ T* shgetp_null(T*, char *key) 232+ Returns a pointer to the structure corresponding to 'key' in233+ the hashmap. Functions ending in "_null" return NULL if the key234+ is not present in the hashmap; the others return a pointer to a235+ structure holding the default value (but not the searched-for key).236+237 hmdefault238 shdefault239 TV hmdefault(T*, TV value)240 TV shdefault(T*, TV value)17 unmodified lines258 hmputs259 shputs260 T hmputs(T*, T item)261 T shputs(T*, T item)237- Inserts a struct with T.key and T.value into the hashmap. If the struct is already262+ Inserts a struct with T.key into the hashmap. If the struct is already263 present in the hashmap, updates it.264 265 hmdel266 shdel20 unmodified lines287 be allocated multiple times in the string arena.288 289 NOTES290 266- * These data structures are realloc'd when they grow, and the macro "functions"267- write to the provided pointer. This means: (a) the pointer must be an lvalue,268- and (b) the pointer to the data structure is not stable, and you must maintain269- it the same as you would a realloc'd pointer. For example, if you pass a pointer270- to a dynamic array to a function which updates it, the function must return271- back the new pointer to the caller. This is the price of trying to do this in C.291+ * These data structures are realloc'd when they grow, and the macro292+ "functions" write to the provided pointer. This means: (a) the pointer293+ must be an lvalue, and (b) the pointer to the data structure is not294+ stable, and you must maintain it the same as you would a realloc'd295+ pointer. For example, if you pass a pointer to a dynamic array to a296+ function which updates it, the function must return back the new297+ pointer to the caller. This is the price of trying to do this in C.298 299+ * The following are the only functions that are thread-safe on a single data300+ structure, i.e. can be run in multiple threads simultaneously on the same301+ data structure302+ hmlen shlen303+ hmlenu shlenu304+ hmget_ts shget_ts305+ hmgeti_ts shgeti_ts306+ hmgets_ts shgets_ts307+308 * You iterate over the contents of a dynamic array and a hashmap in exactly309 the same way, using arrlen/hmlen/shlen:310 311 for (i=0; i < arrlen(foo); ++i)20 unmodified lines332 333 * For compilers other than GCC and clang (e.g. Visual Studio), for hmput/hmget/hmdel334 and variants, the key must be an lvalue (so the macro can take the address of it).335 Extensions are used that eliminate this requirement if you're using C99 and later301- in GCC or clang, or if you're using C++ in GCC.336+ in GCC or clang, or if you're using C++ in GCC. But note that this can make your337+ code less portable.338 339 * To test for presence of a key in a hashmap, just do 'hmgeti(foo,key) >= 0'.340 341 * The iteration order of your data in the hashmap is determined solely by the61 unmodified lines403 404 #define hmput stbds_hmput405 #define hmputs stbds_hmputs406 #define hmget stbds_hmget407+#define hmget_ts stbds_hmget_ts408 #define hmgets stbds_hmgets409 #define hmgetp stbds_hmgetp410+#define hmgetp_ts stbds_hmgetp_ts411+#define hmgetp_null stbds_hmgetp_null412 #define hmgeti stbds_hmgeti413+#define hmgeti_ts stbds_hmgeti_ts414 #define hmdel stbds_hmdel415 #define hmlen stbds_hmlen416 #define hmlenu stbds_hmlenu417 #define hmfree stbds_hmfree418 #define hmdefault stbds_hmdefault419 #define hmdefaults stbds_hmdefaults420 421 #define shput stbds_shput422+#define shputi stbds_shputi423 #define shputs stbds_shputs424 #define shget stbds_shget425+#define shgeti stbds_shgeti426 #define shgets stbds_shgets427 #define shgetp stbds_shgetp386-#define shgeti stbds_shgeti428+#define shgetp_null stbds_shgetp_null429 #define shdel stbds_shdel430 #define shlen stbds_shlen431 #define shlenu stbds_shlenu432 #define shfree stbds_shfree39 unmodified lines472 // Everything below here is implementation details473 //474 475 extern void * stbds_arrgrowf(void *a, size_t elemsize, size_t addlen, size_t min_cap);434-extern void stbds_hmfree_func(void *p, size_t elemsize, size_t keyoff);476+extern void stbds_hmfree_func(void *p, size_t elemsize);477 extern void * stbds_hmget_key(void *a, size_t elemsize, void *key, size_t keysize, int mode);478+extern void * stbds_hmget_key_ts(void *a, size_t elemsize, void *key, size_t keysize, ptrdiff_t *temp, int mode);479 extern void * stbds_hmput_default(void *a, size_t elemsize);480 extern void * stbds_hmput_key(void *a, size_t elemsize, void *key, size_t keysize, int mode);481 extern void * stbds_hmdel_key(void *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode);482 extern void * stbds_shmode_func(size_t elemsize, int mode);30 unmodified lines513 514 #define stbds_header(t) ((stbds_array_header *) (t) - 1)515 #define stbds_temp(t) stbds_header(t)->temp516 474-#define stbds_arrsetcap(a,n) (stbds_arrgrow(a,0,n))475-#define stbds_arrsetlen(a,n) ((stbds_arrcap(a) < n ? stbds_arrsetcap(a,n),0 : 0), (a) ? stbds_header(a)->length = (n) : 0)517+#define stbds_arrsetcap(a,n) (stbds_arrgrow(a,0,n))518+#define stbds_arrsetlen(a,n) ((stbds_arrcap(a) < (size_t) (n) ? stbds_arrsetcap((a),(size_t)(n)),0 : 0), (a) ? stbds_header(a)->length = (size_t) (n) : 0)519 #define stbds_arrcap(a) ((a) ? stbds_header(a)->capacity : 0)520 #define stbds_arrlen(a) ((a) ? (ptrdiff_t) stbds_header(a)->length : 0)521 #define stbds_arrlenu(a) ((a) ? stbds_header(a)->length : 0)522 #define stbds_arrput(a,v) (stbds_arrmaybegrow(a,1), (a)[stbds_header(a)->length++] = (v))523 #define stbds_arrpush stbds_arrput // synonym524 #define stbds_arrpop(a) (stbds_header(a)->length--, (a)[stbds_header(a)->length])482-#define stbds_arraddn(a,n) (stbds_arrmaybegrow(a,n), stbds_header(a)->length += (n))525+#define stbds_arraddn(a,n) (stbds_arrmaybegrow(a,n), stbds_header(a)->length += (n), stbds_header(a)->length-(n))526 #define stbds_arrlast(a) ((a)[stbds_header(a)->length-1])527 #define stbds_arrfree(a) ((void) ((a) ? STBDS_FREE(NULL,stbds_header(a)) : (void)0), (a)=NULL)528 #define stbds_arrdel(a,i) stbds_arrdeln(a,i,1)529 #define stbds_arrdeln(a,i,n) (memmove(&(a)[i], &(a)[(i)+(n)], sizeof *(a) * (stbds_header(a)->length-(n)-(i))), stbds_header(a)->length -= (n))7 unmodified lines537 #define stbds_arrgrow(a,b,c) ((a) = stbds_arrgrowf_wrapper((a), sizeof *(a), (b), (c)))538 539 #define stbds_hmput(t, k, v) \540 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, 0), \498- (t)[stbds_temp((t)-1)].key = (k), \541+ (t)[stbds_temp((t)-1)].key = (k), \542 (t)[stbds_temp((t)-1)].value = (v))543 544 #define stbds_hmputs(t, s) \545 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), &(s).key, sizeof (s).key, STBDS_HM_BINARY), \2 unmodified lines548 #define stbds_hmgeti(t,k) \549 ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_HM_BINARY), \550 stbds_temp((t)-1))551 552+#define stbds_hmgeti_ts(t,k,temp) \553+ ((t) = stbds_hmget_key_ts_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, &(temp), STBDS_HM_BINARY), \554+ (temp))555+556 #define stbds_hmgetp(t, k) \557 ((void) stbds_hmgeti(t,k), &(t)[stbds_temp((t)-1)])558 559+#define stbds_hmgetp_ts(t, k, temp) \560+ ((void) stbds_hmgeti_ts(t,k,temp), &(t)[temp])561+562 #define stbds_hmdel(t,k) \563 (((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_BINARY)),(t)?stbds_temp((t)-1):0)564 565 #define stbds_hmdefault(t, v) \2 unmodified lines568 #define stbds_hmdefaults(t, s) \569 ((t) = stbds_hmput_default_wrapper((t), sizeof *(t)), (t)[-1] = (s))570 571 #define stbds_hmfree(p) \522- ((void) ((p) != NULL ? stbds_hmfree_func((p)-1,sizeof*(p),STBDS_OFFSETOF((p),key)),0 : 0),(p)=NULL)572+ ((void) ((p) != NULL ? stbds_hmfree_func((p)-1,sizeof*(p)),0 : 0),(p)=NULL)573 524-#define stbds_hmgets(t, k) (*stbds_hmgetp(t,k))525-#define stbds_hmget(t, k) (stbds_hmgetp(t,k)->value)526-#define stbds_hmlen(t) ((t) ? (ptrdiff_t) stbds_header((t)-1)->length-1 : 0)527-#define stbds_hmlenu(t) ((t) ? stbds_header((t)-1)->length-1 : 0)574+#define stbds_hmgets(t, k) (*stbds_hmgetp(t,k))575+#define stbds_hmget(t, k) (stbds_hmgetp(t,k)->value)576+#define stbds_hmget_ts(t, k, temp) (stbds_hmgetp_ts(t,k,temp)->value)577+#define stbds_hmlen(t) ((t) ? (ptrdiff_t) stbds_header((t)-1)->length-1 : 0)578+#define stbds_hmlenu(t) ((t) ? stbds_header((t)-1)->length-1 : 0)579+#define stbds_hmgetp_null(t,k) (stbds_hmgeti(t,k) == -1 ? NULL : &(t)[stbds_temp(t)-1])580 581 #define stbds_shput(t, k, v) \582 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \583 (t)[stbds_temp((t)-1)].value = (v))584 585+#define stbds_shputi(t, k, v) \586+ ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \587+ (t)[stbds_temp((t)-1)].value = (v), stbds_temp((t)-1))588+589 #define stbds_shputs(t, s) \590 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (s).key, sizeof (s).key, STBDS_HM_STRING), \591 (t)[stbds_temp((t)-1)] = (s))592 593+#define stbds_pshput(t, p) \594+ ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (p)->key, sizeof (p)->key, STBDS_HM_PTR_TO_STRING), \595+ (t)[stbds_temp((t)-1)] = (p))596+597 #define stbds_shgeti(t,k) \598 ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \599 stbds_temp((t)-1))600 601+#define stbds_pshgeti(t,k) \602+ ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (*(t))->key, STBDS_HM_PTR_TO_STRING), \603+ stbds_temp((t)-1))604+605 #define stbds_shgetp(t, k) \606 ((void) stbds_shgeti(t,k), &(t)[stbds_temp((t)-1)])607 608+#define stbds_pshget(t, k) \609+ ((void) stbds_pshgeti(t,k), (t)[stbds_temp((t)-1)])610+611 #define stbds_shdel(t,k) \612 (((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_STRING)),(t)?stbds_temp((t)-1):0)613+#define stbds_pshdel(t,k) \614+ (((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void*) (k), sizeof (*(t))->key, STBDS_OFFSETOF(*(t),key), STBDS_HM_PTR_TO_STRING)),(t)?stbds_temp((t)-1):0)615 616 #define stbds_sh_new_arena(t) \617 ((t) = stbds_shmode_func_wrapper(t, sizeof *(t), STBDS_SH_ARENA))618 #define stbds_sh_new_strdup(t) \619 ((t) = stbds_shmode_func_wrapper(t, sizeof *(t), STBDS_SH_STRDUP))620 552-#define stbds_shdefault(t, v) stbds_hmdefault(t,v)621+#define stbds_shdefault(t, v) stbds_hmdefault(t,v)622 #define stbds_shdefaults(t, s) stbds_hmdefaults(t,s)623 624 #define stbds_shfree stbds_hmfree625 #define stbds_shlenu stbds_hmlenu626 627 #define stbds_shgets(t, k) (*stbds_shgetp(t,k))628 #define stbds_shget(t, k) (stbds_shgetp(t,k)->value)629+#define stbds_shgetp_null(t,k) (stbds_shgeti(t,k) == -1 ? NULL : &(t)[stbds_temp(t)-1])630 #define stbds_shlen stbds_hmlen631 632 typedef struct633 {16 unmodified lines650 unsigned char block;651 unsigned char mode; // this isn't used by the string arena itself652 };653 584-#define STBDS_HM_BINARY 0585-#define STBDS_HM_STRING 1654+#define STBDS_HM_BINARY 0655+#define STBDS_HM_STRING 1656 657 enum658 {659 STBDS_SH_NONE,660+ STBDS_SH_DEFAULT,661 STBDS_SH_STRDUP,662 STBDS_SH_ARENA663 };664 5 unmodified lines670 }671 template<class T> static T * stbds_hmget_key_wrapper(T *a, size_t elemsize, void *key, size_t keysize, int mode) {672 return (T*)stbds_hmget_key((void*)a, elemsize, key, keysize, mode);673 }674+template<class T> static T * stbds_hmget_key_ts_wrapper(T *a, size_t elemsize, void *key, size_t keysize, ptrdiff_t *temp, int mode) {675+ return (T*)stbds_hmget_key((void*)a, elemsize, key, keysize, temp, mode);676+}677 template<class T> static T * stbds_hmput_default_wrapper(T *a, size_t elemsize) {678 return (T*)stbds_hmput_default((void *)a, elemsize);679 }680 template<class T> static T * stbds_hmput_key_wrapper(T *a, size_t elemsize, void *key, size_t keysize, int mode) {7 unmodified lines688 }689 #else690 #define stbds_arrgrowf_wrapper stbds_arrgrowf691 #define stbds_hmget_key_wrapper stbds_hmget_key692+#define stbds_hmget_key_ts_wrapper stbds_hmget_key_ts693 #define stbds_hmput_default_wrapper stbds_hmput_default694 #define stbds_hmput_key_wrapper stbds_hmput_key695 #define stbds_hmdel_key_wrapper stbds_hmdel_key696 #define stbds_shmode_func_wrapper(t,e,m) stbds_shmode_func(e,m)33 unmodified lines730 //731 // stbds_arr implementation732 //733 734+//int *prev_allocs[65536];735+//int num_prev;736+737 void *stbds_arrgrowf(void *a, size_t elemsize, size_t addlen, size_t min_cap)738 {739 void *b;740 size_t min_len = stbds_arrlen(a) + addlen;10 unmodified lines751 min_cap = 2 * stbds_arrcap(a);752 else if (min_cap < 4)753 min_cap = 4;754 755+ //if (num_prev < 65536) if (a) prev_allocs[num_prev++] = (int *) ((char *) a+1);756+ //if (num_prev == 2201)757+ // num_prev = num_prev;758 b = STBDS_REALLOC(NULL, (a) ? stbds_header(a) : 0, elemsize * min_cap + sizeof(stbds_array_header));759+ //if (num_prev < 65536) prev_allocs[num_prev++] = (int *) (char *) b;760 b = (char *) b + sizeof(stbds_array_header);761 if (a == NULL) {762 stbds_header(b)->length = 0;763 stbds_header(b)->hash_table = 0;764 } else {765 STBDS_STATS(++stbds_array_grow);766 }767 stbds_header(b)->capacity = min_cap;768+769 return b;770 }771 772 //234 unmodified lines1007 #ifndef STBDS_SIPHASH_D_ROUNDS1008 #define STBDS_SIPHASH_D_ROUNDS 11009 #endif1010 1011+#ifdef _MSC_VER1012+#pragma warning(push)1013+#pragma warning(disable:4127) // conditional expression is constant, for do..while(0) and sizeof()==1014+#endif1015+1016 static size_t stbds_siphash_bytes(void *p, size_t len, size_t seed)1017 {1018 unsigned char *d = (unsigned char *) p;1019 size_t i,j;49 unmodified lines1069 v0 ^= data;1070 v2 ^= 0xff;1071 for (j=0; j < STBDS_SIPHASH_D_ROUNDS; ++j)1072 STBDS_SIPROUND();1073+1074 #ifdef STBDS_SIPHASH_2_41075 return v0^v1^v2^v3;1076 #else1077 return v1^v2^v3; // slightly stronger since v0^v3 in above cancels out final round operation? I tweeted at the authors of SipHash about this but they didn't reply81 unmodified lines1159 return stbds_siphash_bytes(p,len,seed);1160 }1161 #endif1162 }1163+#ifdef _MSC_VER1164+#pragma warning(pop)1165+#endif1166 1167+1075-static int stbds_is_key_equal(void *a, size_t elemsize, void *key, size_t keysize, int mode, size_t i)1168+static int stbds_is_key_equal(void *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode, size_t i)1169 {1170 if (mode >= STBDS_HM_STRING)1171 return 0==strcmp((char *) key, * (char **) ((char *) a + elemsize*i));1172 else4 unmodified lines1177 #define STBDS_ARR_TO_HASH(x,elemsize) ((char*) (x) + (elemsize))1178 1179 #define stbds_hash_table(a) ((stbds_hash_index *) stbds_header(a)->hash_table)1180 1088-void stbds_hmfree_func(void *a, size_t elemsize, size_t keyoff)1181+void stbds_hmfree_func(void *a, size_t elemsize)1182 {1183 if (a == NULL) return;1184 if (stbds_hash_table(a) != NULL) {1092- if (stbds_hash_table(a)->string.mode == STBDS_SH_STRDUP) {1093- size_t i;1094- // skip 0th element, which is default1095- for (i=1; i < stbds_header(a)->length; ++i)1096- STBDS_FREE(NULL, *(char**) ((char *) a + elemsize*i));1097- }1098- stbds_strreset(&stbds_hash_table(a)->string);1099- }1185+ if (stbds_hash_table(a)->string.mode == STBDS_SH_STRDUP) {1186+ size_t i;1187+ // skip 0th element, which is default1188+ for (i=1; i < stbds_header(a)->length; ++i)1189+ STBDS_FREE(NULL, *(char**) ((char *) a + elemsize*i));1190+ }1191+ stbds_strreset(&stbds_hash_table(a)->string);1192+ }1193 STBDS_FREE(NULL, stbds_header(a)->hash_table);1194 STBDS_FREE(NULL, stbds_header(a));1195 }1196 1104-static ptrdiff_t stbds_hm_find_slot(void *a, size_t elemsize, void *key, size_t keysize, int mode)1197+static ptrdiff_t stbds_hm_find_slot(void *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode)1198 {1199 void *raw_a = STBDS_HASH_TO_ARR(a,elemsize);1200 stbds_hash_index *table = stbds_hash_table(raw_a);1201 size_t hash = mode >= STBDS_HM_STRING ? stbds_hash_string((char*)key,table->seed) : stbds_hash_bytes(key, keysize,table->seed);12 unmodified lines1214 1215 // start searching from pos to end of bucket, this should help performance on small hash tables that fit in cache1216 for (i=pos & STBDS_BUCKET_MASK; i < STBDS_BUCKET_LENGTH; ++i) {1217 if (bucket->hash[i] == hash) {1125- if (stbds_is_key_equal(a, elemsize, key, keysize, mode, bucket->index[i])) {1218+ if (stbds_is_key_equal(a, elemsize, key, keysize, keyoffset, mode, bucket->index[i])) {1219 return (pos & ~STBDS_BUCKET_MASK)+i;1220 }1221 } else if (bucket->hash[i] == STBDS_HASH_EMPTY) {1222 return -1;3 unmodified lines1226 // search from beginning of bucket to pos1227 limit = pos & STBDS_BUCKET_MASK;1228 for (i = 0; i < limit; ++i) {1229 if (bucket->hash[i] == hash) {1137- if (stbds_is_key_equal(a, elemsize, key, keysize, mode, bucket->index[i])) {1230+ if (stbds_is_key_equal(a, elemsize, key, keysize, keyoffset, mode, bucket->index[i])) {1231 return (pos & ~STBDS_BUCKET_MASK)+i;1232 }1233 } else if (bucket->hash[i] == STBDS_HASH_EMPTY) {1234 return -1;5 unmodified lines1240 step += STBDS_BUCKET_LENGTH;1241 pos &= (table->slot_count-1);1242 }1243 /* NOTREACHED */1151- return -1;1244 }1245 1154-void * stbds_hmget_key(void *a, size_t elemsize, void *key, size_t keysize, int mode)1246+void * stbds_hmget_key_ts(void *a, size_t elemsize, void *key, size_t keysize, ptrdiff_t *temp, int mode)1247 {1248+ size_t keyoffset = 0;1249 if (a == NULL) {1250 // make it non-empty so we can return a temp1251 a = stbds_arrgrowf(0, elemsize, 0, 1);1252 stbds_header(a)->length += 1;1253 memset(a, 0, elemsize);1161- stbds_temp(a) = STBDS_INDEX_EMPTY;1254+ *temp = STBDS_INDEX_EMPTY;1255 // adjust a to point after the default element1256 return STBDS_ARR_TO_HASH(a,elemsize);1257 } else {1258 stbds_hash_index *table;1259 void *raw_a = STBDS_HASH_TO_ARR(a,elemsize);1260 // adjust a to point to the default element1261 table = (stbds_hash_index *) stbds_header(raw_a)->hash_table;1262 if (table == 0) {1170- stbds_temp(raw_a) = -1;1263+ *temp = -1;1264 } else {1172- ptrdiff_t slot = stbds_hm_find_slot(a, elemsize, key, keysize, mode);1265+ ptrdiff_t slot = stbds_hm_find_slot(a, elemsize, key, keysize, keyoffset, mode);1266 if (slot < 0) {1174- stbds_temp(raw_a) = STBDS_INDEX_EMPTY;1267+ *temp = STBDS_INDEX_EMPTY;1268 } else {1269 stbds_hash_bucket *b = &table->storage[slot >> STBDS_BUCKET_SHIFT];1177- stbds_temp(raw_a) = b->index[slot & STBDS_BUCKET_MASK];1270+ *temp = b->index[slot & STBDS_BUCKET_MASK];1271 }1272 }1273 return a;1274 }1275 }1276 1277+void * stbds_hmget_key(void *a, size_t elemsize, void *key, size_t keysize, int mode)1278+{1279+ ptrdiff_t temp;1280+ void *p = stbds_hmget_key_ts(a, elemsize, key, keysize, &temp, mode);1281+ stbds_temp(STBDS_HASH_TO_ARR(p,elemsize)) = temp;1282+ return p;1283+}1284+1285 void * stbds_hmput_default(void *a, size_t elemsize)1286 {1287 // three cases:1288 // a is NULL <- allocate11 unmodified lines1300 static char *stbds_strdup(char *str);1301 1302 void *stbds_hmput_key(void *a, size_t elemsize, void *key, size_t keysize, int mode)1303 {1304+ size_t keyoffset=0;1305 void *raw_a;1306 stbds_hash_index *table;1307 1308 if (a == NULL) {15 unmodified lines1324 size_t slot_count;1325 1326 slot_count = (table == NULL) ? STBDS_BUCKET_LENGTH : table->slot_count*2;1327 nt = stbds_make_hash_index(slot_count, table);1226- if (table) {1328+ if (table)1329 STBDS_FREE(NULL, table);1228- }1330+ else1331+ nt->string.mode = mode >= STBDS_HM_STRING ? STBDS_SH_DEFAULT : 0;1332 stbds_header(a)->hash_table = table = nt;1333 STBDS_STATS(++stbds_hash_grow);1334 }1335 17 unmodified lines1353 1354 // start searching from pos to end of bucket1355 for (i=pos & STBDS_BUCKET_MASK; i < STBDS_BUCKET_LENGTH; ++i) {1356 if (bucket->hash[i] == hash) {1254- if (stbds_is_key_equal(raw_a, elemsize, key, keysize, mode, bucket->index[i])) {1357+ if (stbds_is_key_equal(raw_a, elemsize, key, keysize, keyoffset, mode, bucket->index[i])) {1358 stbds_temp(a) = bucket->index[i];1359 return STBDS_ARR_TO_HASH(a,elemsize);1360 }1361 } else if (bucket->hash[i] == 0) {8 unmodified lines1370 // search from beginning of bucket to pos1371 limit = pos & STBDS_BUCKET_MASK;1372 for (i = 0; i < limit; ++i) {1373 if (bucket->hash[i] == hash) {1271- if (stbds_is_key_equal(raw_a, elemsize, key, keysize, mode, bucket->index[i])) {1374+ if (stbds_is_key_equal(raw_a, elemsize, key, keysize, keyoffset, mode, bucket->index[i])) {1375 stbds_temp(a) = bucket->index[i];1376 return STBDS_ARR_TO_HASH(a,elemsize);1377 }1378 } else if (bucket->hash[i] == 0) {18 unmodified lines1397 ++table->used_count;1398 1399 {1400 ptrdiff_t i = (ptrdiff_t) stbds_arrlen(a);1298- // we want to do stbds_arraddn(1), but we can't use the macros since we don't have something of the right type1401+ // we want to do stbds_arraddn(1), but we can't use the macros since we don't have something of the right type1402 if ((size_t) i+1 > stbds_arrcap(a))1403 *(void **) &a = stbds_arrgrowf(a, elemsize, 1, 0);1404 raw_a = STBDS_ARR_TO_HASH(a,elemsize);1405 4 unmodified lines1410 bucket->index[pos & STBDS_BUCKET_MASK] = i-1;1411 stbds_temp(a) = i-1;1412 1413 switch (table->string.mode) {1311- case STBDS_SH_STRDUP: *(char **) ((char *) a + elemsize*i) = stbds_strdup((char*) key); break;1312- case STBDS_SH_ARENA: *(char **) ((char *) a + elemsize*i) = stbds_stralloc(&table->string, (char*)key); break;1313- default: *(char **) ((char *) a + elemsize*i) = (char *) key; break;1414+ case STBDS_SH_STRDUP: *(char **) ((char *) a + elemsize*i) = stbds_strdup((char*) key); break;1415+ case STBDS_SH_ARENA: *(char **) ((char *) a + elemsize*i) = stbds_stralloc(&table->string, (char*)key); break;1416+ case STBDS_SH_DEFAULT: *(char **) ((char *) a + elemsize*i) = (char *) key; break;1417+ default: memcpy((char *) a + elemsize*i, key, keysize); break;1418 }1419 }1420 return STBDS_ARR_TO_HASH(a,elemsize);1421 }5 unmodified lines1427 stbds_hash_index *h;1428 memset(a, 0, elemsize);1429 stbds_header(a)->length = 1;1430 stbds_header(a)->hash_table = h = (stbds_hash_index *) stbds_make_hash_index(STBDS_BUCKET_LENGTH, NULL);1327- h->string.mode = mode;1431+ h->string.mode = (unsigned char) mode;1432 return STBDS_ARR_TO_HASH(a,elemsize);1433 }1434 1435 void * stbds_hmdel_key(void *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode)8 unmodified lines1444 if (table == 0) {1445 return a;1446 } else {1447 ptrdiff_t slot;1344- slot = stbds_hm_find_slot(a, elemsize, key, keysize, mode);1448+ slot = stbds_hm_find_slot(a, elemsize, key, keysize, keyoffset, mode);1449 if (slot < 0)1450 return a;1451 else {1452 stbds_hash_bucket *b = &table->storage[slot >> STBDS_BUCKET_SHIFT];18 unmodified lines1471 memmove((char*) a + elemsize*old_index, (char*) a + elemsize*final_index, elemsize);1472 1473 // now find the slot for the last element1474 if (mode == STBDS_HM_STRING)1371- slot = stbds_hm_find_slot(a, elemsize, *(char**) ((char *) a+elemsize*old_index + keyoffset), keysize, mode);1475+ slot = stbds_hm_find_slot(a, elemsize, *(char**) ((char *) a+elemsize*old_index + keyoffset), keysize, keyoffset, mode);1476 else1373- slot = stbds_hm_find_slot(a, elemsize, (char* ) a+elemsize*old_index + keyoffset, keysize, mode);1477+ slot = stbds_hm_find_slot(a, elemsize, (char* ) a+elemsize*old_index + keyoffset, keysize, keyoffset, mode);1478 STBDS_ASSERT(slot >= 0);1479 b = &table->storage[slot >> STBDS_BUCKET_SHIFT];1480 i = slot & STBDS_BUCKET_MASK;1481 STBDS_ASSERT(b->index[i] == final_index);15 unmodified lines1497 }1498 }1499 }1500 /* NOTREACHED */1397- return 0;1501 }1502 1503 static char *stbds_strdup(char *str)1504 {5 unmodified lines1510 return p;1511 }1512 1513 #ifndef STBDS_STRING_ARENA_BLOCKSIZE_MIN1411-#define STBDS_STRING_ARENA_BLOCKSIZE_MIN 5121514+#define STBDS_STRING_ARENA_BLOCKSIZE_MIN 512u1515 #endif1516 #ifndef STBDS_STRING_ARENA_BLOCKSIZE_MAX1414-#define STBDS_STRING_ARENA_BLOCKSIZE_MAX 1<<201517+#define STBDS_STRING_ARENA_BLOCKSIZE_MAX (1u<<20)1518 #endif1519 1520 char *stbds_stralloc(stbds_string_arena *a, char *str)1521 {72 unmodified lines1594 #include <assert.h>1595 #endif1596 1597 typedef struct { int key,b,c,d; } stbds_struct;1598+typedef struct { int key[2],b,c,d; } stbds_struct2;1599 1600 static char buffer[256];1601 char *strkey(int n)1602 {11 unmodified lines1614 // VC6 C++ doesn't like the template<> trick on unnamed structures, so do nothing!1615 STBDS_ASSERT(0);1616 #else1617 const int testsize = 100000;1618+ const int testsize2 = testsize/20;1619 int *arr=NULL;1515- struct { int key; int value; } *intmap = NULL;1516- struct { char *key; int value; } *strmap = NULL;1517- struct { stbds_struct key; int value; } *map = NULL;1518- stbds_struct *map2 = NULL;1519- stbds_string_arena sa = { 0 };1620+ struct { int key; int value; } *intmap = NULL;1621+ struct { char *key; int value; } *strmap = NULL;1622+ struct { stbds_struct key; int value; } *map = NULL;1623+ stbds_struct *map2 = NULL;1624+ stbds_struct2 *map3 = NULL;1625+ stbds_string_arena sa = { 0 };1626+ int key3[2] = { 1,2 };1627+ ptrdiff_t temp;1628 1629 int i,j;1630 1631 STBDS_ASSERT(arrlen(arr)==0);27 unmodified lines1659 STBDS_ASSERT(hmgeti(intmap, i) == -1);1660 STBDS_ASSERT(hmget (intmap, i) == -2);1661 for (i=0; i < testsize; i+=2)1662 hmput(intmap, i, i*5);1555- for (i=0; i < testsize; i+=1)1663+ for (i=0; i < testsize; i+=1) {1664 if (i & 1) STBDS_ASSERT(hmget(intmap, i) == -2 );1665 else STBDS_ASSERT(hmget(intmap, i) == i*5);1666+ if (i & 1) STBDS_ASSERT(hmget_ts(intmap, i, temp) == -2 );1667+ else STBDS_ASSERT(hmget_ts(intmap, i, temp) == i*5);1668+ }1669 for (i=0; i < testsize; i+=2)1670 hmput(intmap, i, i*3);1671 for (i=0; i < testsize; i+=1)1672 if (i & 1) STBDS_ASSERT(hmget(intmap, i) == -2 );76 unmodified lines1749 stbds_struct s = { i,i*2,i*3 ,i*4 };1750 stbds_struct t = { i,i*2,i*3+1,i*4 };1751 if (i & 1) STBDS_ASSERT(hmget(map, s) == 0);1752 else STBDS_ASSERT(hmget(map, s) == i*5);1642- STBDS_ASSERT(hmget(map, t) == 0);1753+ if (i & 1) STBDS_ASSERT(hmget_ts(map, s, temp) == 0);1754+ else STBDS_ASSERT(hmget_ts(map, s, temp) == i*5);1755+ //STBDS_ASSERT(hmget(map, t.key) == 0);1756 }1757 1758 for (i=0; i < testsize; i += 2) {1759 stbds_struct s = { i,i*2,i*3,i*4 };5 unmodified lines1765 stbds_struct s = { i,i*2,i*3,i*4 };1766 stbds_struct t = { i,i*2,i*3+1,i*4 };1767 if (i & 1) STBDS_ASSERT(hmgets(map2, s.key).d == 0);1768 else STBDS_ASSERT(hmgets(map2, s.key).d == i*4);1656- STBDS_ASSERT(hmget(map, t) == 0);1769+ //STBDS_ASSERT(hmgetp(map2, t.key) == 0);1770 }1771 hmfree(map2);1772+1773+ for (i=0; i < testsize; i += 2) {1774+ stbds_struct2 s = { { i,i*2 }, i*3,i*4, i*5 };1775+ hmputs(map3, s);1776+ }1777+ for (i=0; i < testsize; i += 1) {1778+ stbds_struct2 s = { { i,i*2}, i*3, i*4, i*5 };1779+ stbds_struct2 t = { { i,i*2}, i*3+1, i*4, i*5 };1780+ if (i & 1) STBDS_ASSERT(hmgets(map3, s.key).d == 0);1781+ else STBDS_ASSERT(hmgets(map3, s.key).d == i*5);1782+ //STBDS_ASSERT(hmgetp(map3, t.key) == 0);1783+ }1784 #endif1785 }1786 #endif1787 42 unmodified lines