#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"); }