]> git.datanom.net - clib.git/blame - src/stack.c
init
[clib.git] / src / stack.c
CommitLineData
392d65ba
MR
1#include <stdlib.h>
2#include "stack.h"
3
4#define INIT_SIZE 100
5
6struct _Stack {
7 int size;
8 void** elems;
9 StackFreeFunc free;
10};
11
12static void free_elem(Stack* stack) {
13 if (stack->free) {
14 for (int i = 0; i < stack->size; i++)
15 stack->free(stack->elems[i]);
16 }
17 free(stack->elems);
18}
19
20static Stack* stack_new_common() {
21 Stack* s = malloc(sizeof(Stack));
22 s->elems = malloc(sizeof(void*) * INIT_SIZE);
23 s->size = 0;
24 s->free = NULL;
25 return s;
26}
27
28Stack* stack_new() {
29 return stack_new_common();
30}
31
32Stack* stack_new_full(StackFreeFunc free) {
33 Stack* s = stack_new_common();
34 s->free = free;
35 return s;
36}
37
38void stack_destroy(Stack* stack) {
39 free_elem(stack);
40 free(stack);
41}
42
43void* stack_pop(Stack* stack) {
44 if (stack_empty(stack))
45 return NULL;
46 stack->size -= 1;
47 void* elem = stack->elems[stack->size];
48 stack->elems[stack->size] = NULL;
49
50 return elem;
51}
52
53void stack_push(Stack* stack, void* elem) {
54 if (stack == NULL)
55 return;
56
57 stack->size += 1;
58 if (stack->size == INIT_SIZE) {
59 stack->elems = realloc(stack->elems, (INIT_SIZE + stack->size) * sizeof(void*));
60 }
61
62 stack->elems[stack->size - 1] = elem;
63}
64
65void* stack_peek(Stack* stack) {
66 if (stack_empty(stack))
67 return NULL;
68 return stack->elems[stack->size - 1];
69}
70
71bool stack_empty(Stack* stack) {
72 return (stack == NULL || stack->size == 0);
73}
74
75void stack_clear(Stack* stack) {
76 if (stack == NULL)
77 return;
78
79 free_elem(stack);
80 stack->size = 0;
81}
82
83int stack_size(Stack* stack) {
84 if (stack_empty(stack))
85 return 0;
86 return stack->size;
87}
88
89/*
90typedef struct _Stack {
91 void** elems;
92 int size;
93 StackFreeFunc free;
94} Stack;
95
96static void free_elem(Stack* stack) {
97 if (stack->free) {
98 for (int i = 0; i < stack->size; i++)
99 stack->free(stack->elems[i]);
100 }
101 free(stack->elems);
102}
103
104Stack* stack_new() {
105 Stack* s = malloc(sizeof(Stack));
106 s->size = 0;
107 s->elems = NULL;
108 s->free = NULL;
109 return s;
110}
111
112Stack* stack_new_full(StackFreeFunc free) {
113 Stack* s = malloc(sizeof(Stack));
114 s->size = 0;
115 s->elems = NULL;
116 s->free = free;
117 return s;
118}
119
120void stack_destroy(Stack* stack) {
121 free_elem(stack);
122 free(stack);
123}
124
125void* stack_pop(Stack* stack) {
126 if (stack_empty(stack))
127 return NULL;
128 stack->size -= 1;
129 void* elem = stack->elems[stack->size];
130 stack->elems[stack->size] = NULL;
131 stack->elems = realloc(stack->elems, stack->size * sizeof(void*));
132
133 return elem;
134}
135
136void stack_push(Stack* stack, void* elem) {
137 if (stack == NULL)
138 return;
139
140 stack->size += 1;
141 stack->elems = realloc(stack->elems, stack->size * sizeof(void*));
142 stack->elems[stack->size - 1] = elem;
143}
144
145void* stack_peek(Stack* stack) {
146 if (stack_empty(stack))
147 return NULL;
148 return stack->elems[stack->size - 1];
149}
150
151bool stack_empty(Stack* stack) {
152 return (stack == NULL || stack->size == 0);
153}
154
155void stack_clear(Stack* stack) {
156 if (stack == NULL)
157 return;
158
159 free_elem(stack);
160 stack->size = 0;
161 stack->elems = NULL;
162}
163
164int stack_size(Stack* stack) {
165 if (stack_empty(stack))
166 return 0;
167 return stack->size;
168}
169*/
This page took 0.081862 seconds and 5 git commands to generate.