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
|
#include "prb.h"
typedef struct {
i32 i32_element;
i16 i16_element;
u8 u8_element;
} type_t;
#define MAX_NODES 10
typedef struct stack_node_t {
i32 value;
struct stack_node_t *next;
} stack_node_t;
typedef struct {
stack_node_t *first;
stack_node_t *last;
} stack_t;
typedef struct node_t {
i32 value;
struct node_t *next;
struct node_t *prev;
} node_t;
typedef struct {
node_t *first;
node_t *last;
} list_t;
i32 main(void)
{
u64 offset = offsetof(type_t, u8_element);
assert(offset == 6);
printf("offsetof(type_t, u8_element) = %lu\n", offset);
i32 clamped = clamp(0, -1, 1);
assert(clamped == 0);
printf("clamp(0, -1, 1) = %d\n", clamped);
clamped = clamp(0, 2, 1);
assert(clamped == 1);
printf("clamp(0, 2, 1) = %d\n", clamped);
i32 nums[] = {0, 1, 2, 3};
printf("mem0: [");
for (i32 i = 0; i < array_count(nums); ++i)
printf("%x%s", nums[i], (i == array_count(nums)-1) ? "" : ", ");
memzero(nums, sizeof(nums));
printf("] -> [");
for (i32 i = 0; i < array_count(nums); ++i) {
assert(!nums[i]);
printf("%x%s", nums[i], (i == array_count(nums)-1) ? "" : ", ");
}
printf("]\n\n");
printf("Singly linked list (stack for example):\n");
stack_node_t stack_nodes[MAX_NODES];
stack_t stack = {0};
for (i32 i = 0; i < MAX_NODES; ++i) {
stack_node_t *node = stack_nodes+i;
node->value = i;
sllpush(stack.first, stack.last, node);
}
for (stack_node_t *node = stack.first; node; node = node->next)
printf("%d%s", node->value, ((node->next) ? " -> " : "\n"));
sllpop(stack.first, stack.last);
printf("After pop:\n");
for (stack_node_t *node = stack.first; node; node = node->next)
printf("%d%s", node->value, ((node->next) ? " -> " : "\n"));
node_t nodes[MAX_NODES];
list_t list = {0};
for (i32 i = 0; i < MAX_NODES; ++i) {
node_t *node = nodes+i;
node->value = i;
dllpushback(list.first, list.last, node);
}
printf("Doubly linked list:\n");
for (node_t *node = list.first; node; node = node->next)
printf("%d%s", node->value, ((node->next) ? " -> " : "\n"));
printf("Remove odd numbers:\n");
for (node_t *node = list.first; node; node = node->next)
if (node->value % 2 == 1)
dllremove(list.first, list.last, node);
for (node_t *node = list.first; node; node = node->next)
printf("%d%s", node->value, ((node->next) ? " -> " : "\n"));
printf("\n");
}
|