-
Notifications
You must be signed in to change notification settings - Fork 37
/
vector.c
122 lines (105 loc) · 2.43 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "vector.h"
#include <string.h>
vector_p create_vector(){
vector_p vec = (vector_p)malloc(sizeof(struct vector));
vec->data = (void**)malloc(sizeof(void*)*BASE_CAP);
vec->sizes = (int*)malloc(sizeof(int)*BASE_CAP);
vec->capacity = BASE_CAP;
vec->length = 0;
vec->destructor = free;
return vec;
}
vector_p subvector(vector_p vec, int start, int end){
vector_p subvec = create_vector();
int i;
void * val;
for(i=start; i<end; i++){
val = vec->data[i];
vector_add(subvec, val, vec->sizes[i]);
}
return subvec;
}
void check_length(vector_p vec){
if(vec->length >= vec->capacity){
vec->capacity*=EXPAND_RATIO;
vec->data = (void**)realloc((void*)vec->data,
vec->capacity*sizeof(void*));
vec->sizes = (int*)realloc((void*)vec->sizes,
vec->capacity*sizeof(int));
}
}
void vector_add(vector_p vec, void* data, size_t n){
check_length(vec);
vec->data[vec->length] = malloc(n);
vec->sizes[vec->length] = n;
memcpy(vec->data[vec->length], data, n);
vec->length++;
}
void* vector_get(vector_p vec, size_t i){
if(i >= vec->length)
return NULL;
return vec->data[i];
}
int vector_set(vector_p vec, size_t i, void* data, size_t n){
if(i >= vec->length)
return -1;
vec->destructor(vec->data[i]);
vec->data[i] = malloc(n);
vec->sizes[i] = n;
memcpy(vec->data[i], data, n);
return 0;
}
int vector_insert(vector_p vec, size_t i, void* data, size_t n){
int x;
if(i > vec->length)
return -1;
check_length(vec);
for(x=vec->length;x>=i;x--){
vec->data[x+1] = vec->data[x];
vec->sizes[x+1] = vec->sizes[x];
}
vec->data[i] = malloc(n);
vec->sizes[i] = n;
memcpy(vec->data[i], data, n);
vec->length++;
return 0;
}
void vector_remove(vector_p vec, size_t i){
int x;
if(i >= vec->length)
return;
vec->destructor(vec->data[i]);
vec->length--;
for(x=i;x<vec->length;++x){
vec->data[x] = vec->data[x+1];
vec->sizes[x] = vec->sizes[x+1];
}
}
int vector_index(vector_p vec, void* data, size_t n){
int x;
void* check;
for(x=0;x<vec->length;++x){
check = vec->data[x];
if(check==data||memcmp(check, data, n)==0){
return x;
}
}
return -1;
}
void destroy_vector(vector_p vec){
int i;
for(i=0;i<vec->length;i++){
vec->destructor(vec->data[i]);
}
free(vec->data);
free(vec->sizes);
free(vec);
}
void vector_swap(vector_p vec, int i, int j){
void * temp;
if(i >= vec->length || j >= vec->length)
return;
temp = vec->data[i];
vec->data[i] = vec->data[j];
vec->data[j] = temp;
}