Skip to content
Myr

stb_ds: major string hash fix, minor other changes

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- arena and strdup string hashes were badly broken due to not setting up default slot correctly - tweak use of seed in 4-byte and 8-byte hash functions to hopefully be slightly stronger - a few internal #ifdefs for performance tuning

authored by Sean Barrett · committed by Sean Barrett · parent 72990ef · Browse files · 1 file+29 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+29 added−19 removed

  • other:+29 added−19 removed

1 file+29 added−19 removed

modified:stb_ds.h+29 added-19 removed
272 unmodified lines
273NOTES - HASH MAP274275 * For compilers other than GCC and clang (e.g. Visual Studio), for hmput/hmget/hmdel276 and variants, the key must be an lvalue (so the macro can take the address of it).277 For GCC and clang, extensions are used that eliminate this requirement if you're278 using C99 and later or using C++.277 Extensions are used that eliminate this requirement if you're using C99 and later278 in GCC or clang, or if you're using C++ in GCC.279280 * To test for presence of a key in a hashmap, just do 'hmget(foo,key) >= 0'.280 * To test for presence of a key in a hashmap, just do 'hmgeti(foo,key) >= 0'.281282 * The iteration order of your data in the hashmap is determined solely by the283 order of insertions and deletions. In particular, if you never delete, new284 keys are always added at the end of the array. This will be consistent
131 unmodified lines
416#if defined(STBDS_HAS_LITERAL_ARRAY) && defined(STBDS_HAS_TYPEOF)417 #if __clang__418 #define STBDS_ADDRESSOF(typevar, value) ((__typeof__(typevar)[1]){value}) // literal array decays to pointer to value419 #else420 #define STBDS_ADDRESSOF(typevar, value) ((typeof(typevar)[]){value}) // literal array decays to pointer to value420 #define STBDS_ADDRESSOF(typevar, value) ((typeof(typevar)[1]){value}) // literal array decays to pointer to value421 #endif422#else423#define STBDS_ADDRESSOF(typevar, value) &(value)424#endif
222 unmodified lines
647//648// stbds_hm hash table implementation649//650651#define STBDS_CACHE_LINE_SIZE 64651#ifdef STBDS_INTERNAL_SMALL_BUCKET652#define STBDS_BUCKET_LENGTH 4653#else654#define STBDS_BUCKET_LENGTH 8653#define STBDS_BUCKET_SHIFT 3655#endif656657#define STBDS_BUCKET_SHIFT (STBDS_BUCKET_LENGTH == 8 ? 3 : 2)658#define STBDS_BUCKET_MASK (STBDS_BUCKET_LENGTH-1)659#define STBDS_CACHE_LINE_SIZE 64660661#define STBDS_ALIGN_FWD(n,a) (((n) + (a) - 1) & ~((a)-1))662663typedef struct
38 unmodified lines
702#define STBDS_SIZE_T_BITS ((sizeof (size_t)) * 8)703704static size_t stbds_probe_position(size_t hash, size_t slot_count, size_t slot_log2)705{701 #if 1702 size_t pos = (hash >> (STBDS_SIZE_T_BITS-slot_log2));703 STBDS_ASSERT(pos < slot_count);704 return pos;705 #else706 return hash & (slot_count-1);706 size_t pos;707 pos = hash & (slot_count-1);708 #ifdef STBDS_INTERNAL_BUCKET_START709 pos &= ~STBDS_BUCKET_MASK;710 #endif711 return pos;712}713714static size_t stbds_log2(size_t slot_count)715{
99 unmodified lines
815 STBDS_STATS(++stbds_rehash_items);816 for (;;) {817 size_t limit,z;818 stbds_hash_bucket *bucket;815 pos &= (t->slot_count-1);819 bucket = &t->storage[pos >> STBDS_BUCKET_SHIFT];820 STBDS_STATS(++stbds_rehash_probes);821822 for (z=pos & STBDS_BUCKET_MASK; z < STBDS_BUCKET_LENGTH; ++z) {
14 unmodified lines
837 }838839 pos += step; // quadratic probing840 step += STBDS_BUCKET_LENGTH;841 pos &= (t->slot_count-1);842 }843 }844 done:845 ;
96 unmodified lines
942 STBDS_SIPROUND();943#ifdef STBDS_SIPHASH_2_4944 return v0^v1^v2^v3;945#else942 return v1^v2^v3; // slightly stronger since v0^v3 in above cancels out final round operation946 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 reply947#endif948}949950size_t stbds_hash_bytes(void *p, size_t len, size_t seed)
6 unmodified lines
957 if (len == 4) {958 unsigned int hash = d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);959 #if 0960 // HASH32-A Bob Jenkin's hash function w/o large constants957 hash ^= seed ^ len;961 hash ^= seed;962 hash -= (hash<<6);963 hash ^= (hash>>17);964 hash -= (hash<<9);965 hash ^= seed;966 hash ^= (hash<<4);967 hash -= (hash<<3);968 hash ^= (hash<<10);969 hash ^= (hash>>15);970 #elif 1971 // HASH32-BB Bob Jenkin's presumably-accidental version of Thomas Wang hash with rotates turned into shifts.972 // Note that converting these back to rotates makes it run a lot slower, presumably due to collisions, so I'm973 // not really sure what's going on.969 hash ^= seed ^ len;974 hash ^= seed;975 hash = (hash ^ 61) ^ (hash >> 16);976 hash = hash + (hash << 3);977 hash = hash ^ (hash >> 4);978 hash = hash * 0x27d4eb2d;979 hash ^= seed;980 hash = hash ^ (hash >> 15);981 #else // HASH32-C - Murmur3982 hash ^= seed;983 hash *= 0xcc9e2d51;984 hash = (hash << 17) | (hash >> 15);985 hash *= 0x1b873593;986 hash ^= seed;987 hash = (hash << 19) | (hash >> 13);988 hash = hash*5 + 0xe6546b64;982 hash ^= len;989 hash ^= hash >> 16;990 hash *= 0x85ebca6b;991 hash ^= seed;992 hash ^= hash >> 13;993 hash *= 0xc2b2ae35;994 hash ^= hash >> 16;995 #endif
16 unmodified lines
1012 return (((size_t) hash << 16 << 16) | hash) ^ seed;1013 } else if (len == 8 && sizeof(size_t) == 8) {1014 size_t hash = d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);1015 hash |= (size_t) (d[4] | (d[5] << 8) | (d[6] << 16) | (d[7] << 24)) << 16 << 16; // avoid warning if size_t == 41009 hash ^= seed ^ len;1016 hash ^= seed;1017 hash = (~hash) + (hash << 21);1018 hash ^= STBDS_ROTATE_RIGHT(hash,24);1019 hash *= 265;1020 hash ^= STBDS_ROTATE_RIGHT(hash,14);1021 hash ^= seed;1022 hash *= 21;1023 hash ^= STBDS_ROTATE_RIGHT(hash,28);1024 hash += (hash << 31);1025 hash = (~hash) + (hash << 18);1018 return hash^seed;1026 return hash;1027 } else {1028 return stbds_siphash_bytes(p,len,seed);1029 }1030#endif
248 unmodified lines
1279void * stbds_shmode_func(size_t elemsize, int mode)1280{1281 void *a = stbds_arrgrowf(0, elemsize, 0, 1);1282 stbds_hash_index *h;1283 memset(a, 0, elemsize);1284 stbds_header(a)->length = 1;1285 stbds_header(a)->hash_table = h = (stbds_hash_index *) stbds_make_hash_index(STBDS_BUCKET_LENGTH, NULL);1286 h->string.mode = mode;1287 return STBDS_ARR_TO_HASH(a,elemsize);1288}
360 unmodified lines