-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Util.c
More file actions
104 lines (90 loc) · 2.68 KB
/
Array_Util.c
File metadata and controls
104 lines (90 loc) · 2.68 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
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
#include "Array_Util.h"
#include <stdlib.h>
ArrayUtil create(int typeSize, int length){
ArrayUtil newArrayUtil;
newArrayUtil.base = calloc(length,typeSize);
newArrayUtil.typeSize = typeSize;
newArrayUtil.length = length;
return newArrayUtil;
};
ArrayUtil resize(ArrayUtil a,int length){
ArrayUtil array = a;
realloc(a.base,(length*a.typeSize));
a.length = length;
for(int i = 0; i < length; i++){
((int *)a.base)[i] = ((int *)array.base)[i];
};
return a;
};
int areEqual(ArrayUtil a, ArrayUtil b){
if(!(a.typeSize == b.typeSize) || !(a.length == b.length))
return 0;
int i = 0;
while(i<a.length){
if(!(((int *)a.base)[i]==((int *)b.base)[i]))
return 0;
i++;
}
return 1;
};
int findIndex(ArrayUtil a,void* element){
for(int i = 0;i < a.length; i++){
if(((int *)a.base)[i] == element)
return i;
};
return -1;
};
void dispose(ArrayUtil a){
free(a.base);
};
void * findFirst(ArrayUtil util, MatchFunc* match, void* hint){
int * array = (int *)util.base;
for(int i = 0;i < util.length * util.typeSize;i+=util.typeSize){
if((*match)(hint,&util.base[i])) return &util.base[i];
};
return NULL;
};
void * findLast(ArrayUtil util, MatchFunc* match, void* hint){
int * array = (int *)util.base;
void *result = NULL;
for(int i = 0;i < util.length * util.typeSize;i+=util.typeSize){
if((*match)(hint,&util.base[i]))
result = &util.base[i];
};
return result;
};
int count(ArrayUtil util, MatchFunc* match, void* hint){
int count = 0;
for(int i = 0;i < util.length * util.typeSize;i+=util.typeSize){
if((*match)(hint,&util.base[i]))
count++;
};
return count;
};
int filter(ArrayUtil util, MatchFunc* match, void* hint, void** destination, int maxItems ){
int count = 0;
for(int i = 0;i < util.length * util.typeSize;i+=util.typeSize){
if((*match)(hint,&util.base[i])){
if(count < maxItems){
((int *)(*destination))[count] = ((char *)util.base)[i];
count++;
}
}
};
return count;
};
void map(ArrayUtil source, ArrayUtil destination, ConvertFunc* convert, void* hint){
for(int i = 0;i < source.length * source.typeSize ;i+= source.typeSize)
(*convert)(hint,&source.base[i],&destination.base[i]);
};
void* reduce(ArrayUtil util, ReducerFunc* reducer, void* hint, void* initialValue){
void * reducedValue = initialValue;
for(int i = 0;i < util.length * util.typeSize ;i+= util.typeSize){
reducedValue = (*reducer)(hint,reducedValue,&util.base[i]);
}
return reducedValue;
};
void forEach(ArrayUtil util, OperationFunc* operation, void* hint){
for(int i = 0;i < util.length * util.typeSize ;i+= util.typeSize)
(*operation)(hint,&util.base[i]);
};