-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayUtilsDef.c
More file actions
125 lines (99 loc) · 2.54 KB
/
arrayUtilsDef.c
File metadata and controls
125 lines (99 loc) · 2.54 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
104
105
106
107
108
109
110
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arrayUtils.h"
ARRAY_UTILS create(int typeSize,int length){
ARRAY_UTILS array;
array.base = (void *)calloc(typeSize,length);
array.typeSize = typeSize;
array.length = length;
return array;
}
int areEqual(ARRAY_UTILS array1,ARRAY_UTILS array2){
if((array1.length == array2.length) && (array1.typeSize == array2.typeSize)){
if(memcmp(array1.base,array2.base,array1.length) == 0){
return 1;
}
}
return 0;
}
ARRAY_UTILS resize(ARRAY_UTILS sampleUtils,int length){
sampleUtils.base = realloc(sampleUtils.base,length*sampleUtils.typeSize);
sampleUtils.length = length;
return sampleUtils;
}
void dispose(ARRAY_UTILS utils){
free(utils.base);
}
int findIndex(ARRAY_UTILS utils,void* element){
void * base = utils.base;
for(int i=0;i<utils.length;i++){
if(memcmp(base,element,utils.typeSize)==0){
return i;
}
base = base + utils.typeSize;
}
return -1;
}
void* findFirst(ARRAY_UTILS util, MatchFunc* match, void* hint){
for(int i=0;i<util.length;i++){
if(match(hint,util.base) == 1){
return util.base;
}
util.base+=util.typeSize;
}
return NULL;
};
void* findLast(ARRAY_UTILS util, MatchFunc* match, void* hint){
util.base += ((util.length-1) * util.typeSize);
for(int i=util.length-1;i>=0;i--){
if(match(hint,util.base) == 1){
return util.base;
}
util.base-=util.typeSize;
}
return NULL;
};
int count(ARRAY_UTILS util, MatchFunc* match, void* hint){
int counter = 0;
for(int i=0;i<util.length;i++){
if(match(hint,util.base) == 1){
counter++;
}
util.base+=util.typeSize;
}
return counter;
};
int filter(ARRAY_UTILS util, MatchFunc* match, void* hint, void** destination, int maxItems ){
int counter = 0;
for(int i=0;i<util.length;i++){
if(match(hint,util.base) == 1){
*destination = util.base;
counter++;
destination++;
}
util.base+=util.typeSize;
}
return counter;
};
void map(ARRAY_UTILS source, ARRAY_UTILS destination, ConvertFunc* convert, void* hint){
for(int i=0;i<source.length;i++){
convert(hint, source.base, destination.base);
source.base+=source.typeSize;
destination.base+=source.typeSize;
}
};
void forEach(ARRAY_UTILS util, OperationFunc* operation, void* hint){
for(int i=0;i<util.length;i++){
operation(hint, util.base);
util.base+=util.typeSize;
}
};
void *reduce(ARRAY_UTILS util, ReducerFunc* reducer, void* hint, void* intialValue){
util.base+=util.typeSize;
for(int i=0;i<util.length;i++){
reducer(hint, intialValue, util.base);
util.base+=util.typeSize;
}
return intialValue;
};