-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarray.c
51 lines (47 loc) · 1.17 KB
/
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
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "darray.h"
typedef uint32_t u32;
darray new_darray(void)
{
darray new_darray = malloc(sizeof(struct darray_s));
//Vamos a usar un approach inspirado en los vectores
// de C++
new_darray->ocupation = new_darray->size = 0;
new_darray->elements = calloc(0, sizeof(u32));
return new_darray;
};
void expand_darray(darray d)
{
u32 new_size = GROWTH_FACTOR * d->size;
d->size = (1 > new_size) ? 1 : new_size;
d->elements = realloc(d->elements, d->size * sizeof(u32));
}
void darray_push(u32 e, darray d)
{
if (d->size == d->ocupation)
expand_darray(d);
d->elements[d->ocupation] = e;
d->ocupation += 1;
}
darray darray_copy(darray d)
{
darray clone = malloc(sizeof(struct darray_s));
clone->size = d->size;
clone->ocupation = d->ocupation;
clone->elements = calloc(d->ocupation, sizeof(u32));
for (u32 i = 0; i < d->ocupation; ++i)
clone->elements[i] = d->elements[i];
return clone;
}
void delete_darray(darray d)
{
free(d->elements);
free(d);
}
u32 darray_get(u32 index, darray d)
{
return d->elements[index];
}