A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
43
Task/Filter/C/filter.c
Normal file
43
Task/Filter/C/filter.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int even_sel(int x) { return !(x & 1); }
|
||||
int tri_sel(int x) { return x % 3; }
|
||||
|
||||
/* using a predicate function sel() to select elements */
|
||||
int* grep(int *in, int len, int *outlen, int (*sel)(int), int inplace)
|
||||
{
|
||||
int i, j, *out;
|
||||
|
||||
if (inplace) out = in;
|
||||
else out = malloc(sizeof(int) * len);
|
||||
|
||||
for (i = j = 0; i < len; i++)
|
||||
if (sel(in[i]))
|
||||
out[j++] = in[i];
|
||||
|
||||
if (!inplace && j < len)
|
||||
out = realloc(out, sizeof(int) * j);
|
||||
|
||||
*outlen = j;
|
||||
return out;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int in[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
int i, len;
|
||||
|
||||
int *even = grep(in, 10, &len, even_sel, 0);
|
||||
printf("Filtered even:");
|
||||
for (i = 0; i < len; i++) printf(" %d", even[i]);
|
||||
printf("\n");
|
||||
|
||||
grep(in, 8, &len, tri_sel, 1);
|
||||
printf("In-place filtered not multiple of 3:");
|
||||
for (i = 0; i < len; i++) printf(" %d", in[i]);
|
||||
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue