#ifndef LICHEN_H #define LICHEN_H #include #include /* --===== random number generation =====-- */ typedef struct li_rand32_t { uint32_t (*rand)(void *); void *state; } li_rand32_t; struct li_xorshift32_t { li_rand32_t generator; uint64_t state; }; // initialize an xorshift32 struct with a specific seed void li_xorshift32_seed(struct li_xorshift32_t *xor, uint32_t seed); // get a pseudo-random number via the xorshift algorithm uint32_t li_xorshift32(void *s); /* --===== generic linked lists =====-- */ typedef struct li_llnode_t { int tag; void *data; struct li_llnode_t *next; } li_llnode_t; li_llnode_t * li_alloc_llnode(); li_llnode_t * li_copy_llnode(li_llnode_t *n); void li_free_llnode(li_llnode_t *n, void (*free_data)(void*)); typedef struct li_ll_t { int tag; size_t count; li_llnode_t *head; li_llnode_t *end; } li_ll_t; // allocate a linked list struct li_ll_t * li_alloc_ll(); // free a linked list void li_free_ll(struct li_ll_t *list, void (*free_data)(void*)); // prepend and append to a linked list int li_ll_append(struct li_ll_t *list, void *data); int li_ll_prepend(struct li_ll_t *list, void *data); // copy a linked list. !!! WARNING !!! if this function fails (returns nonzero) you MUST call li_free_ll on dst yourself! int li_copy_list(struct li_ll_t **dst, struct li_ll_t *src, void *(*copy_data)(void*)); // sort a list. compare_data should return a positive, negative or zero value of the same sign as (a-b) int li_sort_list(struct li_ll_t *list, int (*compare_data)(void *, void*)); #endif