forked from fantaisie-software/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
237 lines (212 loc) · 5.02 KB
/
stack.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* stack - a dynamically resizing stack
* Copyright (c) 2001 Michael B. Allen <mba2000 ioplex.com>
*
* The MIT License
*
* 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.
*/
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include "mba/msgno.h"
#include "mba/iterator.h"
#include "mba/allocator.h"
#include "mba/stack.h"
#define STACK_INIT_SIZE 0
int
stack_init(struct stack *s, unsigned int max_size, struct allocator *al)
{
if (s == NULL) {
PMNO(errno = EINVAL);
return -1;
}
memset(s, 0, sizeof *s);
s->max = max_size ? max_size : INT_MAX;
s->al = al;
return 0;
}
int
stack_deinit(struct stack *s, del_fn data_del, void *context)
{
int ret = 0;
if (s) {
if (data_del) {
while (s->sp > 0) {
ret += data_del(context, s->array[--(s->sp)]);
}
}
ret += allocator_free(s->al, s->array);
}
return ret ? -1 : 0;
}
struct stack *
stack_new(unsigned int max_size, struct allocator *al)
{
struct stack *s;
if ((s = allocator_alloc(al, sizeof *s, 0)) == NULL) {
PMNO(errno);
return NULL;
}
if (stack_init(s, max_size, al) == -1) {
PMNO(errno);
allocator_free(al, s);
return NULL;
}
return s;
}
int
stack_del(struct stack *s, del_fn data_del, void *context)
{
int ret = 0;
if (s) {
ret += stack_deinit(s, data_del, context);
ret += allocator_free(s->al, s);
}
return ret;
}
int
stack_clear(struct stack *s, del_fn data_del, void *context)
{
int ret = 0;
if (s && data_del) {
while (s->sp > 0) {
ret += data_del(s->array[--(s->sp)], context);
}
}
return ret ? -1 : 0;
}
void
stack_iterate(void *s, iter_t *iter)
{
if (s) {
iter->i1 = 0;
}
}
void *
stack_next(void *s, iter_t *iter)
{
if (s) {
struct stack *s0 = s;
if (iter->i1 < s0->sp) {
return s0->array[iter->i1++];
}
}
return NULL;
}
void *
stack_peek(struct stack *s)
{
if (s && s->sp > 0) {
return s->array[s->sp - 1];
}
return NULL;
}
void *
stack_get(struct stack *s, unsigned int idx)
{
if (s == NULL || s->sp == 0 || idx >= s->sp) {
PMNO(errno = ERANGE);
return NULL;
}
return s->array[idx];
}
int
stack_push(struct stack *s, void *data)
{
if (s == NULL) {
PMNF(errno = ERANGE, ": s=NULL");
return -1;
}
if (s->sp == s->size) {
void **new_array;
unsigned int new_size;
if (s->size == s->max) {
PMNF(errno = ERANGE, ": size=%u,max=%u", s->size, s->max);
return -1;
}
if (s->size * 2 > s->max) {
new_size = s->max;
} else if (s->size == 0) {
new_size = 32;
} else {
new_size = s->size * 2;
}
new_array = allocator_realloc(s->al, s->array, sizeof *s->array * new_size);
if (new_array == NULL) {
PMNF(errno, ": new_size=%u,new_array=NULL,sp=%u", new_size, s->sp);
return -1;
}
s->size = new_size;
s->array = new_array;
}
assert(s->sp <= s->size);
s->array[s->sp++] = data;
return 0;
}
void *
stack_pop(struct stack *s)
{
if (s == NULL || s->sp == 0) {
return NULL;
}
if (s->sp < s->size / 4 && s->size > 32) {
void **new_array;
unsigned int new_size = s->size / 2;
new_array = allocator_realloc(s->al, s->array, sizeof *s->array * new_size);
if (new_array == NULL) {
PMNF(errno, ": new_size=%u,new_array=NULL", new_size);
return NULL;
}
s->array = new_array;
s->size = new_size;
}
assert(s->sp > 0 && s->sp <= s->size);
return s->array[--(s->sp)];
}
int
stack_is_empty(const struct stack *s)
{
return s == NULL || s->sp == 0;
}
unsigned int
stack_size(const struct stack *s)
{
return s == NULL ? 0 : s->sp;
}
int
stack_clean(struct stack *s)
{
if (s && s->size > s->sp) {
void **new_array;
unsigned int new_size = s->sp;
int ret = s->size - new_size;
new_array = allocator_realloc(s->al, s->array, sizeof *s->array * new_size);
if (new_size && new_array == NULL) {
AMSG("");
return -1;
}
s->array = new_array;
s->size = new_size;
return ret;
}
return 0;
}