-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
54 lines (48 loc) · 1.23 KB
/
Copy pathutils.c
File metadata and controls
54 lines (48 loc) · 1.23 KB
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
#include "utils.h"
void * vector_init (int idx){
/* Initializing the vector */
Vector *v = malloc(sizeof(Vector));
if (!v) {
fprintf(stderr,"Malloc Failed on allocating space for Vector struct!");
exit(1);
}
v->base = malloc(idx * IDX);
if (!v->base) {
free(v);
fprintf(stderr,"malloc failed on v->base!\n");
exit(1);
}
v->used = 0;
v->capacity = idx * sizeof(int);
return v;
}
void vector_push (Vector *v,int val) {
if ((v->capacity - v->used ) < IDX ) {
fprintf(stderr,"Not enough space in vector!\n");
exit(1);
}
int *ptr = vector_alloc(v,IDX);
memcpy(ptr,&val,IDX);
}
void vector_free (Vector *v) {
free(v->base);
free(v);
}
void * vector_alloc (Vector *v,size_t size) {
if ((v->capacity - v->used) < size){
fprintf(stderr,"Not enough space in vector!\n");
exit(1);
}
void *ptr = v->base + v->used;
v->used += size;
return ptr;
}
void vector_print(Vector *v, int idx) {
int val;
memcpy(&val, v->base + idx * IDX, IDX);
printf("[IDX -> %d] Val -> %d\n", idx, val);
}
void vector_expand (Vector *v,int idx) {
v->capacity += idx * IDX;
v->base = realloc(v->base, v->capacity);
}