File: stack.h

package info (click to toggle)
pev 0.81-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,016 kB
  • sloc: ansic: 20,531; xml: 558; makefile: 450; sh: 397; python: 40
file content (182 lines) | stat: -rw-r--r-- 5,353 bytes parent folder | download | duplicates (3)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
	stack.h - A simple stack implementation compatible with C99.

	The MIT License (MIT)

	Copyright (c) 2013, Jardel Weyrich <jweyrich at gmail dot com>

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#pragma once

#include <stdint.h>

#define STACK_PASTE_2_(_1,_2)		_1 ## _2
#define STACK_PASTE_2(_1,_2)		STACK_PASTE_2_(_1, _2)

#if !defined(STACK_PREFIX)
#	define STACK_PREFIX				PEV_
#endif
#if !defined(STACK_ELEMENT_TYPE)
#	define STACK_ELEMENT_TYPE		void *
#endif
#define STACK_TYPE 					STACK_PASTE_2(STACK_PREFIX, stack_t)
#define STACK_API(fnname)			STACK_PASTE_2(STACK_PREFIX, fnname)

// Use these macros! Don't call functions directly.
#define STACK_ALLOC(capacity)				STACK_API(stack_alloc)(capacity)
#define STACK_DEALLOC(stack_ptr)			STACK_API(stack_dealloc)(stack_ptr)
#define STACK_COUNT(stack_ptr)				STACK_API(stack_count)(stack_ptr)
#define STACK_GROW(stack_ptr, capacity)		STACK_API(stack_grow)(stack_ptr, capacity)
#define STACK_PUSH(stack_ptr, element)		STACK_API(stack_push)(stack_ptr, element)
#define STACK_POP(stack_ptr, element_ptr)	STACK_API(stack_pop)(stack_ptr, element_ptr)
#define STACK_PEEK(stack_ptr, element_ptr)	STACK_API(stack_peek)(stack_ptr, element_ptr)

typedef struct {
	uint16_t capacity;
	uint16_t used;
	STACK_ELEMENT_TYPE *elements;
} STACK_TYPE;

static STACK_TYPE *STACK_API(stack_alloc)(uint16_t capacity);
static void STACK_API(stack_dealloc)(STACK_TYPE *stack);
static uint16_t STACK_API(stack_count)(STACK_TYPE *stack);
static int STACK_API(stack_grow)(STACK_TYPE *stack, uint16_t capacity);
static int STACK_API(stack_push)(STACK_TYPE *stack, STACK_ELEMENT_TYPE element);
static int STACK_API(stack_pop)(STACK_TYPE *stack, STACK_ELEMENT_TYPE *element);
static int STACK_API(stack_peek)(STACK_TYPE *stack, STACK_ELEMENT_TYPE *element);

// ----------------------------------------------------------------------------

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

STACK_TYPE * STACK_API(stack_alloc)(uint16_t capacity) {
	STACK_TYPE *stack = malloc(sizeof(STACK_TYPE));
	if (stack == NULL) {
		fprintf(stderr, "stack: failed to allocate\n");
		return NULL;
	}

	memset(stack, 0, sizeof(*stack));

	if (capacity > 0) {
		int ret = STACK_API(stack_grow)(stack, capacity);
		if (ret < 0) {
			STACK_API(stack_dealloc)(stack);
			return NULL;
		}
	}

	return stack;
}

void STACK_API(stack_dealloc)(STACK_TYPE *stack) {
	assert(stack != NULL);

	if (stack == NULL) {
		fprintf(stderr, "stack: attempt to deallocate NULL stack\n");
		return;
	}

	if (stack->elements != NULL)
		free(stack->elements);

	//memset(stack, 0, sizeof(*stack));
	free(stack);
}

uint16_t STACK_API(stack_count)(STACK_TYPE *stack) {
	assert(stack != NULL);
	return stack->used;
}

int STACK_API(stack_grow)(STACK_TYPE *stack, uint16_t capacity) {
	assert(stack != NULL);
	assert(capacity > stack->capacity);

	if (capacity <= stack->capacity) {
		fprintf(stderr, "stack: capacity cannot be decreased\n");
		return -1;
	}

	const size_t element_size = sizeof(STACK_ELEMENT_TYPE);
	const size_t new_size = capacity * element_size;

	STACK_ELEMENT_TYPE *temp = realloc(stack->elements, new_size);
	if (temp == NULL) {
		fprintf(stderr, "stack: failed to allocate requested capacity\n");
		return -2;
	}

	stack->elements = temp;
	stack->capacity = capacity;

	return 0;
}

int STACK_API(stack_push)(STACK_TYPE *stack, STACK_ELEMENT_TYPE element) {
	assert(stack != NULL);

	// Stack is full?
	if (stack->used >= stack->capacity) {
		// TODO(jweyrich): We could call `stack_grow` instead of failing miserably. Make this behavior adjustable?
		fprintf(stderr, "stack: stack is full - failed to push\n");
		return -1;
	}

	stack->elements[stack->used++] = element;

	return 0;
}

int STACK_API(stack_pop)(STACK_TYPE *stack, STACK_ELEMENT_TYPE *element) {
	assert(stack != NULL);

	// Stack is empty?
	if (stack->used == 0) {
		fprintf(stderr, "stack: stack is empty - failed to pop\n");
		return -1;
	}

	if (element != NULL) {
		*element = stack->elements[--stack->used];
	}

	return 0;
}

int STACK_API(stack_peek)(STACK_TYPE *stack, STACK_ELEMENT_TYPE *element) {
	assert(stack != NULL);

	// Stack is empty?
	if (stack->used == 0) {
		fprintf(stderr, "stack: stack is empty - failed to peek\n");
		return -1;
	}

	if (element != NULL) {
		*element = stack->elements[stack->used - 1];
	}

	return 0;
}