line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#include |
2
|
|
|
|
|
|
|
#include |
3
|
|
|
|
|
|
|
#include |
4
|
|
|
|
|
|
|
#include "b_stack.h" |
5
|
|
|
|
|
|
|
|
6
|
39771
|
|
|
|
|
|
b_stack *b_stack_new(size_t grow_by) { |
7
|
|
|
|
|
|
|
b_stack *stack; |
8
|
39771
|
50
|
|
|
|
|
size_t growth_factor = grow_by > 0? grow_by: B_STACK_DEFAULT_GROWTH_FACTOR; |
9
|
|
|
|
|
|
|
|
10
|
39771
|
50
|
|
|
|
|
if ((stack = malloc(sizeof(*stack))) == NULL) { |
11
|
0
|
|
|
|
|
|
goto error_malloc_stack; |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
39771
|
50
|
|
|
|
|
if ((stack->items = calloc(growth_factor, sizeof(void *))) == NULL) { |
15
|
0
|
|
|
|
|
|
goto error_malloc_items; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
39771
|
|
|
|
|
|
stack->size = growth_factor; |
19
|
39771
|
|
|
|
|
|
stack->count = 0; |
20
|
39771
|
|
|
|
|
|
stack->growth_factor = growth_factor; |
21
|
39771
|
|
|
|
|
|
stack->destructor = NULL; |
22
|
|
|
|
|
|
|
|
23
|
39771
|
|
|
|
|
|
return stack; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
error_malloc_items: |
26
|
0
|
|
|
|
|
|
free(stack); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
error_malloc_stack: |
29
|
0
|
|
|
|
|
|
return NULL; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
46441
|
|
|
|
|
|
void b_stack_set_destructor(b_stack *stack, void (*destructor)(void *)) { |
33
|
46441
|
|
|
|
|
|
stack->destructor = destructor; |
34
|
46441
|
|
|
|
|
|
} |
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
|
43079
|
|
|
|
|
|
void *b_stack_push(b_stack *stack, void *item) { |
57
|
|
|
|
|
|
|
size_t index; |
58
|
|
|
|
|
|
|
|
59
|
43079
|
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
|
43079
|
|
|
|
|
|
index = stack->count; |
66
|
|
|
|
|
|
|
|
67
|
43079
|
|
|
|
|
|
stack->items[index] = item; |
68
|
43079
|
|
|
|
|
|
stack->count++; |
69
|
|
|
|
|
|
|
|
70
|
43079
|
|
|
|
|
|
return item; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
error_resize: |
73
|
0
|
|
|
|
|
|
return NULL; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
13545
|
|
|
|
|
|
void *b_stack_pop(b_stack *stack) { |
77
|
|
|
|
|
|
|
size_t index; |
78
|
|
|
|
|
|
|
void *item; |
79
|
|
|
|
|
|
|
|
80
|
13545
|
50
|
|
|
|
|
if (stack == NULL) return NULL; |
81
|
13545
|
100
|
|
|
|
|
if (stack->count == 0) return NULL; |
82
|
|
|
|
|
|
|
|
83
|
6875
|
|
|
|
|
|
index = stack->count - 1; |
84
|
6875
|
|
|
|
|
|
item = stack->items[index]; |
85
|
|
|
|
|
|
|
|
86
|
6875
|
|
|
|
|
|
stack->items[index] = NULL; |
87
|
6875
|
|
|
|
|
|
stack->count--; |
88
|
|
|
|
|
|
|
|
89
|
6875
|
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
|
6875
|
|
|
|
|
|
return item; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
error_resize: |
98
|
0
|
|
|
|
|
|
return NULL; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
14
|
|
|
|
|
|
void *b_stack_shift(b_stack *stack) { |
102
|
|
|
|
|
|
|
size_t i, last; |
103
|
|
|
|
|
|
|
void *item; |
104
|
|
|
|
|
|
|
|
105
|
14
|
50
|
|
|
|
|
if (stack == NULL) return NULL; |
106
|
14
|
50
|
|
|
|
|
if (stack->count == 0) return NULL; |
107
|
|
|
|
|
|
|
|
108
|
14
|
|
|
|
|
|
last = stack->count - 1; |
109
|
14
|
|
|
|
|
|
item = stack->items[0]; |
110
|
|
|
|
|
|
|
|
111
|
56
|
100
|
|
|
|
|
for (i=1; icount; i++) { |
112
|
42
|
|
|
|
|
|
stack->items[i-1] = stack->items[i]; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
14
|
|
|
|
|
|
stack->items[last] = NULL; |
116
|
14
|
|
|
|
|
|
stack->count--; |
117
|
|
|
|
|
|
|
|
118
|
14
|
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
|
14
|
|
|
|
|
|
return item; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
error_resize: |
127
|
0
|
|
|
|
|
|
return NULL; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
417
|
|
|
|
|
|
void *b_stack_top(b_stack *stack) { |
131
|
417
|
50
|
|
|
|
|
if (stack == NULL) return NULL; |
132
|
417
|
100
|
|
|
|
|
if (stack->count == 0) return NULL; |
133
|
|
|
|
|
|
|
|
134
|
386
|
|
|
|
|
|
return stack->items[stack->count - 1]; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
79050
|
|
|
|
|
|
void *b_stack_item_at(b_stack *stack, size_t index) { |
138
|
79050
|
50
|
|
|
|
|
if (index >= stack->count) return NULL; |
139
|
|
|
|
|
|
|
|
140
|
79050
|
|
|
|
|
|
return stack->items[index]; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
112268
|
|
|
|
|
|
size_t b_stack_count(b_stack *stack) { |
144
|
112268
|
|
|
|
|
|
return stack->count; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
13340
|
|
|
|
|
|
b_stack *b_stack_reverse(b_stack *stack) { |
148
|
|
|
|
|
|
|
size_t i; |
149
|
13340
|
|
|
|
|
|
size_t limit = stack->count / 2; |
150
|
|
|
|
|
|
|
|
151
|
13437
|
100
|
|
|
|
|
for (i=0; i
|
152
|
97
|
|
|
|
|
|
size_t opposite = stack->count - 1 - i; |
153
|
97
|
|
|
|
|
|
void *tmp = stack->items[i]; |
154
|
|
|
|
|
|
|
|
155
|
97
|
|
|
|
|
|
stack->items[i] = stack->items[opposite]; |
156
|
97
|
|
|
|
|
|
stack->items[opposite] = tmp; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
13340
|
|
|
|
|
|
return stack; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
33223
|
|
|
|
|
|
void b_stack_destroy(b_stack *stack) { |
163
|
|
|
|
|
|
|
size_t i; |
164
|
|
|
|
|
|
|
|
165
|
33223
|
50
|
|
|
|
|
if (stack == NULL) return; |
166
|
|
|
|
|
|
|
|
167
|
33223
|
50
|
|
|
|
|
if (stack->destructor) { |
168
|
69413
|
100
|
|
|
|
|
for (i=0; icount; i++) { |
169
|
36190
|
|
|
|
|
|
stack->destructor(stack->items[i]); |
170
|
36190
|
|
|
|
|
|
stack->items[i] = NULL; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
33223
|
|
|
|
|
|
free(stack->items); |
175
|
33223
|
|
|
|
|
|
stack->items = NULL; |
176
|
|
|
|
|
|
|
|
177
|
33223
|
|
|
|
|
|
free(stack); |
178
|
|
|
|
|
|
|
} |