line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#! /usr/bin/env cpppp |
2
|
|
|
|
|
|
|
## param $namespace; |
3
|
|
|
|
|
|
|
## param $vector_t; |
4
|
|
|
|
|
|
|
## param $el_t = 'int'; |
5
|
|
|
|
|
|
|
## param $el_dtor; |
6
|
3
|
|
|
3
|
|
3
|
## param $el_copy_ctor= sub($dest, $src) { "*($dest) = *($src)" }; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
12
|
|
7
|
|
|
|
|
|
|
## |
8
|
|
|
|
|
|
|
## $namespace ||= "vector_$el_t" =~ s/\s*\*\s*/_p/gr =~ s/_t$//r; |
9
|
|
|
|
|
|
|
## $vector_t ||= $namespace . '_t'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
## section PUBLIC; |
12
|
15
|
|
|
15
|
|
33
|
typedef struct $namespace { |
13
|
|
|
|
|
|
|
size_t capacity, count; |
14
|
6
|
|
|
6
|
|
13
|
$el_t el[]; |
15
|
|
|
|
|
|
|
} ${namespace}_t; |
16
|
|
|
|
|
|
|
|
17
|
9
|
|
|
9
|
|
18
|
bool ${namespace}_realloc($vector_t **vec_p, size_t capacity); |
18
|
|
|
|
|
|
|
void ${namespace}_free($vector_t **vec_p); |
19
|
|
|
|
|
|
|
bool ${namespace}_append($vector_t **vec_p, $el_t *value_p); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
## section PRIVATE; |
22
|
|
|
|
|
|
|
|
23
|
6
|
|
|
6
|
|
15
|
bool ${namespace}_realloc($vector_t **vec_p, size_t capacity) { |
|
3
|
|
|
|
|
6
|
|
24
|
|
|
|
|
|
|
$vector_t tmp; |
25
|
|
|
|
|
|
|
size_t size, i; |
26
|
|
|
|
|
|
|
## if ($el_dtor) { |
27
|
|
|
|
|
|
|
// Exists and shrinking? |
28
|
|
|
|
|
|
|
if (*vec_p && capacity < (*vec_p)->count) |
29
|
|
|
|
|
|
|
// Run destructor for each deleted element |
30
|
|
|
|
|
|
|
for (i=(*vec_p)->count; i > capacity;) { |
31
|
0
|
|
|
0
|
|
0
|
$el_dtor((*vec_p)->el[--i]); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
## } |
34
|
3
|
|
|
3
|
|
7
|
size= sizeof(struct $namespace) + capacity * sizeof($el_t); |
|
6
|
|
|
|
|
13
|
|
35
|
6
|
|
|
6
|
|
13
|
tmp= ($vector_t*)( *vec_p? realloc(*vec_p, size) : malloc(size) ); |
36
|
|
|
|
|
|
|
if (!tmp) return false; |
37
|
|
|
|
|
|
|
if (!*vec_p) |
38
|
|
|
|
|
|
|
tmp->count= 0; |
39
|
|
|
|
|
|
|
tmp->capacity= capacity; |
40
|
|
|
|
|
|
|
*vec_p= tmp; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
void ${namespace}_free($vector_t **vec_p) { |
44
|
|
|
|
|
|
|
if (*vec_p) { |
45
|
|
|
|
|
|
|
## if ($el_dtor) { |
46
|
|
|
|
|
|
|
// Run destructor for each deleted element |
47
|
|
|
|
|
|
|
for (i=(*vec_p)->count; i > 0;) { |
48
|
0
|
|
|
0
|
|
0
|
$el_dtor((*vec_p)->el[--i]); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
## } |
51
|
|
|
|
|
|
|
free(*vec_p); |
52
|
|
|
|
|
|
|
*vec_p= NULL; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
3
|
|
|
3
|
|
6
|
bool ${namespace}_append($vector_t **vec_p, $el_t *value_p) { |
|
6
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
8
|
|
57
|
|
|
|
|
|
|
if ((*vec_p)->count >= (*vec_p)->capacity) |
58
|
|
|
|
|
|
|
if (!${namespace}_realloc(vec_p, (*vec_p)->capacity << 1)) |
59
|
|
|
|
|
|
|
return false; |
60
|
3
|
|
|
3
|
|
7
|
${{ $el_copy_ctor->( '(*vec_p)->el + (*vec_p)->count', 'value_p' ) }}; |
61
|
|
|
|
|
|
|
return true; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|