forked from whiteblock/hobbits
-
Notifications
You must be signed in to change notification settings - Fork 7
/
vector.h
168 lines (146 loc) · 2.95 KB
/
vector.h
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef vector_H
#define vector_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct vect* vector;
struct vect{
vector next;
vector prev;
void* data;
};
static vector create_vector(){
vector out = (vector) malloc(sizeof(struct vect));
out->next = NULL;
out->prev = NULL;
out->data = NULL;
return out;
}
size_t vector_length(vector v){
size_t length = 0;
vector tmp = v;
if(v==NULL){
return 0;
}
while(tmp != NULL){
tmp = tmp->next;
length++;
}
return length;
}
void* vector_get(vector v,size_t index){
vector tmp = v;
int i = 0;
while(i != index && tmp != NULL){
tmp = tmp->next;
i++;
}
return tmp->data;
}
void vector_clean(vector v){
vector tmp = v;
vector clear = NULL;
if(v==NULL){
return;
}
while(tmp != NULL){
clear = tmp;
tmp = tmp->next;
clear->next = NULL;
clear->prev = NULL;
if(clear->data)
free(clear->data);
free(clear);
}
}
void* vector_get_f(vector v,size_t index){
char * out = vector_get(v,index);
vector_clean(v);
return out;
}
void vector_pop(vector* v, size_t index){
if(v==NULL || *v == NULL){
return;
}
vector tmp = *v;
vector p;
int i = 0;
if(index == 0){
*v = tmp->next;
free(tmp);
if(*v != NULL){
(*v)->prev = NULL;
}
return;
}
while(i != index && tmp != NULL){
tmp = tmp->next;
i++;
}
p = tmp->prev;
p->next = tmp->next;
tmp->next->prev = p;
free(tmp);
}
void vector_push(vector * v,void* data){
vector add = create_vector();
vector tmp;
add->data = data;
if(v==NULL||*v==NULL){
*v = add;
return;
}
tmp = *v;
while(tmp->next != NULL){
tmp = tmp->next;
}
tmp->next = add;
add->prev = tmp;
}
void vector_free(vector v){
vector tmp = v;
vector clear = NULL;
if(v==NULL){
return;
}
while(tmp->next != NULL){
tmp = tmp->next;
clear = tmp->prev;
clear->next = NULL;
clear->prev = NULL;
clear->data = NULL;
free(clear);
}
free(tmp);
}
vector vector_merge(vector * v1, vector * v2){
int i;
for(i = 0;i<vector_length(*v2);i++){
vector_push(v1,vector_get(*v2,i));
}
if(*v2!=NULL){
free(*v2);
}
return *v1;
}
void vector_print_i(vector v1){
vector v = v1;
puts("Printing vector contents as ints");
while(v!=NULL){
printf("%d\n",*((int *)v->data));
v = v->next;
}
puts("done");
}
void ** vector_to_array(vector v){
size_t size = vector_length(v);
void ** out = (void**)calloc(sizeof(void*),size+1);
out[size] = NULL;
int32_t i = 0;
for(i = 0;i<size;i++){
void* value = vector_get(v,i);
out[i] = value;
}
return out;
}
#endif