-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_darray.c
82 lines (59 loc) · 1.4 KB
/
tg_darray.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
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "tg_common.h"
#include "tg_darray.h"
void tg_darrinit(struct tg_darray *darr, size_t sz)
{
assert(darr != NULL);
darr->sz = sz;
TG_ASSERT((darr->data = malloc(sz)) != NULL,
"%s%s", "Cannot allocate memory while",
" initilizing a dynamic array");
darr->cnt = 0;
darr->max = 1;
}
int tg_darrset(struct tg_darray *darr, size_t pos, void *el)
{
assert(darr != NULL);
assert(el != NULL);
darr->cnt = pos + 1;
if (pos >= darr->max) {
char *p;
darr->max = (pos + 1) * 2;
p = realloc(darr->data, darr->sz * darr->max);
TG_ASSERT(p != NULL, "%s%s", "Cannot allocate memory",
" for a dynamic array");
darr->data = p;
}
memcpy(darr->data + pos * darr->sz, el, darr->sz);
return pos;
}
int tg_darrpop(struct tg_darray *darr, void *el)
{
assert(darr != NULL);
if (darr->cnt == 0) {
TG_SETERROR("%s%s", "Trying to pop out of an empty",
" dynamic array");
return (-1);
}
--darr->cnt;
if (el == NULL)
return 0;
memcpy(el, darr->data + darr->cnt * darr->sz, darr->sz);
return 0;
}
void tg_darrclear(struct tg_darray *darr)
{
free(darr->data);
}
void *tg_darrget(const struct tg_darray *darr, int n)
{
assert(darr != NULL);
if (n < 0 || n >= darr->cnt) {
TG_SETERROR("%s%s", "Trying to get non-existing",
" element out of a dynamyc array");
return NULL;
}
return darr->data + n * darr->sz;
}