Skip to content

agvxov/haste-containers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Haste containers

Family of generic and typesafe C23 containers.

Note

Out of convenience, we compile under C++ too.

Syntax

Every container is a template type like macro, with an interface resembling the following:

example_t(int) e;               // declare
ex_init(e);                     // initialize
ex_dothing(e);                  // perform action
ex_A(e, 1) = 1;                 // perform unsafe access (may over index)
ex_a(e, 1) = 1;                 // perform safe access (may trigger resize)
ex_foreach(auto i, e) { ; }     // iterate
/* dynamic container only */
ex_size(e);         // get number of elements
ex_add(e, 2)        // append element
ex_resize(e, 10);   // pre-allocate for elements
ex_delete(e);       // release memory

Concrete example:

hmap(char*, int) my_hashmap;
hm_init(my_hashmap);

hm_a(my_hashmap, "HW") = 1;     // OK

hm_a(my_hashmap, "HW") = "one"; // NOT OK
/* error: assignment to ‘int’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
 *  8 |     hm_a(my_hashmap, "HW") = "one";
 *    |                            ^
 */

Tip

Consult the headers and tests for more examples.

Usecase

Our datastructures have been optimized for:

  • programmer convenience
  • speed on a single thread

They were not optimized for:

  • space
  • multihreading
  • lifetime ownership

Contents

lib Description
hvec Simple and fast vector. It never shrinks through its life-time.
hlist Linked list which uses hvec as its underlying storage. Removal only unlinks, it does not free any memory. Only destroying the list releases resources.
hstack Trivial static stack. More readable then doing it by hand and prevents dumb off-by-one-errors.
hringbuffer.h Trivial static ringbuffer.
hmap.h Reasonably fast hash table.

Notes

Everything that is reasonable to be (or must be) is a macro.

This project was inspired by klib and the journalism of Daniel Hooper.