stb_ds.h
This file
Path
stb_ds.h
1,895 lines · 67.4 KiB · C
Last changed by
fix accidental check-in of typo in stb_ds.hSean Barrettbefore adoption40adb99
History of this fileTake it
Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
line 1why, one step back1/* stb_ds.h - v0.67 - public domain data structures - Sean Barrett 2019Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 2why, one step back2add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 3–28why, one step back3 This is a single-header-file library that provides easy-to-use4 dynamic arrays and hash tables for C (also works in C++).56 For a gentle introduction:7 http://nothings.org/stb_ds89 To use this library, do this in *one* C or C++ file:10 #define STB_DS_IMPLEMENTATION11 #include "stb_ds.h"1213TABLE OF CONTENTS1415 Table of Contents16 Compile-time options17 License18 Documentation19 Notes20 Notes - Dynamic arrays21 Notes - Hash maps22 Credits2324COMPILE-TIME OPTIONS2526 #define STBDS_NO_SHORT_NAMES2728 This flag needs to be set globally.Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 29why, one step back29add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 30–43why, one step back30 By default stb_ds exposes shorter function names that are not qualified31 with the "stbds_" prefix. If these names conflict with the names in your32 code, define this flag.3334 #define STBDS_SIPHASH_2_43536 This flag only needs to be set in the file containing #define STB_DS_IMPLEMENTATION.3738 By default stb_ds.h hashes using a weaker variant of SipHash and a custom hash for39 4- and 8-byte keys. On 64-bit platforms, you can define the above flag to force40 stb_ds.h to use specification-compliant SipHash-2-4 for all keys. Doing so makes41 hash table insertion about 20% slower on 4- and 8-byte keys, 5% slower on42 64-byte keys, and 10% slower on 256-byte keys on my test computer.43tweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
lines 44–45why, one step back44 #define STBDS_REALLOC(context,ptr,size) better_realloc45 #define STBDS_FREE(context,ptr) better_freeMerge branch 'use-free' of https://github.com/adurdin/stb into workingSean Barrettbefore adoption258c6e1
lines 46–49why, one step back4647 These defines only need to be set in the file containing #define STB_DS_IMPLEMENTATION.4849 By default stb_ds uses stdlib realloc() and free() for memory management. You cantweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
lines 50–52why, one step back50 substitute your own functions instead by defining these symbols. You must either51 define both, or neither. Note that at the moment, 'context' will always be NULL.52 @TODO add an array/hash initialization function that takes a memory context pointer.stb_ds: add documentation for STBDS_UNIT_TESTS, -std=c++0xSean Barrettbefore adoptionbcb2815
lines 53–60why, one step back5354 #define STBDS_UNIT_TESTS5556 Defines a function stbds_unit_tests() that checks the functioning of the data structures.5758 Note that on older versions of gcc (e.g. 5.x.x) you may need to build with '-std=c++0x'59 (or equivalentally '-std=c++11') when using anonymous structures as seen on the web60 page or in STBDS_UNIT_TESTS.Merge branch 'use-free' of https://github.com/adurdin/stb into workingSean Barrettbefore adoption258c6e1
line 61why, one step back61add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 62–73why, one step back62LICENSE6364 Placed in the public domain and also MIT licensed.65 See end of file for detailed license information.6667DOCUMENTATION6869 Dynamic Arrays7071 Non-function interface:7273 Declare an empty dynamic array of type TFix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 74why, one step back74 T* foo = NULL;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 75–92why, one step back7576 Access the i'th item of a dynamic array 'foo' of type T, T* foo:77 foo[i]7879 Functions (actually macros)8081 arrfree:82 void arrfree(T*);83 Frees the array.8485 arrlen:86 ptrdiff_t arrlen(T*);87 Returns the number of elements in the array.8889 arrlenu:90 size_t arrlenu(T*);91 Returns the number of elements in the array as an unsigned type.92update version numbersSean Barrettbefore adoption2c2908f
lines 93–96why, one step back93 arrpop:94 T arrpop(T* a)95 Removes the final element of the array and returns it.96add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 97–106why, one step back97 arrput:98 T arrput(T* a, T b);99 Appends the item b to the end of array a. Returns b.100101 arrins:102 T arrins(T* a, int p, T b);103 Inserts the item b into the middle of array a, into a[p],104 moving the rest of the array over. Returns b.105106 arrinsn:Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
line 107why, one step back107 void arrinsn(T* a, int p, int n);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 108–110why, one step back108 Inserts n uninitialized items into array a starting at a[p],109 moving the rest of the array over.110Merge branch 'stbds-arraddn' of https://github.com/HeroicKatora/stb into workingSean Barrettbefore adoption67881b6
lines 111–112why, one step back111 arraddnptr:112 T* arraddnptr(T* a, int n)stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 113–115why, one step back113 Appends n uninitialized items onto array at the end.114 Returns a pointer to the first uninitialized item added.115add credits for last few PR mergesSean Barrettbefore adoption6f7420a
lines 116–117why, one step back116 arraddnindex:117 size_t arraddnindex(T* a, int n)Merge branch 'stbds-arraddn' of https://github.com/HeroicKatora/stb into workingSean Barrettbefore adoption67881b6
lines 118–120why, one step back118 Appends n uninitialized items onto array at the end.119 Returns the index of the first uninitialized item added.120add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 121–125why, one step back121 arrdel:122 void arrdel(T* a, int p);123 Deletes the element at a[p], moving the rest of the array over.124125 arrdeln:Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
line 126why, one step back126 void arrdeln(T* a, int p, int n);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 127–203why, one step back127 Deletes n elements starting at a[p], moving the rest of the array over.128129 arrdelswap:130 void arrdelswap(T* a, int p);131 Deletes the element at a[p], replacing it with the element from132 the end of the array. O(1) performance.133134 arrsetlen:135 void arrsetlen(T* a, int n);136 Changes the length of the array to n. Allocates uninitialized137 slots at the end if necessary.138139 arrsetcap:140 size_t arrsetcap(T* a, int n);141 Sets the length of allocated storage to at least n. It will not142 change the length of the array.143144 arrcap:145 size_t arrcap(T* a);146 Returns the number of total elements the array can contain without147 needing to be reallocated.148149 Hash maps & String hash maps150151 Given T is a structure type: struct { TK key; TV value; }. Note that some152 functions do not require TV value and can have other fields. For string153 hash maps, TK must be 'char *'.154155 Special interface:156157 stbds_rand_seed:158 void stbds_rand_seed(size_t seed);159 For security against adversarially chosen data, you should seed the160 library with a strong random number. Or at least seed it with time().161162 stbds_hash_string:163 size_t stbds_hash_string(char *str, size_t seed);164 Returns a hash value for a string.165166 stbds_hash_bytes:167 size_t stbds_hash_bytes(void *p, size_t len, size_t seed);168 These functions hash an arbitrary number of bytes. The function169 uses a custom hash for 4- and 8-byte data, and a weakened version170 of SipHash for everything else. On 64-bit platforms you can get171 specification-compliant SipHash-2-4 on all data by defining172 STBDS_SIPHASH_2_4, at a significant cost in speed.173174 Non-function interface:175176 Declare an empty hash map of type T177 T* foo = NULL;178179 Access the i'th entry in a hash table T* foo:180 foo[i]181182 Function interface (actually macros):183184 hmfree185 shfree186 void hmfree(T*);187 void shfree(T*);188 Frees the hashmap and sets the pointer to NULL.189190 hmlen191 shlen192 ptrdiff_t hmlen(T*)193 ptrdiff_t shlen(T*)194 Returns the number of elements in the hashmap.195196 hmlenu197 shlenu198 size_t hmlenu(T*)199 size_t shlenu(T*)200 Returns the number of elements in the hashmap.201202 hmgeti203 shgetistb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 204why, one step back204 hmgeti_tsadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 205–206why, one step back205 ptrdiff_t hmgeti(T*, TK key)206 ptrdiff_t shgeti(T*, char* key)stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 207why, one step back207 ptrdiff_t hmgeti_ts(T*, TK key, ptrdiff_t tempvar)add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 208–211why, one step back208 Returns the index in the hashmap which has the key 'key', or -1209 if the key is not present.210211 hmgetstb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 212why, one step back212 hmget_tsadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 213–215why, one step back213 shget214 TV hmget(T*, TK key)215 TV shget(T*, char* key)stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 216why, one step back216 TV hmget_ts(T*, TK key, ptrdiff_t tempvar)add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 217–225why, one step back217 Returns the value corresponding to 'key' in the hashmap.218 The structure must have a 'value' field219220 hmgets221 shgets222 T hmgets(T*, TK key)223 T shgets(T*, char* key)224 Returns the structure corresponding to 'key' in the hashmap.225stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 226–234why, one step back226 hmgetp227 shgetp228 hmgetp_ts229 hmgetp_null230 shgetp_null231 T* hmgetp(T*, TK key)232 T* shgetp(T*, char* key)233 T* hmgetp_ts(T*, TK key, ptrdiff_t tempvar)234 T* hmgetp_null(T*, TK key)Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 235why, one step back235 T* shgetp_null(T*, char *key)stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 236–240why, one step back236 Returns a pointer to the structure corresponding to 'key' in237 the hashmap. Functions ending in "_null" return NULL if the key238 is not present in the hashmap; the others return a pointer to a239 structure holding the default value (but not the searched-for key).240add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 241–265why, one step back241 hmdefault242 shdefault243 TV hmdefault(T*, TV value)244 TV shdefault(T*, TV value)245 Sets the default value for the hashmap, the value which will be246 returned by hmget/shget if the key is not present.247248 hmdefaults249 shdefaults250 TV hmdefaults(T*, T item)251 TV shdefaults(T*, T item)252 Sets the default struct for the hashmap, the contents which will be253 returned by hmgets/shgets if the key is not present.254255 hmput256 shput257 TV hmput(T*, TK key, TV value)258 TV shput(T*, char* key, TV value)259 Inserts a <key,value> pair into the hashmap. If the key is already260 present in the hashmap, updates its value.261262 hmputs263 shputs264 T hmputs(T*, T item)265 T shputs(T*, T item)stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 266why, one step back266 Inserts a struct with T.key into the hashmap. If the struct is alreadyMerge branch 'master' of https://github.com/rsachetto/stb into workingSean Barrettbefore adoption8cf9f5a
line 267why, one step back267 present in the hashmap, updates it.add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 268–281why, one step back268269 hmdel270 shdel271 int hmdel(T*, TK key)272 int shdel(T*, char* key)273 If 'key' is in the hashmap, deletes its entry and returns 1.274 Otherwise returns 0.275276 Function interface (actually macros) for strings only:277278 sh_new_strdup279 void sh_new_strdup(T*);280 Overwrites the existing pointer with a newly allocated281 string hashmap which will automatically allocate and freeMerge branch 'use-free' of https://github.com/adurdin/stb into workingSean Barrettbefore adoption258c6e1
line 282why, one step back282 each string key using realloc/freeadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 283–294why, one step back283284 sh_new_arena285 void sh_new_arena(T*);286 Overwrites the existing pointer with a newly allocated287 string hashmap which will automatically allocate each string288 key to a string arena. Every string key ever used by this289 hash table remains in the arena until the arena is freed.290 Additionally, any key which is deleted and reinserted will291 be allocated multiple times in the string arena.292293NOTES294stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 295–301why, one step back295 * These data structures are realloc'd when they grow, and the macro296 "functions" write to the provided pointer. This means: (a) the pointer297 must be an lvalue, and (b) the pointer to the data structure is not298 stable, and you must maintain it the same as you would a realloc'd299 pointer. For example, if you pass a pointer to a dynamic array to a300 function which updates it, the function must return back the new301 pointer to the caller. This is the price of trying to do this in C.add stb_ds.h and testsSean Barrettbefore adoption477471d
line 302why, one step back302stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 303–311why, one step back303 * The following are the only functions that are thread-safe on a single data304 structure, i.e. can be run in multiple threads simultaneously on the same305 data structure306 hmlen shlen307 hmlenu shlenu308 hmget_ts shget_ts309 hmgeti_ts shgeti_ts310 hmgets_ts shgets_ts311add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 312–338why, one step back312 * You iterate over the contents of a dynamic array and a hashmap in exactly313 the same way, using arrlen/hmlen/shlen:314315 for (i=0; i < arrlen(foo); ++i)316 ... foo[i] ...317318 * All operations except arrins/arrdel are O(1) amortized, but individual319 operations can be slow, so these data structures may not be suitable320 for real time use. Dynamic arrays double in capacity as needed, so321 elements are copied an average of once. Hash tables double/halve322 their size as needed, with appropriate hysteresis to maintain O(1)323 performance.324325NOTES - DYNAMIC ARRAY326327 * If you know how long a dynamic array is going to be in advance, you can avoid328 extra memory allocations by using arrsetlen to allocate it to that length in329 advance and use foo[n] while filling it out, or arrsetcap to allocate the memory330 for that length and use arrput/arrpush as normal.331332 * Unlike some other versions of the dynamic array, this version should333 be safe to use with strict-aliasing optimizations.334335NOTES - HASH MAP336337 * For compilers other than GCC and clang (e.g. Visual Studio), for hmput/hmget/hmdel338 and variants, the key must be an lvalue (so the macro can take the address of it).Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 339why, one step back339 Extensions are used that eliminate this requirement if you're using C99 and laterstb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 340–341why, one step back340 in GCC or clang, or if you're using C++ in GCC. But note that this can make your341 code less portable.add stb_ds.h and testsSean Barrettbefore adoption477471d
line 342why, one step back342Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 343why, one step back343 * To test for presence of a key in a hashmap, just do 'hmgeti(foo,key) >= 0'.add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 344–362why, one step back344345 * The iteration order of your data in the hashmap is determined solely by the346 order of insertions and deletions. In particular, if you never delete, new347 keys are always added at the end of the array. This will be consistent348 across all platforms and versions of the library. However, you should not349 attempt to serialize the internal hash table, as the hash is not consistent350 between different platforms, and may change with future versions of the library.351352 * Use sh_new_arena() for string hashmaps that you never delete from. Initialize353 with NULL if you're managing the memory for your strings, or your strings are354 never freed (at least until the hashmap is freed). Otherwise, use sh_new_strdup().355 @TODO: make an arena variant that garbage collects the strings with a trivial356 copy collector into a new arena whenever the table shrinks / rebuilds. Since357 current arena recommendation is to only use arena if it never deletes, then358 this can just replace current arena implementation.359360 * If adversarial input is a serious concern and you're on a 64-bit platform,361 enable STBDS_SIPHASH_2_4 (see the 'Compile-time options' section), and pass362 a strong random number to stbds_rand_seed.Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 363why, one step back363add stb_ds.h and testsSean Barrettbefore adoption477471d
line 364why, one step back364 * The default value for the hash table is stored in foo[-1], so if youstb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
lines 365–366why, one step back365 use code like 'hmget(T,k)->value = 5' you can accidentally overwrite366 the value stored by hmdefault if 'k' is not present.add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 367–371why, one step back367368CREDITS369370 Sean Barrett -- library, idea for dynamic array API/implementation371 Per Vognsen -- idea for hash table API/implementationupdate version numbersSean Barrettbefore adoption2c2908f
line 372why, one step back372 Rafael Sachetto -- arrpop()add credits for last few PR mergesSean Barrettbefore adoption6f7420a
line 373why, one step back373 github:HeroicKatora -- arraddn() reworkingupdate versionSean Barrettbefore adoption9f1836f
lines 374–376why, one step back374375 Bugfixes:376 Andy DurdinMerge branch 'master' of https://github.com/nothings/stbSean Barrettbefore adoption76254f7
line 377why, one step back377 Shane Liesegangstb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 378why, one step back378 Vinh TruongMerge branch 'stbds-arraddn' of https://github.com/HeroicKatora/stb into workingSean Barrettbefore adoption67881b6
line 379why, one step back379 Andreas Molzeradd credits for last few PR mergesSean Barrettbefore adoption6f7420a
line 380why, one step back380 github:hashitakustb_ds: move temp_key added by PR into hash_table structure instead of main header structSean Barrettbefore adoption59f06c1
line 381why, one step back381 github:srdjanstipicMerge branch 'working'Sean Barrettbefore adoption6b647c0
line 382why, one step back382 Macoy MadsonMerge branch 'stbds_custom_free_fix' of https://github.com/avennstrom/stb into fooSean Barrettbefore adoption0188581
line 383why, one step back383 Andreas VennstromMerge branch 'master' into workingSean Barrettbefore adoption6e9f34d
line 384why, one step back384 Tobias Mansfield-Williamsadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 385–386why, one step back385*/386Merge branch 'master' into workingSean Barrettbefore adoptiond940053
lines 387–390why, one step back387#ifdef STBDS_UNIT_TESTS388#define _CRT_SECURE_NO_WARNINGS389#endif390add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 391–401why, one step back391#ifndef INCLUDE_STB_DS_H392#define INCLUDE_STB_DS_H393394#include <stddef.h>395#include <string.h>396397#ifndef STBDS_NO_SHORT_NAMES398#define arrlen stbds_arrlen399#define arrlenu stbds_arrlenu400#define arrput stbds_arrput401#define arrpush stbds_arrputMerge branch 'master' of https://github.com/rsachetto/stb into workingSean Barrettbefore adoption8cf9f5a
line 402why, one step back402#define arrpop stbds_arrpopadd stb_ds.h and testsSean Barrettbefore adoption477471d
line 403why, one step back403#define arrfree stbds_arrfreeadd credits for last few PR mergesSean Barrettbefore adoption6f7420a
line 404why, one step back404#define arraddn stbds_arraddn // deprecated, use one of the following instead:Merge branch 'stbds-arraddn' of https://github.com/HeroicKatora/stb into workingSean Barrettbefore adoption67881b6
line 405why, one step back405#define arraddnptr stbds_arraddnptradd credits for last few PR mergesSean Barrettbefore adoption6f7420a
line 406why, one step back406#define arraddnindex stbds_arraddnindexadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 407–419why, one step back407#define arrsetlen stbds_arrsetlen408#define arrlast stbds_arrlast409#define arrins stbds_arrins410#define arrinsn stbds_arrinsn411#define arrdel stbds_arrdel412#define arrdeln stbds_arrdeln413#define arrdelswap stbds_arrdelswap414#define arrcap stbds_arrcap415#define arrsetcap stbds_arrsetcap416417#define hmput stbds_hmput418#define hmputs stbds_hmputs419#define hmget stbds_hmgetstb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 420why, one step back420#define hmget_ts stbds_hmget_tsadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 421–422why, one step back421#define hmgets stbds_hmgets422#define hmgetp stbds_hmgetpstb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 423–424why, one step back423#define hmgetp_ts stbds_hmgetp_ts424#define hmgetp_null stbds_hmgetp_nulladd stb_ds.h and testsSean Barrettbefore adoption477471d
line 425why, one step back425#define hmgeti stbds_hmgetistb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 426why, one step back426#define hmgeti_ts stbds_hmgeti_tsadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 427–434why, one step back427#define hmdel stbds_hmdel428#define hmlen stbds_hmlen429#define hmlenu stbds_hmlenu430#define hmfree stbds_hmfree431#define hmdefault stbds_hmdefault432#define hmdefaults stbds_hmdefaults433434#define shput stbds_shputstb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 435why, one step back435#define shputi stbds_shputiadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 436–437why, one step back436#define shputs stbds_shputs437#define shget stbds_shgetstb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 438why, one step back438#define shgeti stbds_shgetiadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 439–440why, one step back439#define shgets stbds_shgets440#define shgetp stbds_shgetpstb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 441why, one step back441#define shgetp_null stbds_shgetp_nulladd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 442–452why, one step back442#define shdel stbds_shdel443#define shlen stbds_shlen444#define shlenu stbds_shlenu445#define shfree stbds_shfree446#define shdefault stbds_shdefault447#define shdefaults stbds_shdefaults448#define sh_new_arena stbds_sh_new_arena449#define sh_new_strdup stbds_sh_new_strdup450451#define stralloc stbds_stralloc452#define strreset stbds_strresetFix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 453why, one step back453#endifMerge branch 'use-free' of https://github.com/adurdin/stb into workingSean Barrettbefore adoption258c6e1
lines 454–459why, one step back454455#if defined(STBDS_REALLOC) && !defined(STBDS_FREE) || !defined(STBDS_REALLOC) && defined(STBDS_FREE)456#error "You must define both STBDS_REALLOC and STBDS_FREE, or neither."457#endif458#if !defined(STBDS_REALLOC) && !defined(STBDS_FREE)459#include <stdlib.h>tweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
lines 460–461why, one step back460#define STBDS_REALLOC(c,p,s) realloc(p,s)461#define STBDS_FREE(c,p) free(p)Merge branch 'use-free' of https://github.com/adurdin/stb into workingSean Barrettbefore adoption258c6e1
lines 462–463why, one step back462#endif463Merge branch 'unused#801' of https://github.com/hashitaku/stb into testSean Barrettbefore adoption206529e
lines 464–469why, one step back464#ifdef _MSC_VER465#define STBDS_NOTUSED(v) (void)(v)466#else467#define STBDS_NOTUSED(v) (void)sizeof(v)468#endif469add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 470–494why, one step back470#ifdef __cplusplus471extern "C" {472#endif473474// for security against attackers, seed the library with a random number, at least time() but stronger is better475extern void stbds_rand_seed(size_t seed);476477// these are the hash functions used internally if you want to test them or use them for other purposes478extern size_t stbds_hash_bytes(void *p, size_t len, size_t seed);479extern size_t stbds_hash_string(char *str, size_t seed);480481// this is a simple string arena allocator, initialize with e.g. 'stbds_string_arena my_arena={0}'.482typedef struct stbds_string_arena stbds_string_arena;483extern char * stbds_stralloc(stbds_string_arena *a, char *str);484extern void stbds_strreset(stbds_string_arena *a);485486// have to #define STBDS_UNIT_TESTS to call this487extern void stbds_unit_tests(void);488489///////////////490//491// Everything below here is implementation details492//493494extern void * stbds_arrgrowf(void *a, size_t elemsize, size_t addlen, size_t min_cap);Merge branch 'stbds_custom_free_fix' of https://github.com/avennstrom/stb into fooSean Barrettbefore adoption0188581
line 495why, one step back495extern void stbds_arrfreef(void *a);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 496why, one step back496extern void stbds_hmfree_func(void *p, size_t elemsize);add stb_ds.h and testsSean Barrettbefore adoption477471d
line 497why, one step back497extern void * stbds_hmget_key(void *a, size_t elemsize, void *key, size_t keysize, int mode);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 498why, one step back498extern void * stbds_hmget_key_ts(void *a, size_t elemsize, void *key, size_t keysize, ptrdiff_t *temp, int mode);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 499–510why, one step back499extern void * stbds_hmput_default(void *a, size_t elemsize);500extern void * stbds_hmput_key(void *a, size_t elemsize, void *key, size_t keysize, int mode);501extern void * stbds_hmdel_key(void *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode);502extern void * stbds_shmode_func(size_t elemsize, int mode);503504#ifdef __cplusplus505}506#endif507508#if defined(__GNUC__) || defined(__clang__)509#define STBDS_HAS_TYPEOF510#ifdef __cplusplusstb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 511why, one step back511//#define STBDS_HAS_LITERAL_ARRAY // this is currently broken for clangadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 512–522why, one step back512#endif513#endif514515#if !defined(__cplusplus)516#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L517#define STBDS_HAS_LITERAL_ARRAY518#endif519#endif520521// this macro takes the address of the argument, but on gcc/clang can accept rvalues522#if defined(STBDS_HAS_LITERAL_ARRAY) && defined(STBDS_HAS_TYPEOF)stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
lines 523–525why, one step back523 #if __clang__524 #define STBDS_ADDRESSOF(typevar, value) ((__typeof__(typevar)[1]){value}) // literal array decays to pointer to value525 #elseMerge branch 'master' into workingSean Barrettbefore adoptiond940053
line 526why, one step back526 #define STBDS_ADDRESSOF(typevar, value) ((typeof(typevar)[1]){value}) // literal array decays to pointer to valuestb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 527why, one step back527 #endifadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 528–535why, one step back528#else529#define STBDS_ADDRESSOF(typevar, value) &(value)530#endif531532#define STBDS_OFFSETOF(var,field) ((char *) &(var)->field - (char *) (var))533534#define stbds_header(t) ((stbds_array_header *) (t) - 1)535#define stbds_temp(t) stbds_header(t)->tempstb_ds: move temp_key added by PR into hash_table structure instead of main header structSean Barrettbefore adoption59f06c1
line 536why, one step back536#define stbds_temp_key(t) (*(char **) stbds_header(t)->hash_table)add stb_ds.h and testsSean Barrettbefore adoption477471d
line 537why, one step back537Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
lines 538–556why, one step back538#define stbds_arrsetcap(a,n) (stbds_arrgrow(a,0,n))539#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)540#define stbds_arrcap(a) ((a) ? stbds_header(a)->capacity : 0)541#define stbds_arrlen(a) ((a) ? (ptrdiff_t) stbds_header(a)->length : 0)542#define stbds_arrlenu(a) ((a) ? stbds_header(a)->length : 0)543#define stbds_arrput(a,v) (stbds_arrmaybegrow(a,1), (a)[stbds_header(a)->length++] = (v))544#define stbds_arrpush stbds_arrput // synonym545#define stbds_arrpop(a) (stbds_header(a)->length--, (a)[stbds_header(a)->length])546#define stbds_arraddn(a,n) ((void)(stbds_arraddnindex(a, n))) // deprecated, use one of the following instead:547#define stbds_arraddnptr(a,n) (stbds_arrmaybegrow(a,n), (n) ? (stbds_header(a)->length += (n), &(a)[stbds_header(a)->length-(n)]) : (a))548#define stbds_arraddnindex(a,n)(stbds_arrmaybegrow(a,n), (n) ? (stbds_header(a)->length += (n), stbds_header(a)->length-(n)) : stbds_arrlen(a))549#define stbds_arraddnoff stbds_arraddnindex550#define stbds_arrlast(a) ((a)[stbds_header(a)->length-1])551#define stbds_arrfree(a) ((void) ((a) ? STBDS_FREE(NULL,stbds_header(a)) : (void)0), (a)=NULL)552#define stbds_arrdel(a,i) stbds_arrdeln(a,i,1)553#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))554#define stbds_arrdelswap(a,i) ((a)[i] = stbds_arrlast(a), stbds_header(a)->length -= 1)555#define stbds_arrinsn(a,i,n) (stbds_arraddn((a),(n)), memmove(&(a)[(i)+(n)], &(a)[i], sizeof *(a) * (stbds_header(a)->length-(n)-(i))))556#define stbds_arrins(a,i,v) (stbds_arrinsn((a),(i),1), (a)[i]=(v))add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 557–560why, one step back557558#define stbds_arrmaybegrow(a,n) ((!(a) || stbds_header(a)->length + (n) > stbds_header(a)->capacity) \559 ? (stbds_arrgrow(a,n,0),0) : 0)560stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 561why, one step back561#define stbds_arrgrow(a,b,c) ((a) = stbds_arrgrowf_wrapper((a), sizeof *(a), (b), (c)))add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 562–563why, one step back562563#define stbds_hmput(t, k, v) \stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 564why, one step back564 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, 0), \stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 565why, one step back565 (t)[stbds_temp((t)-1)].key = (k), \add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 566–572why, one step back566 (t)[stbds_temp((t)-1)].value = (v))567568#define stbds_hmputs(t, s) \569 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), &(s).key, sizeof (s).key, STBDS_HM_BINARY), \570 (t)[stbds_temp((t)-1)] = (s))571572#define stbds_hmgeti(t,k) \stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 573why, one step back573 ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_HM_BINARY), \add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 574–575why, one step back574 stbds_temp((t)-1))575stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 576–579why, one step back576#define stbds_hmgeti_ts(t,k,temp) \577 ((t) = stbds_hmget_key_ts_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, &(temp), STBDS_HM_BINARY), \578 (temp))579add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 580–582why, one step back580#define stbds_hmgetp(t, k) \581 ((void) stbds_hmgeti(t,k), &(t)[stbds_temp((t)-1)])582stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 583–585why, one step back583#define stbds_hmgetp_ts(t, k, temp) \584 ((void) stbds_hmgeti_ts(t,k,temp), &(t)[temp])585add stb_ds.h and testsSean Barrettbefore adoption477471d
line 586why, one step back586#define stbds_hmdel(t,k) \stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 587why, one step back587 (((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)add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 588–595why, one step back588589#define stbds_hmdefault(t, v) \590 ((t) = stbds_hmput_default_wrapper((t), sizeof *(t)), (t)[-1].value = (v))591592#define stbds_hmdefaults(t, s) \593 ((t) = stbds_hmput_default_wrapper((t), sizeof *(t)), (t)[-1] = (s))594595#define stbds_hmfree(p) \stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 596why, one step back596 ((void) ((p) != NULL ? stbds_hmfree_func((p)-1,sizeof*(p)),0 : 0),(p)=NULL)add stb_ds.h and testsSean Barrettbefore adoption477471d
line 597why, one step back597stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 598–602why, one step back598#define stbds_hmgets(t, k) (*stbds_hmgetp(t,k))599#define stbds_hmget(t, k) (stbds_hmgetp(t,k)->value)600#define stbds_hmget_ts(t, k, temp) (stbds_hmgetp_ts(t,k,temp)->value)601#define stbds_hmlen(t) ((t) ? (ptrdiff_t) stbds_header((t)-1)->length-1 : 0)602#define stbds_hmlenu(t) ((t) ? stbds_header((t)-1)->length-1 : 0)stb_ds: fix arrisn, shgetp_nullSean Barrettbefore adoption781609b
line 603why, one step back603#define stbds_hmgetp_null(t,k) (stbds_hmgeti(t,k) == -1 ? NULL : &(t)[stbds_temp((t)-1)])add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 604–605why, one step back604605#define stbds_shput(t, k, v) \stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 606why, one step back606 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 607why, one step back607 (t)[stbds_temp((t)-1)].value = (v))add stb_ds.h and testsSean Barrettbefore adoption477471d
line 608why, one step back608stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 609–612why, one step back609#define stbds_shputi(t, k, v) \610 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \611 (t)[stbds_temp((t)-1)].value = (v), stbds_temp((t)-1))612add stb_ds.h and testsSean Barrettbefore adoption477471d
line 613why, one step back613#define stbds_shputs(t, s) \stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 614why, one step back614 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (s).key, sizeof (s).key, STBDS_HM_STRING), \Merge branch 'master' of https://github.com/srdjanstipic/stb into workingSean Barrettbefore adoptionef23148
line 615why, one step back615 (t)[stbds_temp((t)-1)] = (s), \stb_ds: move temp_key added by PR into hash_table structure instead of main header structSean Barrettbefore adoption59f06c1
line 616why, one step back616 (t)[stbds_temp((t)-1)].key = stbds_temp_key((t)-1)) // above line overwrites whole structure, so must rewrite key here if it was allocated internallyadd stb_ds.h and testsSean Barrettbefore adoption477471d
line 617why, one step back617stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 618–621why, one step back618#define stbds_pshput(t, p) \619 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (p)->key, sizeof (p)->key, STBDS_HM_PTR_TO_STRING), \620 (t)[stbds_temp((t)-1)] = (p))621add stb_ds.h and testsSean Barrettbefore adoption477471d
line 622why, one step back622#define stbds_shgeti(t,k) \stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 623why, one step back623 ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 624why, one step back624 stbds_temp((t)-1))add stb_ds.h and testsSean Barrettbefore adoption477471d
line 625why, one step back625stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 626–629why, one step back626#define stbds_pshgeti(t,k) \627 ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (*(t))->key, STBDS_HM_PTR_TO_STRING), \628 stbds_temp((t)-1))629add stb_ds.h and testsSean Barrettbefore adoption477471d
line 630why, one step back630#define stbds_shgetp(t, k) \stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 631why, one step back631 ((void) stbds_shgeti(t,k), &(t)[stbds_temp((t)-1)])add stb_ds.h and testsSean Barrettbefore adoption477471d
line 632why, one step back632stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 633–635why, one step back633#define stbds_pshget(t, k) \634 ((void) stbds_pshgeti(t,k), (t)[stbds_temp((t)-1)])635add stb_ds.h and testsSean Barrettbefore adoption477471d
line 636why, one step back636#define stbds_shdel(t,k) \stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 637why, one step back637 (((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)stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 638–639why, one step back638#define stbds_pshdel(t,k) \639 (((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)add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 640–645why, one step back640641#define stbds_sh_new_arena(t) \642 ((t) = stbds_shmode_func_wrapper(t, sizeof *(t), STBDS_SH_ARENA))643#define stbds_sh_new_strdup(t) \644 ((t) = stbds_shmode_func_wrapper(t, sizeof *(t), STBDS_SH_STRDUP))645stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 646why, one step back646#define stbds_shdefault(t, v) stbds_hmdefault(t,v)add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 647–653why, one step back647#define stbds_shdefaults(t, s) stbds_hmdefaults(t,s)648649#define stbds_shfree stbds_hmfree650#define stbds_shlenu stbds_hmlenu651652#define stbds_shgets(t, k) (*stbds_shgetp(t,k))653#define stbds_shget(t, k) (stbds_shgetp(t,k)->value)stb_ds: fix arrisn, shgetp_nullSean Barrettbefore adoption781609b
line 654why, one step back654#define stbds_shgetp_null(t,k) (stbds_shgeti(t,k) == -1 ? NULL : &(t)[stbds_temp((t)-1)])add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 655–678why, one step back655#define stbds_shlen stbds_hmlen656657typedef struct658{659 size_t length;660 size_t capacity;661 void * hash_table;662 ptrdiff_t temp;663} stbds_array_header;664665typedef struct stbds_string_block666{667 struct stbds_string_block *next;668 char storage[8];669} stbds_string_block;670671struct stbds_string_arena672{673 stbds_string_block *storage;674 size_t remaining;675 unsigned char block;676 unsigned char mode; // this isn't used by the string arena itself677};678stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 679–680why, one step back679#define STBDS_HM_BINARY 0680#define STBDS_HM_STRING 1add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 681–684why, one step back681682enum683{684 STBDS_SH_NONE,stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 685why, one step back685 STBDS_SH_DEFAULT,add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 686–698why, one step back686 STBDS_SH_STRDUP,687 STBDS_SH_ARENA688};689690#ifdef __cplusplus691// in C we use implicit assignment from these void*-returning functions to T*.692// in C++ these templates make the same code work693template<class T> static T * stbds_arrgrowf_wrapper(T *a, size_t elemsize, size_t addlen, size_t min_cap) {694 return (T*)stbds_arrgrowf((void *)a, elemsize, addlen, min_cap);695}696template<class T> static T * stbds_hmget_key_wrapper(T *a, size_t elemsize, void *key, size_t keysize, int mode) {697 return (T*)stbds_hmget_key((void*)a, elemsize, key, keysize, mode);698}stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 699why, one step back699template<class T> static T * stbds_hmget_key_ts_wrapper(T *a, size_t elemsize, void *key, size_t keysize, ptrdiff_t *temp, int mode) {Fix typo in stbds_hmget_key_ts_wrapperNerdbefore adoption498bd3e
line 700why, one step back700 return (T*)stbds_hmget_key_ts((void*)a, elemsize, key, keysize, temp, mode);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 701why, one step back701}add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 702–711why, one step back702template<class T> static T * stbds_hmput_default_wrapper(T *a, size_t elemsize) {703 return (T*)stbds_hmput_default((void *)a, elemsize);704}705template<class T> static T * stbds_hmput_key_wrapper(T *a, size_t elemsize, void *key, size_t keysize, int mode) {706 return (T*)stbds_hmput_key((void*)a, elemsize, key, keysize, mode);707}708template<class T> static T * stbds_hmdel_key_wrapper(T *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode){709 return (T*)stbds_hmdel_key((void*)a, elemsize, key, keysize, keyoffset, mode);710}711template<class T> static T * stbds_shmode_func_wrapper(T *, size_t elemsize, int mode) {stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 712why, one step back712 return (T*)stbds_shmode_func(elemsize, mode);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 713–716why, one step back713}714#else715#define stbds_arrgrowf_wrapper stbds_arrgrowf716#define stbds_hmget_key_wrapper stbds_hmget_keystb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 717why, one step back717#define stbds_hmget_key_ts_wrapper stbds_hmget_key_tsadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 718–736why, one step back718#define stbds_hmput_default_wrapper stbds_hmput_default719#define stbds_hmput_key_wrapper stbds_hmput_key720#define stbds_hmdel_key_wrapper stbds_hmdel_key721#define stbds_shmode_func_wrapper(t,e,m) stbds_shmode_func(e,m)722#endif723724#endif // INCLUDE_STB_DS_H725726727//////////////////////////////////////////////////////////////////////////////728//729// IMPLEMENTATION730//731732#ifdef STB_DS_IMPLEMENTATION733#include <assert.h>734#include <string.h>735736#ifndef STBDS_ASSERTMerge branch 'master' into workingSean Barrettbefore adoptiond940053
line 737why, one step back737#define STBDS_ASSERT_WAS_UNDEFINEDadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 738–758why, one step back738#define STBDS_ASSERT(x) ((void) 0)739#endif740741#ifdef STBDS_STATISTICS742#define STBDS_STATS(x) x743size_t stbds_array_grow;744size_t stbds_hash_grow;745size_t stbds_hash_shrink;746size_t stbds_hash_rebuild;747size_t stbds_hash_probes;748size_t stbds_hash_alloc;749size_t stbds_rehash_probes;750size_t stbds_rehash_items;751#else752#define STBDS_STATS(x)753#endif754755//756// stbds_arr implementation757//758stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 759–761why, one step back759//int *prev_allocs[65536];760//int num_prev;761add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 762–763why, one step back762void *stbds_arrgrowf(void *a, size_t elemsize, size_t addlen, size_t min_cap)763{stb_ds: fix arrisn, shgetp_nullSean Barrettbefore adoption781609b
line 764why, one step back764 stbds_array_header temp={0}; // force debuggingadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 765–766why, one step back765 void *b;766 size_t min_len = stbds_arrlen(a) + addlen;stb_ds: fix arrisn, shgetp_nullSean Barrettbefore adoption781609b
line 767why, one step back767 (void) sizeof(temp);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 768–781why, one step back768769 // compute the minimum capacity needed770 if (min_len > min_cap)771 min_cap = min_len;772773 if (min_cap <= stbds_arrcap(a))774 return a;775776 // increase needed capacity to guarantee O(1) amortized777 if (min_cap < 2 * stbds_arrcap(a))778 min_cap = 2 * stbds_arrcap(a);779 else if (min_cap < 4)780 min_cap = 4;781stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 782–784why, one step back782 //if (num_prev < 65536) if (a) prev_allocs[num_prev++] = (int *) ((char *) a+1);783 //if (num_prev == 2201)784 // num_prev = num_prev;tweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
line 785why, one step back785 b = STBDS_REALLOC(NULL, (a) ? stbds_header(a) : 0, elemsize * min_cap + sizeof(stbds_array_header));stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 786why, one step back786 //if (num_prev < 65536) prev_allocs[num_prev++] = (int *) (char *) b;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 787–790why, one step back787 b = (char *) b + sizeof(stbds_array_header);788 if (a == NULL) {789 stbds_header(b)->length = 0;790 stbds_header(b)->hash_table = 0;stb_ds: fix arrisn, shgetp_nullSean Barrettbefore adoption781609b
line 791why, one step back791 stbds_header(b)->temp = 0;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 792–795why, one step back792 } else {793 STBDS_STATS(++stbds_array_grow);794 }795 stbds_header(b)->capacity = min_cap;stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 796why, one step back796add stb_ds.h and testsSean Barrettbefore adoption477471d
line 797why, one step back797 return b;Merge branch 'stbds_custom_free_fix' of https://github.com/avennstrom/stb into fooSean Barrettbefore adoption0188581
lines 798–802why, one step back798}799800void stbds_arrfreef(void *a)801{802 STBDS_FREE(NULL, stbds_header(a));add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 803–808why, one step back803}804805//806// stbds_hm hash table implementation807//808Merge branch 'master' into workingSean Barrettbefore adoptiond940053
lines 809–811why, one step back809#ifdef STBDS_INTERNAL_SMALL_BUCKET810#define STBDS_BUCKET_LENGTH 4811#elseadd stb_ds.h and testsSean Barrettbefore adoption477471d
line 812why, one step back812#define STBDS_BUCKET_LENGTH 8Merge branch 'master' into workingSean Barrettbefore adoptiond940053
lines 813–815why, one step back813#endif814815#define STBDS_BUCKET_SHIFT (STBDS_BUCKET_LENGTH == 8 ? 3 : 2)add stb_ds.h and testsSean Barrettbefore adoption477471d
line 816why, one step back816#define STBDS_BUCKET_MASK (STBDS_BUCKET_LENGTH-1)Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 817why, one step back817#define STBDS_CACHE_LINE_SIZE 64add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 818–828why, one step back818819#define STBDS_ALIGN_FWD(n,a) (((n) + (a) - 1) & ~((a)-1))820821typedef struct822{823 size_t hash [STBDS_BUCKET_LENGTH];824 ptrdiff_t index[STBDS_BUCKET_LENGTH];825} stbds_hash_bucket; // in 32-bit, this is one 64-byte cache line; in 64-bit, each array is one 64-byte cache line826827typedef struct828{stb_ds: move temp_key added by PR into hash_table structure instead of main header structSean Barrettbefore adoption59f06c1
line 829why, one step back829 char * temp_key; // this MUST be the first field of the hash tableadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 830–864why, one step back830 size_t slot_count;831 size_t used_count;832 size_t used_count_threshold;833 size_t used_count_shrink_threshold;834 size_t tombstone_count;835 size_t tombstone_count_threshold;836 size_t seed;837 size_t slot_count_log2;838 stbds_string_arena string;839 stbds_hash_bucket *storage; // not a separate allocation, just 64-byte aligned storage after this struct840} stbds_hash_index;841842#define STBDS_INDEX_EMPTY -1843#define STBDS_INDEX_DELETED -2844#define STBDS_INDEX_IN_USE(x) ((x) >= 0)845846#define STBDS_HASH_EMPTY 0847#define STBDS_HASH_DELETED 1848849static size_t stbds_hash_seed=0x31415926;850851void stbds_rand_seed(size_t seed)852{853 stbds_hash_seed = seed;854}855856#define stbds_load_32_or_64(var, temp, v32, v64_hi, v64_lo) \857 temp = v64_lo ^ v32, temp <<= 16, temp <<= 16, temp >>= 16, temp >>= 16, /* discard if 32-bit */ \858 var = v64_hi, var <<= 16, var <<= 16, /* discard if 32-bit */ \859 var ^= temp ^ v32860861#define STBDS_SIZE_T_BITS ((sizeof (size_t)) * 8)862863static size_t stbds_probe_position(size_t hash, size_t slot_count, size_t slot_log2)864{Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 865why, one step back865 size_t pos;fix PR to work on VC6Sean Barrettbefore adoptionb3a74a5
line 866why, one step back866 STBDS_NOTUSED(slot_log2);Merge branch 'master' into workingSean Barrettbefore adoptiond940053
lines 867–869why, one step back867 pos = hash & (slot_count-1);868 #ifdef STBDS_INTERNAL_BUCKET_START869 pos &= ~STBDS_BUCKET_MASK;add stb_ds.h and testsSean Barrettbefore adoption477471d
line 870why, one step back870 #endifMerge branch 'master' into workingSean Barrettbefore adoptiond940053
line 871why, one step back871 return pos;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 872–886why, one step back872}873874static size_t stbds_log2(size_t slot_count)875{876 size_t n=0;877 while (slot_count > 1) {878 slot_count >>= 1;879 ++n;880 }881 return n;882}883884static stbds_hash_index *stbds_make_hash_index(size_t slot_count, stbds_hash_index *ot)885{886 stbds_hash_index *t;tweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
line 887why, one step back887 t = (stbds_hash_index *) STBDS_REALLOC(NULL,0,(slot_count >> STBDS_BUCKET_SHIFT) * sizeof(stbds_hash_bucket) + sizeof(stbds_hash_index) + STBDS_CACHE_LINE_SIZE-1);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 888–919why, one step back888 t->storage = (stbds_hash_bucket *) STBDS_ALIGN_FWD((size_t) (t+1), STBDS_CACHE_LINE_SIZE);889 t->slot_count = slot_count;890 t->slot_count_log2 = stbds_log2(slot_count);891 t->tombstone_count = 0;892 t->used_count = 0;893894 #if 0 // A1895 t->used_count_threshold = slot_count*12/16; // if 12/16th of table is occupied, grow896 t->tombstone_count_threshold = slot_count* 2/16; // if tombstones are 2/16th of table, rebuild897 t->used_count_shrink_threshold = slot_count* 4/16; // if table is only 4/16th full, shrink898 #elif 1 // A2899 //t->used_count_threshold = slot_count*12/16; // if 12/16th of table is occupied, grow900 //t->tombstone_count_threshold = slot_count* 3/16; // if tombstones are 3/16th of table, rebuild901 //t->used_count_shrink_threshold = slot_count* 4/16; // if table is only 4/16th full, shrink902903 // compute without overflowing904 t->used_count_threshold = slot_count - (slot_count>>2);905 t->tombstone_count_threshold = (slot_count>>3) + (slot_count>>4);906 t->used_count_shrink_threshold = slot_count >> 2;907908 #elif 0 // B1909 t->used_count_threshold = slot_count*13/16; // if 13/16th of table is occupied, grow910 t->tombstone_count_threshold = slot_count* 2/16; // if tombstones are 2/16th of table, rebuild911 t->used_count_shrink_threshold = slot_count* 5/16; // if table is only 5/16th full, shrink912 #else // C1913 t->used_count_threshold = slot_count*14/16; // if 14/16th of table is occupied, grow914 t->tombstone_count_threshold = slot_count* 2/16; // if tombstones are 2/16th of table, rebuild915 t->used_count_shrink_threshold = slot_count* 6/16; // if table is only 6/16th full, shrink916 #endif917 // Following statistics were measured on a Core i7-6700 @ 4.00Ghz, compiled with clang 7.0.1 -O2918 // Note that the larger tables have high variance as they were run fewer times919 // A1 A2 B1 C1Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
lines 920–926why, one step back920 // 0.10ms : 0.10ms : 0.10ms : 0.11ms : 2,000 inserts creating 2K table921 // 0.96ms : 0.95ms : 0.97ms : 1.04ms : 20,000 inserts creating 20K table922 // 14.48ms : 14.46ms : 10.63ms : 11.00ms : 200,000 inserts creating 200K table923 // 195.74ms : 196.35ms : 203.69ms : 214.92ms : 2,000,000 inserts creating 2M table924 // 2193.88ms : 2209.22ms : 2285.54ms : 2437.17ms : 20,000,000 inserts creating 20M table925 // 65.27ms : 53.77ms : 65.33ms : 65.47ms : 500,000 inserts & deletes in 2K table926 // 72.78ms : 62.45ms : 71.95ms : 72.85ms : 500,000 inserts & deletes in 20K tableadd stb_ds.h and testsSean Barrettbefore adoption477471d
line 927why, one step back927 // 89.47ms : 77.72ms : 96.49ms : 96.75ms : 500,000 inserts & deletes in 200K tableFix trailing whitespaceSean Barrettbefore adoption2bb4a0a
lines 928–929why, one step back928 // 97.58ms : 98.14ms : 97.18ms : 97.53ms : 500,000 inserts & deletes in 2M table929 // 118.61ms : 119.62ms : 120.16ms : 118.86ms : 500,000 inserts & deletes in 20M tableadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 930–943why, one step back930 // 192.11ms : 194.39ms : 196.38ms : 195.73ms : 500,000 inserts & deletes in 200M table931932 if (slot_count <= STBDS_BUCKET_LENGTH)933 t->used_count_shrink_threshold = 0;934 // to avoid infinite loop, we need to guarantee that at least one slot is empty and will terminate probes935 STBDS_ASSERT(t->used_count_threshold + t->tombstone_count_threshold < t->slot_count);936 STBDS_STATS(++stbds_hash_alloc);937 if (ot) {938 t->string = ot->string;939 // reuse old seed so we can reuse old hashes so below "copy out old data" doesn't do any hashing940 t->seed = ot->seed;941 } else {942 size_t a,b,temp;943 memset(&t->string, 0, sizeof(t->string));fix accidental check-in of typo in stb_ds.hSean Barrettbefore adoption40adb99
line 944why, one step back944 t->seed = stbds_hash_seed;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 945–1000why, one step back945 // LCG946 // in 32-bit, a = 2147001325 b = 715136305947 // in 64-bit, a = 2862933555777941757 b = 3037000493948 stbds_load_32_or_64(a,temp, 2147001325, 0x27bb2ee6, 0x87b0b0fd);949 stbds_load_32_or_64(b,temp, 715136305, 0, 0xb504f32d);950 stbds_hash_seed = stbds_hash_seed * a + b;951 }952953 {954 size_t i,j;955 for (i=0; i < slot_count >> STBDS_BUCKET_SHIFT; ++i) {956 stbds_hash_bucket *b = &t->storage[i];957 for (j=0; j < STBDS_BUCKET_LENGTH; ++j)958 b->hash[j] = STBDS_HASH_EMPTY;959 for (j=0; j < STBDS_BUCKET_LENGTH; ++j)960 b->index[j] = STBDS_INDEX_EMPTY;961 }962 }963964 // copy out the old data, if any965 if (ot) {966 size_t i,j;967 t->used_count = ot->used_count;968 for (i=0; i < ot->slot_count >> STBDS_BUCKET_SHIFT; ++i) {969 stbds_hash_bucket *ob = &ot->storage[i];970 for (j=0; j < STBDS_BUCKET_LENGTH; ++j) {971 if (STBDS_INDEX_IN_USE(ob->index[j])) {972 size_t hash = ob->hash[j];973 size_t pos = stbds_probe_position(hash, t->slot_count, t->slot_count_log2);974 size_t step = STBDS_BUCKET_LENGTH;975 STBDS_STATS(++stbds_rehash_items);976 for (;;) {977 size_t limit,z;978 stbds_hash_bucket *bucket;979 bucket = &t->storage[pos >> STBDS_BUCKET_SHIFT];980 STBDS_STATS(++stbds_rehash_probes);981982 for (z=pos & STBDS_BUCKET_MASK; z < STBDS_BUCKET_LENGTH; ++z) {983 if (bucket->hash[z] == 0) {984 bucket->hash[z] = hash;985 bucket->index[z] = ob->index[j];986 goto done;987 }988 }989990 limit = pos & STBDS_BUCKET_MASK;991 for (z = 0; z < limit; ++z) {992 if (bucket->hash[z] == 0) {993 bucket->hash[z] = hash;994 bucket->index[z] = ob->index[j];995 goto done;996 }997 }998999 pos += step; // quadratic probing1000 step += STBDS_BUCKET_LENGTH;Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1001why, one step back1001 pos &= (t->slot_count-1);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1002–1045why, one step back1002 }1003 }1004 done:1005 ;1006 }1007 }1008 }10091010 return t;1011}10121013#define STBDS_ROTATE_LEFT(val, n) (((val) << (n)) | ((val) >> (STBDS_SIZE_T_BITS - (n))))1014#define STBDS_ROTATE_RIGHT(val, n) (((val) >> (n)) | ((val) << (STBDS_SIZE_T_BITS - (n))))10151016size_t stbds_hash_string(char *str, size_t seed)1017{1018 size_t hash = seed;1019 while (*str)1020 hash = STBDS_ROTATE_LEFT(hash, 9) + (unsigned char) *str++;10211022 // Thomas Wang 64-to-32 bit mix function, hopefully also works in 32 bits1023 hash ^= seed;1024 hash = (~hash) + (hash << 18);1025 hash ^= hash ^ STBDS_ROTATE_RIGHT(hash,31);1026 hash = hash * 21;1027 hash ^= hash ^ STBDS_ROTATE_RIGHT(hash,11);1028 hash += (hash << 6);1029 hash ^= STBDS_ROTATE_RIGHT(hash,22);1030 return hash+seed;1031}10321033#ifdef STBDS_SIPHASH_2_41034#define STBDS_SIPHASH_C_ROUNDS 21035#define STBDS_SIPHASH_D_ROUNDS 41036typedef int STBDS_SIPHASH_2_4_can_only_be_used_in_64_bit_builds[sizeof(size_t) == 8 ? 1 : -1];1037#endif10381039#ifndef STBDS_SIPHASH_C_ROUNDS1040#define STBDS_SIPHASH_C_ROUNDS 11041#endif1042#ifndef STBDS_SIPHASH_D_ROUNDS1043#define STBDS_SIPHASH_D_ROUNDS 11044#endif1045stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 1046–1050why, one step back1046#ifdef _MSC_VER1047#pragma warning(push)1048#pragma warning(disable:4127) // conditional expression is constant, for do..while(0) and sizeof()==1049#endif1050add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1051–1061why, one step back1051static size_t stbds_siphash_bytes(void *p, size_t len, size_t seed)1052{1053 unsigned char *d = (unsigned char *) p;1054 size_t i,j;1055 size_t v0,v1,v2,v3, data;10561057 // hash that works on 32- or 64-bit registers without knowing which we have1058 // (computes different results on 32-bit and 64-bit platform)1059 // derived from siphash, but on 32-bit platforms very different as it uses 4 32-bit state not 4 64-bit1060 v0 = ((((size_t) 0x736f6d65 << 16) << 16) + 0x70736575) ^ seed;1061 v1 = ((((size_t) 0x646f7261 << 16) << 16) + 0x6e646f6d) ^ ~seed;Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 1062why, one step back1062 v2 = ((((size_t) 0x6c796765 << 16) << 16) + 0x6e657261) ^ seed;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1063–1091why, one step back1063 v3 = ((((size_t) 0x74656462 << 16) << 16) + 0x79746573) ^ ~seed;10641065 #ifdef STBDS_TEST_SIPHASH_2_41066 // hardcoded with key material in the siphash test vectors1067 v0 ^= 0x0706050403020100ull ^ seed;1068 v1 ^= 0x0f0e0d0c0b0a0908ull ^ ~seed;1069 v2 ^= 0x0706050403020100ull ^ seed;1070 v3 ^= 0x0f0e0d0c0b0a0908ull ^ ~seed;1071 #endif10721073 #define STBDS_SIPROUND() \1074 do { \1075 v0 += v1; v1 = STBDS_ROTATE_LEFT(v1, 13); v1 ^= v0; v0 = STBDS_ROTATE_LEFT(v0,STBDS_SIZE_T_BITS/2); \1076 v2 += v3; v3 = STBDS_ROTATE_LEFT(v3, 16); v3 ^= v2; \1077 v2 += v1; v1 = STBDS_ROTATE_LEFT(v1, 17); v1 ^= v2; v2 = STBDS_ROTATE_LEFT(v2,STBDS_SIZE_T_BITS/2); \1078 v0 += v3; v3 = STBDS_ROTATE_LEFT(v3, 21); v3 ^= v0; \1079 } while (0)10801081 for (i=0; i+sizeof(size_t) <= len; i += sizeof(size_t), d += sizeof(size_t)) {1082 data = d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);1083 data |= (size_t) (d[4] | (d[5] << 8) | (d[6] << 16) | (d[7] << 24)) << 16 << 16; // discarded if size_t == 410841085 v3 ^= data;1086 for (j=0; j < STBDS_SIPHASH_C_ROUNDS; ++j)1087 STBDS_SIPROUND();1088 v0 ^= data;1089 }1090 data = len << (STBDS_SIZE_T_BITS-8);1091 switch (len - i) {Merge branch 'master' of https://github.com/MarcoLizza/stb into workingSean Barrettbefore adoptionefdaadc
lines 1092–1098why, one step back1092 case 7: data |= ((size_t) d[6] << 24) << 24; // fall through1093 case 6: data |= ((size_t) d[5] << 20) << 20; // fall through1094 case 5: data |= ((size_t) d[4] << 16) << 16; // fall through1095 case 4: data |= (d[3] << 24); // fall through1096 case 3: data |= (d[2] << 16); // fall through1097 case 2: data |= (d[1] << 8); // fall through1098 case 1: data |= d[0]; // fall throughadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1099–1107why, one step back1099 case 0: break;1100 }1101 v3 ^= data;1102 for (j=0; j < STBDS_SIPHASH_C_ROUNDS; ++j)1103 STBDS_SIPROUND();1104 v0 ^= data;1105 v2 ^= 0xff;1106 for (j=0; j < STBDS_SIPHASH_D_ROUNDS; ++j)1107 STBDS_SIPROUND();stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1108why, one step back1108add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1109–1111why, one step back1109#ifdef STBDS_SIPHASH_2_41110 return v0^v1^v2^v3;1111#elseMerge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1112why, one step back1112 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 replyadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1113–1126why, one step back1113#endif1114}11151116size_t stbds_hash_bytes(void *p, size_t len, size_t seed)1117{1118#ifdef STBDS_SIPHASH_2_41119 return stbds_siphash_bytes(p,len,seed);1120#else1121 unsigned char *d = (unsigned char *) p;11221123 if (len == 4) {1124 unsigned int hash = d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);1125 #if 01126 // HASH32-A Bob Jenkin's hash function w/o large constantsMerge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1127why, one step back1127 hash ^= seed;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1128–1130why, one step back1128 hash -= (hash<<6);1129 hash ^= (hash>>17);1130 hash -= (hash<<9);Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1131why, one step back1131 hash ^= seed;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1132–1139why, one step back1132 hash ^= (hash<<4);1133 hash -= (hash<<3);1134 hash ^= (hash<<10);1135 hash ^= (hash>>15);1136 #elif 11137 // HASH32-BB Bob Jenkin's presumably-accidental version of Thomas Wang hash with rotates turned into shifts.1138 // Note that converting these back to rotates makes it run a lot slower, presumably due to collisions, so I'm1139 // not really sure what's going on.Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1140why, one step back1140 hash ^= seed;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1141–1144why, one step back1141 hash = (hash ^ 61) ^ (hash >> 16);1142 hash = hash + (hash << 3);1143 hash = hash ^ (hash >> 4);1144 hash = hash * 0x27d4eb2d;Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1145why, one step back1145 hash ^= seed;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1146–1147why, one step back1146 hash = hash ^ (hash >> 15);1147 #else // HASH32-C - Murmur3Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1148why, one step back1148 hash ^= seed;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1149–1156why, one step back1149 hash *= 0xcc9e2d51;1150 hash = (hash << 17) | (hash >> 15);1151 hash *= 0x1b873593;1152 hash ^= seed;1153 hash = (hash << 19) | (hash >> 13);1154 hash = hash*5 + 0xe6546b64;1155 hash ^= hash >> 16;1156 hash *= 0x85ebca6b;Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1157why, one step back1157 hash ^= seed;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1158–1164why, one step back1158 hash ^= hash >> 13;1159 hash *= 0xc2b2ae35;1160 hash ^= hash >> 16;1161 #endif1162 // Following statistics were measured on a Core i7-6700 @ 4.00Ghz, compiled with clang 7.0.1 -O21163 // Note that the larger tables have high variance as they were run fewer times1164 // HASH32-A // HASH32-BB // HASH32-CFix trailing whitespaceSean Barrettbefore adoption2bb4a0a
lines 1165–1171why, one step back1165 // 0.10ms // 0.10ms // 0.10ms : 2,000 inserts creating 2K table1166 // 0.96ms // 0.95ms // 0.99ms : 20,000 inserts creating 20K table1167 // 14.69ms // 14.43ms // 14.97ms : 200,000 inserts creating 200K table1168 // 199.99ms // 195.36ms // 202.05ms : 2,000,000 inserts creating 2M table1169 // 2234.84ms // 2187.74ms // 2240.38ms : 20,000,000 inserts creating 20M table1170 // 55.68ms // 53.72ms // 57.31ms : 500,000 inserts & deletes in 2K table1171 // 63.43ms // 61.99ms // 65.73ms : 500,000 inserts & deletes in 20K tableadd stb_ds.h and testsSean Barrettbefore adoption477471d
line 1172why, one step back1172 // 80.04ms // 77.96ms // 81.83ms : 500,000 inserts & deletes in 200K tableFix trailing whitespaceSean Barrettbefore adoption2bb4a0a
lines 1173–1174why, one step back1173 // 100.42ms // 97.40ms // 102.39ms : 500,000 inserts & deletes in 2M table1174 // 119.71ms // 120.59ms // 121.63ms : 500,000 inserts & deletes in 20M tableadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1175–1179why, one step back1175 // 185.28ms // 195.15ms // 187.74ms : 500,000 inserts & deletes in 200M table1176 // 15.58ms // 14.79ms // 15.52ms : 200,000 inserts creating 200K table with varying key spacing11771178 return (((size_t) hash << 16 << 16) | hash) ^ seed;1179 } else if (len == 8 && sizeof(size_t) == 8) {stb_ds: fix shift warnings in later VS compilersSean Barrettbefore adoption1aeb8ec
lines 1180–1181why, one step back1180 size_t hash = d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);1181 hash |= (size_t) (d[4] | (d[5] << 8) | (d[6] << 16) | (d[7] << 24)) << 16 << 16; // avoid warning if size_t == 4Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1182why, one step back1182 hash ^= seed;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1183–1186why, one step back1183 hash = (~hash) + (hash << 21);1184 hash ^= STBDS_ROTATE_RIGHT(hash,24);1185 hash *= 265;1186 hash ^= STBDS_ROTATE_RIGHT(hash,14);Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1187why, one step back1187 hash ^= seed;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1188–1191why, one step back1188 hash *= 21;1189 hash ^= STBDS_ROTATE_RIGHT(hash,28);1190 hash += (hash << 31);1191 hash = (~hash) + (hash << 18);Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1192why, one step back1192 return hash;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1193–1197why, one step back1193 } else {1194 return stbds_siphash_bytes(p,len,seed);1195 }1196#endif1197}stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 1198–1200why, one step back1198#ifdef _MSC_VER1199#pragma warning(pop)1200#endifadd stb_ds.h and testsSean Barrettbefore adoption477471d
line 1201why, one step back1201stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 1202–1203why, one step back12021203static int stbds_is_key_equal(void *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode, size_t i)add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1204–1205why, one step back1204{1205 if (mode >= STBDS_HM_STRING)stb_ds: use keyoffset in key comparisonSean Barrettbefore adoptiondfdb7d9
line 1206why, one step back1206 return 0==strcmp((char *) key, * (char **) ((char *) a + elemsize*i + keyoffset));add stb_ds.h and testsSean Barrettbefore adoption477471d
line 1207why, one step back1207 elsestb_ds: use keyoffset in key comparisonSean Barrettbefore adoptiondfdb7d9
line 1208why, one step back1208 return 0==memcmp(key, (char *) a + elemsize*i + keyoffset, keysize);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1209–1214why, one step back1209}12101211#define STBDS_HASH_TO_ARR(x,elemsize) ((char*) (x) - (elemsize))1212#define STBDS_ARR_TO_HASH(x,elemsize) ((char*) (x) + (elemsize))12131214#define stbds_hash_table(a) ((stbds_hash_index *) stbds_header(a)->hash_table)Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 1215why, one step back1215stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1216why, one step back1216void stbds_hmfree_func(void *a, size_t elemsize)add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1217–1219why, one step back1217{1218 if (a == NULL) return;1219 if (stbds_hash_table(a) != NULL) {stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 1220–1227why, one step back1220 if (stbds_hash_table(a)->string.mode == STBDS_SH_STRDUP) {1221 size_t i;1222 // skip 0th element, which is default1223 for (i=1; i < stbds_header(a)->length; ++i)1224 STBDS_FREE(NULL, *(char**) ((char *) a + elemsize*i));1225 }1226 stbds_strreset(&stbds_hash_table(a)->string);1227 }tweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
lines 1228–1229why, one step back1228 STBDS_FREE(NULL, stbds_header(a)->hash_table);1229 STBDS_FREE(NULL, stbds_header(a));add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1230–1231why, one step back1230}1231stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1232why, one step back1232static ptrdiff_t stbds_hm_find_slot(void *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode)add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1233–1252why, one step back1233{1234 void *raw_a = STBDS_HASH_TO_ARR(a,elemsize);1235 stbds_hash_index *table = stbds_hash_table(raw_a);1236 size_t hash = mode >= STBDS_HM_STRING ? stbds_hash_string((char*)key,table->seed) : stbds_hash_bytes(key, keysize,table->seed);1237 size_t step = STBDS_BUCKET_LENGTH;1238 size_t limit,i;1239 size_t pos;1240 stbds_hash_bucket *bucket;12411242 if (hash < 2) hash += 2; // stored hash values are forbidden from being 0, so we can detect empty slots12431244 pos = stbds_probe_position(hash, table->slot_count, table->slot_count_log2);12451246 for (;;) {1247 STBDS_STATS(++stbds_hash_probes);1248 bucket = &table->storage[pos >> STBDS_BUCKET_SHIFT];12491250 // start searching from pos to end of bucket, this should help performance on small hash tables that fit in cache1251 for (i=pos & STBDS_BUCKET_MASK; i < STBDS_BUCKET_LENGTH; ++i) {1252 if (bucket->hash[i] == hash) {stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1253why, one step back1253 if (stbds_is_key_equal(a, elemsize, key, keysize, keyoffset, mode, bucket->index[i])) {add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1254–1264why, one step back1254 return (pos & ~STBDS_BUCKET_MASK)+i;1255 }1256 } else if (bucket->hash[i] == STBDS_HASH_EMPTY) {1257 return -1;1258 }1259 }12601261 // search from beginning of bucket to pos1262 limit = pos & STBDS_BUCKET_MASK;1263 for (i = 0; i < limit; ++i) {1264 if (bucket->hash[i] == hash) {stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1265why, one step back1265 if (stbds_is_key_equal(a, elemsize, key, keysize, keyoffset, mode, bucket->index[i])) {add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1266–1280why, one step back1266 return (pos & ~STBDS_BUCKET_MASK)+i;1267 }1268 } else if (bucket->hash[i] == STBDS_HASH_EMPTY) {1269 return -1;1270 }1271 }12721273 // quadratic probing1274 pos += step;1275 step += STBDS_BUCKET_LENGTH;1276 pos &= (table->slot_count-1);1277 }1278 /* NOTREACHED */1279}1280stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1281why, one step back1281void * stbds_hmget_key_ts(void *a, size_t elemsize, void *key, size_t keysize, ptrdiff_t *temp, int mode)add stb_ds.h and testsSean Barrettbefore adoption477471d
line 1282why, one step back1282{stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1283why, one step back1283 size_t keyoffset = 0;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1284–1288why, one step back1284 if (a == NULL) {1285 // make it non-empty so we can return a temp1286 a = stbds_arrgrowf(0, elemsize, 0, 1);1287 stbds_header(a)->length += 1;1288 memset(a, 0, elemsize);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1289why, one step back1289 *temp = STBDS_INDEX_EMPTY;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1290–1297why, one step back1290 // adjust a to point after the default element1291 return STBDS_ARR_TO_HASH(a,elemsize);1292 } else {1293 stbds_hash_index *table;1294 void *raw_a = STBDS_HASH_TO_ARR(a,elemsize);1295 // adjust a to point to the default element1296 table = (stbds_hash_index *) stbds_header(raw_a)->hash_table;1297 if (table == 0) {stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1298why, one step back1298 *temp = -1;add stb_ds.h and testsSean Barrettbefore adoption477471d
line 1299why, one step back1299 } else {stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1300why, one step back1300 ptrdiff_t slot = stbds_hm_find_slot(a, elemsize, key, keysize, keyoffset, mode);add stb_ds.h and testsSean Barrettbefore adoption477471d
line 1301why, one step back1301 if (slot < 0) {stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1302why, one step back1302 *temp = STBDS_INDEX_EMPTY;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1303–1304why, one step back1303 } else {1304 stbds_hash_bucket *b = &table->storage[slot >> STBDS_BUCKET_SHIFT];stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1305why, one step back1305 *temp = b->index[slot & STBDS_BUCKET_MASK];add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1306–1311why, one step back1306 }1307 }1308 return a;1309 }1310}1311stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 1312–1319why, one step back1312void * stbds_hmget_key(void *a, size_t elemsize, void *key, size_t keysize, int mode)1313{1314 ptrdiff_t temp;1315 void *p = stbds_hmget_key_ts(a, elemsize, key, keysize, &temp, mode);1316 stbds_temp(STBDS_HASH_TO_ARR(p,elemsize)) = temp;1317 return p;1318}1319add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1320–1338why, one step back1320void * stbds_hmput_default(void *a, size_t elemsize)1321{1322 // three cases:1323 // a is NULL <- allocate1324 // a has a hash table but no entries, because of shmode <- grow1325 // a has entries <- do nothing1326 if (a == NULL || stbds_header(STBDS_HASH_TO_ARR(a,elemsize))->length == 0) {1327 a = stbds_arrgrowf(a ? STBDS_HASH_TO_ARR(a,elemsize) : NULL, elemsize, 0, 1);1328 stbds_header(a)->length += 1;1329 memset(a, 0, elemsize);1330 a=STBDS_ARR_TO_HASH(a,elemsize);1331 }1332 return a;1333}13341335static char *stbds_strdup(char *str);13361337void *stbds_hmput_key(void *a, size_t elemsize, void *key, size_t keysize, int mode)1338{stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1339why, one step back1339 size_t keyoffset=0;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1340–1362why, one step back1340 void *raw_a;1341 stbds_hash_index *table;13421343 if (a == NULL) {1344 a = stbds_arrgrowf(0, elemsize, 0, 1);1345 memset(a, 0, elemsize);1346 stbds_header(a)->length += 1;1347 // adjust a to point AFTER the default element1348 a = STBDS_ARR_TO_HASH(a,elemsize);1349 }13501351 // adjust a to point to the default element1352 raw_a = a;1353 a = STBDS_HASH_TO_ARR(a,elemsize);13541355 table = (stbds_hash_index *) stbds_header(a)->hash_table;13561357 if (table == NULL || table->used_count >= table->used_count_threshold) {1358 stbds_hash_index *nt;1359 size_t slot_count;13601361 slot_count = (table == NULL) ? STBDS_BUCKET_LENGTH : table->slot_count*2;1362 nt = stbds_make_hash_index(slot_count, table);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1363why, one step back1363 if (table)tweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
line 1364why, one step back1364 STBDS_FREE(NULL, table);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 1365–1366why, one step back1365 else1366 nt->string.mode = mode >= STBDS_HM_STRING ? STBDS_SH_DEFAULT : 0;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1367–1384why, one step back1367 stbds_header(a)->hash_table = table = nt;1368 STBDS_STATS(++stbds_hash_grow);1369 }13701371 // we iterate hash table explicitly because we want to track if we saw a tombstone1372 {1373 size_t hash = mode >= STBDS_HM_STRING ? stbds_hash_string((char*)key,table->seed) : stbds_hash_bytes(key, keysize,table->seed);1374 size_t step = STBDS_BUCKET_LENGTH;1375 size_t pos;1376 ptrdiff_t tombstone = -1;1377 stbds_hash_bucket *bucket;13781379 // stored hash values are forbidden from being 0, so we can detect empty slots to early out quickly1380 if (hash < 2) hash += 2;13811382 pos = stbds_probe_position(hash, table->slot_count, table->slot_count_log2);13831384 for (;;) {stb_ds: fix arrisn, shgetp_nullSean Barrettbefore adoption781609b
line 1385why, one step back1385 size_t limit, i;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1386–1391why, one step back1386 STBDS_STATS(++stbds_hash_probes);1387 bucket = &table->storage[pos >> STBDS_BUCKET_SHIFT];13881389 // start searching from pos to end of bucket1390 for (i=pos & STBDS_BUCKET_MASK; i < STBDS_BUCKET_LENGTH; ++i) {1391 if (bucket->hash[i] == hash) {stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1392why, one step back1392 if (stbds_is_key_equal(raw_a, elemsize, key, keysize, keyoffset, mode, bucket->index[i])) {add stb_ds.h and testsSean Barrettbefore adoption477471d
line 1393why, one step back1393 stbds_temp(a) = bucket->index[i];Merge branch 'working'Sean Barrettbefore adoption6b647c0
lines 1394–1395why, one step back1394 if (mode >= STBDS_HM_STRING)1395 stbds_temp_key(a) = * (char **) ((char *) raw_a + elemsize*bucket->index[i] + keyoffset);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1396–1410why, one step back1396 return STBDS_ARR_TO_HASH(a,elemsize);1397 }1398 } else if (bucket->hash[i] == 0) {1399 pos = (pos & ~STBDS_BUCKET_MASK) + i;1400 goto found_empty_slot;1401 } else if (tombstone < 0) {1402 if (bucket->index[i] == STBDS_INDEX_DELETED)1403 tombstone = (ptrdiff_t) ((pos & ~STBDS_BUCKET_MASK) + i);1404 }1405 }14061407 // search from beginning of bucket to pos1408 limit = pos & STBDS_BUCKET_MASK;1409 for (i = 0; i < limit; ++i) {1410 if (bucket->hash[i] == hash) {stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1411why, one step back1411 if (stbds_is_key_equal(raw_a, elemsize, key, keysize, keyoffset, mode, bucket->index[i])) {add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1412–1437why, one step back1412 stbds_temp(a) = bucket->index[i];1413 return STBDS_ARR_TO_HASH(a,elemsize);1414 }1415 } else if (bucket->hash[i] == 0) {1416 pos = (pos & ~STBDS_BUCKET_MASK) + i;1417 goto found_empty_slot;1418 } else if (tombstone < 0) {1419 if (bucket->index[i] == STBDS_INDEX_DELETED)1420 tombstone = (ptrdiff_t) ((pos & ~STBDS_BUCKET_MASK) + i);1421 }1422 }14231424 // quadratic probing1425 pos += step;1426 step += STBDS_BUCKET_LENGTH;1427 pos &= (table->slot_count-1);1428 }1429 found_empty_slot:1430 if (tombstone >= 0) {1431 pos = tombstone;1432 --table->tombstone_count;1433 }1434 ++table->used_count;14351436 {1437 ptrdiff_t i = (ptrdiff_t) stbds_arrlen(a);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1438why, one step back1438 // we want to do stbds_arraddn(1), but we can't use the macros since we don't have something of the right typeadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1439–1450why, one step back1439 if ((size_t) i+1 > stbds_arrcap(a))1440 *(void **) &a = stbds_arrgrowf(a, elemsize, 1, 0);1441 raw_a = STBDS_ARR_TO_HASH(a,elemsize);14421443 STBDS_ASSERT((size_t) i+1 <= stbds_arrcap(a));1444 stbds_header(a)->length = i+1;1445 bucket = &table->storage[pos >> STBDS_BUCKET_SHIFT];1446 bucket->hash[pos & STBDS_BUCKET_MASK] = hash;1447 bucket->index[pos & STBDS_BUCKET_MASK] = i-1;1448 stbds_temp(a) = i-1;14491450 switch (table->string.mode) {Merge branch 'master' of https://github.com/srdjanstipic/stb into workingSean Barrettbefore adoptionef23148
lines 1451–1453why, one step back1451 case STBDS_SH_STRDUP: stbds_temp_key(a) = *(char **) ((char *) a + elemsize*i) = stbds_strdup((char*) key); break;1452 case STBDS_SH_ARENA: stbds_temp_key(a) = *(char **) ((char *) a + elemsize*i) = stbds_stralloc(&table->string, (char*)key); break;1453 case STBDS_SH_DEFAULT: stbds_temp_key(a) = *(char **) ((char *) a + elemsize*i) = (char *) key; break;stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1454why, one step back1454 default: memcpy((char *) a + elemsize*i, key, keysize); break;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1455–1464why, one step back1455 }1456 }1457 return STBDS_ARR_TO_HASH(a,elemsize);1458 }1459}14601461void * stbds_shmode_func(size_t elemsize, int mode)1462{1463 void *a = stbds_arrgrowf(0, elemsize, 0, 1);1464 stbds_hash_index *h;Merge branch 'master' into workingSean Barrettbefore adoptiond940053
lines 1465–1466why, one step back1465 memset(a, 0, elemsize);1466 stbds_header(a)->length = 1;add stb_ds.h and testsSean Barrettbefore adoption477471d
line 1467why, one step back1467 stbds_header(a)->hash_table = h = (stbds_hash_index *) stbds_make_hash_index(STBDS_BUCKET_LENGTH, NULL);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1468why, one step back1468 h->string.mode = (unsigned char) mode;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1469–1484why, one step back1469 return STBDS_ARR_TO_HASH(a,elemsize);1470}14711472void * stbds_hmdel_key(void *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode)1473{1474 if (a == NULL) {1475 return 0;1476 } else {1477 stbds_hash_index *table;1478 void *raw_a = STBDS_HASH_TO_ARR(a,elemsize);1479 table = (stbds_hash_index *) stbds_header(raw_a)->hash_table;1480 stbds_temp(raw_a) = 0;1481 if (table == 0) {1482 return a;1483 } else {1484 ptrdiff_t slot;stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1485why, one step back1485 slot = stbds_hm_find_slot(a, elemsize, key, keysize, keyoffset, mode);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1486–1502why, one step back1486 if (slot < 0)1487 return a;1488 else {1489 stbds_hash_bucket *b = &table->storage[slot >> STBDS_BUCKET_SHIFT];1490 int i = slot & STBDS_BUCKET_MASK;1491 ptrdiff_t old_index = b->index[i];1492 ptrdiff_t final_index = (ptrdiff_t) stbds_arrlen(raw_a)-1-1; // minus one for the raw_a vs a, and minus one for 'last'1493 STBDS_ASSERT(slot < (ptrdiff_t) table->slot_count);1494 --table->used_count;1495 ++table->tombstone_count;1496 stbds_temp(raw_a) = 1;1497 STBDS_ASSERT(table->used_count >= 0);1498 //STBDS_ASSERT(table->tombstone_count < table->slot_count/4);1499 b->hash[i] = STBDS_HASH_DELETED;1500 b->index[i] = STBDS_INDEX_DELETED;15011502 if (mode == STBDS_HM_STRING && table->string.mode == STBDS_SH_STRDUP)tweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
line 1503why, one step back1503 STBDS_FREE(NULL, *(char**) ((char *) a+elemsize*old_index));add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1504–1506why, one step back15041505 // if indices are the same, memcpy is a no-op, but back-pointer-fixup will fail, so skip1506 if (old_index != final_index) {Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 1507why, one step back1507 // swap deleteadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1508–1511why, one step back1508 memmove((char*) a + elemsize*old_index, (char*) a + elemsize*final_index, elemsize);15091510 // now find the slot for the last element1511 if (mode == STBDS_HM_STRING)stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1512why, one step back1512 slot = stbds_hm_find_slot(a, elemsize, *(char**) ((char *) a+elemsize*old_index + keyoffset), keysize, keyoffset, mode);add stb_ds.h and testsSean Barrettbefore adoption477471d
line 1513why, one step back1513 elsestb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1514why, one step back1514 slot = stbds_hm_find_slot(a, elemsize, (char* ) a+elemsize*old_index + keyoffset, keysize, keyoffset, mode);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1515–1524why, one step back1515 STBDS_ASSERT(slot >= 0);1516 b = &table->storage[slot >> STBDS_BUCKET_SHIFT];1517 i = slot & STBDS_BUCKET_MASK;1518 STBDS_ASSERT(b->index[i] == final_index);1519 b->index[i] = old_index;1520 }1521 stbds_header(raw_a)->length -= 1;15221523 if (table->used_count < table->used_count_shrink_threshold && table->slot_count > STBDS_BUCKET_LENGTH) {1524 stbds_header(raw_a)->hash_table = stbds_make_hash_index(table->slot_count>>1, table);tweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
line 1525why, one step back1525 STBDS_FREE(NULL, table);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1526–1528why, one step back1526 STBDS_STATS(++stbds_hash_shrink);1527 } else if (table->tombstone_count > table->tombstone_count_threshold) {1528 stbds_header(raw_a)->hash_table = stbds_make_hash_index(table->slot_count , table);tweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
line 1529why, one step back1529 STBDS_FREE(NULL, table);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1530–1544why, one step back1530 STBDS_STATS(++stbds_hash_rebuild);1531 }15321533 return a;1534 }1535 }1536 }1537 /* NOTREACHED */1538}15391540static char *stbds_strdup(char *str)1541{1542 // to keep replaceable allocator simple, we don't want to use strdup.1543 // rolling our own also avoids problem of strdup vs _strdup1544 size_t len = strlen(str)+1;tweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
line 1545why, one step back1545 char *p = (char*) STBDS_REALLOC(NULL, 0, len);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1546–1550why, one step back1546 memmove(p, str, len);1547 return p;1548}15491550#ifndef STBDS_STRING_ARENA_BLOCKSIZE_MINstb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1551why, one step back1551#define STBDS_STRING_ARENA_BLOCKSIZE_MIN 512uadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1552–1553why, one step back1552#endif1553#ifndef STBDS_STRING_ARENA_BLOCKSIZE_MAXstb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1554why, one step back1554#define STBDS_STRING_ARENA_BLOCKSIZE_MAX (1u<<20)add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1555–1577why, one step back1555#endif15561557char *stbds_stralloc(stbds_string_arena *a, char *str)1558{1559 char *p;1560 size_t len = strlen(str)+1;1561 if (len > a->remaining) {1562 // compute the next blocksize1563 size_t blocksize = a->block;15641565 // size is 512, 512, 1024, 1024, 2048, 2048, 4096, 4096, etc., so that1566 // there are log(SIZE) allocations to free when we destroy the table1567 blocksize = (size_t) (STBDS_STRING_ARENA_BLOCKSIZE_MIN) << (blocksize>>1);15681569 // if size is under 1M, advance to next blocktype1570 if (blocksize < (size_t)(STBDS_STRING_ARENA_BLOCKSIZE_MAX))1571 ++a->block;15721573 if (len > blocksize) {1574 // if string is larger than blocksize, then just allocate the full size.1575 // note that we still advance string_block so block size will continue1576 // increasing, so e.g. if somebody only calls this with 1000-long strings,1577 // eventually the arena will start doubling and handling those as welltweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
line 1578why, one step back1578 stbds_string_block *sb = (stbds_string_block *) STBDS_REALLOC(NULL, 0, sizeof(*sb)-8 + len);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1579–1590why, one step back1579 memmove(sb->storage, str, len);1580 if (a->storage) {1581 // insert it after the first element, so that we don't waste the space there1582 sb->next = a->storage->next;1583 a->storage->next = sb;1584 } else {1585 sb->next = 0;1586 a->storage = sb;1587 a->remaining = 0; // this is redundant, but good for clarity1588 }1589 return sb->storage;1590 } else {tweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
line 1591why, one step back1591 stbds_string_block *sb = (stbds_string_block *) STBDS_REALLOC(NULL, 0, sizeof(*sb)-8 + blocksize);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1592–1610why, one step back1592 sb->next = a->storage;1593 a->storage = sb;1594 a->remaining = blocksize;1595 }1596 }15971598 STBDS_ASSERT(len <= a->remaining);1599 p = a->storage->storage + a->remaining - len;1600 a->remaining -= len;1601 memmove(p, str, len);1602 return p;1603}16041605void stbds_strreset(stbds_string_arena *a)1606{1607 stbds_string_block *x,*y;1608 x = a->storage;1609 while (x) {1610 y = x->next;tweak STBDS_REALLOC/STBDS_FREE changeSean Barrettbefore adoption8f0bade
line 1611why, one step back1611 STBDS_FREE(NULL, x);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1612–1625why, one step back1612 x = y;1613 }1614 memset(a, 0, sizeof(*a));1615}16161617#endif16181619//////////////////////////////////////////////////////////////////////////////1620//1621// UNIT TESTS1622//16231624#ifdef STBDS_UNIT_TESTS1625#include <stdio.h>Merge branch 'master' into workingSean Barrettbefore adoptiond940053
lines 1626–1628why, one step back1626#ifdef STBDS_ASSERT_WAS_UNDEFINED1627#undef STBDS_ASSERT1628#endifadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1629–1634why, one step back1629#ifndef STBDS_ASSERT1630#define STBDS_ASSERT assert1631#include <assert.h>1632#endif16331634typedef struct { int key,b,c,d; } stbds_struct;stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1635why, one step back1635typedef struct { int key[2],b,c,d; } stbds_struct2;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1636–1639why, one step back16361637static char buffer[256];1638char *strkey(int n)1639{Merge branch 'master' into workingSean Barrettbefore adoptiond940053
lines 1640–1642why, one step back1640#if defined(_WIN32) && defined(__STDC_WANT_SECURE_LIB__)1641 sprintf_s(buffer, sizeof(buffer), "test_%d", n);1642#elseadd stb_ds.h and testsSean Barrettbefore adoption477471d
line 1643why, one step back1643 sprintf(buffer, "test_%d", n);Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1644why, one step back1644#endifadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1645–1649why, one step back1645 return buffer;1646}16471648void stbds_unit_tests(void)1649{stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
lines 1650–1653why, one step back1650#if defined(_MSC_VER) && _MSC_VER <= 1200 && defined(__cplusplus)1651 // VC6 C++ doesn't like the template<> trick on unnamed structures, so do nothing!1652 STBDS_ASSERT(0);1653#elseadd stb_ds.h and testsSean Barrettbefore adoption477471d
line 1654why, one step back1654 const int testsize = 100000;stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1655why, one step back1655 const int testsize2 = testsize/20;add stb_ds.h and testsSean Barrettbefore adoption477471d
line 1656why, one step back1656 int *arr=NULL;stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1657why, one step back1657 struct { int key; int value; } *intmap = NULL;Merge branch 'master' of https://github.com/srdjanstipic/stb into workingSean Barrettbefore adoptionef23148
line 1658why, one step back1658 struct { char *key; int value; } *strmap = NULL, s;stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 1659–1664why, one step back1659 struct { stbds_struct key; int value; } *map = NULL;1660 stbds_struct *map2 = NULL;1661 stbds_struct2 *map3 = NULL;1662 stbds_string_arena sa = { 0 };1663 int key3[2] = { 1,2 };1664 ptrdiff_t temp;add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1665–1667why, one step back16651666 int i,j;1667stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 1668why, one step back1668 STBDS_ASSERT(arrlen(arr)==0);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1669–1686why, one step back1669 for (i=0; i < 20000; i += 50) {1670 for (j=0; j < i; ++j)1671 arrpush(arr,j);1672 arrfree(arr);1673 }16741675 for (i=0; i < 4; ++i) {1676 arrpush(arr,1); arrpush(arr,2); arrpush(arr,3); arrpush(arr,4);1677 arrdel(arr,i);1678 arrfree(arr);1679 arrpush(arr,1); arrpush(arr,2); arrpush(arr,3); arrpush(arr,4);1680 arrdelswap(arr,i);1681 arrfree(arr);1682 }16831684 for (i=0; i < 5; ++i) {1685 arrpush(arr,1); arrpush(arr,2); arrpush(arr,3); arrpush(arr,4);1686 stbds_arrins(arr,i,5);stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 1687why, one step back1687 STBDS_ASSERT(arr[i] == 5);add stb_ds.h and testsSean Barrettbefore adoption477471d
line 1688why, one step back1688 if (i < 4)stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 1689why, one step back1689 STBDS_ASSERT(arr[4] == 4);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1690–1692why, one step back1690 arrfree(arr);1691 }1692stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
lines 1693–1697why, one step back1693 i = 1;1694 STBDS_ASSERT(hmgeti(intmap,i) == -1);1695 hmdefault(intmap, -2);1696 STBDS_ASSERT(hmgeti(intmap, i) == -1);1697 STBDS_ASSERT(hmget (intmap, i) == -2);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1698–1699why, one step back1698 for (i=0; i < testsize; i+=2)1699 hmput(intmap, i, i*5);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1700why, one step back1700 for (i=0; i < testsize; i+=1) {stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 1701why, one step back1701 if (i & 1) STBDS_ASSERT(hmget(intmap, i) == -2 );stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 1702why, one step back1702 else STBDS_ASSERT(hmget(intmap, i) == i*5);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 1703–1705why, one step back1703 if (i & 1) STBDS_ASSERT(hmget_ts(intmap, i, temp) == -2 );1704 else STBDS_ASSERT(hmget_ts(intmap, i, temp) == i*5);1705 }add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1706–1708why, one step back1706 for (i=0; i < testsize; i+=2)1707 hmput(intmap, i, i*3);1708 for (i=0; i < testsize; i+=1)stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 1709why, one step back1709 if (i & 1) STBDS_ASSERT(hmget(intmap, i) == -2 );stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 1710why, one step back1710 else STBDS_ASSERT(hmget(intmap, i) == i*3);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1711–1713why, one step back1711 for (i=2; i < testsize; i+=4)1712 hmdel(intmap, i); // delete half the entries1713 for (i=0; i < testsize; i+=1)stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 1714why, one step back1714 if (i & 3) STBDS_ASSERT(hmget(intmap, i) == -2 );stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 1715why, one step back1715 else STBDS_ASSERT(hmget(intmap, i) == i*3);add stb_ds.h and testsSean Barrettbefore adoption477471d
line 1716why, one step back1716 for (i=0; i < testsize; i+=1)Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 1717why, one step back1717 hmdel(intmap, i); // delete the rest of the entriesadd stb_ds.h and testsSean Barrettbefore adoption477471d
line 1718why, one step back1718 for (i=0; i < testsize; i+=1)stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 1719why, one step back1719 STBDS_ASSERT(hmget(intmap, i) == -2 );add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1720–1724why, one step back1720 hmfree(intmap);1721 for (i=0; i < testsize; i+=2)1722 hmput(intmap, i, i*3);1723 hmfree(intmap);1724stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
lines 1725–1726why, one step back1725 #if defined(__clang__) || defined(__GNUC__)1726 #ifndef __cplusplusadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1727–1730why, one step back1727 intmap = NULL;1728 hmput(intmap, 15, 7);1729 hmput(intmap, 11, 3);1730 hmput(intmap, 9, 5);stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
lines 1731–1733why, one step back1731 STBDS_ASSERT(hmget(intmap, 9) == 5);1732 STBDS_ASSERT(hmget(intmap, 11) == 3);1733 STBDS_ASSERT(hmget(intmap, 15) == 7);add stb_ds.h and testsSean Barrettbefore adoption477471d
line 1734why, one step back1734 #endifstb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 1735why, one step back1735 #endifadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1736–1739why, one step back17361737 for (i=0; i < testsize; ++i)1738 stralloc(&sa, strkey(i));1739 strreset(&sa);Merge branch 'master' of https://github.com/srdjanstipic/stb into workingSean Barrettbefore adoptionef23148
lines 1740–1768why, one step back17401741 {1742 s.key = "a", s.value = 1;1743 shputs(strmap, s);1744 STBDS_ASSERT(*strmap[0].key == 'a');1745 STBDS_ASSERT(strmap[0].key == s.key);1746 STBDS_ASSERT(strmap[0].value == s.value);1747 shfree(strmap);1748 }17491750 {1751 s.key = "a", s.value = 1;1752 sh_new_strdup(strmap);1753 shputs(strmap, s);1754 STBDS_ASSERT(*strmap[0].key == 'a');1755 STBDS_ASSERT(strmap[0].key != s.key);1756 STBDS_ASSERT(strmap[0].value == s.value);1757 shfree(strmap);1758 }17591760 {1761 s.key = "a", s.value = 1;1762 sh_new_arena(strmap);1763 shputs(strmap, s);1764 STBDS_ASSERT(*strmap[0].key == 'a');1765 STBDS_ASSERT(strmap[0].key != s.key);1766 STBDS_ASSERT(strmap[0].value == s.value);1767 shfree(strmap);1768 }add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1769–1770why, one step back17691770 for (j=0; j < 2; ++j) {stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 1771why, one step back1771 STBDS_ASSERT(shgeti(strmap,"foo") == -1);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1772–1775why, one step back1772 if (j == 0)1773 sh_new_strdup(strmap);1774 else1775 sh_new_arena(strmap);stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
lines 1776–1778why, one step back1776 STBDS_ASSERT(shgeti(strmap,"foo") == -1);1777 shdefault(strmap, -2);1778 STBDS_ASSERT(shgeti(strmap,"foo") == -1);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1779–1781why, one step back1779 for (i=0; i < testsize; i+=2)1780 shput(strmap, strkey(i), i*3);1781 for (i=0; i < testsize; i+=1)stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 1782why, one step back1782 if (i & 1) STBDS_ASSERT(shget(strmap, strkey(i)) == -2 );stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 1783why, one step back1783 else STBDS_ASSERT(shget(strmap, strkey(i)) == i*3);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1784–1786why, one step back1784 for (i=2; i < testsize; i+=4)1785 shdel(strmap, strkey(i)); // delete half the entries1786 for (i=0; i < testsize; i+=1)stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 1787why, one step back1787 if (i & 3) STBDS_ASSERT(shget(strmap, strkey(i)) == -2 );stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 1788why, one step back1788 else STBDS_ASSERT(shget(strmap, strkey(i)) == i*3);add stb_ds.h and testsSean Barrettbefore adoption477471d
line 1789why, one step back1789 for (i=0; i < testsize; i+=1)Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 1790why, one step back1790 shdel(strmap, strkey(i)); // delete the rest of the entriesadd stb_ds.h and testsSean Barrettbefore adoption477471d
line 1791why, one step back1791 for (i=0; i < testsize; i+=1)stb_ds: fix bug with shgeti not returning correct valueSean Barrettbefore adoption61be29d
line 1792why, one step back1792 STBDS_ASSERT(shget(strmap, strkey(i)) == -2 );add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1793–1816why, one step back1793 shfree(strmap);1794 }17951796 {1797 struct { char *key; char value; } *hash = NULL;1798 char name[4] = "jen";1799 shput(hash, "bob" , 'h');1800 shput(hash, "sally" , 'e');1801 shput(hash, "fred" , 'l');1802 shput(hash, "jen" , 'x');1803 shput(hash, "doug" , 'o');18041805 shput(hash, name , 'l');1806 shfree(hash);1807 }18081809 for (i=0; i < testsize; i += 2) {1810 stbds_struct s = { i,i*2,i*3,i*4 };1811 hmput(map, s, i*5);1812 }18131814 for (i=0; i < testsize; i += 1) {1815 stbds_struct s = { i,i*2,i*3 ,i*4 };1816 stbds_struct t = { i,i*2,i*3+1,i*4 };stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
lines 1817–1818why, one step back1817 if (i & 1) STBDS_ASSERT(hmget(map, s) == 0);1818 else STBDS_ASSERT(hmget(map, s) == i*5);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 1819–1821why, one step back1819 if (i & 1) STBDS_ASSERT(hmget_ts(map, s, temp) == 0);1820 else STBDS_ASSERT(hmget_ts(map, s, temp) == i*5);1821 //STBDS_ASSERT(hmget(map, t.key) == 0);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1822–1831why, one step back1822 }18231824 for (i=0; i < testsize; i += 2) {1825 stbds_struct s = { i,i*2,i*3,i*4 };1826 hmputs(map2, s);1827 }1828 hmfree(map);18291830 for (i=0; i < testsize; i += 1) {1831 stbds_struct s = { i,i*2,i*3,i*4 };Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1832why, one step back1832 stbds_struct t = { i,i*2,i*3+1,i*4 };stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
lines 1833–1834why, one step back1833 if (i & 1) STBDS_ASSERT(hmgets(map2, s.key).d == 0);1834 else STBDS_ASSERT(hmgets(map2, s.key).d == i*4);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
line 1835why, one step back1835 //STBDS_ASSERT(hmgetp(map2, t.key) == 0);add stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1836–1837why, one step back1836 }1837 hmfree(map2);stb_ds.h: thread-safe functions; pointer-returning functions; change return value of arraddnSean Barrettbefore adoptiond693c61
lines 1838–1849why, one step back18381839 for (i=0; i < testsize; i += 2) {1840 stbds_struct2 s = { { i,i*2 }, i*3,i*4, i*5 };1841 hmputs(map3, s);1842 }1843 for (i=0; i < testsize; i += 1) {1844 stbds_struct2 s = { { i,i*2}, i*3, i*4, i*5 };1845 stbds_struct2 t = { { i,i*2}, i*3+1, i*4, i*5 };1846 if (i & 1) STBDS_ASSERT(hmgets(map3, s.key).d == 0);1847 else STBDS_ASSERT(hmgets(map3, s.key).d == i*5);1848 //STBDS_ASSERT(hmgetp(map3, t.key) == 0);1849 }stb_ds v0.3: fixes for compiling client code in C++Sean Barrettbefore adoptionf9133c3
line 1850why, one step back1850#endifadd stb_ds.h and testsSean Barrettbefore adoption477471d
lines 1851–1895why, one step back1851}1852#endif185318541855/*1856------------------------------------------------------------------------------1857This software is available under 2 licenses -- choose whichever you prefer.1858------------------------------------------------------------------------------1859ALTERNATIVE A - MIT License1860Copyright (c) 2019 Sean Barrett1861Permission is hereby granted, free of charge, to any person obtaining a copy of1862this software and associated documentation files (the "Software"), to deal in1863the Software without restriction, including without limitation the rights to1864use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies1865of the Software, and to permit persons to whom the Software is furnished to do1866so, subject to the following conditions:1867The above copyright notice and this permission notice shall be included in all1868copies or substantial portions of the Software.1869THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR1870IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1871FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE1872AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER1873LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,1874OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE1875SOFTWARE.1876------------------------------------------------------------------------------1877ALTERNATIVE B - Public Domain (www.unlicense.org)1878This is free and unencumbered software released into the public domain.1879Anyone is free to copy, modify, publish, use, compile, sell, or distribute this1880software, either in source code form or as a compiled binary, for any purpose,1881commercial or non-commercial, and by any means.1882In jurisdictions that recognize copyright laws, the author or authors of this1883software dedicate any and all copyright interest in the software to the public1884domain. We make this dedication for the benefit of the public at large and to1885the detriment of our heirs and successors. We intend this dedication to be an1886overt act of relinquishment in perpetuity of all present and future rights to1887this software under copyright law.1888THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR1889IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1890FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE1891AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN1892ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION1893WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.1894------------------------------------------------------------------------------1895*/