| 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
 | #include "lichen.h"
#include <stdio.h>
#include <string.h>
void li_xorshift32_seed(struct li_xorshift32_t *xor, uint32_t seed) {
  xor->state = seed ? seed : 1;
  xor->generator.rand = li_xorshift32;
  xor->generator.state = &(xor->state);
}
uint32_t li_xorshift32(void *s) {
  uint64_t x = *((uint64_t*)s);
  x ^= x >> 12;
  x ^= x << 25;
  x ^= x >> 27;
  x *= 0x2545F4914F6CDD1DULL;
  *((uint64_t*)s) = x;
  // return just top 32 bits
  return x >> 32;
}
li_llnode_t * li_alloc_llnode() {
  li_llnode_t *n = malloc(sizeof(li_llnode_t));
  if (n == NULL) {
    fprintf(stderr, "failed to allocate linked-list node\n");
    return NULL;
  } else {
    n->tag = 0;
    n->data = 0;
    n->next = NULL;
    return n;
  }
}
li_llnode_t * li_copy_llnode(li_llnode_t *n) {
  li_llnode_t *m = li_alloc_llnode();
  if (m == NULL) {
    return NULL;
  }
  memcpy(m, n, sizeof(li_llnode_t));
  return m;
}
void li_free_llnode(li_llnode_t *n, void (*free_data)(void*)) {
  if (n != NULL) {
    free_data(n->data);
    li_free_llnode(n->next, free_data);
  }
  free(n);
}
struct li_ll_t * li_alloc_ll() {
  struct li_ll_t *list = malloc(sizeof(struct li_ll_t));
  if (list == NULL) {
    fprintf(stderr, "failed to allocate linked list\n");
    return NULL;
  }
  list->tag = 0;
  list->count = 0;
  list->head = NULL;
  list->end = NULL;
  return list;
}
void li_free_ll(struct li_ll_t *list, void (*free_data)(void*)) {
  if (list != NULL) {
    li_free_llnode(list->head, free_data);
  }
  free(list);
}
int li_ll_append(struct li_ll_t *list, void *data) {
  li_llnode_t *n = li_alloc_llnode();
  if (n == NULL) {
    return 1;
  }
  n->data = data;
  if (list->end != NULL) {
    list->end->next = n;
    list->end = n;
  } else {
    list->head = n;
    list->end = n;
  }
  list->count += 1;
  return 0;
}
int li_ll_prepend(struct li_ll_t *list, void *data) {
  li_llnode_t *n = li_alloc_llnode();
  if (n == NULL) {
    return 1;
  }
  n->data = data;
  if (list->head != NULL) {
    n->next = list->head;
    list->head = n;
  } else {
    list->head = n;
    list->end = n;
  }
  list->count += 1;
  return 0;
}
int li_copy_list(struct li_ll_t **dst, struct li_ll_t *src, void *(*copy_data)(void*)) {
  *dst = li_alloc_ll();
  if (*dst == NULL) {
    return 1;
  }
  (*dst)->tag = src->tag;
  for (li_llnode_t *n = src->head; n != NULL; n = n->next) {
    void *dup = copy_data(n->data);
    if (dup == NULL) {
      return 1;
    }
    if (li_ll_append(*dst, dup) != 0) {
      return 1;
    }
  }
  return 0;
}
 |