line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#include |
2
|
|
|
|
|
|
|
#include |
3
|
|
|
|
|
|
|
#include |
4
|
|
|
|
|
|
|
#include "b_stack.h" |
5
|
|
|
|
|
|
|
|
6
|
40971
|
|
|
|
|
|
b_stack *b_stack_new(size_t grow_by) { |
7
|
|
|
|
|
|
|
b_stack *stack; |
8
|
40971
|
50
|
|
|
|
|
size_t growth_factor = grow_by > 0? grow_by: B_STACK_DEFAULT_GROWTH_FACTOR; |
9
|
|
|
|
|
|
|
|
10
|
40971
|
50
|
|
|
|
|
if ((stack = malloc(sizeof(*stack))) == NULL) { |
11
|
0
|
|
|
|
|
|
goto error_malloc_stack; |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
40971
|
50
|
|
|
|
|
if ((stack->items = calloc(growth_factor, sizeof(void *))) == NULL) { |
15
|
0
|
|
|
|
|
|
goto error_malloc_items; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
40971
|
|
|
|
|
|
stack->size = growth_factor; |
19
|
40971
|
|
|
|
|
|
stack->count = 0; |
20
|
40971
|
|
|
|
|
|
stack->growth_factor = growth_factor; |
21
|
40971
|
|
|
|
|
|
stack->destructor = NULL; |
22
|
|
|
|
|
|
|
|
23
|
40971
|
|
|
|
|
|
return stack; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
error_malloc_items: |
26
|
0
|
|
|
|
|
|
free(stack); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
error_malloc_stack: |
29
|
0
|
|
|
|
|
|
return NULL; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
48040
|
|
|
|
|
|
void b_stack_set_destructor(b_stack *stack, void (*destructor)(void *)) { |
33
|
48040
|
|
|
|
|
|
stack->destructor = destructor; |
34
|
48040
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
static void **b_stack_resize(b_stack *stack, size_t newsize) { |
37
|
|
|
|
|
|
|
void **newitems; |
38
|
|
|
|
|
|
|
|
39
|
0
|
0
|
|
|
|
|
if (newsize == 0) { |
40
|
0
|
|
|
|
|
|
return stack->items; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
if ((newitems = realloc(stack->items, newsize * sizeof(void *))) == NULL) { |
44
|
0
|
|
|
|
|
|
goto error_realloc; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
stack->size = newsize; |
48
|
0
|
|
|
|
|
|
stack->items = newitems; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
return newitems; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
error_realloc: |
53
|
0
|
|
|
|
|
|
return NULL; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
45877
|
|
|
|
|
|
void *b_stack_push(b_stack *stack, void *item) { |
57
|
|
|
|
|
|
|
size_t index; |
58
|
|
|
|
|
|
|
|
59
|
45877
|
50
|
|
|
|
|
if (stack->count == stack->size) { |
60
|
0
|
0
|
|
|
|
|
if (b_stack_resize(stack, stack->size + stack->growth_factor) == NULL) { |
61
|
0
|
|
|
|
|
|
goto error_resize; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
45877
|
|
|
|
|
|
index = stack->count; |
66
|
|
|
|
|
|
|
|
67
|
45877
|
|
|
|
|
|
stack->items[index] = item; |
68
|
45877
|
|
|
|
|
|
stack->count++; |
69
|
|
|
|
|
|
|
|
70
|
45877
|
|
|
|
|
|
return item; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
error_resize: |
73
|
0
|
|
|
|
|
|
return NULL; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
15141
|
|
|
|
|
|
void *b_stack_pop(b_stack *stack) { |
77
|
|
|
|
|
|
|
size_t index; |
78
|
|
|
|
|
|
|
void *item; |
79
|
|
|
|
|
|
|
|
80
|
15141
|
50
|
|
|
|
|
if (stack == NULL) return NULL; |
81
|
15141
|
100
|
|
|
|
|
if (stack->count == 0) return NULL; |
82
|
|
|
|
|
|
|
|
83
|
8072
|
|
|
|
|
|
index = stack->count - 1; |
84
|
8072
|
|
|
|
|
|
item = stack->items[index]; |
85
|
|
|
|
|
|
|
|
86
|
8072
|
|
|
|
|
|
stack->items[index] = NULL; |
87
|
8072
|
|
|
|
|
|
stack->count--; |
88
|
|
|
|
|
|
|
|
89
|
8072
|
50
|
|
|
|
|
if (index == stack->size - (stack->growth_factor * 2)) { |
90
|
0
|
0
|
|
|
|
|
if (b_stack_resize(stack, stack->size - stack->growth_factor) == NULL) { |
91
|
0
|
|
|
|
|
|
goto error_resize; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
8072
|
|
|
|
|
|
return item; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
error_resize: |
98
|
0
|
|
|
|
|
|
return NULL; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
413
|
|
|
|
|
|
void *b_stack_shift(b_stack *stack) { |
102
|
|
|
|
|
|
|
size_t i, last; |
103
|
|
|
|
|
|
|
void *item; |
104
|
|
|
|
|
|
|
|
105
|
413
|
50
|
|
|
|
|
if (stack == NULL) return NULL; |
106
|
413
|
50
|
|
|
|
|
if (stack->count == 0) return NULL; |
107
|
|
|
|
|
|
|
|
108
|
413
|
|
|
|
|
|
last = stack->count - 1; |
109
|
413
|
|
|
|
|
|
item = stack->items[0]; |
110
|
|
|
|
|
|
|
|
111
|
1651
|
100
|
|
|
|
|
for (i=1; icount; i++) { |
112
|
1238
|
|
|
|
|
|
stack->items[i-1] = stack->items[i]; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
413
|
|
|
|
|
|
stack->items[last] = NULL; |
116
|
413
|
|
|
|
|
|
stack->count--; |
117
|
|
|
|
|
|
|
|
118
|
413
|
50
|
|
|
|
|
if (last == stack->size - (stack->growth_factor * 2)) { |
119
|
0
|
0
|
|
|
|
|
if (b_stack_resize(stack, stack->size - stack->growth_factor) == NULL) { |
120
|
0
|
|
|
|
|
|
goto error_resize; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
413
|
|
|
|
|
|
return item; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
error_resize: |
127
|
0
|
|
|
|
|
|
return NULL; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
819
|
|
|
|
|
|
void *b_stack_top(b_stack *stack) { |
131
|
819
|
50
|
|
|
|
|
if (stack == NULL) return NULL; |
132
|
819
|
100
|
|
|
|
|
if (stack->count == 0) return NULL; |
133
|
|
|
|
|
|
|
|
134
|
787
|
|
|
|
|
|
return stack->items[stack->count - 1]; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
81853
|
|
|
|
|
|
void *b_stack_item_at(b_stack *stack, size_t index) { |
138
|
81853
|
50
|
|
|
|
|
if (index >= stack->count) return NULL; |
139
|
|
|
|
|
|
|
|
140
|
81853
|
|
|
|
|
|
return stack->items[index]; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
116268
|
|
|
|
|
|
size_t b_stack_count(b_stack *stack) { |
144
|
116268
|
|
|
|
|
|
return stack->count; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
14138
|
|
|
|
|
|
b_stack *b_stack_reverse(b_stack *stack) { |
148
|
|
|
|
|
|
|
size_t i; |
149
|
14138
|
|
|
|
|
|
size_t limit = stack->count / 2; |
150
|
|
|
|
|
|
|
|
151
|
14634
|
100
|
|
|
|
|
for (i=0; i
|
152
|
496
|
|
|
|
|
|
size_t opposite = stack->count - 1 - i; |
153
|
496
|
|
|
|
|
|
void *tmp = stack->items[i]; |
154
|
|
|
|
|
|
|
|
155
|
496
|
|
|
|
|
|
stack->items[i] = stack->items[opposite]; |
156
|
496
|
|
|
|
|
|
stack->items[opposite] = tmp; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
14138
|
|
|
|
|
|
return stack; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
34423
|
|
|
|
|
|
void b_stack_destroy(b_stack *stack) { |
163
|
|
|
|
|
|
|
size_t i; |
164
|
|
|
|
|
|
|
|
165
|
34423
|
50
|
|
|
|
|
if (stack == NULL) return; |
166
|
|
|
|
|
|
|
|
167
|
34423
|
50
|
|
|
|
|
if (stack->destructor) { |
168
|
71815
|
100
|
|
|
|
|
for (i=0; icount; i++) { |
169
|
37392
|
|
|
|
|
|
stack->destructor(stack->items[i]); |
170
|
37392
|
|
|
|
|
|
stack->items[i] = NULL; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
34423
|
|
|
|
|
|
free(stack->items); |
175
|
34423
|
|
|
|
|
|
stack->items = NULL; |
176
|
|
|
|
|
|
|
|
177
|
34423
|
|
|
|
|
|
free(stack); |
178
|
|
|
|
|
|
|
} |