aboutsummaryrefslogtreecommitdiff
path: root/src/stack-array.c
blob: ca68a03e608be99cec78a50dad56a61ebe2102a5 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "util.h"

/* --8<-- stack2_type */
typedef int T;

struct stack {
	T     *array;
	size_t sz, p;
};
/* -->8-- */

/* --8<-- stack2_init */
void
stack_init(struct stack *stack)
{
	stack->array = NULL;
	stack->sz    = 0;
	stack->p     = 0;
}
/* -->8-- */

/* --8<-- stack2_grow_if_needed */
static bool
stack_grow_if_needed(struct stack *stack)
{
	if ( stack->p == stack->sz ) {
		if ( stack->array == NULL ) {
			static const size_t initial_size = 10;

			if ( (stack->array = reallocarray(NULL, initial_size, sizeof stack->array[0])) != NULL ) {
				stack->sz = initial_size;
				stack->p  = 0;
			}
			else {
				ERROR("out of memory");
				return false;
			}
		}
		else {
			const size_t new_size = stack->sz * 3 / 2;

			T *new_array;

			if ( (new_array = reallocarray(stack->array, new_size, sizeof stack->array[0])) != NULL ) {
				stack->array = new_array;
				stack->sz    = new_size;
			}
			else {
				ERROR("out of memory");
				return false;
			}
		}
	}
	return true;
}
/* -->8-- */

/* --8<-- stack2_push */
bool
stack_push(struct stack *stack, T data)
{
	bool success = stack_grow_if_needed(stack);

	if ( success ) {
		stack->array[stack->p++] = data;
	}

	return success;
}
/* -->8-- */

/* --8<-- stack2_pop */
bool
stack_pop(struct stack *stack, T *data)
{
	if ( stack->p != 0 ) {
		--stack->p;

		if ( data ) {
			*data = stack->array[stack->p];
		}
		return true;
	}
	else
		return false;
}
/* -->8-- */

/* --8<-- stack2_empty */
bool
stack_empty(struct stack *stack)
{
	return stack->p == 0;
}
/* -->8-- */

/* --8<-- stack2_free */
void
stack_free(struct stack *stack)
{
	free(stack->array);
}
/* -->8-- */

void
test_stack(void)
{
	struct stack stack[1];

	stack_init(stack);

	assert(stack_empty(stack) == true);

	for ( int i = 0; i != 5; ++i )
		stack_push(stack, i);

	assert(stack_empty(stack) == false);

	T i;
	assert(stack_pop(stack, &i) == true);
	assert(i == 4);
	assert(stack_pop(stack, &i) == true);
	assert(i == 3);
	assert(stack_pop(stack, &i) == true);
	assert(i == 2);
	assert(stack_pop(stack, &i) == true);
	assert(i == 1);
	assert(stack_pop(stack, &i) == true);
	assert(i == 0);

	assert(stack_pop(stack, &i) == false);

	assert(stack_empty(stack) == true);

	assert(stack[0].sz == 10);
	assert(stack[0].p == 0);

	for ( int i = 0; i != 23; ++i )
		stack_push(stack, i);

	assert(stack[0].sz == 33);
	assert(stack[0].p == 23);

	stack_free(stack);
}

int
main(void)
{
	test_stack();
	return EXIT_SUCCESS;
}