diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..51fe6b9e2db4aa40ae2fe9daeecde54bbbd3a7f3 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +CC=gcc -Wall -Wextra -g -fsanitize=address -fsanitize=leak + +Vecteur: Vecteur.o main.o + $(CC) Vecteur.o main.o -o run -lm + +Vecteur.o: Vecteur.c Vecteur.h + gcc -Wall -Wextra -c Vecteur.c + +main.o: main.c + gcc -Wall -Wextra -c main.c + +clean: + rm -f *.o + \ No newline at end of file diff --git a/Vecteur.c b/Vecteur.c new file mode 100644 index 0000000000000000000000000000000000000000..34c806d2b94f9a50b62eae073d66e91bc5e5721c --- /dev/null +++ b/Vecteur.c @@ -0,0 +1,182 @@ +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <stdio.h> +#include <time.h> +#include "Vecteur.h" +#define VECTOR_INIT_CAPACITY 4 + +vector creat_vector() +{ + vector vec; + vec.capacity = VECTOR_INIT_CAPACITY; + vec.length = 0; + vec.content = malloc(VECTOR_INIT_CAPACITY * sizeof(type)); + + if(vec.content == NULL) + { + EXIT_FAILURE; + } + return vec; +} + +int vector_length(vector v) +{ + return v.length; + +} + +void vector_push(vector *v, type element) +{ + + if (v->length == v->capacity) + { + v->content = realloc( v->content, 2*VECTOR_INIT_CAPACITY * sizeof(type) ); + v->capacity *= 2; + + } + v->content[v->capacity-1] = element; + v->length++; +} + +vector vector_pop(vector *v) +{ + if (v->length < (0.25*v->capacity)) + { + v->content = realloc( v->content, 0.5*v->capacity * sizeof(type) ); + v->capacity *= 0.5; + + } + + if(v->length != 0) + { + v->content[v->length - 1] = 0; + v->length--; + + } + + return *v; +} + +void vector_set(vector *v, int index, type element) +{ + if(0< index && index < v->length) + { + v->content[index] = element; + } + else + { + EXIT_FAILURE; + + } + +} + +type vector_get(vector *v, int index) +{ + + if(index < v->length) + { + return v->content[index]; + + } + else + { + EXIT_FAILURE; + } + +} + +type vector_remove(vector *v, int index) +{ + type valsuppr; + valsuppr = v->content[index]; + for (int i = index; i< v->length-1; i++) + { + v->content[i] = v->content[i+1]; + } + + v->length--; + + return valsuppr; +} + +void vector_insert(vector *v, type element, int index) +{ + if(index<v->length) + { + for(int i = v->length; i > index; i--) + { + v->content[i] = v->content[i+1]; + } + v->content[index] = element; + v->length++; + } +} +void vector_empty(vector *v) +{ + v->capacity = VECTOR_INIT_CAPACITY; + v->length = 0; + free(v->content); + v->content = malloc(VECTOR_INIT_CAPACITY * sizeof(type)); +} +void vector_free(vector *v) +{ + free(v->content); +} + + +void vector_print(vector *v, void (*print)(type)) +{ + for (int i = 0 ; i < v->length; i++){ + print(v->content[i]); + } + printf("\n"); + + +} +vector vector_map(vector *v, type (*f)(type)) +{ + vector vec = creat_vector(); + for(int i = 0; i< v->length; i++) + { + vector_push(&vec, f(v->content[i])); + } + return vec; +} + +bool lower_than_five(type a) +{ + return (a < 5); +} + +vector vector_filter(vector *v, bool (*f)(type)) +{ + vector vec = creat_vector(); + for(int i = 0; i< v->length; i++) + { + if(f(v->content[i])) + { + vector_push(&vec, v->content[i]); + } + + } + return vec; + + +} + +type add(type lhs, type rhs) { +return lhs + rhs; +} + +type vector_reduce(vector *v, type neutral, type (*f)(type, type)) +{ + type red = neutral; + for (int i = 0 ; i < v->length; i++){ + red = f(v->content[i], red); + } + return red; + + +} \ No newline at end of file diff --git a/Vecteur.h b/Vecteur.h new file mode 100644 index 0000000000000000000000000000000000000000..0aa96dbb1f115fd2fdd9efaf994b23d183362309 --- /dev/null +++ b/Vecteur.h @@ -0,0 +1,35 @@ +#ifndef _VECTEUR_H_ +#define _VECTEUR_H_ +#include <stdbool.h> + +#define VECTOR_INIT_CAPACITY 4 +typedef int type; +typedef void (*print)(type); +typedef type (*f)(type); + + +typedef struct _vector { + type *content; // actual content of the vector + int capacity; // capacity allocated + int length; // actual length +} vector; + +vector creat_vector(); +int vector_length(vector v); +void vector_push(vector *v, type element); +vector vector_pop(vector *v); +void vector_set(vector *v, int index, type element); +type vector_get(vector *v, int index); +type vector_remove(vector *v, int index); +void vector_insert(vector *v, type element, int index); +void vector_empty(vector *v); +void vector_free(vector *v) ; +void vector_print(vector *v, void (*print)(type)); +vector vector_map(vector *v, type (*f)(type)); +bool lower_than_five(type a); +vector vector_filter(vector *v, bool (*f)(type)); +type add(type lhs, type rhs); +type vector_reduce(vector *v, type neutral, type (*f)(type, type)); + + +#endif \ No newline at end of file diff --git a/Vecteur.o b/Vecteur.o new file mode 100644 index 0000000000000000000000000000000000000000..1f6a77321df84d34889f7f76398873f9c477e1d7 Binary files /dev/null and b/Vecteur.o differ diff --git a/main.c b/main.c new file mode 100644 index 0000000000000000000000000000000000000000..6525576788210c17acefa6309b24defd8627c065 --- /dev/null +++ b/main.c @@ -0,0 +1,19 @@ +#include <stdlib.h> +#include <stdbool.h> +#include <stdio.h> +#include <time.h> +#include "Vecteur.h" +#define VECTOR_INIT_CAPACITY 4 + + +int main() +{ + + vector vec = creat_vector(); + vector_push(&vec, 1); + vector_push(&vec, 2); + + + + +} \ No newline at end of file diff --git a/main.o b/main.o new file mode 100644 index 0000000000000000000000000000000000000000..0771a90afceae875b4be7307dbf0d15d0deeb1f5 Binary files /dev/null and b/main.o differ diff --git a/run b/run new file mode 100755 index 0000000000000000000000000000000000000000..74c9bdf8fca7edd4704ea80c91f3bd89a5eed8c1 Binary files /dev/null and b/run differ