
On each shput and shdel, whereas sh_new_arena bundles multiple allocations Using sh_new_strdup is slower, because the strings must be allocated and freed Use sh_new_arena for hash tables if you never delete keys.
#Hashtab in c code
Or you can have string keys be stored in an arena private to this hash table:Įach of the above code fragments will print hi there.
You can have the internal copy of keys each be allocated separately with malloc: Provide two optional facilities for storing permanent strings. Work, since the string will be overwritten later. Using a string stored on the stack as in the earlier " jen" code will not usually Note that by default shput stores whatever char* pointer was passed in. Printf("%4d %s\n", hash.value, hash.key) Here's some sample code for creating and manipulating a hash table: hash tables) are created by defining a structure with a key field and a value field. The name 'arrpush' is an alias for 'arrput' for compatibility with older stb libraries. it is not possible to shrink the capacity (currently)
arrsetcap - sets the internal capacity. arrcap - returns the internal capacity, the maximum length the array can be without reallocating it. arrswapdel - deletes an item from the middle of the array, replacing it with the formerly last item. arrdel - deletes an item from the middle of the array, moving the following items over. arrins - inserts an item in the middle of the array.
arrlast - the last item in the array as an lvalue. arrsetlen - sets the length of the array (leaves new slots uninitialized). arraddn - adds n uninitialized values to the dynamic array. arrput - copy an object into the dynamic array. arrlenu - the length of the dynamic array as an unsigned type. arrlen - the length of the dynamic array. Here's some sample code for creating and manipulating a dynamic array:įor (int i=0 i, where the core object is stable even if the array elements Want to use something template-based like vector.)ĭynamic arrays are accessed using a pointer to the type of data you want in the array. Works in C++, although if you do not need C/C++ interoperation you'd probably Of two basic container types: dynamic arrays and hash tables. Stb_ds.h stb_ds.h stb_ds.h is a public-domain single-header-file C library