-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.c
executable file
·105 lines (91 loc) · 2.77 KB
/
vector.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include "vector.h"
// Dynamic Array utility functions - definition
void vector_init(Vector *vector) {
// initialize size and capacity
vector->size = 0;
vector->capacity = VECTOR_INITIAL_CAPACITY;
// allocate memory for vector->data
vector->data = malloc(sizeof(void *) * vector->capacity);
}
void vector_append(Vector *vector, void *data_ptr) {
// make sure there's room to expand into
vector_double_capacity_if_full(vector);
// append the value and increment vector->size
vector->data[vector->size++] = data_ptr;
}
void *vector_get(Vector *vector, int index) {
if (index >= vector->size || index < 0) {
printf("Index %d out of bounds for vector of size %d\n", index, vector->size);
exit(EXIT_FAILURE);
}
return vector->data[index];
}
void *vector_last(Vector *vector){
if(vector->size == 0)
return NULL;
return vector->data[vector->size-1];
}
void *vector_remove_last(Vector *vector){
if(vector->size == 0)
return NULL;
vector->size --;
return vector->data[vector->size];
}
void *vector_remove_at(Vector *vector, int index){
if(vector->size == 0 || index < 0 || index > (vector->size -1))
return NULL;
void *ret_data = vector->data[index];
if(index != vector->size - 1)
memcpy(vector->data+index*sizeof(void *),
vector->data+(index+1)*sizeof(void *), sizeof(void *)*(vector->size -1-index));
vector->size --;
return ret_data;
}
void vector_set(Vector *vector, int index, void *data_ptr) {
// zero fill the vector up to the desired index
while (index >= vector->size) {
vector_append(vector, 0); }
// set the value at the desired index
vector->data[index] = data_ptr;
}
void vector_double_capacity_if_full(Vector *vector) {
if (vector->size >= vector->capacity) {
// double vector->capacity and resize the allocated memory accordingly
vector->capacity *= 2;
vector->data = realloc(vector->data, sizeof(int) * vector->capacity);
}
}
void vector_inner_free(Vector *vector) {
for(int i=0; i< vector->size; i++){
void *data_p = vector_get(vector, i);
free(data_p);
}
free(vector->data);
}
int vector_first_index_of_int(Vector * vector, int * target_number){
int length = vector->size;
for(int i = 0; i<length; i++){
void *data_ptr = vector->data[i];
int *number_ptr = (int *)data_ptr;
if(*number_ptr == *target_number)
return i;
}
return -1;
}
int vector_first_index_of_string(Vector *vector, char * target_string){
int length = vector->size;
for(int i = 0; i<length; i++){
void *data_ptr = vector->data[i];
char *string_ptr = (char *)data_ptr;
if(strcmp(string_ptr, target_string) == 0)
return i;
}
return -1;
}