blob: 3a8cce9df74853f65de3d042636a9df04f0e60c1 (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 | #ifndef LICHEN_H
#define LICHEN_H
#include <stdlib.h>
#include <stdint.h>
/* --===== 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
 |