ajout toolbox.c
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#include "toolbox.h"
|
#include "toolbox.h"
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -25,21 +26,24 @@ struct array *sort(struct array *arr, comparator f)
|
|||||||
free(temp);
|
free(temp);
|
||||||
return arr;
|
return arr;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct array *map(struct array *arr, size_t output_size, mapper f)
|
struct array *map(struct array *arr, size_t output_size, mapper f)
|
||||||
{
|
{
|
||||||
|
|
||||||
void *data = calloc(arr->len, output_size);
|
void *data = calloc(arr->len, output_size);
|
||||||
if(!data) return NULL;
|
if(!data) return NULL;
|
||||||
|
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
while (index != arr->len){
|
while (index != arr->len){
|
||||||
f(data[index], arr->data + index * arr->elem_size);
|
void *data2 = data + index * output_size;
|
||||||
|
void *n = arr->data + index * arr->elem_size;
|
||||||
|
|
||||||
|
f(data2, n);
|
||||||
index ++;
|
index ++;
|
||||||
}
|
}
|
||||||
memcpy(arr->data, data);
|
|
||||||
|
free(arr->data);
|
||||||
|
arr->data = data;
|
||||||
arr->elem_size = output_size;
|
arr->elem_size = output_size;
|
||||||
|
|
||||||
return arr;
|
return arr;
|
||||||
@@ -47,17 +51,53 @@ struct array *map(struct array *arr, size_t output_size, mapper f)
|
|||||||
|
|
||||||
struct array *filter(struct array *arr, predicate f)
|
struct array *filter(struct array *arr, predicate f)
|
||||||
{
|
{
|
||||||
(void)arr;
|
size_t index = 0;
|
||||||
(void)f;
|
int flon = 0;
|
||||||
return NULL;
|
while(index != arr->len){
|
||||||
|
|
||||||
|
void *cur = arr->data + index * arr->elem_size;
|
||||||
|
|
||||||
|
if(f(cur) != 0) flon ++;
|
||||||
|
|
||||||
|
index ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
index = 0;
|
||||||
|
void *data1 = calloc(flon, arr->elem_size);
|
||||||
|
if(!data1 && flon > 0) return NULL;
|
||||||
|
size_t index2 = 0;
|
||||||
|
|
||||||
|
while(index != arr->len){
|
||||||
|
void *cur = arr->data + index * arr->elem_size;
|
||||||
|
|
||||||
|
if(f(cur) != 0){
|
||||||
|
void *n = data1 + index2 * arr->elem_size;
|
||||||
|
memcpy(n, cur, arr->elem_size);
|
||||||
|
index2 ++;
|
||||||
|
}
|
||||||
|
index ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(arr->data);
|
||||||
|
arr->data = data1;
|
||||||
|
arr->len = flon;
|
||||||
|
|
||||||
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *reduce(struct array *arr, void *start_value, reducer f)
|
void *reduce(struct array *arr, void *start_value, reducer f)
|
||||||
{
|
{
|
||||||
(void)arr;
|
void *rslt = start_value;
|
||||||
(void)f;
|
size_t index = 0;
|
||||||
(void)start_value;
|
|
||||||
return NULL;
|
while(index != arr->len){
|
||||||
|
void *cur = arr->data + index * arr->elem_size;
|
||||||
|
|
||||||
|
rslt = f(rslt, cur);
|
||||||
|
index ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rslt;
|
||||||
}
|
}
|
||||||
|
|
||||||
void foreach(struct array *arr, acceptor f)
|
void foreach(struct array *arr, acceptor f)
|
||||||
@@ -71,7 +111,78 @@ void foreach(struct array *arr, acceptor f)
|
|||||||
index ++;
|
index ++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int *add(int *res, int *n)
|
||||||
|
{
|
||||||
|
*res += *n;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_int(int *n)
|
||||||
|
{
|
||||||
|
printf("%d\n", *n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
struct array arr = {
|
||||||
|
.data = calloc(4, sizeof(int)),
|
||||||
|
.len = 4,
|
||||||
|
.elem_size = sizeof(int),
|
||||||
|
};
|
||||||
|
|
||||||
|
int *data = arr.data;
|
||||||
|
data[0] = 0;
|
||||||
|
data[1] = 1;
|
||||||
|
data[2] = 2;
|
||||||
|
data[3] = 3;
|
||||||
|
|
||||||
|
int res = 0;
|
||||||
|
reduce(&arr, &res, (reducer)&add);
|
||||||
|
printf("%d\n", res);
|
||||||
|
|
||||||
|
free(arr.data);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int odd(int *n)
|
||||||
|
{
|
||||||
|
return *n % 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_int(int *n)
|
||||||
|
{
|
||||||
|
printf("%d\n", *n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
struct array arr = {
|
||||||
|
.data = calloc(4, sizeof(int)),
|
||||||
|
.len = 4,
|
||||||
|
.elem_size = sizeof(int),
|
||||||
|
};
|
||||||
|
|
||||||
|
int *data = arr.data;
|
||||||
|
data[0] = 0;
|
||||||
|
data[1] = 1;
|
||||||
|
data[2] = 2;
|
||||||
|
data[3] = 3;
|
||||||
|
|
||||||
|
foreach(filter(&arr, (predicate)&odd), (acceptor)&print_int);
|
||||||
|
|
||||||
|
free(arr.data);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@@ -103,7 +214,7 @@ int main(void)
|
|||||||
|
|
||||||
free(arr.data);
|
free(arr.data);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user