verstable.h 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  1. /*------------------------------------------------- VERSTABLE v2.1.1 ---------------------------------------------------
  2. Verstable is a C99-compatible, open-addressing hash table using quadratic probing and the following additions:
  3. * All keys that hash (i.e. "belong") to the same bucket (their "home bucket") are linked together by an 11-bit integer
  4. specifying the quadratic displacement, relative to that bucket, of the next key in the chain.
  5. * If a chain of keys exists for a given bucket, then it always begins at that bucket. To maintain this policy, a 1-bit
  6. flag is used to mark whether the key occupying a bucket belongs there. When inserting a new key, if the bucket it
  7. belongs to is occupied by a key that does not belong there, then the occupying key is evicted and the new key takes
  8. the bucket.
  9. * A 4-bit fragment of each key's hash code is also stored.
  10. * The aforementioned metadata associated with each bucket (the 4-bit hash fragment, the 1-bit flag, and the 11-bit link
  11. to the next key in the chain) are stored together in a uint16_t array rather than in the bucket alongside the key and
  12. (optionally) the value.
  13. One way to conceptualize this scheme is as a chained hash table in which overflowing keys are stored not in separate
  14. memory allocations but in otherwise unused buckets. In this regard, it shares similarities with Malte Skarupke's Bytell
  15. hash table (https://www.youtube.com/watch?v=M2fKMP47slQ) and traditional "coalesced hashing".
  16. Advantages of this scheme include:
  17. * Fast lookups impervious to load factor: If the table contains any key belonging to the lookup key's home bucket, then
  18. that bucket contains the first in a traversable chain of all keys belonging to it. Hence, only the home bucket and
  19. other buckets containing keys belonging to it are ever probed. Moreover, the stored hash fragments allow skipping most
  20. non-matching keys in the chain without accessing the actual buckets array or calling the (potentially expensive) key
  21. comparison function.
  22. * Fast insertions: Insertions are faster than they are in other schemes that move keys around (e.g. Robin Hood) because
  23. they only move, at most, one existing key.
  24. * Fast, tombstone-free deletions: Deletions, which usually require tombstones in quadratic-probing hash tables, are
  25. tombstone-free and only move, at most, one existing key.
  26. * Fast iteration: The separate metadata array allows keys in sparsely populated tables to be found without incurring the
  27. frequent cache misses that would result from traversing the buckets array.
  28. Usage example:
  29. +---------------------------------------------------------+----------------------------------------------------------+
  30. | Using the generic macro API (C11 and later): | Using the prefixed functions API (C99 and later): |
  31. |---------------------------------------------------------+----------------------------------------------------------+
  32. | #include <stdio.h> | #include <stdio.h> |
  33. | | |
  34. | // Instantiating a set template. | // Instantiating a set template. |
  35. | #define NAME int_set | #define NAME int_set |
  36. | #define KEY_TY int | #define KEY_TY int |
  37. | #include "verstable.h" | #define HASH_FN vt_hash_integer |
  38. | | #define CMPR_FN vt_cmpr_integer |
  39. | // Instantiating a map template. | #include "verstable.h" |
  40. | #define NAME int_int_map | |
  41. | #define KEY_TY int | // Instantiating a map template. |
  42. | #define VAL_TY int | #define NAME int_int_map |
  43. | #include "verstable.h" | #define KEY_TY int |
  44. | | #define VAL_TY int |
  45. | int main( void ) | #define HASH_FN vt_hash_integer |
  46. | { | #define CMPR_FN vt_cmpr_integer |
  47. | // Set. | #include "verstable.h" |
  48. | | |
  49. | int_set our_set; | int main( void ) |
  50. | vt_init( &our_set ); | { |
  51. | | // Set. |
  52. | // Inserting keys. | |
  53. | for( int i = 0; i < 10; ++i ) | int_set our_set; |
  54. | { | int_set_init( &our_set ); |
  55. | int_set_itr itr = vt_insert( &our_set, i ); | |
  56. | if( vt_is_end( itr ) ) | // Inserting keys. |
  57. | { | for( int i = 0; i < 10; ++i ) |
  58. | // Out of memory, so abort. | { |
  59. | vt_cleanup( &our_set ); | int_set_itr itr = |
  60. | return 1; | int_set_insert( &our_set, i ); |
  61. | } | if( int_set_is_end( itr ) ) |
  62. | } | { |
  63. | | // Out of memory, so abort. |
  64. | // Erasing keys. | int_set_cleanup( &our_set ); |
  65. | for( int i = 0; i < 10; i += 3 ) | return 1; |
  66. | vt_erase( &our_set, i ); | } |
  67. | | } |
  68. | // Retrieving keys. | |
  69. | for( int i = 0; i < 10; ++i ) | // Erasing keys. |
  70. | { | for( int i = 0; i < 10; i += 3 ) |
  71. | int_set_itr itr = vt_get( &our_set, i ); | int_set_erase( &our_set, i ); |
  72. | if( !vt_is_end( itr ) ) | |
  73. | printf( "%d ", itr.data->key ); | // Retrieving keys. |
  74. | } | for( int i = 0; i < 10; ++i ) |
  75. | // Printed: 1 2 4 5 7 8 | { |
  76. | | int_set_itr itr = int_set_get( &our_set, i ); |
  77. | // Iteration. | if( !int_set_is_end( itr ) ) |
  78. | for( | printf( "%d ", itr.data->key ); |
  79. | int_set_itr itr = vt_first( &our_set ); | } |
  80. | !vt_is_end( itr ); | // Printed: 1 2 4 5 7 8 |
  81. | itr = vt_next( itr ) | |
  82. | ) | // Iteration. |
  83. | printf( "%d ", itr.data->key ); | for( |
  84. | // Printed: 2 4 7 1 5 8 | int_set_itr itr = |
  85. | | int_set_first( &our_set ); |
  86. | vt_cleanup( &our_set ); | !int_set_is_end( itr ); |
  87. | | itr = int_set_next( itr ) |
  88. | // Map. | ) |
  89. | | printf( "%d ", itr.data->key ); |
  90. | int_int_map our_map; | // Printed: 2 4 7 1 5 8 |
  91. | vt_init( &our_map ); | |
  92. | | int_set_cleanup( &our_set ); |
  93. | // Inserting keys and values. | |
  94. | for( int i = 0; i < 10; ++i ) | // Map. |
  95. | { | |
  96. | int_int_map_itr itr = | int_int_map our_map; |
  97. | vt_insert( &our_map, i, i + 1 ); | int_int_map_init( &our_map ); |
  98. | if( vt_is_end( itr ) ) | |
  99. | { | // Inserting keys and values. |
  100. | // Out of memory, so abort. | for( int i = 0; i < 10; ++i ) |
  101. | vt_cleanup( &our_map ); | { |
  102. | return 1; | int_int_map_itr itr = |
  103. | } | int_int_map_insert( &our_map, i, i + 1 ); |
  104. | } | if( int_int_map_is_end( itr ) ) |
  105. | | { |
  106. | // Erasing keys and values. | // Out of memory, so abort. |
  107. | for( int i = 0; i < 10; i += 3 ) | int_int_map_cleanup( &our_map ); |
  108. | vt_erase( &our_map, i ); | return 1; |
  109. | | } |
  110. | // Retrieving keys and values. | } |
  111. | for( int i = 0; i < 10; ++i ) | |
  112. | { | // Erasing keys and values. |
  113. | int_int_map_itr itr = vt_get( &our_map, i ); | for( int i = 0; i < 10; i += 3 ) |
  114. | if( !vt_is_end( itr ) ) | int_int_map_erase( &our_map, i ); |
  115. | printf( | |
  116. | "%d:%d ", | // Retrieving keys and values. |
  117. | itr.data->key, | for( int i = 0; i < 10; ++i ) |
  118. | itr.data->val | { |
  119. | ); | int_int_map_itr itr = |
  120. | } | int_int_map_get( &our_map, i ); |
  121. | // Printed: 1:2 2:3 4:5 5:6 7:8 8:9 | if( !int_int_map_is_end( itr ) ) |
  122. | | printf( |
  123. | // Iteration. | "%d:%d ", |
  124. | for( | itr.data->key, |
  125. | int_int_map_itr itr = vt_first( &our_map ); | itr.data->val |
  126. | !vt_is_end( itr ); | ); |
  127. | itr = vt_next( itr ) | } |
  128. | ) | // Printed: 1:2 2:3 4:5 5:6 7:8 8:9 |
  129. | printf( | |
  130. | "%d:%d ", | // Iteration. |
  131. | itr.data->key, | for( |
  132. | itr.data->val | int_int_map_itr itr = |
  133. | ); | int_int_map_first( &our_map ); |
  134. | // Printed: 2:3 4:5 7:8 1:2 5:6 8:9 | !int_int_map_is_end( itr ); |
  135. | | itr = int_int_map_next( itr ) |
  136. | vt_cleanup( &our_map ); | ) |
  137. | } | printf( |
  138. | | "%d:%d ", |
  139. | | itr.data->key, |
  140. | | itr.data->val |
  141. | | ); |
  142. | | // Printed: 2:3 4:5 7:8 1:2 5:6 8:9 |
  143. | | |
  144. | | int_int_map_cleanup( &our_map ); |
  145. | | } |
  146. | | |
  147. +---------------------------------------------------------+----------------------------------------------------------+
  148. API:
  149. Instantiating a hash table template:
  150. Create a new hash table type in the following manner:
  151. #define NAME <your chosen type name>
  152. #define KEY_TY <type>
  153. #include "verstable.h"
  154. The NAME macro specifies the name of hash table type that the library will declare, the prefix for the functions
  155. associated with it, and the prefix for the associated iterator type.
  156. The KEY_TY macro specifies the key type.
  157. In C99, it is also always necessary to define HASH_FN and CMPR_FN (see below) before including the header.
  158. The following macros may also be defined before including the header:
  159. #define VAL_TY <type>
  160. The type of the value associated with each key.
  161. If this macro is defined, the hash table acts as a map associating keys with values.
  162. Otherwise, it acts as a set containing only keys.
  163. #define HASH_FN <function name>
  164. The name of the existing function used to hash each key.
  165. The function should have the signature uint64_t ( KEY_TY key ) and return a 64-bit hash code.
  166. For best performance, the hash function should provide a high level of entropy across all bits.
  167. There are two default hash functions: vt_hash_integer for all integer types up to 64 bits in size, and
  168. vt_hash_string for NULL-terminated strings (i.e. char *).
  169. When KEY_TY is one of such types and the compiler is in C11 mode or later, HASH_FN may be left undefined, in
  170. which case the appropriate default function is inferred from KEY_TY.
  171. Otherwise, HASH_FN must be defined.
  172. #define CMPR_FN <function name>
  173. The name of the existing function used to compare two keys.
  174. The function should have the signature bool ( KEY_TY key_1, KEY_TY key_2 ) and return true if the two keys are
  175. equal.
  176. There are two default comparison functions: vt_cmpr_integer for all integer types up to 64 bits in size, and
  177. vt_cmpr_string for NULL-terminated strings (i.e. char *).
  178. As with the default hash functions, in C11 or later the appropriate default comparison function is inferred if
  179. KEY_TY is one of such types and CMPR_FN is left undefined.
  180. Otherwise, CMPR_FN must be defined.
  181. #define MAX_LOAD <floating point value>
  182. The floating-point load factor at which the hash table automatically doubles the size of its internal buckets
  183. array.
  184. The default is 0.9, i.e. 90%.
  185. #define KEY_DTOR_FN <function name>
  186. The name of the existing destructor function, with the signature void ( KEY_TY key ), called on a key when it is
  187. erased from the table or replaced by a newly inserted key.
  188. The API functions that may call the key destructor are NAME_insert, NAME_erase, NAME_erase_itr, NAME_clear,
  189. and NAME_cleanup.
  190. #define VAL_DTOR_FN <function name>
  191. The name of the existing destructor function, with the signature void ( VAL_TY val ), called on a value when it
  192. is erased from the table or replaced by a newly inserted value.
  193. The API functions that may call the value destructor are NAME_insert, NAME_erase, NAME_erase_itr, NAME_clear,
  194. and NAME_cleanup.
  195. #define CTX_TY <type>
  196. The type of the hash table type's ctx (context) member.
  197. This member only exists if CTX_TY was defined.
  198. It is intended to be used in conjunction with MALLOC_FN and FREE_FN (see below).
  199. #define MALLOC_FN <function name>
  200. The name of the existing function used to allocate memory.
  201. If CTX_TY was defined, the signature should be void *( size_t size, CTX_TY *ctx ), where size is the number of
  202. bytes to allocate and ctx points to the table's ctx member.
  203. Otherwise, the signature should be void *( size_t size ).
  204. The default wraps stdlib.h's malloc.
  205. #define FREE_FN <function name>
  206. The name of the existing function used to free memory.
  207. If CTX_TY was defined, the signature should be void ( void *ptr, size_t size, CTX_TY *ctx ), where ptr points to
  208. the memory to free, size is the number of bytes that were allocated, and ctx points to the table's ctx member.
  209. Otherwise, the signature should be void ( void *ptr, size_t size ).
  210. The default wraps stdlib.h's free.
  211. #define HEADER_MODE
  212. #define IMPLEMENTATION_MODE
  213. By default, all hash table functions are defined as static inline functions, the intent being that a given hash
  214. table template should be instantiated once per translation unit; for best performance, this is the recommended
  215. way to use the library.
  216. However, it is also possible separate the struct definitions and function declarations from the function
  217. definitions such that one implementation can be shared across all translation units (as in a traditional header
  218. and source file pair).
  219. In that case, instantiate a template wherever it is needed by defining HEADER_MODE, along with only NAME,
  220. KEY_TY, and (optionally) VAL_TY, CTX_TY, and header guards, and including the library, e.g.:
  221. #ifndef INT_INT_MAP_H
  222. #define INT_INT_MAP_H
  223. #define NAME int_int_map
  224. #define KEY_TY int
  225. #define VAL_TY int
  226. #define HEADER_MODE
  227. #include "verstable.h"
  228. #endif
  229. In one source file, define IMPLEMENTATION_MODE, along with NAME, KEY_TY, and any of the aforementioned optional
  230. macros, and include the library, e.g.:
  231. #define NAME int_int_map
  232. #define KEY_TY int
  233. #define VAL_TY int
  234. #define HASH_FN vt_hash_integer // C99.
  235. #define CMPR_FN vt_cmpr_integer // C99.
  236. #define MAX_LOAD 0.8
  237. #define IMPLEMENTATION_MODE
  238. #include "verstable.h"
  239. Including the library automatically undefines all the aforementioned macros after they have been used to instantiate
  240. the template.
  241. Functions:
  242. The functions associated with a hash table type are all prefixed with the name the user supplied via the NAME macro.
  243. In C11 and later, the generic "vt_"-prefixed macros may be used to automatically select the correct version of the
  244. specified function based on the arguments.
  245. void NAME_init( NAME *table )
  246. void NAME_init( NAME *table, CTX_TY ctx )
  247. // C11 generic macro: vt_init.
  248. Initializes the table for use.
  249. If CTX_TY was defined, ctx sets the table's ctx member.
  250. bool NAME_init_clone( NAME *table, NAME *source )
  251. bool NAME_init_clone( NAME *table, NAME *source, CTX_TY ctx )
  252. // C11 generic macro: vt_init_clone.
  253. Initializes the table as a shallow copy of the specified source table.
  254. If CTX_TY was defined, ctx sets the table's ctx member.
  255. Returns false in the case of memory allocation failure.
  256. size_t NAME_size( NAME *table ) // C11 generic macro: vt_size.
  257. Returns the number of keys currently in the table.
  258. size_t NAME_bucket_count( NAME *table ) // C11 generic macro: vt_bucket_count.
  259. Returns the table's current bucket count.
  260. NAME_itr NAME_insert( NAME *table, KEY_TY key )
  261. NAME_itr NAME_insert( NAME *table, KEY_TY key, VAL_TY val )
  262. // C11 generic macro: vt_insert.
  263. Inserts the specified key (and value, if VAL_TY was defined) into the hash table.
  264. If the same key already exists, then the new key (and value) replaces the existing key (and value).
  265. Returns an iterator to the new key, or an end iterator in the case of memory allocation failure.
  266. NAME_itr NAME_get_or_insert( NAME *table, KEY_TY key )
  267. NAME_itr NAME_get_or_insert( NAME *table, KEY_TY key, VAL_TY val )
  268. // C11 generic macro: vt_get_or_insert.
  269. Inserts the specified key (and value, if VAL_TY was defined) if it does not already exist in the table.
  270. Returns an iterator to the new key if it was inserted, or an iterator to the existing key, or an end iterator if
  271. the key did not exist but the new key could not be inserted because of memory allocation failure.
  272. Determine whether the key was inserted by comparing the table's size before and after the call.
  273. NAME_itr NAME_get( NAME *table, KEY_TY key ) // C11 generic macro: vt_get.
  274. Returns a iterator to the specified key, or an end iterator if no such key exists.
  275. bool NAME_erase( NAME *table, KEY_TY key ) // C11 generic macro: vt_erase.
  276. Erases the specified key (and associated value, if VAL_TY was defined), if it exists.
  277. Returns true if a key was erased.
  278. NAME_itr NAME_erase_itr( NAME *table, NAME_itr itr ) // C11 generic macro: vt_erase_itr.
  279. Erases the key (and associated value, if VAL_TY was defined) pointed to by the specified iterator.
  280. Returns an iterator to the next key in the table, or an end iterator if the erased key was the last one.
  281. bool NAME_reserve( NAME *table, size_t size ) // C11 generic macro: vt_reserve.
  282. Ensures that the bucket count is large enough to support the specified key count (i.e. size) without rehashing.
  283. Returns false if unsuccessful due to memory allocation failure.
  284. bool NAME_shrink( NAME *table ) // C11 generic macro: vt_shrink.
  285. Shrinks the bucket count to best accommodate the current size.
  286. Returns false if unsuccessful due to memory allocation failure.
  287. NAME_itr NAME_first( NAME *table ) // C11 generic macro: vt_first.
  288. Returns an iterator to the first key in the table, or an end iterator if the table is empty.
  289. bool NAME_is_end( NAME *table, NAME_itr itr ) // C11 generic macro: vt_is_end.
  290. Returns true if the iterator is an end iterator.
  291. NAME_itr NAME_next( NAME_itr itr ) // C11 generic macro: vt_next.
  292. Returns an iterator to the key after the one pointed to by the specified iterator, or an end iterator if the
  293. specified iterator points to the last key in the table.
  294. void NAME_clear( NAME *table ) // C11 generic macro: vt_clear.
  295. Erases all keys (and values, if VAL_TY was defined) in the table.
  296. void NAME_cleanup( NAME *table ) // C11 generic macro: vt_cleanup.
  297. Erases all keys (and values, if VAL_TY was defined) in the table, frees all memory associated with it, and
  298. initializes it for reuse.
  299. Iterators:
  300. Access the key (and value, if VAL_TY was defined) that an iterator points to using the NAME_itr struct's data
  301. member:
  302. itr.data->key
  303. itr.data->val
  304. Functions that may insert new keys (NAME_insert and NAME_get_or_insert), erase keys (NAME_erase and NAME_erase_itr),
  305. or reallocate the internal bucket array (NAME_reserve and NAME_shrink) invalidate all exiting iterators.
  306. To delete keys during iteration and resume iterating, use the return value of NAME_erase_itr.
  307. Version history:
  308. 18/06/2024 2.1.1: Fixed a bug affecting iteration on big-endian platforms under MSVC.
  309. 27/05/2024 2.1.0: Replaced the Murmur3 mixer with the fast-hash mixer as the default integer hash function.
  310. Fixed a bug that could theoretically cause a crash on rehash (triggerable in testing using
  311. NAME_shrink with a maximum load factor significantly higher than 1.0).
  312. 06/02/2024 2.0.0: Improved custom allocator support by introducing the CTX_TY option and allowing user-supplied free
  313. functions to receive the allocation size.
  314. Improved documentation.
  315. Introduced various optimizations, including storing the buckets-array size mask instead of the
  316. bucket count, eliminating empty-table checks, combining the buckets memory and metadata memory into
  317. one allocation, and adding branch prediction macros.
  318. Fixed a bug that caused a key to be used after destruction during erasure.
  319. 12/12/2023 1.0.0: Initial release.
  320. License (MIT):
  321. Copyright (c) 2023-2024 Jackson L. Allan
  322. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  323. documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  324. rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
  325. persons to whom the Software is furnished to do so, subject to the following conditions:
  326. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  327. Software.
  328. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  329. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  330. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  331. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  332. */
  333. /*--------------------------------------------------------------------------------------------------------------------*/
  334. /* Common header section */
  335. /*--------------------------------------------------------------------------------------------------------------------*/
  336. #ifndef VERSTABLE_H
  337. #define VERSTABLE_H
  338. #include <limits.h>
  339. #include <stddef.h>
  340. #include <stdint.h>
  341. #include <stdlib.h>
  342. #include <stdbool.h>
  343. #include <string.h>
  344. // Two-way concatenation macro.
  345. #define VT_CAT_( a, b ) a##b
  346. #define VT_CAT( a, b ) VT_CAT_( a, b )
  347. // Branch optimization macros.
  348. #ifdef __GNUC__
  349. #define VT_LIKELY( expression ) __builtin_expect( (bool)( expression ), true )
  350. #define VT_UNLIKELY( expression ) __builtin_expect( (bool)( expression ), false )
  351. #else
  352. #define VT_LIKELY( expression ) ( expression )
  353. #define VT_UNLIKELY( expression ) ( expression )
  354. #endif
  355. // Masks for manipulating and extracting data from a bucket's uint16_t metadatum.
  356. #define VT_EMPTY 0x0000
  357. #define VT_HASH_FRAG_MASK 0xF000 // 0b1111000000000000.
  358. #define VT_IN_HOME_BUCKET_MASK 0x0800 // 0b0000100000000000.
  359. #define VT_DISPLACEMENT_MASK 0x07FF // 0b0000011111111111, also denotes the displacement limit. Set to VT_LOAD to 1.0
  360. // to test proper handling of encroachment on the displacement limit during
  361. // inserts.
  362. // Extracts a hash fragment from a uint64_t hash code.
  363. // We take the highest four bits so that keys that map (via modulo) to the same bucket have distinct hash fragments.
  364. static inline uint16_t vt_hashfrag( uint64_t hash )
  365. {
  366. return ( hash >> 48 ) & VT_HASH_FRAG_MASK;
  367. }
  368. // Standard quadratic probing formula that guarantees that all buckets are visited when the bucket count is a power of
  369. // two (at least in theory, because the displacement limit could terminate the search early when the bucket count is
  370. // high).
  371. static inline size_t vt_quadratic( uint16_t displacement )
  372. {
  373. return ( (size_t)displacement * displacement + displacement ) / 2;
  374. }
  375. #define VT_MIN_NONZERO_BUCKET_COUNT 8 // Must be a power of two.
  376. // Function to find the left-most non-zero uint16_t in a uint64_t.
  377. // This function is used when we scan four buckets at a time while iterating and relies on compiler intrinsics wherever
  378. // possible.
  379. #if defined( __GNUC__ ) && ULLONG_MAX == 0xFFFFFFFFFFFFFFFF
  380. static inline int vt_first_nonzero_uint16( uint64_t val )
  381. {
  382. const uint16_t endian_checker = 0x0001;
  383. if( *(const char *)&endian_checker ) // Little-endian (the compiler will optimize away the check at -O1 and above).
  384. return __builtin_ctzll( val ) / 16;
  385. return __builtin_clzll( val ) / 16;
  386. }
  387. #elif defined( _MSC_VER ) && ( defined( _M_X64 ) || defined( _M_ARM64 ) )
  388. #include <intrin.h>
  389. #pragma intrinsic(_BitScanForward64)
  390. #pragma intrinsic(_BitScanReverse64)
  391. static inline int vt_first_nonzero_uint16( uint64_t val )
  392. {
  393. unsigned long result;
  394. const uint16_t endian_checker = 0x0001;
  395. if( *(const char *)&endian_checker )
  396. _BitScanForward64( &result, val );
  397. else
  398. {
  399. _BitScanReverse64( &result, val );
  400. result = 63 - result;
  401. }
  402. return result / 16;
  403. }
  404. #else
  405. static inline int vt_first_nonzero_uint16( uint64_t val )
  406. {
  407. int result = 0;
  408. uint32_t half;
  409. memcpy( &half, &val, sizeof( uint32_t ) );
  410. if( !half )
  411. result += 2;
  412. uint16_t quarter;
  413. memcpy( &quarter, (char *)&val + result * sizeof( uint16_t ), sizeof( uint16_t ) );
  414. if( !quarter )
  415. result += 1;
  416. return result;
  417. }
  418. #endif
  419. // When the bucket count is zero, setting the metadata pointer to point to a VT_EMPTY placeholder, rather than NULL,
  420. // allows us to avoid checking for a zero bucket count during insertion and lookup.
  421. static const uint16_t vt_empty_placeholder_metadatum = VT_EMPTY;
  422. // Default hash and comparison functions.
  423. // Fast-hash, as described by https://jonkagstrom.com/bit-mixer-construction and
  424. // https://code.google.com/archive/p/fast-hash.
  425. // In testing, this hash function provided slightly better performance than the Murmur3 mixer.
  426. static inline uint64_t vt_hash_integer( uint64_t key )
  427. {
  428. key ^= key >> 23;
  429. key *= 0x2127599bf4325c37ull;
  430. key ^= key >> 47;
  431. return key;
  432. }
  433. // FNV-1a.
  434. static inline uint64_t vt_hash_string( const char *key )
  435. {
  436. uint64_t hash = 0xcbf29ce484222325ull;
  437. while( *key )
  438. hash = ( (unsigned char)*key++ ^ hash ) * 0x100000001b3ull;
  439. return hash;
  440. }
  441. static inline bool vt_cmpr_integer( uint64_t key_1, uint64_t key_2 )
  442. {
  443. return key_1 == key_2;
  444. }
  445. static inline bool vt_cmpr_string( const char *key_1, const char *key_2 )
  446. {
  447. return strcmp( key_1, key_2 ) == 0;
  448. }
  449. // Default allocation and free functions.
  450. static inline void *vt_malloc( size_t size )
  451. {
  452. return malloc( size );
  453. }
  454. static inline void vt_free( void *ptr, size_t size )
  455. {
  456. (void)size;
  457. free( ptr );
  458. }
  459. static inline void *vt_malloc_with_ctx( size_t size, void *ctx )
  460. {
  461. (void)ctx;
  462. return malloc( size );
  463. }
  464. static inline void vt_free_with_ctx( void *ptr, size_t size, void *ctx )
  465. {
  466. (void)size;
  467. (void)ctx;
  468. free( ptr );
  469. }
  470. // The rest of the common header section pertains to the C11 generic macro API.
  471. // This interface is based on the extendible-_Generic mechanism documented in detail at
  472. // https://github.com/JacksonAllan/CC/blob/main/articles/Better_C_Generics_Part_1_The_Extendible_Generic.md.
  473. // In summary, instantiating a template also defines wrappers for the template's types and functions with names in the
  474. // pattern of vt_table_NNNN and vt_init_NNNN, where NNNN is an automatically generated integer unique to the template
  475. // instance in the current translation unit.
  476. // These wrappers plug in to _Generic-based API macros, which use preprocessor magic to automatically generate _Generic
  477. // slots for every existing template instance.
  478. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined( VT_NO_C11_GENERIC_API )
  479. // Octal counter that supports up to 511 hash table templates.
  480. #define VT_TEMPLATE_COUNT_D1 0 // Digit 1, i.e. least significant digit.
  481. #define VT_TEMPLATE_COUNT_D2 0
  482. #define VT_TEMPLATE_COUNT_D3 0
  483. // Four-way concatenation macro.
  484. #define VT_CAT_4_( a, b, c, d ) a##b##c##d
  485. #define VT_CAT_4( a, b, c, d ) VT_CAT_4_( a, b, c, d )
  486. // Provides the current value of the counter as a three-digit octal number preceded by 0.
  487. #define VT_TEMPLATE_COUNT VT_CAT_4( 0, VT_TEMPLATE_COUNT_D3, VT_TEMPLATE_COUNT_D2, VT_TEMPLATE_COUNT_D1 )
  488. // _Generic-slot generation macros.
  489. #define VT_GENERIC_SLOT( ty, fn, n ) , VT_CAT( ty, n ): VT_CAT( fn, n )
  490. #define VT_R1_0( ty, fn, d3, d2 )
  491. #define VT_R1_1( ty, fn, d3, d2 ) VT_GENERIC_SLOT( ty, fn, VT_CAT_4( 0, d3, d2, 0 ) )
  492. #define VT_R1_2( ty, fn, d3, d2 ) VT_GENERIC_SLOT( ty, fn, VT_CAT_4( 0, d3, d2, 1 ) ) VT_R1_1( ty, fn, d3, d2 )
  493. #define VT_R1_3( ty, fn, d3, d2 ) VT_GENERIC_SLOT( ty, fn, VT_CAT_4( 0, d3, d2, 2 ) ) VT_R1_2( ty, fn, d3, d2 )
  494. #define VT_R1_4( ty, fn, d3, d2 ) VT_GENERIC_SLOT( ty, fn, VT_CAT_4( 0, d3, d2, 3 ) ) VT_R1_3( ty, fn, d3, d2 )
  495. #define VT_R1_5( ty, fn, d3, d2 ) VT_GENERIC_SLOT( ty, fn, VT_CAT_4( 0, d3, d2, 4 ) ) VT_R1_4( ty, fn, d3, d2 )
  496. #define VT_R1_6( ty, fn, d3, d2 ) VT_GENERIC_SLOT( ty, fn, VT_CAT_4( 0, d3, d2, 5 ) ) VT_R1_5( ty, fn, d3, d2 )
  497. #define VT_R1_7( ty, fn, d3, d2 ) VT_GENERIC_SLOT( ty, fn, VT_CAT_4( 0, d3, d2, 6 ) ) VT_R1_6( ty, fn, d3, d2 )
  498. #define VT_R1_8( ty, fn, d3, d2 ) VT_GENERIC_SLOT( ty, fn, VT_CAT_4( 0, d3, d2, 7 ) ) VT_R1_7( ty, fn, d3, d2 )
  499. #define VT_R2_0( ty, fn, d3 )
  500. #define VT_R2_1( ty, fn, d3 ) VT_R1_8( ty, fn, d3, 0 )
  501. #define VT_R2_2( ty, fn, d3 ) VT_R1_8( ty, fn, d3, 1 ) VT_R2_1( ty, fn, d3 )
  502. #define VT_R2_3( ty, fn, d3 ) VT_R1_8( ty, fn, d3, 2 ) VT_R2_2( ty, fn, d3 )
  503. #define VT_R2_4( ty, fn, d3 ) VT_R1_8( ty, fn, d3, 3 ) VT_R2_3( ty, fn, d3 )
  504. #define VT_R2_5( ty, fn, d3 ) VT_R1_8( ty, fn, d3, 4 ) VT_R2_4( ty, fn, d3 )
  505. #define VT_R2_6( ty, fn, d3 ) VT_R1_8( ty, fn, d3, 5 ) VT_R2_5( ty, fn, d3 )
  506. #define VT_R2_7( ty, fn, d3 ) VT_R1_8( ty, fn, d3, 6 ) VT_R2_6( ty, fn, d3 )
  507. #define VT_R2_8( ty, fn, d3 ) VT_R1_8( ty, fn, d3, 7 ) VT_R2_7( ty, fn, d3 )
  508. #define VT_R3_0( ty, fn )
  509. #define VT_R3_1( ty, fn ) VT_R2_8( ty, fn, 0 )
  510. #define VT_R3_2( ty, fn ) VT_R2_8( ty, fn, 1 ) VT_R3_1( ty, fn )
  511. #define VT_R3_3( ty, fn ) VT_R2_8( ty, fn, 2 ) VT_R3_2( ty, fn )
  512. #define VT_R3_4( ty, fn ) VT_R2_8( ty, fn, 3 ) VT_R3_3( ty, fn )
  513. #define VT_R3_5( ty, fn ) VT_R2_8( ty, fn, 4 ) VT_R3_4( ty, fn )
  514. #define VT_R3_6( ty, fn ) VT_R2_8( ty, fn, 5 ) VT_R3_5( ty, fn )
  515. #define VT_R3_7( ty, fn ) VT_R2_8( ty, fn, 6 ) VT_R3_6( ty, fn )
  516. #define VT_GENERIC_SLOTS( ty, fn ) \
  517. VT_CAT( VT_R1_, VT_TEMPLATE_COUNT_D1 )( ty, fn, VT_TEMPLATE_COUNT_D3, VT_TEMPLATE_COUNT_D2 ) \
  518. VT_CAT( VT_R2_, VT_TEMPLATE_COUNT_D2 )( ty, fn, VT_TEMPLATE_COUNT_D3 ) \
  519. VT_CAT( VT_R3_, VT_TEMPLATE_COUNT_D3 )( ty, fn ) \
  520. // Actual generic API macros.
  521. // vt_init must be handled as a special case because it could take one or two arguments, depending on whether CTX_TY
  522. // was defined.
  523. #define VT_ARG_3( _1, _2, _3, ... ) _3
  524. #define vt_init( ... ) VT_ARG_3( __VA_ARGS__, vt_init_with_ctx, vt_init_without_ctx, )( __VA_ARGS__ )
  525. #define vt_init_without_ctx( table ) _Generic( *( table ) VT_GENERIC_SLOTS( vt_table_, vt_init_ ) )( table )
  526. #define vt_init_with_ctx( table, ... ) _Generic( *( table ) \
  527. VT_GENERIC_SLOTS( vt_table_, vt_init_ ) \
  528. )( table, __VA_ARGS__ ) \
  529. #define vt_init_clone( table, ... ) _Generic( *( table ) \
  530. VT_GENERIC_SLOTS( vt_table_, vt_init_clone_ ) \
  531. )( table, __VA_ARGS__ ) \
  532. #define vt_size( table )_Generic( *( table ) VT_GENERIC_SLOTS( vt_table_, vt_size_ ) )( table )
  533. #define vt_bucket_count( table ) _Generic( *( table ) VT_GENERIC_SLOTS( vt_table_, vt_bucket_count_ ) )( table )
  534. #define vt_is_end( itr ) _Generic( itr VT_GENERIC_SLOTS( vt_table_itr_, vt_is_end_ ) )( itr )
  535. #define vt_insert( table, ... ) _Generic( *( table ) VT_GENERIC_SLOTS( vt_table_, vt_insert_ ) )( table, __VA_ARGS__ )
  536. #define vt_get_or_insert( table, ... ) _Generic( *( table ) \
  537. VT_GENERIC_SLOTS( vt_table_, vt_get_or_insert_ ) \
  538. )( table, __VA_ARGS__ ) \
  539. #define vt_get( table, ... ) _Generic( *( table ) VT_GENERIC_SLOTS( vt_table_, vt_get_ ) )( table, __VA_ARGS__ )
  540. #define vt_erase( table, ... ) _Generic( *( table ) VT_GENERIC_SLOTS( vt_table_, vt_erase_ ) )( table, __VA_ARGS__ )
  541. #define vt_next( itr ) _Generic( itr VT_GENERIC_SLOTS( vt_table_itr_, vt_next_ ) )( itr )
  542. #define vt_erase_itr( table, ... ) _Generic( *( table ) \
  543. VT_GENERIC_SLOTS( vt_table_, vt_erase_itr_ ) \
  544. )( table, __VA_ARGS__ ) \
  545. #define vt_reserve( table, ... ) _Generic( *( table ) VT_GENERIC_SLOTS( vt_table_, vt_reserve_ ) )( table, __VA_ARGS__ )
  546. #define vt_shrink( table ) _Generic( *( table ) VT_GENERIC_SLOTS( vt_table_, vt_shrink_ ) )( table )
  547. #define vt_first( table ) _Generic( *( table ) VT_GENERIC_SLOTS( vt_table_, vt_first_ ) )( table )
  548. #define vt_clear( table ) _Generic( *( table ) VT_GENERIC_SLOTS( vt_table_, vt_clear_ ) )( table )
  549. #define vt_cleanup( table ) _Generic( *( table ) VT_GENERIC_SLOTS( vt_table_, vt_cleanup_ ) )( table )
  550. #endif
  551. #endif
  552. /*--------------------------------------------------------------------------------------------------------------------*/
  553. /* Prefixed structs */
  554. /*--------------------------------------------------------------------------------------------------------------------*/
  555. #ifndef IMPLEMENTATION_MODE
  556. typedef struct
  557. {
  558. KEY_TY key;
  559. #ifdef VAL_TY
  560. VAL_TY val;
  561. #endif
  562. } VT_CAT( NAME, _bucket );
  563. typedef struct
  564. {
  565. VT_CAT( NAME, _bucket ) *data;
  566. uint16_t *metadatum;
  567. uint16_t *metadata_end; // Iterators carry an internal end pointer so that NAME_is_end does not need the table to be
  568. // passed in as an argument.
  569. // This also allows for the zero-bucket-count check to occur once in NAME_first, rather than
  570. // repeatedly in NAME_is_end.
  571. size_t home_bucket; // SIZE_MAX if home bucket is unknown.
  572. } VT_CAT( NAME, _itr );
  573. typedef struct
  574. {
  575. size_t key_count;
  576. size_t buckets_mask; // Rather than storing the bucket count directly, we store the bit mask used to reduce a hash
  577. // code or displacement-derived bucket index to the buckets array, i.e. the bucket count minus
  578. // one.
  579. // Consequently, a zero bucket count (i.e. when .metadata points to the placeholder) constitutes
  580. // a special case, represented by all bits unset (i.e. zero).
  581. VT_CAT( NAME, _bucket ) *buckets;
  582. uint16_t *metadata; // As described above, each metadatum consists of a 4-bit hash-code fragment (X), a 1-bit flag
  583. // indicating whether the key in this bucket begins a chain associated with the bucket (Y), and
  584. // an 11-bit value indicating the quadratic displacement of the next key in the chain (Z):
  585. // XXXXYZZZZZZZZZZZ.
  586. #ifdef CTX_TY
  587. CTX_TY ctx;
  588. #endif
  589. } NAME;
  590. #endif
  591. /*--------------------------------------------------------------------------------------------------------------------*/
  592. /* Function prototypes */
  593. /*--------------------------------------------------------------------------------------------------------------------*/
  594. #if defined( HEADER_MODE ) || defined( IMPLEMENTATION_MODE )
  595. #define VT_API_FN_QUALIFIERS
  596. #else
  597. #define VT_API_FN_QUALIFIERS static inline
  598. #endif
  599. #ifndef IMPLEMENTATION_MODE
  600. VT_API_FN_QUALIFIERS void VT_CAT( NAME, _init )(
  601. NAME *
  602. #ifdef CTX_TY
  603. , CTX_TY
  604. #endif
  605. );
  606. VT_API_FN_QUALIFIERS bool VT_CAT( NAME, _init_clone )(
  607. NAME *,
  608. NAME *
  609. #ifdef CTX_TY
  610. , CTX_TY
  611. #endif
  612. );
  613. VT_API_FN_QUALIFIERS size_t VT_CAT( NAME, _size )( const NAME * );
  614. VT_API_FN_QUALIFIERS size_t VT_CAT( NAME, _bucket_count )( const NAME * );
  615. VT_API_FN_QUALIFIERS bool VT_CAT( NAME, _is_end )( VT_CAT( NAME, _itr ) );
  616. VT_API_FN_QUALIFIERS VT_CAT( NAME, _itr ) VT_CAT( NAME, _insert )(
  617. NAME *,
  618. KEY_TY
  619. #ifdef VAL_TY
  620. , VAL_TY
  621. #endif
  622. );
  623. VT_API_FN_QUALIFIERS VT_CAT( NAME, _itr ) VT_CAT( NAME, _get_or_insert )(
  624. NAME *,
  625. KEY_TY
  626. #ifdef VAL_TY
  627. , VAL_TY
  628. #endif
  629. );
  630. VT_API_FN_QUALIFIERS VT_CAT( NAME, _itr ) VT_CAT( NAME, _get )(
  631. NAME *table,
  632. KEY_TY key
  633. );
  634. VT_API_FN_QUALIFIERS bool VT_CAT( NAME, _erase )( NAME *, KEY_TY );
  635. VT_API_FN_QUALIFIERS VT_CAT( NAME, _itr ) VT_CAT( NAME, _next )( VT_CAT( NAME, _itr ) );
  636. VT_API_FN_QUALIFIERS bool VT_CAT( NAME, _reserve )( NAME *, size_t );
  637. VT_API_FN_QUALIFIERS bool VT_CAT( NAME, _shrink )( NAME * );
  638. VT_API_FN_QUALIFIERS VT_CAT( NAME, _itr ) VT_CAT( NAME, _first )( NAME * );
  639. VT_API_FN_QUALIFIERS void VT_CAT( NAME, _clear )( NAME * );
  640. VT_API_FN_QUALIFIERS void VT_CAT( NAME, _cleanup )( NAME * );
  641. // Not an API function, but must be prototyped anyway because it is called by the inline NAME_erase_itr below.
  642. VT_API_FN_QUALIFIERS bool VT_CAT( NAME, _erase_itr_raw ) ( NAME *, VT_CAT( NAME, _itr ) );
  643. // Erases the key pointed to by itr and returns an iterator to the next key in the table.
  644. // This function must be inlined to ensure that the compiler optimizes away the NAME_fast_forward call if the returned
  645. // iterator is discarded.
  646. #ifdef __GNUC__
  647. static inline __attribute__((always_inline))
  648. #elif defined( _MSC_VER )
  649. static __forceinline
  650. #else
  651. static inline
  652. #endif
  653. VT_CAT( NAME, _itr ) VT_CAT( NAME, _erase_itr )( NAME *table, VT_CAT( NAME, _itr ) itr )
  654. {
  655. if( VT_CAT( NAME, _erase_itr_raw )( table, itr ) )
  656. return VT_CAT( NAME, _next )( itr );
  657. return itr;
  658. }
  659. #endif
  660. /*--------------------------------------------------------------------------------------------------------------------*/
  661. /* Function implementations */
  662. /*--------------------------------------------------------------------------------------------------------------------*/
  663. #ifndef HEADER_MODE
  664. // Default settings.
  665. #ifndef MAX_LOAD
  666. #define MAX_LOAD 0.9
  667. #endif
  668. #if !defined( MALLOC ) || !defined( FREE )
  669. #include <stdlib.h>
  670. #endif
  671. #ifndef MALLOC_FN
  672. #ifdef CTX_TY
  673. #define MALLOC_FN vt_malloc_with_ctx
  674. #else
  675. #define MALLOC_FN vt_malloc
  676. #endif
  677. #endif
  678. #ifndef FREE_FN
  679. #ifdef CTX_TY
  680. #define FREE_FN vt_free_with_ctx
  681. #else
  682. #define FREE_FN vt_free
  683. #endif
  684. #endif
  685. #ifndef HASH_FN
  686. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  687. #ifdef _MSC_VER // In MSVC, the compound literal in the _Generic triggers a warning about unused local variables at /W4.
  688. #define HASH_FN \
  689. _Pragma( "warning( push )" ) \
  690. _Pragma( "warning( disable: 4189 )" ) \
  691. _Generic( ( KEY_TY ){ 0 }, char *: vt_hash_string, const char*: vt_hash_string, default: vt_hash_integer ) \
  692. _Pragma( "warning( pop )" )
  693. #else
  694. #define HASH_FN _Generic( ( KEY_TY ){ 0 }, char *: vt_hash_string, const char*: vt_hash_string, default: vt_hash_integer )
  695. #endif
  696. #else
  697. #error Hash function inference is only available in C11 and later. In C99, you need to define HASH_FN manually to \
  698. vt_hash_integer, vt_hash_string, or your own custom function with the signature uint64_t ( KEY_TY ).
  699. #endif
  700. #endif
  701. #ifndef CMPR_FN
  702. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  703. #ifdef _MSC_VER
  704. #define CMPR_FN \
  705. _Pragma( "warning( push )" ) \
  706. _Pragma( "warning( disable: 4189 )" ) \
  707. _Generic( ( KEY_TY ){ 0 }, char *: vt_cmpr_string, const char*: vt_cmpr_string, default: vt_cmpr_integer ) \
  708. _Pragma( "warning( pop )" )
  709. #else
  710. #define CMPR_FN _Generic( ( KEY_TY ){ 0 }, char *: vt_cmpr_string, const char*: vt_cmpr_string, default: vt_cmpr_integer )
  711. #endif
  712. #else
  713. #error Comparison function inference is only available in C11 and later. In C99, you need to define CMPR_FN manually \
  714. to vt_cmpr_integer, vt_cmpr_string, or your own custom function with the signature bool ( KEY_TY, KEY_TY ).
  715. #endif
  716. #endif
  717. VT_API_FN_QUALIFIERS void VT_CAT( NAME, _init )(
  718. NAME *table
  719. #ifdef CTX_TY
  720. , CTX_TY ctx
  721. #endif
  722. )
  723. {
  724. table->key_count = 0;
  725. table->buckets_mask = 0x0000000000000000ull;
  726. table->buckets = NULL;
  727. table->metadata = (uint16_t *)&vt_empty_placeholder_metadatum;
  728. #ifdef CTX_TY
  729. table->ctx = ctx;
  730. #endif
  731. }
  732. // For efficiency, especially in the case of a small table, the buckets array and metadata share the same dynamic memory
  733. // allocation:
  734. // +-----------------------------+-----+----------------+--------+
  735. // | Buckets | Pad | Metadata | Excess |
  736. // +-----------------------------+-----+----------------+--------+
  737. // Any allocated metadata array requires four excess elements to ensure that iteration functions, which read four
  738. // metadata at a time, never read beyond the end of it.
  739. // This function returns the offset of the beginning of the metadata, i.e. the size of the buckets array plus the
  740. // (usually zero) padding.
  741. // It assumes that the bucket count is not zero.
  742. static inline size_t VT_CAT( NAME, _metadata_offset )( NAME *table )
  743. {
  744. // Use sizeof, rather than alignof, for C99 compatibility.
  745. return ( ( ( table->buckets_mask + 1 ) * sizeof( VT_CAT( NAME, _bucket ) ) + sizeof( uint16_t ) - 1 ) /
  746. sizeof( uint16_t ) ) * sizeof( uint16_t );
  747. }
  748. // Returns the total allocation size, including the buckets array, padding, metadata, and excess metadata.
  749. // As above, this function assumes that the bucket count is not zero.
  750. static inline size_t VT_CAT( NAME, _total_alloc_size )( NAME *table )
  751. {
  752. return VT_CAT( NAME, _metadata_offset )( table ) + ( table->buckets_mask + 1 + 4 ) * sizeof( uint16_t );
  753. }
  754. VT_API_FN_QUALIFIERS bool VT_CAT( NAME, _init_clone )(
  755. NAME *table,
  756. NAME *source
  757. #ifdef CTX_TY
  758. , CTX_TY ctx
  759. #endif
  760. )
  761. {
  762. table->key_count = source->key_count;
  763. table->buckets_mask = source->buckets_mask;
  764. #ifdef CTX_TY
  765. table->ctx = ctx;
  766. #endif
  767. if( !source->buckets_mask )
  768. {
  769. table->metadata = (uint16_t *)&vt_empty_placeholder_metadatum;
  770. table->buckets = NULL;
  771. return true;
  772. }
  773. void *allocation = MALLOC_FN(
  774. VT_CAT( NAME, _total_alloc_size )( table )
  775. #ifdef CTX_TY
  776. , &table->ctx
  777. #endif
  778. );
  779. if( VT_UNLIKELY( !allocation ) )
  780. return false;
  781. table->buckets = (VT_CAT( NAME, _bucket ) *)allocation;
  782. table->metadata = (uint16_t *)( (unsigned char *)allocation + VT_CAT( NAME, _metadata_offset )( table ) );
  783. memcpy( allocation, source->buckets, VT_CAT( NAME, _total_alloc_size )( table ) );
  784. return true;
  785. }
  786. VT_API_FN_QUALIFIERS size_t VT_CAT( NAME, _size )( const NAME *table )
  787. {
  788. return table->key_count;
  789. }
  790. VT_API_FN_QUALIFIERS size_t VT_CAT( NAME, _bucket_count )( const NAME *table )
  791. {
  792. // If the bucket count is zero, buckets_mask will be zero, not the bucket count minus one.
  793. // We account for this special case by adding (bool)buckets_mask rather than one.
  794. return table->buckets_mask + (bool)table->buckets_mask;
  795. }
  796. VT_API_FN_QUALIFIERS bool VT_CAT( NAME, _is_end )( VT_CAT( NAME, _itr ) itr )
  797. {
  798. return itr.metadatum == itr.metadata_end;
  799. }
  800. // Finds the earliest empty bucket in which a key belonging to home_bucket can be placed, assuming that home_bucket
  801. // is already occupied.
  802. // The reason to begin the search at home_bucket, rather than the end of the existing chain, is that keys deleted from
  803. // other chains might have freed up buckets that could fall in this chain before the final key.
  804. // Returns true if an empty bucket within the range of the displacement limit was found, in which case the final two
  805. // pointer arguments contain the index of the empty bucket and its quadratic displacement from home_bucket.
  806. static inline bool VT_CAT( NAME, _find_first_empty )(
  807. NAME *table,
  808. size_t home_bucket,
  809. size_t *empty,
  810. uint16_t *displacement
  811. )
  812. {
  813. *displacement = 1;
  814. size_t linear_dispacement = 1;
  815. while( true )
  816. {
  817. *empty = ( home_bucket + linear_dispacement ) & table->buckets_mask;
  818. if( table->metadata[ *empty ] == VT_EMPTY )
  819. return true;
  820. if( VT_UNLIKELY( ++*displacement == VT_DISPLACEMENT_MASK ) )
  821. return false;
  822. linear_dispacement += *displacement;
  823. }
  824. }
  825. // Finds the key in the chain beginning in home_bucket after which to link a new key with displacement_to_empty
  826. // quadratic displacement and returns the index of the bucket containing that key.
  827. // Although the new key could simply be linked to the end of the chain, keeping the chain ordered by displacement
  828. // theoretically improves cache locality during lookups.
  829. static inline size_t VT_CAT( NAME, _find_insert_location_in_chain )(
  830. NAME *table,
  831. size_t home_bucket,
  832. uint16_t displacement_to_empty
  833. )
  834. {
  835. size_t candidate = home_bucket;
  836. while( true )
  837. {
  838. uint16_t displacement = table->metadata[ candidate ] & VT_DISPLACEMENT_MASK;
  839. if( displacement > displacement_to_empty )
  840. return candidate;
  841. candidate = ( home_bucket + vt_quadratic( displacement ) ) & table->buckets_mask;
  842. }
  843. }
  844. // Frees up a bucket occupied by a key not belonging there so that a new key belonging there can be placed there as the
  845. // beginning of a new chain.
  846. // This requires:
  847. // * Finding the previous key in the chain to which the occupying key belongs by rehashing it and then traversing the
  848. // chain.
  849. // * Disconnecting the key from the chain.
  850. // * Finding the appropriate empty bucket to which to move the key.
  851. // * Moving the key (and value) data to the empty bucket.
  852. // * Re-linking the key to the chain.
  853. // Returns true if the eviction succeeded, or false if no empty bucket to which to evict the occupying key could be
  854. // found within the displacement limit.
  855. static inline bool VT_CAT( NAME, _evict )( NAME *table, size_t bucket )
  856. {
  857. // Find the previous key in chain.
  858. size_t home_bucket = HASH_FN( table->buckets[ bucket ].key ) & table->buckets_mask;
  859. size_t prev = home_bucket;
  860. while( true )
  861. {
  862. size_t next = ( home_bucket + vt_quadratic( table->metadata[ prev ] & VT_DISPLACEMENT_MASK ) ) &
  863. table->buckets_mask;
  864. if( next == bucket )
  865. break;
  866. prev = next;
  867. }
  868. // Disconnect the key from chain.
  869. table->metadata[ prev ] = ( table->metadata[ prev ] & ~VT_DISPLACEMENT_MASK ) | ( table->metadata[ bucket ] &
  870. VT_DISPLACEMENT_MASK );
  871. // Find the empty bucket to which to move the key.
  872. size_t empty;
  873. uint16_t displacement;
  874. if( VT_UNLIKELY( !VT_CAT( NAME, _find_first_empty )( table, home_bucket, &empty, &displacement ) ) )
  875. return false;
  876. // Find the key in the chain after which to link the moved key.
  877. prev = VT_CAT( NAME, _find_insert_location_in_chain )( table, home_bucket, displacement );
  878. // Move the key (and value) data.
  879. table->buckets[ empty ] = table->buckets[ bucket ];
  880. // Re-link the key to the chain from its new bucket.
  881. table->metadata[ empty ] = ( table->metadata[ bucket ] & VT_HASH_FRAG_MASK ) | ( table->metadata[ prev ] &
  882. VT_DISPLACEMENT_MASK );
  883. table->metadata[ prev ] = ( table->metadata[ prev ] & ~VT_DISPLACEMENT_MASK ) | displacement;
  884. return true;
  885. }
  886. // Returns an end iterator, i.e. any iterator for which .metadatum == .metadata_end.
  887. // This function just cleans up the library code in functions that return an end iterator as a failure indicator.
  888. static inline VT_CAT( NAME, _itr ) VT_CAT( NAME, _end_itr )( void )
  889. {
  890. VT_CAT( NAME, _itr ) itr = { NULL, NULL, NULL, 0 };
  891. return itr;
  892. }
  893. // Inserts a key, optionally replacing the existing key if it already exists.
  894. // There are two main cases that must be handled:
  895. // * If the key's home bucket is empty or occupied by a key that does not belong there, then the key is inserted there,
  896. // evicting the occupying key if there is one.
  897. // * Otherwise, the chain of keys beginning at the home bucket is (if unique is false) traversed in search of a matching
  898. // key.
  899. // If none is found, then the new key is inserted at the earliest available bucket, per quadratic probing from the
  900. // home bucket, and then linked to the chain in a manner that maintains its quadratic order.
  901. // The unique argument tells the function whether to skip searching for the key before inserting it (on rehashing, this
  902. // step is unnecessary).
  903. // The replace argument tells the function whether to replace an existing key.
  904. // If replace is true, the function returns an iterator to the inserted key, or an end iterator if the key was not
  905. // inserted because of the maximum load factor or displacement limit constraints.
  906. // If replace is false, then the return value is as described above, except that if the key already exists, the function
  907. // returns an iterator to the existing key.
  908. static inline VT_CAT( NAME, _itr ) VT_CAT( NAME, _insert_raw )(
  909. NAME *table,
  910. KEY_TY key,
  911. #ifdef VAL_TY
  912. VAL_TY *val,
  913. #endif
  914. bool unique,
  915. bool replace
  916. )
  917. {
  918. uint64_t hash = HASH_FN( key );
  919. uint16_t hashfrag = vt_hashfrag( hash );
  920. size_t home_bucket = hash & table->buckets_mask;
  921. // Case 1: The home bucket is empty or contains a key that doesn't belong there.
  922. // This case also implicitly handles the case of a zero bucket count, since home_bucket will be zero and metadata[ 0 ]
  923. // will be the empty placeholder.
  924. // In that scenario, the zero buckets_mask triggers the below load-factor check.
  925. if( !( table->metadata[ home_bucket ] & VT_IN_HOME_BUCKET_MASK ) )
  926. {
  927. if(
  928. // Load-factor check.
  929. VT_UNLIKELY( table->key_count + 1 > VT_CAT( NAME, _bucket_count )( table ) * MAX_LOAD ) ||
  930. // Vacate the home bucket if it contains a key.
  931. ( table->metadata[ home_bucket ] != VT_EMPTY && VT_UNLIKELY( !VT_CAT( NAME, _evict )( table, home_bucket ) ) )
  932. )
  933. return VT_CAT( NAME, _end_itr )();
  934. table->buckets[ home_bucket ].key = key;
  935. #ifdef VAL_TY
  936. table->buckets[ home_bucket ].val = *val;
  937. #endif
  938. table->metadata[ home_bucket ] = hashfrag | VT_IN_HOME_BUCKET_MASK | VT_DISPLACEMENT_MASK;
  939. ++table->key_count;
  940. VT_CAT( NAME, _itr ) itr = {
  941. table->buckets + home_bucket,
  942. table->metadata + home_bucket,
  943. table->metadata + table->buckets_mask + 1, // Iteration stopper (i.e. the first of the four excess metadata).
  944. home_bucket
  945. };
  946. return itr;
  947. }
  948. // Case 2: The home bucket contains the beginning of a chain.
  949. // Optionally, check the existing chain.
  950. if( !unique )
  951. {
  952. size_t bucket = home_bucket;
  953. while( true )
  954. {
  955. if(
  956. ( table->metadata[ bucket ] & VT_HASH_FRAG_MASK ) == hashfrag &&
  957. VT_LIKELY( CMPR_FN( table->buckets[ bucket ].key, key ) )
  958. )
  959. {
  960. if( replace )
  961. {
  962. #ifdef KEY_DTOR_FN
  963. KEY_DTOR_FN( table->buckets[ bucket ].key );
  964. #endif
  965. table->buckets[ bucket ].key = key;
  966. #ifdef VAL_TY
  967. #ifdef VAL_DTOR_FN
  968. VAL_DTOR_FN( table->buckets[ bucket ].val );
  969. #endif
  970. table->buckets[ bucket ].val = *val;
  971. #endif
  972. }
  973. VT_CAT( NAME, _itr ) itr = {
  974. table->buckets + bucket,
  975. table->metadata + bucket,
  976. table->metadata + table->buckets_mask + 1,
  977. home_bucket
  978. };
  979. return itr;
  980. }
  981. uint16_t displacement = table->metadata[ bucket ] & VT_DISPLACEMENT_MASK;
  982. if( displacement == VT_DISPLACEMENT_MASK )
  983. break;
  984. bucket = ( home_bucket + vt_quadratic( displacement ) ) & table->buckets_mask;
  985. }
  986. }
  987. size_t empty;
  988. uint16_t displacement;
  989. if(
  990. VT_UNLIKELY(
  991. // Load-factor check.
  992. table->key_count + 1 > VT_CAT( NAME, _bucket_count )( table ) * MAX_LOAD ||
  993. // Find the earliest empty bucket, per quadratic probing.
  994. !VT_CAT( NAME, _find_first_empty )( table, home_bucket, &empty, &displacement )
  995. )
  996. )
  997. return VT_CAT( NAME, _end_itr )();
  998. // Insert the new key (and value) in the empty bucket and link it to the chain.
  999. size_t prev = VT_CAT( NAME, _find_insert_location_in_chain )( table, home_bucket, displacement );
  1000. table->buckets[ empty ].key = key;
  1001. #ifdef VAL_TY
  1002. table->buckets[ empty ].val = *val;
  1003. #endif
  1004. table->metadata[ empty ] = hashfrag | ( table->metadata[ prev ] & VT_DISPLACEMENT_MASK );
  1005. table->metadata[ prev ] = ( table->metadata[ prev ] & ~VT_DISPLACEMENT_MASK ) | displacement;
  1006. ++table->key_count;
  1007. VT_CAT( NAME, _itr ) itr = {
  1008. table->buckets + empty,
  1009. table->metadata + empty,
  1010. table->metadata + table->buckets_mask + 1,
  1011. home_bucket
  1012. };
  1013. return itr;
  1014. }
  1015. // Resizes the bucket array.
  1016. // This function assumes that bucket_count is a power of two and large enough to accommodate all keys without violating
  1017. // the maximum load factor.
  1018. // Returns false in the case of allocation failure.
  1019. // As this function is called very rarely in _insert and _get_or_insert, ideally it should not be inlined into those
  1020. // functions.
  1021. // In testing, the no-inline approach showed a performance benefit when inserting existing keys (i.e. replacing).
  1022. #ifdef __GNUC__
  1023. #pragma GCC diagnostic push
  1024. #pragma GCC diagnostic ignored "-Wattributes" // Silence warning about combining noinline with static inline.
  1025. __attribute__((noinline)) static inline
  1026. #elif defined( _MSC_VER )
  1027. __declspec(noinline) static inline
  1028. #else
  1029. static inline
  1030. #endif
  1031. bool VT_CAT( NAME, _rehash )( NAME *table, size_t bucket_count )
  1032. {
  1033. // The attempt to resize the bucket array and rehash the keys must occur inside a loop that incrementally doubles the
  1034. // target bucket count because a failure could theoretically occur at any load factor due to the displacement limit.
  1035. while( true )
  1036. {
  1037. NAME new_table = {
  1038. 0,
  1039. bucket_count - 1,
  1040. NULL,
  1041. NULL
  1042. #ifdef CTX_TY
  1043. , table->ctx
  1044. #endif
  1045. };
  1046. void *allocation = MALLOC_FN(
  1047. VT_CAT( NAME, _total_alloc_size )( &new_table )
  1048. #ifdef CTX_TY
  1049. , &new_table.ctx
  1050. #endif
  1051. );
  1052. if( VT_UNLIKELY( !allocation ) )
  1053. return false;
  1054. new_table.buckets = (VT_CAT( NAME, _bucket ) *)allocation;
  1055. new_table.metadata = (uint16_t *)( (unsigned char *)allocation + VT_CAT( NAME, _metadata_offset )( &new_table ) );
  1056. memset( new_table.metadata, 0x00, ( bucket_count + 4 ) * sizeof( uint16_t ) );
  1057. // Iteration stopper at the end of the actual metadata array (i.e. the first of the four excess metadata).
  1058. new_table.metadata[ bucket_count ] = 0x01;
  1059. for( size_t bucket = 0; bucket < VT_CAT( NAME, _bucket_count )( table ); ++bucket )
  1060. if( table->metadata[ bucket ] != VT_EMPTY )
  1061. {
  1062. VT_CAT( NAME, _itr ) itr = VT_CAT( NAME, _insert_raw )(
  1063. &new_table,
  1064. table->buckets[ bucket ].key,
  1065. #ifdef VAL_TY
  1066. &table->buckets[ bucket ].val,
  1067. #endif
  1068. true,
  1069. false
  1070. );
  1071. if( VT_UNLIKELY( VT_CAT( NAME, _is_end )( itr ) ) )
  1072. break;
  1073. }
  1074. // If a key could not be reinserted due to the displacement limit, double the bucket count and retry.
  1075. if( VT_UNLIKELY( new_table.key_count < table->key_count ) )
  1076. {
  1077. FREE_FN(
  1078. new_table.buckets,
  1079. VT_CAT( NAME, _total_alloc_size )( &new_table )
  1080. #ifdef CTX_TY
  1081. , &new_table.ctx
  1082. #endif
  1083. );
  1084. bucket_count *= 2;
  1085. continue;
  1086. }
  1087. if( table->buckets_mask )
  1088. FREE_FN(
  1089. table->buckets,
  1090. VT_CAT( NAME, _total_alloc_size )( table )
  1091. #ifdef CTX_TY
  1092. , &table->ctx
  1093. #endif
  1094. );
  1095. *table = new_table;
  1096. return true;
  1097. }
  1098. }
  1099. #ifdef __GNUC__
  1100. #pragma GCC diagnostic pop
  1101. #endif
  1102. // Inserts a key, replacing the existing key if it already exists.
  1103. // This function wraps insert_raw in a loop that handles growing and rehashing the table if a new key cannot be inserted
  1104. // because of the maximum load factor or displacement limit constraints.
  1105. // Returns an iterator to the inserted key, or an end iterator in the case of allocation failure.
  1106. VT_API_FN_QUALIFIERS VT_CAT( NAME, _itr ) VT_CAT( NAME, _insert )(
  1107. NAME *table,
  1108. KEY_TY key
  1109. #ifdef VAL_TY
  1110. , VAL_TY val
  1111. #endif
  1112. )
  1113. {
  1114. while( true )
  1115. {
  1116. VT_CAT( NAME, _itr ) itr = VT_CAT( NAME, _insert_raw )(
  1117. table,
  1118. key,
  1119. #ifdef VAL_TY
  1120. &val,
  1121. #endif
  1122. false,
  1123. true
  1124. );
  1125. if(
  1126. // Lookup succeeded, in which case itr points to the found key.
  1127. VT_LIKELY( !VT_CAT( NAME, _is_end )( itr ) ) ||
  1128. // Lookup failed and rehash also fails, in which case itr is an end iterator.
  1129. VT_UNLIKELY(
  1130. !VT_CAT( NAME, _rehash )(
  1131. table, table->buckets_mask ? VT_CAT( NAME, _bucket_count )( table ) * 2 : VT_MIN_NONZERO_BUCKET_COUNT
  1132. )
  1133. )
  1134. )
  1135. return itr;
  1136. }
  1137. }
  1138. // Same as NAME_insert, except that if the key already exists, no insertion occurs and the function returns an iterator
  1139. // to the existing key.
  1140. VT_API_FN_QUALIFIERS VT_CAT( NAME, _itr ) VT_CAT( NAME, _get_or_insert )(
  1141. NAME *table,
  1142. KEY_TY key
  1143. #ifdef VAL_TY
  1144. , VAL_TY val
  1145. #endif
  1146. )
  1147. {
  1148. while( true )
  1149. {
  1150. VT_CAT( NAME, _itr ) itr = VT_CAT( NAME, _insert_raw )(
  1151. table,
  1152. key,
  1153. #ifdef VAL_TY
  1154. &val,
  1155. #endif
  1156. false,
  1157. false
  1158. );
  1159. if(
  1160. // Lookup succeeded, in which case itr points to the found key.
  1161. VT_LIKELY( !VT_CAT( NAME, _is_end )( itr ) ) ||
  1162. // Lookup failed and rehash also fails, in which case itr is an end iterator.
  1163. VT_UNLIKELY(
  1164. !VT_CAT( NAME, _rehash )(
  1165. table, table->buckets_mask ? VT_CAT( NAME, _bucket_count )( table ) * 2 : VT_MIN_NONZERO_BUCKET_COUNT
  1166. )
  1167. )
  1168. )
  1169. return itr;
  1170. }
  1171. }
  1172. // Returns an iterator pointing to the specified key, or an end iterator if the key does not exist.
  1173. VT_API_FN_QUALIFIERS VT_CAT( NAME, _itr ) VT_CAT( NAME, _get )( NAME *table, KEY_TY key )
  1174. {
  1175. uint64_t hash = HASH_FN( key );
  1176. size_t home_bucket = hash & table->buckets_mask;
  1177. // If the home bucket is empty or contains a key that does not belong there, then our key does not exist.
  1178. // This check also implicitly handles the case of a zero bucket count, since home_bucket will be zero and
  1179. // metadata[ 0 ] will be the empty placeholder.
  1180. if( !( table->metadata[ home_bucket ] & VT_IN_HOME_BUCKET_MASK ) )
  1181. return VT_CAT( NAME, _end_itr )();
  1182. // Traverse the chain of keys belonging to the home bucket.
  1183. uint16_t hashfrag = vt_hashfrag( hash );
  1184. size_t bucket = home_bucket;
  1185. while( true )
  1186. {
  1187. if(
  1188. ( table->metadata[ bucket ] & VT_HASH_FRAG_MASK ) == hashfrag &&
  1189. VT_LIKELY( CMPR_FN( table->buckets[ bucket ].key, key ) )
  1190. )
  1191. {
  1192. VT_CAT( NAME, _itr ) itr = {
  1193. table->buckets + bucket,
  1194. table->metadata + bucket,
  1195. table->metadata + table->buckets_mask + 1,
  1196. home_bucket
  1197. };
  1198. return itr;
  1199. }
  1200. uint16_t displacement = table->metadata[ bucket ] & VT_DISPLACEMENT_MASK;
  1201. if( displacement == VT_DISPLACEMENT_MASK )
  1202. return VT_CAT( NAME, _end_itr )();
  1203. bucket = ( home_bucket + vt_quadratic( displacement ) ) & table->buckets_mask;
  1204. }
  1205. }
  1206. // Erases the key pointed to by the specified iterator.
  1207. // The erasure always occurs at the end of the chain to which the key belongs.
  1208. // If the key to be erased is not the last in the chain, it is swapped with the last so that erasure occurs at the end.
  1209. // This helps keep a chain's keys close to their home bucket for the sake of cache locality.
  1210. // Returns true if, in the case of iteration from first to end, NAME_next should now be called on the iterator to find
  1211. // the next key.
  1212. // This return value is necessary because at the iterator location, the erasure could result in an empty bucket, a
  1213. // bucket containing a moved key already visited during the iteration, or a bucket containing a moved key not yet
  1214. // visited.
  1215. VT_API_FN_QUALIFIERS bool VT_CAT( NAME, _erase_itr_raw )( NAME *table, VT_CAT( NAME, _itr ) itr )
  1216. {
  1217. --table->key_count;
  1218. size_t itr_bucket = itr.metadatum - table->metadata;
  1219. // For now, we only call the value's destructor because the key may need to be hashed below to determine the home
  1220. // bucket.
  1221. #ifdef VAL_DTOR_FN
  1222. VAL_DTOR_FN( table->buckets[ itr_bucket ].val );
  1223. #endif
  1224. // Case 1: The key is the only one in its chain, so just remove it.
  1225. if(
  1226. table->metadata[ itr_bucket ] & VT_IN_HOME_BUCKET_MASK &&
  1227. ( table->metadata[ itr_bucket ] & VT_DISPLACEMENT_MASK ) == VT_DISPLACEMENT_MASK
  1228. )
  1229. {
  1230. #ifdef KEY_DTOR_FN
  1231. KEY_DTOR_FN( table->buckets[ itr_bucket ].key );
  1232. #endif
  1233. table->metadata[ itr_bucket ] = VT_EMPTY;
  1234. return true;
  1235. }
  1236. // Case 2 and 3 require that we know the key's home bucket, which the iterator may not have recorded.
  1237. if( itr.home_bucket == SIZE_MAX )
  1238. {
  1239. if( table->metadata[ itr_bucket ] & VT_IN_HOME_BUCKET_MASK )
  1240. itr.home_bucket = itr_bucket;
  1241. else
  1242. itr.home_bucket = HASH_FN( table->buckets[ itr_bucket ].key ) & table->buckets_mask;
  1243. }
  1244. // The key can now be safely destructed for cases 2 and 3.
  1245. #ifdef KEY_DTOR_FN
  1246. KEY_DTOR_FN( table->buckets[ itr_bucket ].key );
  1247. #endif
  1248. // Case 2: The key is the last in a multi-key chain.
  1249. // Traverse the chain from the beginning and find the penultimate key.
  1250. // Then disconnect the key and erase.
  1251. if( ( table->metadata[ itr_bucket ] & VT_DISPLACEMENT_MASK ) == VT_DISPLACEMENT_MASK )
  1252. {
  1253. size_t bucket = itr.home_bucket;
  1254. while( true )
  1255. {
  1256. uint16_t displacement = table->metadata[ bucket ] & VT_DISPLACEMENT_MASK;
  1257. size_t next = ( itr.home_bucket + vt_quadratic( displacement ) ) & table->buckets_mask;
  1258. if( next == itr_bucket )
  1259. {
  1260. table->metadata[ bucket ] |= VT_DISPLACEMENT_MASK;
  1261. table->metadata[ itr_bucket ] = VT_EMPTY;
  1262. return true;
  1263. }
  1264. bucket = next;
  1265. }
  1266. }
  1267. // Case 3: The chain has multiple keys, and the key is not the last one.
  1268. // Traverse the chain from the key to be erased and find the last and penultimate keys.
  1269. // Disconnect the last key from the chain, and swap it with the key to erase.
  1270. size_t bucket = itr_bucket;
  1271. while( true )
  1272. {
  1273. size_t prev = bucket;
  1274. bucket = ( itr.home_bucket + vt_quadratic( table->metadata[ bucket ] & VT_DISPLACEMENT_MASK ) ) &
  1275. table->buckets_mask;
  1276. if( ( table->metadata[ bucket ] & VT_DISPLACEMENT_MASK ) == VT_DISPLACEMENT_MASK )
  1277. {
  1278. table->buckets[ itr_bucket ] = table->buckets[ bucket ];
  1279. table->metadata[ itr_bucket ] = ( table->metadata[ itr_bucket ] & ~VT_HASH_FRAG_MASK ) | (
  1280. table->metadata[ bucket ] & VT_HASH_FRAG_MASK );
  1281. table->metadata[ prev ] |= VT_DISPLACEMENT_MASK;
  1282. table->metadata[ bucket ] = VT_EMPTY;
  1283. // Whether the iterator should be advanced depends on whether the key moved to the iterator bucket came from
  1284. // before or after that bucket.
  1285. // In the former case, the iteration would already have hit the moved key, so the iterator should still be
  1286. // advanced.
  1287. if( bucket > itr_bucket )
  1288. return false;
  1289. return true;
  1290. }
  1291. }
  1292. }
  1293. // Erases the specified key, if it exists.
  1294. // Returns true if a key was erased.
  1295. VT_API_FN_QUALIFIERS bool VT_CAT( NAME, _erase )( NAME *table, KEY_TY key )
  1296. {
  1297. VT_CAT( NAME, _itr ) itr = VT_CAT( NAME, _get)( table, key );
  1298. if( VT_CAT( NAME, _is_end )( itr ) )
  1299. return false;
  1300. VT_CAT( NAME, _erase_itr_raw )( table, itr );
  1301. return true;
  1302. }
  1303. // Finds the first occupied bucket at or after the bucket pointed to by itr.
  1304. // This function scans four buckets at a time, ideally using intrinsics.
  1305. static inline void VT_CAT( NAME, _fast_forward )( VT_CAT( NAME, _itr ) *itr )
  1306. {
  1307. while( true )
  1308. {
  1309. uint64_t metadata;
  1310. memcpy( &metadata, itr->metadatum, sizeof( uint64_t ) );
  1311. if( metadata )
  1312. {
  1313. int offset = vt_first_nonzero_uint16( metadata );
  1314. itr->data += offset;
  1315. itr->metadatum += offset;
  1316. itr->home_bucket = SIZE_MAX;
  1317. return;
  1318. }
  1319. itr->data += 4;
  1320. itr->metadatum += 4;
  1321. }
  1322. }
  1323. VT_API_FN_QUALIFIERS VT_CAT( NAME, _itr ) VT_CAT( NAME, _next )( VT_CAT( NAME, _itr ) itr )
  1324. {
  1325. ++itr.data;
  1326. ++itr.metadatum;
  1327. VT_CAT( NAME, _fast_forward )( &itr );
  1328. return itr;
  1329. }
  1330. // Returns the minimum bucket count required to accommodate a certain number of keys, which is governed by the maximum
  1331. // load factor.
  1332. static inline size_t VT_CAT( NAME, _min_bucket_count_for_size )( size_t size )
  1333. {
  1334. if( size == 0 )
  1335. return 0;
  1336. // Round up to a power of two.
  1337. size_t bucket_count = VT_MIN_NONZERO_BUCKET_COUNT;
  1338. while( size > bucket_count * MAX_LOAD )
  1339. bucket_count *= 2;
  1340. return bucket_count;
  1341. }
  1342. VT_API_FN_QUALIFIERS bool VT_CAT( NAME, _reserve )( NAME *table, size_t size )
  1343. {
  1344. size_t bucket_count = VT_CAT( NAME, _min_bucket_count_for_size )( size );
  1345. if( bucket_count <= VT_CAT( NAME, _bucket_count )( table ) )
  1346. return true;
  1347. return VT_CAT( NAME, _rehash )( table, bucket_count );
  1348. }
  1349. VT_API_FN_QUALIFIERS bool VT_CAT( NAME, _shrink )( NAME *table )
  1350. {
  1351. size_t bucket_count = VT_CAT( NAME, _min_bucket_count_for_size )( table->key_count );
  1352. if( bucket_count == VT_CAT( NAME, _bucket_count )( table ) ) // Shrink unnecessary.
  1353. return true;
  1354. if( bucket_count == 0 )
  1355. {
  1356. FREE_FN(
  1357. table->buckets,
  1358. VT_CAT( NAME, _total_alloc_size )( table )
  1359. #ifdef CTX_TY
  1360. , &table->ctx
  1361. #endif
  1362. );
  1363. table->buckets_mask = 0x0000000000000000ull;
  1364. table->metadata = (uint16_t *)&vt_empty_placeholder_metadatum;
  1365. return true;
  1366. }
  1367. return VT_CAT( NAME, _rehash )( table, bucket_count );
  1368. }
  1369. VT_API_FN_QUALIFIERS VT_CAT( NAME, _itr ) VT_CAT( NAME, _first )( NAME *table )
  1370. {
  1371. if( !table->key_count )
  1372. return VT_CAT( NAME, _end_itr )();
  1373. VT_CAT( NAME, _itr ) itr = { table->buckets, table->metadata, table->metadata + table->buckets_mask + 1, SIZE_MAX };
  1374. VT_CAT( NAME, _fast_forward )( &itr );
  1375. return itr;
  1376. }
  1377. VT_API_FN_QUALIFIERS void VT_CAT( NAME, _clear )( NAME *table )
  1378. {
  1379. if( !table->key_count )
  1380. return;
  1381. for( size_t i = 0; i < VT_CAT( NAME, _bucket_count )( table ); ++i )
  1382. {
  1383. if( table->metadata[ i ] != VT_EMPTY )
  1384. {
  1385. #ifdef KEY_DTOR_FN
  1386. KEY_DTOR_FN( table->buckets[ i ].key );
  1387. #endif
  1388. #ifdef VAL_DTOR_FN
  1389. VAL_DTOR_FN( table->buckets[ i ].val );
  1390. #endif
  1391. }
  1392. table->metadata[ i ] = VT_EMPTY;
  1393. }
  1394. table->key_count = 0;
  1395. }
  1396. VT_API_FN_QUALIFIERS void VT_CAT( NAME, _cleanup )( NAME *table )
  1397. {
  1398. if( !table->buckets_mask )
  1399. return;
  1400. #if defined( KEY_DTOR_FN ) || defined( VAL_DTOR_FN )
  1401. VT_CAT( NAME, _clear )( table );
  1402. #endif
  1403. FREE_FN(
  1404. table->buckets,
  1405. VT_CAT( NAME, _total_alloc_size )( table )
  1406. #ifdef CTX_TY
  1407. , &table->ctx
  1408. #endif
  1409. );
  1410. VT_CAT( NAME, _init )(
  1411. table
  1412. #ifdef CTX_TY
  1413. , table->ctx
  1414. #endif
  1415. );
  1416. }
  1417. #endif
  1418. /*--------------------------------------------------------------------------------------------------------------------*/
  1419. /* Wrapper types and functions for the C11 generic API */
  1420. /*--------------------------------------------------------------------------------------------------------------------*/
  1421. #if defined(__STDC_VERSION__) && \
  1422. __STDC_VERSION__ >= 201112L && \
  1423. !defined( IMPLEMENTATION_MODE ) && \
  1424. !defined( VT_NO_C11_GENERIC_API ) \
  1425. typedef NAME VT_CAT( vt_table_, VT_TEMPLATE_COUNT );
  1426. typedef VT_CAT( NAME, _itr ) VT_CAT( vt_table_itr_, VT_TEMPLATE_COUNT );
  1427. static inline void VT_CAT( vt_init_, VT_TEMPLATE_COUNT )(
  1428. NAME *table
  1429. #ifdef CTX_TY
  1430. , CTX_TY ctx
  1431. #endif
  1432. )
  1433. {
  1434. VT_CAT( NAME, _init )(
  1435. table
  1436. #ifdef CTX_TY
  1437. , ctx
  1438. #endif
  1439. );
  1440. }
  1441. static inline bool VT_CAT( vt_init_clone_, VT_TEMPLATE_COUNT )(
  1442. NAME *table,
  1443. NAME* source
  1444. #ifdef CTX_TY
  1445. , CTX_TY ctx
  1446. #endif
  1447. )
  1448. {
  1449. return VT_CAT( NAME, _init_clone )(
  1450. table,
  1451. source
  1452. #ifdef CTX_TY
  1453. , ctx
  1454. #endif
  1455. );
  1456. }
  1457. static inline size_t VT_CAT( vt_size_, VT_TEMPLATE_COUNT )( const NAME *table )
  1458. {
  1459. return VT_CAT( NAME, _size )( table );
  1460. }
  1461. static inline size_t VT_CAT( vt_bucket_count_, VT_TEMPLATE_COUNT )( const NAME *table )
  1462. {
  1463. return VT_CAT( NAME, _bucket_count )( table );
  1464. }
  1465. static inline bool VT_CAT( vt_is_end_, VT_TEMPLATE_COUNT )( VT_CAT( NAME, _itr ) itr )
  1466. {
  1467. return VT_CAT( NAME, _is_end )( itr );
  1468. }
  1469. static inline VT_CAT( NAME, _itr ) VT_CAT( vt_insert_, VT_TEMPLATE_COUNT )(
  1470. NAME *table,
  1471. KEY_TY key
  1472. #ifdef VAL_TY
  1473. , VAL_TY val
  1474. #endif
  1475. )
  1476. {
  1477. return VT_CAT( NAME, _insert )(
  1478. table,
  1479. key
  1480. #ifdef VAL_TY
  1481. , val
  1482. #endif
  1483. );
  1484. }
  1485. static inline VT_CAT( NAME, _itr ) VT_CAT( vt_get_or_insert_, VT_TEMPLATE_COUNT )(
  1486. NAME *table,
  1487. KEY_TY key
  1488. #ifdef VAL_TY
  1489. , VAL_TY val
  1490. #endif
  1491. )
  1492. {
  1493. return VT_CAT( NAME, _get_or_insert )(
  1494. table,
  1495. key
  1496. #ifdef VAL_TY
  1497. , val
  1498. #endif
  1499. );
  1500. }
  1501. static inline VT_CAT( NAME, _itr ) VT_CAT( vt_get_, VT_TEMPLATE_COUNT )( NAME *table, KEY_TY key )
  1502. {
  1503. return VT_CAT( NAME, _get )( table, key );
  1504. }
  1505. static inline bool VT_CAT( vt_erase_, VT_TEMPLATE_COUNT )( NAME *table, KEY_TY key )
  1506. {
  1507. return VT_CAT( NAME, _erase )( table, key );
  1508. }
  1509. static inline VT_CAT( NAME, _itr ) VT_CAT( vt_next_, VT_TEMPLATE_COUNT )( VT_CAT( NAME, _itr ) itr )
  1510. {
  1511. return VT_CAT( NAME, _next )( itr );
  1512. }
  1513. static inline VT_CAT( NAME, _itr ) VT_CAT( vt_erase_itr_, VT_TEMPLATE_COUNT )( NAME *table, VT_CAT( NAME, _itr ) itr )
  1514. {
  1515. return VT_CAT( NAME, _erase_itr )( table, itr );
  1516. }
  1517. static inline bool VT_CAT( vt_reserve_, VT_TEMPLATE_COUNT )( NAME *table, size_t bucket_count )
  1518. {
  1519. return VT_CAT( NAME, _reserve )( table, bucket_count );
  1520. }
  1521. static inline bool VT_CAT( vt_shrink_, VT_TEMPLATE_COUNT )( NAME *table )
  1522. {
  1523. return VT_CAT( NAME, _shrink )( table );
  1524. }
  1525. static inline VT_CAT( NAME, _itr ) VT_CAT( vt_first_, VT_TEMPLATE_COUNT )( NAME *table )
  1526. {
  1527. return VT_CAT( NAME, _first )( table );
  1528. }
  1529. static inline void VT_CAT( vt_clear_, VT_TEMPLATE_COUNT )( NAME *table )
  1530. {
  1531. VT_CAT( NAME, _clear )( table );
  1532. }
  1533. static inline void VT_CAT( vt_cleanup_, VT_TEMPLATE_COUNT )( NAME *table )
  1534. {
  1535. VT_CAT( NAME, _cleanup )( table );
  1536. }
  1537. // Increment the template counter.
  1538. #if VT_TEMPLATE_COUNT_D1 == 0
  1539. #undef VT_TEMPLATE_COUNT_D1
  1540. #define VT_TEMPLATE_COUNT_D1 1
  1541. #elif VT_TEMPLATE_COUNT_D1 == 1
  1542. #undef VT_TEMPLATE_COUNT_D1
  1543. #define VT_TEMPLATE_COUNT_D1 2
  1544. #elif VT_TEMPLATE_COUNT_D1 == 2
  1545. #undef VT_TEMPLATE_COUNT_D1
  1546. #define VT_TEMPLATE_COUNT_D1 3
  1547. #elif VT_TEMPLATE_COUNT_D1 == 3
  1548. #undef VT_TEMPLATE_COUNT_D1
  1549. #define VT_TEMPLATE_COUNT_D1 4
  1550. #elif VT_TEMPLATE_COUNT_D1 == 4
  1551. #undef VT_TEMPLATE_COUNT_D1
  1552. #define VT_TEMPLATE_COUNT_D1 5
  1553. #elif VT_TEMPLATE_COUNT_D1 == 5
  1554. #undef VT_TEMPLATE_COUNT_D1
  1555. #define VT_TEMPLATE_COUNT_D1 6
  1556. #elif VT_TEMPLATE_COUNT_D1 == 6
  1557. #undef VT_TEMPLATE_COUNT_D1
  1558. #define VT_TEMPLATE_COUNT_D1 7
  1559. #elif VT_TEMPLATE_COUNT_D1 == 7
  1560. #undef VT_TEMPLATE_COUNT_D1
  1561. #define VT_TEMPLATE_COUNT_D1 0
  1562. #if VT_TEMPLATE_COUNT_D2 == 0
  1563. #undef VT_TEMPLATE_COUNT_D2
  1564. #define VT_TEMPLATE_COUNT_D2 1
  1565. #elif VT_TEMPLATE_COUNT_D2 == 1
  1566. #undef VT_TEMPLATE_COUNT_D2
  1567. #define VT_TEMPLATE_COUNT_D2 2
  1568. #elif VT_TEMPLATE_COUNT_D2 == 2
  1569. #undef VT_TEMPLATE_COUNT_D2
  1570. #define VT_TEMPLATE_COUNT_D2 3
  1571. #elif VT_TEMPLATE_COUNT_D2 == 3
  1572. #undef VT_TEMPLATE_COUNT_D2
  1573. #define VT_TEMPLATE_COUNT_D2 4
  1574. #elif VT_TEMPLATE_COUNT_D2 == 4
  1575. #undef VT_TEMPLATE_COUNT_D2
  1576. #define VT_TEMPLATE_COUNT_D2 5
  1577. #elif VT_TEMPLATE_COUNT_D2 == 5
  1578. #undef VT_TEMPLATE_COUNT_D2
  1579. #define VT_TEMPLATE_COUNT_D2 6
  1580. #elif VT_TEMPLATE_COUNT_D2 == 6
  1581. #undef VT_TEMPLATE_COUNT_D2
  1582. #define VT_TEMPLATE_COUNT_D2 7
  1583. #elif VT_TEMPLATE_COUNT_D2 == 7
  1584. #undef VT_TEMPLATE_COUNT_D2
  1585. #define VT_TEMPLATE_COUNT_D2 0
  1586. #if VT_TEMPLATE_COUNT_D3 == 0
  1587. #undef VT_TEMPLATE_COUNT_D3
  1588. #define VT_TEMPLATE_COUNT_D3 1
  1589. #elif VT_TEMPLATE_COUNT_D3 == 1
  1590. #undef VT_TEMPLATE_COUNT_D3
  1591. #define VT_TEMPLATE_COUNT_D3 2
  1592. #elif VT_TEMPLATE_COUNT_D3 == 2
  1593. #undef VT_TEMPLATE_COUNT_D3
  1594. #define VT_TEMPLATE_COUNT_D3 3
  1595. #elif VT_TEMPLATE_COUNT_D3 == 3
  1596. #undef VT_TEMPLATE_COUNT_D3
  1597. #define VT_TEMPLATE_COUNT_D3 4
  1598. #elif VT_TEMPLATE_COUNT_D3 == 4
  1599. #undef VT_TEMPLATE_COUNT_D3
  1600. #define VT_TEMPLATE_COUNT_D3 5
  1601. #elif VT_TEMPLATE_COUNT_D3 == 5
  1602. #undef VT_TEMPLATE_COUNT_D3
  1603. #define VT_TEMPLATE_COUNT_D3 6
  1604. #elif VT_TEMPLATE_COUNT_D3 == 6
  1605. #undef VT_TEMPLATE_COUNT_D3
  1606. #define VT_TEMPLATE_COUNT_D3 7
  1607. #elif VT_TEMPLATE_COUNT_D3 == 7
  1608. #error Sorry, the number of template instances is limited to 511. Define VT_NO_C11_GENERIC_API globally and use the \
  1609. C99 prefixed function API to circumvent this restriction.
  1610. #endif
  1611. #endif
  1612. #endif
  1613. #endif
  1614. #undef NAME
  1615. #undef KEY_TY
  1616. #undef VAL_TY
  1617. #undef HASH_FN
  1618. #undef CMPR_FN
  1619. #undef MAX_LOAD
  1620. #undef KEY_DTOR_FN
  1621. #undef VAL_DTOR_FN
  1622. #undef CTX_TY
  1623. #undef MALLOC_FN
  1624. #undef FREE_FN
  1625. #undef HEADER_MODE
  1626. #undef IMPLEMENTATION_MODE
  1627. #undef VT_API_FN_QUALIFIERS