diff --git a/TP6 b/TP6 new file mode 100755 index 0000000000000000000000000000000000000000..2f98da30d9fba3114c6af65c833191964d6ad880 Binary files /dev/null and b/TP6 differ diff --git a/main.c b/main.c new file mode 100644 index 0000000000000000000000000000000000000000..fcbacee1cf3625991cb66f917c6b2720d4f72679 --- /dev/null +++ b/main.c @@ -0,0 +1,6 @@ +#include "vector.h" + +int main() +{ + return 0; +} \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000000000000000000000000000000000000..f10629ef72f03008694b086e67748d76947bffdd --- /dev/null +++ b/makefile @@ -0,0 +1,19 @@ +CC=gcc +FLAGS= -Wall -Werror +CFLAGS= -g -std=c99 +LDFLAGS= -fsanitize=address -fsanitize=leak + + +main: main.o vector.o + $(CC) main.o vector.o -o TP6 $(CFLAGS) $(LDFLAGS) + +main.o: main.c + $(CC) $(FLAGS) $(CFLAGS) $(LDFLAGS) -c $^ + +vector.o: vector.c vector.h + $(CC) $(FLAGS) $(CFLAGS) $(LDFLAGS) -c $< + +clean: + rm -f *.o main + +rebuild: clean TP6 \ No newline at end of file diff --git a/vector.c b/vector.c new file mode 100644 index 0000000000000000000000000000000000000000..34e3cab033a86b99146ccc339112b13fdb20cfe2 --- /dev/null +++ b/vector.c @@ -0,0 +1,165 @@ +/* + * Author : Alec Schmidt + * Date : 22.02.2022 + */ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include <stdbool.h> +#include "vector.h" + +#define VECTOR_INIT_CAPACITY 4 + +typedef struct _vector { + type *data; // actual content of the vector + int capacity; // capacity allocated + int length; // actual length +} vector; + + +vector vector_create() +{ + vector v; + v.capacity = VECTOR_INIT_CAPACITY; + v.length = 0; + v.data = malloc(VECTOR_INIT_CAPACITY * sizeof(type)); + + return v; +} + +int vector_lenght(vector *v) +{ + return v->length; +} + +void vector_push(vector *v, type item) +{ + if (v->length == v->capacity) + { + v->capacity *=2; + v->data = realloc(v->data, v->capacity * sizeof(type)); + assert(v->data!=NULL); + } + + v->data[v->length] = item; + v->length++; +} + +void vector_pop(vector *v, type item) +{ + assert(v->length>0); + if (v->length-1 == v->capacity/2) + { + v->capacity /=2; + v->data = realloc(v->data, v->capacity * sizeof(type)); + assert(v->data!=NULL); + } + + v->length--; +} + +void vector_set(vector *v, int index, type element) +{ + assert(index <= v->length && index > 0); + + v->data[index] = element; +} + +type vector_get(vector *v, int index) +{ + assert(index <= v->length && index > 0); + return v->data[index]; +} + +void vector_remove(vector *v, int index) +{ + assert(index <= v->length && index > 0); + + for (int i = index; i < v->length; i++) + { + v->data[i] = v->data[i+1]; + } + v->length--; +} + +void vector_insert(vector *v, type element, int index) +{ + assert(index <= v->length && index > 0); + + if (v->capacity == v->length) + { + v->capacity *=2; + v->data = realloc(v->data, v->capacity * sizeof(type)); + assert(v->data!=NULL); + } + + v->length++; + for (size_t i = v->length-1; i > index; i--) + { + v->data[i] = v->data[i-1]; + } + + v->data[index] = element; +} + +void vector_empty(vector *v) +{ + free(v->data); + vector v2 = vector_create(); + v = &v2; +} + +void vector_free(vector *v) +{ + free(v->data); + free(v); +} + +void vector_print(vector *v, void (*print)(type)) +{ + printf("["); + for (int i = 0; i < v->length; i++) + { + print(v->data[i]); + printf(","); + } + printf("]"); +} + +vector vector_map(vector *v, type (*f)(type)) +{ + vector v2 = vector_create(); + + for (int i = 0; i < v->length; i++) + { + vector_push(&v2, f(v->data[i])); + } + + return v2; +} + +vector vector_filter(vector *v, bool (*f)(type)) +{ + vector v2 = vector_create(); + + for (int i = 0; i < v->length; i++) + { + if (f(v->data[i])) + { + vector_push(&v2, v->data[i]); + } + + } + return v2; +} + +type vector_reduce(vector *v, type neutral, type (*f)(type, type)) +{ + type result = neutral; + + for (int i = 0; i < v->length; i++) + { + f(neutral, v->data[i]); + } + return result; +} \ No newline at end of file diff --git a/vector.h b/vector.h new file mode 100644 index 0000000000000000000000000000000000000000..80236af066fa2d3928543a53d2f186efbc548e8e --- /dev/null +++ b/vector.h @@ -0,0 +1,21 @@ +#ifndef _VECTOR_H_ +#define _VECTOR_H_ + +typedef int type; +typedef struct _vector vector; +vector vector_create(); +int vector_lenght(vector *v); +void vector_push(vector *v, type item); +void vector_pop(vector *v, type item); +void vector_set(vector *v, int index, type element); +type vector_get(vector *v, int index); +void 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)); +vector vector_filter(vector *v, bool (*f)(type)); +type vector_reduce(vector *v, type neutral, type (*f)(type, type)); + +#endif \ No newline at end of file