-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
73 lines (67 loc) · 1.48 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
//
#include <malloc.h>
#include <string.h>
//#include <assert.h>
typedef struct{
void** items;
size_t length;
size_t capacity;
} Vector;
void Vector_init(Vector* vector) {
vector->items = NULL;
vector->length = 0;
vector->capacity = 0;
}
void Vector_free(Vector* vector) {
free(vector->items);
Vector_init(vector);
}
void Vector_free_ex(Vector *vector) {
size_t i;
for (i = 0; i < vector->length; i++) {
free(vector->items[i]);
}
Vector_free(vector);
}
Vector str_split(const char* str, const char* delims) {
Vector Str;
char *str_t, *p;
void **pp;
//
str_t = strdup(str);
//
Str.length = 0;
Str.items = NULL;
//
for (p = str_t; p = strtok(p, delims), p != NULL; p = NULL) {
pp = realloc(Str.items, sizeof(char*) * (Str.length+1));
//assert(pp != NULL);
Str.items = pp;
Str.items[Str.length] = strdup(p);
Str.length++;
}
free(str_t);
return Str;
}
int Vector_in_Vector(Vector *arr_v, Vector *arr_s) {
size_t i, j;
int flag_t, flag_i;
flag_t = 0;
for (i = 0; i < arr_s->length; i++) {
flag_i = 0;
for (j = 0; j < arr_v->length; j++) {
if (strcmp(arr_s->items[i], arr_v->items[j]) == 0) {
flag_i = 1;
break;
}
}
if (flag_i == 1) {
flag_t = 1;
}
else {
flag_t = 0;
break;
}
}
return flag_t;
}