-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrayUtil.c
138 lines (124 loc) · 3.34 KB
/
arrayUtil.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "arrayUtil.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
ArrayUtil initializeArrayUtil (void* base_pointer,int typesize,int length){
ArrayUtil a;
a.base_ptr = base_pointer;
a.typesize = typesize;
a.length = length;
return a;
}
int areEqual (struct ArrayUtil a, struct ArrayUtil b){
int i;
char *base_a = (char*)a.base_ptr;
char *base_b = (char*)b.base_ptr;
if(a.typesize == b.typesize && a.length == b.length){
for(i=0 ; i<a.length*a.typesize ; i++){
if(base_a[i] != base_b[i]){
return 0;
}
}
}
else
return 0;
return 1;
}
ArrayUtil create (int typesize, int length){
char *array = calloc(typesize,length*typesize);
ArrayUtil result = initializeArrayUtil(array,typesize,length);
return result;
}
ArrayUtil resize (ArrayUtil a, int new_length){
int i,size = new_length*a.typesize;
char* base_a = (char*)a.base_ptr;
char* array = calloc(new_length,size);
ArrayUtil result = {array,a.typesize,new_length};
for (i =0 ; i < size ; i++) {
if(i > a.typesize*a.length-1){
break;
}
array[i] = base_a[i];
}
return result;
}
int findIndex(ArrayUtil util,void* ele){
int i=0,size = util.typesize*util.length;
char* base = ((char*)util.base_ptr);
char* target = ((char*)base+size);
while(base<target){
if(memcmp(base,ele,util.typesize) == 0 ){
return i;
}
base+=util.typesize;
i++;
}
return -1;
};
void dispose(ArrayUtil util){
free(util.base_ptr);
}
void* findFirst (ArrayUtil util, int (*fn)(void*,void*),void* hint){
void* base = util.base_ptr;
void* target = util.base_ptr + util.typesize*util.length;
for(; base<=target; base+=util.typesize){
if(fn(hint,base)){
return base;
}
}
return NULL;
}
void forEach(ArrayUtil util, void (*operation)(void*, void*), void* hint){
void* base = util.base_ptr;
void* target = base + util.length * util.typesize;
for (; base<target ; base+=util.typesize) {
operation(hint,base);
}
};
void* reduce(ArrayUtil util, void* (*reducer)(void*,void*,void*), void* hint, void* pv){
void* base = util.base_ptr;
void* target = base + util.typesize*util.length;
for(; base<target ; base+=util.typesize){
pv = reducer(hint,pv,base);
}
return pv;
};
int count(ArrayUtil util, int (*fn_ptr)(void *, void *), void *hint){
int count =0;
int res;
void* base = util.base_ptr;
void* target = base + (util.typesize * util.length);
for(; base<target ; base+=util.typesize) {
res = fn_ptr(hint,base);
res && count++;
}
return count;
};
void* findLast(ArrayUtil util, int (*fn)(void*, void*), void* hint){
void* base = util.base_ptr;
void* target = util.base_ptr + (util.length-1)*util.typesize;
for (; target>base ; target-=util.typesize) {
if(fn(hint,(void*)target))
return (void*)target;
}
return NULL;
}
void map(ArrayUtil util1, ArrayUtil util2, void (*function)(void*, void*, void*), void* hint){
void* src = util1.base_ptr;
void* dest = util2.base_ptr;
int size = util1.typesize > util2.typesize ? util1.typesize : util2.typesize;
void* target = dest + util1.length*size;
for (; dest<target ; dest+=util2.typesize, src+=util1.typesize){
function(hint,src,dest);
}
}
int filter(ArrayUtil util, int(*function)(void*, void*), void* hint, void **destination, int maxItems){
int i,count=0;
void* base = util.base_ptr;
for (i=0 ; i<util.length ; base+=util.typesize,i++) {
if(function(hint,base)){
destination[count++] = base;
}
}
return count;
}