diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..96249cede61f2cea6cf2a665bfce534ab0b1bee5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +main \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..35a3c7cc1b64c381febca2510429e03fe9bdab69 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +compile=gcc -Wall -Wextra -fsanitize=address +execName=main +libraryName=vector + +rerun: clean run + +run: build + ./$(execName) + +rebuild: clean build + +build: $(execName).o $(libraryName).o + $(compile) $^ -o $(execName) -lm + +$(execName).o: $(execName).c + $(compile) -c $< + +$(libraryName).o: $(libraryName).c $(libraryName).h + $(compile) -c $< + +clean: + rm *.o $(execName) \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000000000000000000000000000000000000..18cbb62a8bf8992a6f2c50748d7350d127b2e04b --- /dev/null +++ b/main.c @@ -0,0 +1,36 @@ +#include "vector.h" +#include <stdio.h> + +void print_int(int elem){ + printf("%d",elem); +} + +int main(){ + vector* v = vector_create(); + + vector_push(v, 1); + vector_push(v, 2); + vector_push(v, 3); + vector_push(v, 4); + vector_push(v, 5); + vector_print(v, print_int); + + vector_pop(v); + vector_print(v, print_int); + + vector_insert(v, 10, 2); + vector_print(v, print_int); + + vector_remove(v, 1); + vector_print(v, print_int); + + vector_remove(v, 0); + vector_print(v, print_int); + + vector_empty(v); + vector_print(v, print_int); + + vector_free(v); + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/vector.c b/vector.c new file mode 100644 index 0000000000000000000000000000000000000000..8d6ba8db9be98526512d9ea115e6d144acc340fd --- /dev/null +++ b/vector.c @@ -0,0 +1,102 @@ +#include <stddef.h> +#include <stdlib.h> +#include <stdio.h> +#include <assert.h> +#include "vector.h" +#define VECTOR_INIT_CAPACITY 4 + +struct _vector { + type *content; // actual content of the vector + int capacity; // capacity allocated + int length; // actual length +}; + +//typedef struct _vector vector; + +static void handle_bounds(vector* v, int index){ + if(index < 0 || index >= v->length){ + assert(false); + } +} + +vector* vector_create(){ + vector* v = malloc(sizeof(*v)); + v->capacity = VECTOR_INIT_CAPACITY; + v->length = 0; + v->content = malloc(v->capacity * sizeof(type)); + return v; +} + +int vector_length(vector* v){ + return v->length; +} + +void vector_push(vector* v, type element){ + if(v->length >= v->capacity){ + v->capacity *= 2; + v->content = realloc(v->content, v->capacity * sizeof(type)); + } + v->content[v->length] = element; + v->length += 1; +} + +void vector_pop(vector* v){ + v->length -= 1; + if(v->length < 0){ + assert(false); + } + if(v->length < (v->capacity/4)){ + v->capacity /= 2; + v->content = realloc(v->content, v->capacity * sizeof(type)); + } +} + +void vector_set(vector* v, int index, type element){ + handle_bounds(v, index); + v->content[index] = element; +} + +type vector_get(vector* v, int index){ + handle_bounds(v, index); + return v->content[index]; +} + +void vector_remove(vector* v, int index){ + handle_bounds(v, index); + for(int i = index; i < v->length-1; i++){ + v->content[i] = v->content[i+1]; + } + vector_pop(v); +} + +void vector_insert(vector *v, type element, int index){ + handle_bounds(v, index); + vector_push(v,0); + for(int i = v->length-1; i > index; i--){ + v->content[i] = v->content[i-1]; + } + v->content[index] = element; +} + +void vector_empty(vector *v){ + v->length = 0; + v->capacity = VECTOR_INIT_CAPACITY; + v->content = realloc(v->content, v->capacity * sizeof(type)); +} + +void vector_free(vector* v){ + free(v->content); + free(v); +} + +void vector_print(vector* v, void (*print)(type)){ + printf("["); + for(int i = 0; i < v->length-1; i++){ + print(v->content[i]); + printf(", "); + } + if(v->length >= 1){ + print(v->content[v->length-1]); + } + printf("]\n"); +} \ No newline at end of file diff --git a/vector.h b/vector.h new file mode 100644 index 0000000000000000000000000000000000000000..0281931595ff14b7a3d4a9911c915bb684620920 --- /dev/null +++ b/vector.h @@ -0,0 +1,26 @@ +#ifndef _VECTOR_H_ +#define _VECTOR_H_ +#include <stdlib.h> +#include <stdbool.h> + +typedef int type; +struct _vector; +typedef struct _vector vector; + +vector* vector_create(); +int vector_length(vector* v); +void vector_push(vector* v, type element); +void vector_pop(vector* v); +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)); +vector* vector_reduce(vector *v, type neutral, type (*f)(type, type)); + +#endif \ No newline at end of file