diff options
Diffstat (limited to 'example/macros.c')
-rw-r--r-- | example/macros.c | 110 |
1 files changed, 29 insertions, 81 deletions
diff --git a/example/macros.c b/example/macros.c index 169a2ca..0e836b0 100644 --- a/example/macros.c +++ b/example/macros.c @@ -1,96 +1,44 @@ #include "prb.h" +#include <stdio.h> -typedef struct { - i32 i32_element; - i16 i16_element; - u8 u8_element; -} type_t; +struct type { + i32 i32_element; + i16 i16_element; + u8 u8_element; +}; -#define MAX_NODES 10 +#define nodes_count 10 -typedef struct stack_node_t { - i32 value; - struct stack_node_t *next; -} stack_node_t; +struct stack_node { + i32 value; + struct stack_node *next; +}; -typedef struct { - stack_node_t *first; - stack_node_t *last; -} stack_t; +struct stack { + struct stack_node *first; + struct stack_node *last; +}; -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; +static void print_stack(struct stack s) +{ + for (struct stack_node *node = s.first; node; node = node->next) + printf("%d%s", node->value, ((node->next) ? " -> " : "\n")); +} i32 main(void) { - u64 offset = offsetof(type_t, u8_element); + u64 offset = offsetof(struct type, 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; + info("offsetof(type_t, u8_element) = %lu", offset); + info("stack:"); + struct stack_node nodes[nodes_count]; + struct stack stack = {0}; + for (i32 i = 0; i < array_count(nodes); ++i) { + struct stack_node *node = 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")); - + print_stack(stack); 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"); + print_stack(stack); } |