toolbox.c
This commit is contained in:
Executable
BIN
Binary file not shown.
@@ -1,18 +1,48 @@
|
|||||||
#include "toolbox.h"
|
#include "toolbox.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
struct array *sort(struct array *arr, comparator f)
|
struct array *sort(struct array *arr, comparator f)
|
||||||
{
|
{
|
||||||
(void)arr;
|
|
||||||
(void)f;
|
void *temp = malloc(arr->elem_size);
|
||||||
return NULL;
|
|
||||||
|
for(size_t index1 = 0; index1 < arr->len - 1; index1 ++ ){
|
||||||
|
|
||||||
|
for(size_t index2 = index1; index2 < arr->len; index2 ++){
|
||||||
|
|
||||||
|
void *data1 = arr->data + index1 * arr->elem_size;
|
||||||
|
void *data2 = arr->data + index2 * arr->elem_size;
|
||||||
|
|
||||||
|
if(f(data1,data2) > 0){
|
||||||
|
memcpy(temp, data2, arr->elem_size);
|
||||||
|
memcpy(data2, data1, arr->elem_size);
|
||||||
|
memcpy(data1, temp, arr->elem_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(temp);
|
||||||
|
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)arr;
|
|
||||||
(void)f;
|
void *data = calloc(arr->len, output_size);
|
||||||
(void)output_size;
|
if(!data) return NULL;
|
||||||
return NULL;
|
|
||||||
|
size_t index = 0;
|
||||||
|
while (index != arr->len){
|
||||||
|
f(data[index], arr->data + index * arr->elem_size);
|
||||||
|
index ++;
|
||||||
|
}
|
||||||
|
memcpy(arr->data, data);
|
||||||
|
arr->elem_size = output_size;
|
||||||
|
|
||||||
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct array *filter(struct array *arr, predicate f)
|
struct array *filter(struct array *arr, predicate f)
|
||||||
@@ -32,6 +62,110 @@ void *reduce(struct array *arr, void *start_value, reducer f)
|
|||||||
|
|
||||||
void foreach(struct array *arr, acceptor f)
|
void foreach(struct array *arr, acceptor f)
|
||||||
{
|
{
|
||||||
(void)arr;
|
// arr.data est un array de x valeurs
|
||||||
(void)f;
|
size_t index = 0;
|
||||||
|
|
||||||
|
while(index != arr->len){
|
||||||
|
|
||||||
|
f(arr->data + index * arr->elem_size);
|
||||||
|
index ++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void next(double *res, int *n)
|
||||||
|
{
|
||||||
|
*res = *n + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_double(double *n)
|
||||||
|
{
|
||||||
|
printf("%.1lf\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(map(&arr, sizeof(double), (mapper)&next), (acceptor)&print_double);
|
||||||
|
|
||||||
|
free(arr.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int reverse(int *a, int *b)
|
||||||
|
{
|
||||||
|
return *b - *a;
|
||||||
|
}
|
||||||
|
|
||||||
|
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(sort(&arr, (comparator)&reverse), (acceptor)&print_int);
|
||||||
|
|
||||||
|
free(arr.data);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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(&arr, (acceptor)&print_int);
|
||||||
|
|
||||||
|
free(arr.data);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user