ajout toolbox.c
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "toolbox.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
@@ -25,21 +26,24 @@ struct array *sort(struct array *arr, comparator f)
|
||||
free(temp);
|
||||
return arr;
|
||||
|
||||
|
||||
}
|
||||
|
||||
struct array *map(struct array *arr, size_t output_size, mapper f)
|
||||
{
|
||||
|
||||
{
|
||||
void *data = calloc(arr->len, output_size);
|
||||
if(!data) return NULL;
|
||||
|
||||
size_t index = 0;
|
||||
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 ++;
|
||||
}
|
||||
memcpy(arr->data, data);
|
||||
|
||||
free(arr->data);
|
||||
arr->data = data;
|
||||
arr->elem_size = output_size;
|
||||
|
||||
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)
|
||||
{
|
||||
(void)arr;
|
||||
(void)f;
|
||||
return NULL;
|
||||
size_t index = 0;
|
||||
int flon = 0;
|
||||
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)arr;
|
||||
(void)f;
|
||||
(void)start_value;
|
||||
return NULL;
|
||||
void *rslt = start_value;
|
||||
size_t index = 0;
|
||||
|
||||
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)
|
||||
@@ -71,7 +111,78 @@ void foreach(struct array *arr, acceptor f)
|
||||
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 <stdlib.h>
|
||||
|
||||
@@ -103,7 +214,7 @@ int main(void)
|
||||
|
||||
free(arr.data);
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user